spibus.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*!
  2. * Copyright (C) 2001-2010 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file arch/avr32/dev/spibus.c
  36. * \brief General AVR32 SPI bus controller routines.
  37. *
  38. * \verbatim
  39. * $Id: spibus.c,v 1.3 2009/02/13 14:52:05 haraldkipp Exp $
  40. * \endverbatim
  41. */
  42. #include <cfg/spi.h>
  43. #include <sys/timer.h>
  44. #include <sys/nutdebug.h>
  45. #include <stdlib.h>
  46. #include <memdebug.h>
  47. #include <dev/spibus_avr32.h>
  48. #include <avr32/io.h>
  49. #if defined(SPIBUS0_DOUBLE_BUFFER) || defined(SPIBUS1_DOUBLE_BUFFER)
  50. #warning Double buffered SPI not tested for AVR32 targets.
  51. /*!
  52. * \brief AVR32 SPI interrupt handler.
  53. */
  54. static void Avr32SpiInterrupt(void *arg)
  55. {
  56. NutEventPostFromIrq((void **) arg);
  57. }
  58. #endif /* SPIBUS0_DOUBLE_BUFFER */
  59. /*!
  60. * \brief Update SPI shadow registers.
  61. *
  62. * \param node Specifies the SPI bus node.
  63. *
  64. * \return Always 0.
  65. */
  66. int Avr32SpiSetup(NUTSPINODE * node)
  67. {
  68. uint32_t clk;
  69. uint32_t clkdiv;
  70. AVR32SPIREG *spireg;
  71. NUTASSERT(node != NULL);
  72. NUTASSERT(node->node_stat != NULL);
  73. NUTASSERT(node->node_bus != NULL);
  74. NUTASSERT(node->node_bus->bus_base != 0);
  75. spireg = node->node_stat;
  76. spireg->mr &= ~(AVR32_SPI_MR_MODFDIS_MASK | AVR32_SPI_MR_LLB_MASK | AVR32_SPI_MR_PCSDEC_MASK);
  77. if ((node->node_mode & SPI_MODE_FAULT) == 0) {
  78. spireg->mr |= AVR32_SPI_MR_MODFDIS_MASK;
  79. }
  80. if (node->node_mode & SPI_MODE_LOOPBACK) {
  81. spireg->mr |= AVR32_SPI_MR_LLB_MASK;
  82. }
  83. spireg->mr |= 0xe0 << AVR32_SPI_PCS_OFFSET;
  84. spireg->csr &=
  85. ~(AVR32_SPI_CSR0_BITS_MASK | AVR32_SPI_CSR0_CPOL_MASK | AVR32_SPI_CSR0_NCPHA_MASK | AVR32_SPI_CSR0_CSAAT_MASK |
  86. AVR32_SPI_CSR0_SCBR_MASK);
  87. if (node->node_bits) {
  88. spireg->csr |= ((uint32_t) (node->node_bits - 8) << AVR32_SPI_CSR0_BITS_OFFSET) & AVR32_SPI_CSR0_BITS_MASK;
  89. }
  90. if (node->node_mode & SPI_MODE_CPOL) {
  91. spireg->csr |= AVR32_SPI_CSR0_CPOL_MASK;
  92. }
  93. if ((node->node_mode & SPI_MODE_CPHA) == 0) {
  94. spireg->csr |= AVR32_SPI_CSR0_NCPHA_MASK;
  95. }
  96. if (node->node_mode & SPI_MODE_CSKEEP) {
  97. spireg->csr |= AVR32_SPI_CSR0_CSAAT_MASK;
  98. }
  99. /* Query peripheral clock. */
  100. clk = NutClockGet(NUT_HWCLK_PERIPHERAL_A);
  101. /* Calculate the SPI clock divider. Avoid rounding errors. */
  102. clkdiv = (clk + node->node_rate - 1) / node->node_rate;
  103. /* The divider value minimum is 1. */
  104. if (clkdiv < 1) {
  105. clkdiv++;
  106. }
  107. /* The divider value maximum is 255. */
  108. else if (clkdiv > 255) {
  109. clkdiv = 255;
  110. }
  111. spireg->csr |= clkdiv << AVR32_SPI_CSR0_SCBR_OFFSET;
  112. /* Update interface parameters. */
  113. node->node_rate = clk / clkdiv;
  114. node->node_mode &= ~SPI_MODE_UPDATE;
  115. return 0;
  116. }
  117. /*!
  118. * \brief Initialize an SPI bus node.
  119. *
  120. * This routine is called for each SPI node, which is registered via
  121. * NutRegisterSpiDevice().
  122. *
  123. * \param node Specifies the SPI bus node.
  124. *
  125. * \return 0 on success or -1 if there is no valid chip select.
  126. */
  127. int Avr32SpiBusNodeInit(NUTSPINODE * node)
  128. {
  129. int rc = 0;
  130. NUTSPIBUS *bus;
  131. /* Sanity check. */
  132. NUTASSERT(node != NULL);
  133. NUTASSERT(node->node_bus != NULL);
  134. bus = node->node_bus;
  135. /* Try to deactivate the node's chip select. */
  136. #if defined(AVR32_SPI1_ADDRESS)
  137. if (bus->bus_base == AVR32_SPI1_ADDRESS) {
  138. rc = Avr32Spi1ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  139. } else
  140. #endif
  141. rc = Avr32Spi0ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  142. /* It should not hurt us being called more than once. Thus, we
  143. ** check whether any initialization had been taken place already. */
  144. if (rc == 0 && node->node_stat == NULL) {
  145. /* Allocate and set our shadow registers. */
  146. AVR32SPIREG *spireg = malloc(sizeof(AVR32SPIREG));
  147. if (spireg) {
  148. /* Set interface defaults. */
  149. spireg->mr = AVR32_SPI_MR_MODFDIS_MASK | AVR32_SPI_MR_MSTR_MASK;
  150. spireg->mr |= (1 << (AVR32_SPI_MR_PCS_OFFSET + node->node_cs));
  151. spireg->csr = 0;
  152. /* Update with node's defaults. */
  153. node->node_stat = (void *) spireg;
  154. Avr32SpiSetup(node);
  155. /*
  156. * Register and enable SPI interrupt handler.
  157. */
  158. #if !defined(SPIBUS1_POLLING_MODE) && defined(AVR32_SPI1_ADDRESS)
  159. if (bus->bus_base == AVR32_SPI1_ADDRESS) {
  160. #if defined(SPIBUS1_DOUBLE_BUFFER)
  161. NutRegisterIrqHandler(bus->bus_sig, Avr32SpiInterrupt, &bus->bus_ready);
  162. #else
  163. NutRegisterIrqHandler(bus->bus_sig, Avr32SpiBus1Interrupt, &bus->bus_ready);
  164. #endif
  165. outr(bus->bus_base + AVR32_SPI_IDR, (unsigned int) -1);
  166. NutIrqEnable(bus->bus_sig);
  167. } else
  168. #endif /* !SPIBUS1_POLLING_MODE */
  169. {
  170. #if !defined(SPIBUS0_POLLING_MODE)
  171. #if defined(SPIBUS0_DOUBLE_BUFFER)
  172. NutRegisterIrqHandler(bus->bus_sig, Avr32SpiInterrupt, &bus->bus_ready);
  173. #else
  174. NutRegisterIrqHandler(bus->bus_sig, Avr32SpiBus0Interrupt, &bus->bus_ready);
  175. #endif
  176. outr(bus->bus_base + AVR32_SPI_IDR, (unsigned int) -1);
  177. NutIrqEnable(bus->bus_sig);
  178. #endif /* !SPIBUS0_POLLING_MODE */
  179. }
  180. } else {
  181. /* Out of memory? */
  182. rc = -1;
  183. }
  184. }
  185. return rc;
  186. }
  187. #if defined(SPIBUS0_POLLING_MODE) || defined(SPIBUS1_POLLING_MODE)
  188. /*!
  189. * \brief Transfer data on the SPI bus in polling mode.
  190. *
  191. * A device must have been selected by calling At91SpiSelect().
  192. *
  193. * \param node Specifies the SPI bus node.
  194. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  195. * byte values are transmitted.
  196. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  197. * data is discarded.
  198. * \param xlen Number of bytes to transfer.
  199. *
  200. * \return Always 0.
  201. */
  202. int Avr32SpiBusPollTransfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  203. {
  204. uint8_t b = 0xff;
  205. uint8_t *txp = (uint8_t *) txbuf;
  206. uint8_t *rxp = (uint8_t *) rxbuf;
  207. uintptr_t base;
  208. /* Sanity check. */
  209. NUTASSERT(node != NULL);
  210. NUTASSERT(node->node_bus != NULL);
  211. NUTASSERT(node->node_bus->bus_base != 0);
  212. base = node->node_bus->bus_base;
  213. while (xlen--) {
  214. if (txp) {
  215. b = *txp++;
  216. }
  217. /* Transmission starts by writing the transmit data. */
  218. outr(base + AVR32_SPI_TDR, (b << AVR32_SPI_TDR_TD_OFFSET));
  219. /* Wait for receiver data register full. */
  220. while ((inr(base + AVR32_SPI_SR) & AVR32_SPI_RDRF_MASK) == 0);
  221. /* Read incoming data. */
  222. b = (uint8_t) inr(base + AVR32_SPI_RDR) >> AVR32_SPI_RDR_RD_OFFSET;
  223. if (rxp) {
  224. *rxp++ = b;
  225. }
  226. }
  227. return 0;
  228. }
  229. #endif
  230. #if defined(SPIBUS0_DOUBLE_BUFFER) || defined(SPIBUS1_DOUBLE_BUFFER)
  231. /*!
  232. * \brief Transfer data on the SPI bus using double buffered PDC.
  233. *
  234. * A device must have been selected by calling Avr32SpiSelect().
  235. *
  236. * Note, that the transfer may be still in progress when returning
  237. * from this function.
  238. *
  239. * \todo Not yet done. Given up after SAM7SE SDRAM problems.
  240. *
  241. * \param node Specifies the SPI bus node.
  242. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  243. * byte values are transmitted.
  244. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  245. * data is discarded.
  246. * \param xlen Number of bytes to transfer.
  247. *
  248. * \return Always -1.
  249. */
  250. int Avr32SpiBusDblBufTransfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  251. {
  252. #error not implemented
  253. return -1;
  254. }
  255. #endif
  256. #if !defined(SPIBUS0_DOUBLE_BUFFER) || !defined(SPIBUS1_DOUBLE_BUFFER)
  257. /*!
  258. * \brief Wait until all SPI bus transfers are done.
  259. *
  260. * \param node Specifies the SPI bus node.
  261. * \param tmo Timeout in milliseconds. To disable timeout, set this
  262. * parameter to NUT_WAIT_INFINITE.
  263. *
  264. * \return Always 0.
  265. */
  266. int Avr32SpiBusWait(NUTSPINODE * node, uint32_t tmo)
  267. {
  268. while ((inr(node->node_bus->bus_base + AVR32_SPI_SR) & AVR32_SPI_SR_RXBUFF) == 0) {
  269. if (NutEventWait(&node->node_bus->bus_ready, tmo)) {
  270. return -1;
  271. }
  272. }
  273. return 0;
  274. }
  275. #endif /* SPIBUS_POLLING_MODE */