lpc176x_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (C) 2012 by Simon Budig (simon@budig.de)
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * \verbatim
  35. * $Id$
  36. * \endverbatim
  37. */
  38. #include <sys/timer.h>
  39. #include <sys/nutdebug.h>
  40. #include <dev/spibus.h>
  41. #include <dev/gpio.h>
  42. #include <arch/cm3.h>
  43. #include <arch/cm3/nxp/mach/lpc_sc.h>
  44. #include <arch/cm3/nxp/lpc176x_clk.h>
  45. #include <arch/cm3/nxp/lpc176x_spi.h>
  46. #include <errno.h>
  47. #include <stdlib.h>
  48. #define SPIBUS_SCK_PORT NUTGPIO_PORT0
  49. #define SPIBUS_MISO_PORT NUTGPIO_PORT0
  50. #define SPIBUS_MOSI_PORT NUTGPIO_PORT0
  51. #define SPIBUS_SCK_PIN 15
  52. #define SPIBUS_MISO_PIN 17
  53. #define SPIBUS_MOSI_PIN 18
  54. #define SPIBUS_SCK_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL3)
  55. #define SPIBUS_MISO_PIN_CFG (GPIO_CFG_PERIPHERAL3)
  56. #define SPIBUS_MOSI_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL3)
  57. static int Lpc17xxSpiSetup (NUTSPINODE * node);
  58. static int Lpc17xxSpiBusNodeInit (NUTSPINODE * node);
  59. static int Lpc17xxSpiBusSelect (NUTSPINODE * node, uint32_t tmo);
  60. static int Lpc17xxSpiBusDeselect (NUTSPINODE * node);
  61. static int Lpc17xxSpiBusTransfer (NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen);
  62. // TODO: Add a mutex for the bus access
  63. NUTSPIBUS spiBus0Lpc17xx = {
  64. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  65. NULL, /*!< Bus ready signal (bus_ready). */
  66. LPC_SPI_BASE, /*!< Bus base address (bus_base). */
  67. NULL, /*!< Bus interrupt handler (bus_sig). */
  68. Lpc17xxSpiBusNodeInit, /*!< Initialize the bus (bus_initnode). */
  69. Lpc17xxSpiBusSelect, /*!< Select the specified device (bus_alloc). */
  70. Lpc17xxSpiBusDeselect, /*!< Deselect the specified device (bus_release). */
  71. Lpc17xxSpiBusTransfer,
  72. NutSpiBusWait,
  73. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  74. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  75. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  76. };
  77. /*!
  78. * \brief Set the specified chip select to a given level.
  79. */
  80. static int Lpc17xxSpiChipSelect(uint_fast8_t cs, uint_fast8_t hi)
  81. {
  82. GpioPinConfigSet(cs / 32, cs % 32, GPIO_CFG_OUTPUT);
  83. GpioPinSet(cs / 32, cs % 32, hi);
  84. return 0;
  85. }
  86. /*! \brief Deselect a device on the SPI bus.
  87. *
  88. * Deactivates the chip select and unlocks the bus.
  89. *
  90. * \param node Specifies the SPI bus node.
  91. *
  92. * \return Always 0.
  93. */
  94. static int Lpc17xxSpiBusDeselect(NUTSPINODE * node)
  95. {
  96. /* Sanity check. */
  97. NUTASSERT(node != NULL);
  98. NUTASSERT(node->node_bus != NULL);
  99. NutSpiBusWait(node, NUT_WAIT_INFINITE);
  100. /* Deactivate the node's chip select. */
  101. Lpc17xxSpiChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  102. /* Release the bus. */
  103. NutEventPostAsync(&node->node_bus->bus_mutex);
  104. return 0;
  105. }
  106. /*! \brief Select a device on the SPI bus.
  107. *
  108. * Locks and activates the bus for the specified node.
  109. *
  110. * \param node Specifies the SPI bus node.
  111. * \param tmo Timeout in milliseconds. To disable timeout, set this
  112. * parameter to NUT_WAIT_INFINITE.
  113. *
  114. * \return 0 on success. In case of an error, -1 is returned and the bus
  115. * is not locked.
  116. */
  117. static int Lpc17xxSpiBusSelect(NUTSPINODE * node, uint32_t tmo)
  118. {
  119. int rc;
  120. LPC_SPI_TypeDef* base;
  121. /* Sanity check. */
  122. NUTASSERT(node != NULL);
  123. NUTASSERT(node->node_bus != NULL);
  124. NUTASSERT(node->node_stat != NULL);
  125. base=(LPC_SPI_TypeDef*)(node->node_bus->bus_base);
  126. /* Allocate the bus. */
  127. rc = NutEventWait(&node->node_bus->bus_mutex, tmo);
  128. if (rc) {
  129. errno = EIO;
  130. } else {
  131. LPC_SPI_TypeDef *spireg = node->node_stat;
  132. /* Dectivate the IO Pins to avoid glitches*/
  133. GpioPinConfigSet(SPIBUS_SCK_PORT, SPIBUS_SCK_PIN, GPIO_CFG_DISABLED); //SCK
  134. GpioPinConfigSet(SPIBUS_MISO_PORT, SPIBUS_MISO_PIN, GPIO_CFG_DISABLED ); //MISO
  135. GpioPinConfigSet(SPIBUS_MOSI_PORT, SPIBUS_MOSI_PIN, GPIO_CFG_DISABLED); //MOSI
  136. /* If the mode update bit is set, then update our shadow registers. */
  137. if (node->node_mode & SPI_MODE_UPDATE) {
  138. Lpc17xxSpiSetup(node);
  139. }
  140. /* Set SPI mode. */
  141. base->SPCR = spireg->SPCR;
  142. base->SPCCR = spireg->SPCCR;
  143. GpioPinConfigSet(SPIBUS_SCK_PORT, SPIBUS_SCK_PIN, SPIBUS_SCK_PIN_CFG); //SCK
  144. GpioPinConfigSet(SPIBUS_MISO_PORT, SPIBUS_MISO_PIN, SPIBUS_MISO_PIN_CFG); //MISO
  145. GpioPinConfigSet(SPIBUS_MOSI_PORT, SPIBUS_MOSI_PIN, SPIBUS_MOSI_PIN_CFG); //MOSI
  146. /* Finally activate the node's chip select. */
  147. rc = Lpc17xxSpiChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) != 0);
  148. if (rc) {
  149. /* Release the bus in case of an error. */
  150. NutEventPost(&node->node_bus->bus_mutex);
  151. }
  152. }
  153. return rc;
  154. }
  155. /*!
  156. * \brief Update SPI shadow registers.
  157. *
  158. * \param node Specifies the SPI bus node.
  159. *
  160. * \return Always 0.
  161. */
  162. static int Lpc17xxSpiSetup(NUTSPINODE * node)
  163. {
  164. uint32_t clk;
  165. uint32_t clkdiv;
  166. LPC_SPI_TypeDef *spireg;
  167. NUTASSERT(node != NULL);
  168. NUTASSERT(node->node_stat != NULL);
  169. NUTASSERT(node->node_bus != NULL);
  170. NUTASSERT(node->node_bus->bus_base != 0);
  171. spireg = node->node_stat;
  172. spireg->SPCR &= ~(SPI_CR_BITENABLE | SPI_CR_BITS_MASK | SPI_CR_CPOL | SPI_CR_CPHA);
  173. if (node->node_bits != 8) {
  174. spireg->SPCR |= (SPI_CR_BITENABLE | SPI_CR_BITS(node->node_bits));
  175. }
  176. if (node->node_mode & SPI_MODE_CPOL) {
  177. spireg->SPCR |= SPI_CR_CPOL;
  178. }
  179. if (node->node_mode & SPI_MODE_CPHA) {
  180. spireg->SPCR |= SPI_CR_CPHA;
  181. }
  182. spireg->SPCR |= SPI_CR_MSTR; /* master only for now */
  183. clk = NutClockGet(NUT_HWCLK_PCLK);
  184. clk /= Lpc176x_PclkDivGet(CLKPWR_PCLKSEL_SPI);
  185. /* Calculate the SPI clock divider. Avoid rounding errors. */
  186. clkdiv = (clk + node->node_rate - 1) / node->node_rate;
  187. /* The divider value minimum is 8. */
  188. if (clkdiv < 8) {
  189. clkdiv = 8;
  190. }
  191. /* The divider value maximum is 255. */
  192. else if (clkdiv > 255) {
  193. clkdiv = 255;
  194. }
  195. /* must be even */
  196. clkdiv &= 0xfe;
  197. spireg->SPCCR = clkdiv;
  198. /* Update interface parameters. */
  199. node->node_rate = clk / clkdiv;
  200. node->node_mode &= ~SPI_MODE_UPDATE;
  201. return 0;
  202. }
  203. /*!
  204. * \brief Initialize an SPI bus node.
  205. *
  206. * This routine is called for each SPI node, which is registered via
  207. * NutRegisterSpiDevice().
  208. *
  209. * \param node Specifies the SPI bus node.
  210. *
  211. * \return 0 on success or -1 if there is no valid chip select.
  212. */
  213. static int Lpc17xxSpiBusNodeInit(NUTSPINODE * node)
  214. {
  215. int rc = -1;
  216. /* Sanity check. */
  217. NUTASSERT(node != NULL);
  218. NUTASSERT(node->node_bus != NULL);
  219. /* Try to deactivate the node's chip select. */
  220. rc = Lpc17xxSpiChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  221. /* It should not hurt us being called more than once. Thus, we
  222. ** check wether any initialization had been taken place already. */
  223. if (rc == 0 && node->node_stat == NULL)
  224. {
  225. /* Allocate and set our shadow registers. */
  226. LPC_SPI_TypeDef *spireg = malloc(sizeof(LPC_SPI_TypeDef));
  227. if (spireg) {
  228. /* Update with node's defaults. */
  229. node->node_stat = (void *)spireg;
  230. Lpc17xxSpiSetup(node);
  231. }
  232. else {
  233. /* Out of memory? */
  234. rc = -1;
  235. }
  236. }
  237. return rc;
  238. }
  239. /*!
  240. * \brief Transfer data on the SPI bus using single buffered interrupt mode.
  241. *
  242. * A device must have been selected by calling At91SpiSelect().
  243. *
  244. * \param node Specifies the SPI bus node.
  245. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  246. * byte values are transmitted.
  247. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  248. * data is discarded.
  249. * \param xlen Number of bytes to transfer.
  250. *
  251. * \return Always 0.
  252. */
  253. static int Lpc17xxSpiBusTransfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  254. {
  255. LPC_SPI_TypeDef* base;
  256. /* Sanity check. */
  257. NUTASSERT(node != NULL);
  258. NUTASSERT(node->node_bus != NULL);
  259. NUTASSERT(node->node_bus->bus_base != 0);
  260. base = (LPC_SPI_TypeDef*) node->node_bus->bus_base;
  261. unsigned char *tx = (unsigned char*) txbuf;
  262. unsigned char *rx = (unsigned char*) rxbuf;
  263. while (xlen-- > 0) {
  264. unsigned char b = tx ? (*tx++) : 0xff;
  265. base->SPDR = b;
  266. /* wait until receive buffer no longer empty */
  267. while ((base->SPSR & SPI_SR_SPIF) == 0)
  268. ;
  269. b = base->SPDR;
  270. if (rx) {
  271. *rx++ = b;
  272. }
  273. }
  274. return 0;
  275. }