lpc17xx_ssp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 <cfg/arch.h>
  39. #include <sys/timer.h>
  40. #include <sys/nutdebug.h>
  41. #include <dev/spibus.h>
  42. #include <dev/gpio.h>
  43. #include <arch/cm3.h>
  44. #include <arch/cm3/nxp/mach/lpc_sc.h>
  45. #include <arch/cm3/nxp/mach/lpc_ssp.h>
  46. #if defined(MCU_LPC176x)
  47. #include <arch/cm3/nxp/lpc176x.h>
  48. #include <arch/cm3/nxp/lpc176x_clk.h>
  49. #elif defined(MCU_LPC177x_8x)
  50. #include <arch/cm3/nxp/lpc177x_8x.h>
  51. #include <arch/cm3/nxp/lpc177x_8x_clk.h>
  52. #elif defined(MCU_LPC407x_8x)
  53. #include <arch/cm3/nxp/lpc407x_8x.h>
  54. #include <arch/cm3/nxp/lpc407x_8x_clk.h>
  55. #else
  56. #warning "Unknown LPC familiy"
  57. #endif
  58. #include <errno.h>
  59. #include <stdlib.h>
  60. #if defined(MCU_LPC176x)
  61. #define SSP0BUS_SCK_PORT NUTGPIO_PORT0
  62. #define SSP0BUS_MISO_PORT NUTGPIO_PORT0
  63. #define SSP0BUS_MOSI_PORT NUTGPIO_PORT0
  64. #define SSP0BUS_SCK_PIN 15
  65. #define SSP0BUS_MISO_PIN 17
  66. #define SSP0BUS_MOSI_PIN 18
  67. #define SSP0BUS_SCK_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL2)
  68. #define SSP0BUS_MISO_PIN_CFG (GPIO_CFG_PERIPHERAL2)
  69. #define SSP0BUS_MOSI_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL2)
  70. #define SSP1BUS_SCK_PORT NUTGPIO_PORT0
  71. #define SSP1BUS_MISO_PORT NUTGPIO_PORT0
  72. #define SSP1BUS_MOSI_PORT NUTGPIO_PORT0
  73. #define SSP1BUS_SCK_PIN 7
  74. #define SSP1BUS_MISO_PIN 8
  75. #define SSP1BUS_MOSI_PIN 9
  76. #define SSP1BUS_SCK_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL2)
  77. #define SSP1BUS_MISO_PIN_CFG (GPIO_CFG_PERIPHERAL2)
  78. #define SSP1BUS_MOSI_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL2)
  79. #elif defined(MCU_LPC177x_8x) || defined(MCU_LPC407x_8x)
  80. #define SSP0BUS_SCK_PORT NUTGPIO_PORT2
  81. #define SSP0BUS_MISO_PORT NUTGPIO_PORT2
  82. #define SSP0BUS_MOSI_PORT NUTGPIO_PORT2
  83. #define SSP0BUS_SCK_PIN 22
  84. #define SSP0BUS_MISO_PIN 26
  85. #define SSP0BUS_MOSI_PIN 27
  86. #define SSP0BUS_SCK_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL2)
  87. #define SSP0BUS_MISO_PIN_CFG (GPIO_CFG_PERIPHERAL2)
  88. #define SSP0BUS_MOSI_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL2)
  89. #define SSP1BUS_SCK_PORT NUTGPIO_PORT4
  90. #define SSP1BUS_MISO_PORT NUTGPIO_PORT4
  91. #define SSP1BUS_MOSI_PORT NUTGPIO_PORT4
  92. #define SSP1BUS_SCK_PIN 20
  93. #define SSP1BUS_MISO_PIN 22
  94. #define SSP1BUS_MOSI_PIN 23
  95. #define SSP1BUS_SCK_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL3)
  96. #define SSP1BUS_MISO_PIN_CFG (GPIO_CFG_PERIPHERAL3)
  97. #define SSP1BUS_MOSI_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL3)
  98. #if defined(LPC_SSP2_BASE)
  99. #define SSP2BUS_SCK_PORT NUTGPIO_PORT1
  100. #define SSP2BUS_MISO_PORT NUTGPIO_PORT1
  101. #define SSP2BUS_MOSI_PORT NUTGPIO_PORT1
  102. #define SSP2BUS_SCK_PIN 0
  103. #define SSP2BUS_MISO_PIN 4
  104. #define SSP2BUS_MOSI_PIN 1
  105. #define SSP2BUS_SCK_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL4)
  106. #define SSP2BUS_MISO_PIN_CFG (GPIO_CFG_PERIPHERAL4)
  107. #define SSP2BUS_MOSI_PIN_CFG (GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHERAL4)
  108. #endif
  109. #endif
  110. static int Lpc17xxSspSetup (NUTSPINODE * node);
  111. static int Lpc17xxSspBusNodeInit (NUTSPINODE * node);
  112. static int Lpc17xxSspBusSelect (NUTSPINODE * node, uint32_t tmo);
  113. static int Lpc17xxSspBusDeselect (NUTSPINODE * node);
  114. static int Lpc17xxSspBusTransfer (NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen);
  115. NUTSPIBUS spiBus0Lpc17xxSsp = {
  116. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  117. NULL, /*!< Bus ready signal (bus_ready). */
  118. LPC_SSP0_BASE, /*!< Bus base address (bus_base). */
  119. NULL, /*!< Bus interrupt handler (bus_sig). */
  120. Lpc17xxSspBusNodeInit, /*!< Initialize the bus (bus_initnode). */
  121. Lpc17xxSspBusSelect, /*!< Select the specified device (bus_alloc). */
  122. Lpc17xxSspBusDeselect, /*!< Deselect the specified device (bus_release). */
  123. Lpc17xxSspBusTransfer,
  124. NutSpiBusWait,
  125. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  126. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  127. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  128. };
  129. NUTSPIBUS spiBus1Lpc17xxSsp = {
  130. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  131. NULL, /*!< Bus ready signal (bus_ready). */
  132. LPC_SSP1_BASE, /*!< Bus base address (bus_base). */
  133. NULL, /*!< Bus interrupt handler (bus_sig). */
  134. Lpc17xxSspBusNodeInit, /*!< Initialize the bus (bus_initnode). */
  135. Lpc17xxSspBusSelect, /*!< Select the specified device (bus_alloc). */
  136. Lpc17xxSspBusDeselect, /*!< Deselect the specified device (bus_release). */
  137. Lpc17xxSspBusTransfer,
  138. NutSpiBusWait,
  139. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  140. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  141. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  142. };
  143. #if defined(LPC_SSP2_BASE)
  144. NUTSPIBUS spiBus2Lpc17xxSsp = {
  145. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  146. NULL, /*!< Bus ready signal (bus_ready). */
  147. LPC_SSP2_BASE, /*!< Bus base address (bus_base). */
  148. NULL, /*!< Bus interrupt handler (bus_sig). */
  149. Lpc17xxSspBusNodeInit, /*!< Initialize the bus (bus_initnode). */
  150. Lpc17xxSspBusSelect, /*!< Select the specified device (bus_alloc). */
  151. Lpc17xxSspBusDeselect, /*!< Deselect the specified device (bus_release). */
  152. Lpc17xxSspBusTransfer,
  153. NutSpiBusWait,
  154. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  155. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  156. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  157. };
  158. #endif
  159. /*!
  160. * \brief Set the specified chip select to a given level.
  161. */
  162. static int Lpc17xxSspChipSelect(uint_fast8_t cs, uint_fast8_t hi)
  163. {
  164. if (cs != 0xFF) {
  165. GpioPinSet(cs / 32, cs % 32, hi);
  166. }
  167. return 0;
  168. }
  169. /*! \brief Deselect a device on the SSP bus.
  170. *
  171. * Deactivates the chip select and unlocks the bus.
  172. *
  173. * \param node Specifies the SSP bus node.
  174. *
  175. * \return Always 0.
  176. */
  177. static int Lpc17xxSspBusDeselect(NUTSPINODE * node)
  178. {
  179. /* Sanity check. */
  180. NUTASSERT(node != NULL);
  181. NUTASSERT(node->node_bus != NULL);
  182. NutSpiBusWait(node, NUT_WAIT_INFINITE);
  183. /* Deactivate the node's chip select. */
  184. Lpc17xxSspChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  185. /* Release the bus. */
  186. NutEventPostAsync(&node->node_bus->bus_mutex);
  187. return 0;
  188. }
  189. /*! \brief Select a device on the SSP bus.
  190. *
  191. * Locks and activates the bus for the specified node.
  192. *
  193. * \param node Specifies the SSP bus node.
  194. * \param tmo Timeout in milliseconds. To disable timeout, set this
  195. * parameter to NUT_WAIT_INFINITE.
  196. *
  197. * \return 0 on success. In case of an error, -1 is returned and the bus
  198. * is not locked.
  199. */
  200. static int Lpc17xxSspBusSelect(NUTSPINODE * node, uint32_t tmo)
  201. {
  202. int rc;
  203. LPC_SSP_TypeDef* base;
  204. /* Sanity check. */
  205. NUTASSERT(node != NULL);
  206. NUTASSERT(node->node_bus != NULL);
  207. NUTASSERT(node->node_stat != NULL);
  208. base = (LPC_SSP_TypeDef*) (node->node_bus->bus_base);
  209. /* Allocate the bus. */
  210. rc = NutEventWait(&node->node_bus->bus_mutex, tmo);
  211. if (rc) {
  212. errno = EIO;
  213. } else {
  214. LPC_SSP_TypeDef *sspreg = node->node_stat;
  215. /* If the mode update bit is set, then update our shadow registers. */
  216. if (node->node_mode & SPI_MODE_UPDATE) {
  217. Lpc17xxSspSetup(node);
  218. }
  219. /* Set SPI mode. */
  220. base->CR0 = sspreg->CR0;
  221. base->CR1 = sspreg->CR1;
  222. base->CPSR = sspreg->CPSR;
  223. /* Finally activate the node's chip select. */
  224. rc = Lpc17xxSspChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) != 0);
  225. if (rc) {
  226. /* Release the bus in case of an error. */
  227. NutEventPost(&node->node_bus->bus_mutex);
  228. }
  229. }
  230. return rc;
  231. }
  232. /*!
  233. * \brief Update SSP shadow registers.
  234. *
  235. * \param node Specifies the SPI bus node.
  236. *
  237. * \return Always 0.
  238. */
  239. static int Lpc17xxSspSetup(NUTSPINODE * node)
  240. {
  241. uint32_t clk;
  242. uint32_t prescale;
  243. uint32_t cr0_div;
  244. uint32_t cmp_clk;
  245. LPC_SSP_TypeDef *sspreg;
  246. NUTASSERT(node != NULL);
  247. NUTASSERT(node->node_stat != NULL);
  248. NUTASSERT(node->node_bus != NULL);
  249. NUTASSERT(node->node_bus->bus_base != 0);
  250. sspreg = node->node_stat;
  251. sspreg->CR0 &= ~(SSP_CR0_DSS_MSK | SSP_CR0_FRF_MSK | SSP_CR0_CPOL | SSP_CR0_CPHA | SSP_CR0_SCR_MSK);
  252. sspreg->CR0 |= SSP_CR0_DSS (node->node_bits);
  253. if (node->node_mode & SPI_MODE_CPOL) {
  254. sspreg->CR0 |= SSP_CR0_CPOL;
  255. }
  256. if (node->node_mode & SPI_MODE_CPHA) {
  257. sspreg->CR0 |= SSP_CR0_CPHA;
  258. }
  259. sspreg->CR0 |= SSP_CR0_FRF_SPI;
  260. /* master only for now */
  261. sspreg->CR1 &= ~(SSP_CR1_LBM | SSP_CR1_SSE | SSP_CR1_MS | SSP_CR1_SOD);
  262. clk = NutClockGet(NUT_HWCLK_PCLK);
  263. #if defined(MCU_LPC176x)
  264. if (node->node_bus->bus_base == LPC_SSP0_BASE) {
  265. clk /= Lpc176x_PclkDivGet(CLKPWR_PCLKSEL_SSP0);
  266. } else if (node->node_bus->bus_base == LPC_SSP1_BASE) {
  267. clk /= Lpc176x_PclkDivGet(CLKPWR_PCLKSEL_SSP1);
  268. }
  269. #if defined(LPC_SSP2_BASE)
  270. else if (node->node_bus->bus_base == LPC_SSP2_BASE) {
  271. clk /= Lpc176x_PclkDivGet(CLKPWR_PCLKSEL_SSP2);
  272. }
  273. #endif
  274. #endif
  275. /* Find closest divider to get at or under the target frequency.
  276. Use smallest prescale possible and rely on the divider to get
  277. the closest target frequency */
  278. cr0_div = 0;
  279. cmp_clk = 0xFFFFFFFF;
  280. prescale = 2;
  281. while (cmp_clk > node->node_rate)
  282. {
  283. cmp_clk = clk / ((cr0_div + 1) * prescale);
  284. if (cmp_clk > node->node_rate)
  285. {
  286. cr0_div++;
  287. if (cr0_div > 0xFF)
  288. {
  289. cr0_div = 0;
  290. prescale += 2;
  291. }
  292. }
  293. }
  294. /* Write computed prescaler and divider back to register */
  295. sspreg->CR0 &= ~SSP_CR0_SCR_MSK;
  296. sspreg->CR0 |= (cr0_div << SSP_CR0_SCR_LSB) & SSP_CR0_SCR_MSK;
  297. sspreg->CPSR = prescale & 0xFF;
  298. /* Update interface parameters. */
  299. node->node_rate = (clk / prescale) / (cr0_div + 1);
  300. node->node_mode &= ~SPI_MODE_UPDATE;
  301. return 0;
  302. }
  303. /*!
  304. * \brief Initialize an SSP bus node.
  305. *
  306. * This routine is called for each SSP node, which is registered via
  307. * NutRegisterSpiDevice().
  308. *
  309. * \param node Specifies the SSP bus node.
  310. *
  311. * \return 0 on success or -1 if there is no valid chip select.
  312. */
  313. static int Lpc17xxSspBusNodeInit(NUTSPINODE * node)
  314. {
  315. int rc = -1;
  316. /* Sanity check. */
  317. NUTASSERT(node != NULL);
  318. NUTASSERT(node->node_bus != NULL);
  319. if (node->node_bus->bus_base == LPC_SSP0_BASE) {
  320. GpioPinConfigSet(SSP0BUS_SCK_PORT, SSP0BUS_SCK_PIN, SSP0BUS_SCK_PIN_CFG); // SCK
  321. GpioPinConfigSet(SSP0BUS_MISO_PORT, SSP0BUS_MISO_PIN, SSP0BUS_MISO_PIN_CFG); // MISO
  322. GpioPinConfigSet(SSP0BUS_MOSI_PORT, SSP0BUS_MOSI_PIN, SSP0BUS_MOSI_PIN_CFG); // MOSI
  323. } else if (node->node_bus->bus_base == LPC_SSP1_BASE) {
  324. GpioPinConfigSet(SSP1BUS_SCK_PORT, SSP1BUS_SCK_PIN, SSP1BUS_SCK_PIN_CFG); // SCK
  325. GpioPinConfigSet(SSP1BUS_MISO_PORT, SSP1BUS_MISO_PIN, SSP1BUS_MISO_PIN_CFG); // MISO
  326. GpioPinConfigSet(SSP1BUS_MOSI_PORT, SSP1BUS_MOSI_PIN, SSP1BUS_MOSI_PIN_CFG); // MOSI
  327. }
  328. #if defined(LPC_SSP2_BASE)
  329. else if (node->node_bus->bus_base == LPC_SSP2_BASE) {
  330. GpioPinConfigSet(SSP2BUS_SCK_PORT, SSP2BUS_SCK_PIN, SSP2BUS_SCK_PIN_CFG); // SCK
  331. GpioPinConfigSet(SSP2BUS_MISO_PORT, SSP2BUS_MISO_PIN, SSP2BUS_MISO_PIN_CFG); // MISO
  332. GpioPinConfigSet(SSP2BUS_MOSI_PORT, SSP2BUS_MOSI_PIN, SSP2BUS_MOSI_PIN_CFG); // MOSI
  333. }
  334. #endif
  335. if (node->node_cs != 0xFF) {
  336. GpioPinConfigSet(node->node_cs / 32, node->node_cs % 32, GPIO_CFG_OUTPUT);
  337. }
  338. /* Try to deactivate the node's chip select. */
  339. rc = Lpc17xxSspChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  340. /* It should not hurt us being called more than once. Thus, we
  341. ** check wether any initialization had been taken place already. */
  342. if (rc == 0 && node->node_stat == NULL)
  343. {
  344. /* Allocate and set our shadow registers. */
  345. LPC_SSP_TypeDef *sspreg = malloc(sizeof(LPC_SSP_TypeDef));
  346. if (sspreg) {
  347. /* Update with node's defaults. */
  348. node->node_stat = (void *)sspreg;
  349. Lpc17xxSspSetup(node);
  350. }
  351. else {
  352. /* Out of memory? */
  353. rc = -1;
  354. }
  355. }
  356. return rc;
  357. }
  358. /*!
  359. * \brief Transfer data on the SSP bus using single buffered interrupt mode.
  360. *
  361. * A device must have been selected by calling At91SpiSelect().
  362. *
  363. * \param node Specifies the SSP bus node.
  364. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  365. * byte values are transmitted.
  366. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  367. * data is discarded.
  368. * \param xlen Number of bytes to transfer.
  369. *
  370. * \return Always 0.
  371. */
  372. static int Lpc17xxSspBusTransfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  373. {
  374. LPC_SSP_TypeDef* base;
  375. /* Sanity check. */
  376. NUTASSERT(node != NULL);
  377. NUTASSERT(node->node_bus != NULL);
  378. NUTASSERT(node->node_bus->bus_base != 0);
  379. base = (LPC_SSP_TypeDef*) node->node_bus->bus_base;
  380. unsigned char *tx = (unsigned char*) txbuf;
  381. unsigned char *rx = (unsigned char*) rxbuf;
  382. base->CR1 |= SSP_CR1_SSE;
  383. while (xlen-- > 0) {
  384. unsigned char b = tx ? (*tx++) : 0xff;
  385. base->DR = b;
  386. /* wait until receive buffer no longer empty */
  387. while ((base->SR & SSP_SR_RNE) == 0)
  388. ;
  389. b = base->DR;
  390. if (rx) {
  391. *rx++ = b;
  392. }
  393. }
  394. base->CR1 &= ~SSP_CR1_SSE;
  395. return 0;
  396. }