PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/os/hal/platforms/LPC13xx/spi_lld.c

https://bitbucket.org/minux/chibios
C | 404 lines | 194 code | 45 blank | 165 comment | 40 complexity | d8fc2201886e05565194e849eb254760 MD5 | raw file
  1. /*
  2. ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file LPC13xx/spi_lld.c
  15. * @brief LPC13xx low level SPI driver code.
  16. *
  17. * @addtogroup SPI
  18. * @{
  19. */
  20. #include "ch.h"
  21. #include "hal.h"
  22. #if HAL_USE_SPI || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver exported variables. */
  25. /*===========================================================================*/
  26. #if LPC13xx_SPI_USE_SSP0 || defined(__DOXYGEN__)
  27. /** @brief SPI1 driver identifier.*/
  28. SPIDriver SPID1;
  29. #endif
  30. #if LPC13xx_SPI_USE_SSP1 || defined(__DOXYGEN__)
  31. /** @brief SPI2 driver identifier.*/
  32. SPIDriver SPID2;
  33. #endif
  34. /*===========================================================================*/
  35. /* Driver local variables and types. */
  36. /*===========================================================================*/
  37. /*===========================================================================*/
  38. /* Driver local functions. */
  39. /*===========================================================================*/
  40. /**
  41. * @brief Preloads the transmit FIFO.
  42. *
  43. * @param[in] spip pointer to the @p SPIDriver object
  44. */
  45. static void ssp_fifo_preload(SPIDriver *spip) {
  46. LPC_SSP_TypeDef *ssp = spip->ssp;
  47. uint32_t n = spip->txcnt > LPC13xx_SSP_FIFO_DEPTH ?
  48. LPC13xx_SSP_FIFO_DEPTH : spip->txcnt;
  49. while(((ssp->SR & SR_TNF) != 0) && (n > 0)) {
  50. if (spip->txptr != NULL) {
  51. if ((ssp->CR0 & CR0_DSSMASK) > CR0_DSS8BIT) {
  52. const uint16_t *p = spip->txptr;
  53. ssp->DR = *p++;
  54. spip->txptr = p;
  55. }
  56. else {
  57. const uint8_t *p = spip->txptr;
  58. ssp->DR = *p++;
  59. spip->txptr = p;
  60. }
  61. }
  62. else
  63. ssp->DR = 0xFFFFFFFF;
  64. n--;
  65. spip->txcnt--;
  66. }
  67. }
  68. /**
  69. * @brief Common IRQ handler.
  70. *
  71. * @param[in] spip pointer to the @p SPIDriver object
  72. */
  73. static void spi_serve_interrupt(SPIDriver *spip) {
  74. LPC_SSP_TypeDef *ssp = spip->ssp;
  75. if ((ssp->MIS & MIS_ROR) != 0) {
  76. /* The overflow condition should never happen because priority is given
  77. to receive but a hook macro is provided anyway...*/
  78. LPC13xx_SPI_SSP_ERROR_HOOK(spip);
  79. }
  80. ssp->ICR = ICR_RT | ICR_ROR;
  81. while ((ssp->SR & SR_RNE) != 0) {
  82. if (spip->rxptr != NULL) {
  83. if ((ssp->CR0 & CR0_DSSMASK) > CR0_DSS8BIT) {
  84. uint16_t *p = spip->rxptr;
  85. *p++ = ssp->DR;
  86. spip->rxptr = p;
  87. }
  88. else {
  89. uint8_t *p = spip->rxptr;
  90. *p++ = ssp->DR;
  91. spip->rxptr = p;
  92. }
  93. }
  94. else
  95. (void)ssp->DR;
  96. if (--spip->rxcnt == 0) {
  97. chDbgAssert(spip->txcnt == 0,
  98. "spi_serve_interrupt(), #1", "counter out of synch");
  99. /* Stops the IRQ sources.*/
  100. ssp->IMSC = 0;
  101. /* Portable SPI ISR code defined in the high level driver, note, it is
  102. a macro.*/
  103. _spi_isr_code(spip);
  104. return;
  105. }
  106. }
  107. ssp_fifo_preload(spip);
  108. if (spip->txcnt == 0)
  109. ssp->IMSC = IMSC_ROR | IMSC_RT | IMSC_RX;
  110. }
  111. /*===========================================================================*/
  112. /* Driver interrupt handlers. */
  113. /*===========================================================================*/
  114. #if LPC13xx_SPI_USE_SSP0 || defined(__DOXYGEN__)
  115. /**
  116. * @brief SSP0 interrupt handler.
  117. *
  118. * @isr
  119. */
  120. CH_IRQ_HANDLER(VectorF4) {
  121. CH_IRQ_PROLOGUE();
  122. spi_serve_interrupt(&SPID1);
  123. CH_IRQ_EPILOGUE();
  124. }
  125. #endif
  126. #if LPC13xx_SPI_USE_SSP1 || defined(__DOXYGEN__)
  127. /**
  128. * @brief SSP1 interrupt handler.
  129. *
  130. * @isr
  131. */
  132. CH_IRQ_HANDLER(Vector124) {
  133. CH_IRQ_PROLOGUE();
  134. spi_serve_interrupt(&SPID2);
  135. CH_IRQ_EPILOGUE();
  136. }
  137. #endif
  138. /*===========================================================================*/
  139. /* Driver exported functions. */
  140. /*===========================================================================*/
  141. /**
  142. * @brief Low level SPI driver initialization.
  143. *
  144. * @notapi
  145. */
  146. void spi_lld_init(void) {
  147. #if LPC13xx_SPI_USE_SSP0
  148. spiObjectInit(&SPID1);
  149. SPID1.ssp = LPC_SSP0;
  150. LPC_IOCON->SCK_LOC = LPC13xx_SPI_SCK0_SELECTOR;
  151. #if LPC13xx_SPI_SCK0_SELECTOR == SCK0_IS_PIO0_10
  152. LPC_IOCON->SWCLK_PIO0_10 = 0xC2; /* SCK0 without resistors. */
  153. #elif LPC13xx_SPI_SCK0_SELECTOR == SCK0_IS_PIO2_11
  154. LPC_IOCON->PIO2_11 = 0xC1; /* SCK0 without resistors. */
  155. #else /* LPC13xx_SPI_SCK0_SELECTOR == SCK0_IS_PIO0_6 */
  156. LPC_IOCON->PIO0_6 = 0xC2; /* SCK0 without resistors. */
  157. #endif
  158. LPC_IOCON->PIO0_8 = 0xC1; /* MISO0 without resistors. */
  159. LPC_IOCON->PIO0_9 = 0xC1; /* MOSI0 without resistors. */
  160. #endif /* LPC13xx_SPI_USE_SSP0 */
  161. #if LPC13xx_SPI_USE_SSP1
  162. spiObjectInit(&SPID2);
  163. SPID2.ssp = LPC_SSP1;
  164. LPC_IOCON->PIO2_1 = 0xC2; /* SCK1 without resistors. */
  165. LPC_IOCON->PIO2_2 = 0xC2; /* MISO1 without resistors. */
  166. LPC_IOCON->PIO2_3 = 0xC2; /* MOSI1 without resistors. */
  167. #endif /* LPC13xx_SPI_USE_SSP0 */
  168. }
  169. /**
  170. * @brief Configures and activates the SPI peripheral.
  171. *
  172. * @param[in] spip pointer to the @p SPIDriver object
  173. *
  174. * @notapi
  175. */
  176. void spi_lld_start(SPIDriver *spip) {
  177. if (spip->state == SPI_STOP) {
  178. /* Clock activation.*/
  179. #if LPC13xx_SPI_USE_SSP0
  180. if (&SPID1 == spip) {
  181. LPC_SYSCON->SSP0CLKDIV = LPC13xx_SPI_SSP0CLKDIV;
  182. LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 11);
  183. LPC_SYSCON->PRESETCTRL |= 1;
  184. nvicEnableVector(SSP0_IRQn,
  185. CORTEX_PRIORITY_MASK(LPC13xx_SPI_SSP0_IRQ_PRIORITY));
  186. }
  187. #endif
  188. #if LPC13xx_SPI_USE_SSP1
  189. if (&SPID2 == spip) {
  190. LPC_SYSCON->SSP1CLKDIV = LPC13xx_SPI_SSP1CLKDIV;
  191. LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 18);
  192. LPC_SYSCON->PRESETCTRL |= 4;
  193. nvicEnableVector(SSP1_IRQn,
  194. CORTEX_PRIORITY_MASK(LPC13xx_SPI_SSP1_IRQ_PRIORITY));
  195. }
  196. #endif
  197. }
  198. /* Configuration.*/
  199. spip->ssp->CR1 = 0;
  200. spip->ssp->ICR = ICR_RT | ICR_ROR;
  201. spip->ssp->CR0 = spip->config->cr0;
  202. spip->ssp->CPSR = spip->config->cpsr;
  203. spip->ssp->CR1 = CR1_SSE;
  204. }
  205. /**
  206. * @brief Deactivates the SPI peripheral.
  207. *
  208. * @param[in] spip pointer to the @p SPIDriver object
  209. *
  210. * @notapi
  211. */
  212. void spi_lld_stop(SPIDriver *spip) {
  213. if (spip->state != SPI_STOP) {
  214. spip->ssp->CR1 = 0;
  215. spip->ssp->CR0 = 0;
  216. spip->ssp->CPSR = 0;
  217. #if LPC13xx_SPI_USE_SSP0
  218. if (&SPID1 == spip) {
  219. LPC_SYSCON->PRESETCTRL &= ~1;
  220. LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 11);
  221. LPC_SYSCON->SSP0CLKDIV = 0;
  222. nvicDisableVector(SSP0_IRQn);
  223. }
  224. #endif
  225. #if LPC13xx_SPI_USE_SSP1
  226. if (&SPID2 == spip) {
  227. LPC_SYSCON->PRESETCTRL &= ~4;
  228. LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 18);
  229. LPC_SYSCON->SSP1CLKDIV = 0;
  230. nvicDisableVector(SSP1_IRQn);
  231. }
  232. #endif
  233. }
  234. }
  235. /**
  236. * @brief Asserts the slave select signal and prepares for transfers.
  237. *
  238. * @param[in] spip pointer to the @p SPIDriver object
  239. *
  240. * @notapi
  241. */
  242. void spi_lld_select(SPIDriver *spip) {
  243. palClearPad(spip->config->ssport, spip->config->sspad);
  244. }
  245. /**
  246. * @brief Deasserts the slave select signal.
  247. * @details The previously selected peripheral is unselected.
  248. *
  249. * @param[in] spip pointer to the @p SPIDriver object
  250. *
  251. * @notapi
  252. */
  253. void spi_lld_unselect(SPIDriver *spip) {
  254. palSetPad(spip->config->ssport, spip->config->sspad);
  255. }
  256. /**
  257. * @brief Ignores data on the SPI bus.
  258. * @details This function transmits a series of idle words on the SPI bus and
  259. * ignores the received data. This function can be invoked even
  260. * when a slave select signal has not been yet asserted.
  261. *
  262. * @param[in] spip pointer to the @p SPIDriver object
  263. * @param[in] n number of words to be ignored
  264. *
  265. * @notapi
  266. */
  267. void spi_lld_ignore(SPIDriver *spip, size_t n) {
  268. spip->rxptr = NULL;
  269. spip->txptr = NULL;
  270. spip->rxcnt = spip->txcnt = n;
  271. ssp_fifo_preload(spip);
  272. spip->ssp->IMSC = IMSC_ROR | IMSC_RT | IMSC_TX | IMSC_RX;
  273. }
  274. /**
  275. * @brief Exchanges data on the SPI bus.
  276. * @details This asynchronous function starts a simultaneous transmit/receive
  277. * operation.
  278. * @post At the end of the operation the configured callback is invoked.
  279. * @note The buffers are organized as uint8_t arrays for data sizes below or
  280. * equal to 8 bits else it is organized as uint16_t arrays.
  281. *
  282. * @param[in] spip pointer to the @p SPIDriver object
  283. * @param[in] n number of words to be exchanged
  284. * @param[in] txbuf the pointer to the transmit buffer
  285. * @param[out] rxbuf the pointer to the receive buffer
  286. *
  287. * @notapi
  288. */
  289. void spi_lld_exchange(SPIDriver *spip, size_t n,
  290. const void *txbuf, void *rxbuf) {
  291. spip->rxptr = rxbuf;
  292. spip->txptr = txbuf;
  293. spip->rxcnt = spip->txcnt = n;
  294. ssp_fifo_preload(spip);
  295. spip->ssp->IMSC = IMSC_ROR | IMSC_RT | IMSC_TX | IMSC_RX;
  296. }
  297. /**
  298. * @brief Sends data over the SPI bus.
  299. * @details This asynchronous function starts a transmit operation.
  300. * @post At the end of the operation the configured callback is invoked.
  301. * @note The buffers are organized as uint8_t arrays for data sizes below or
  302. * equal to 8 bits else it is organized as uint16_t arrays.
  303. *
  304. * @param[in] spip pointer to the @p SPIDriver object
  305. * @param[in] n number of words to send
  306. * @param[in] txbuf the pointer to the transmit buffer
  307. *
  308. * @notapi
  309. */
  310. void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf) {
  311. spip->rxptr = NULL;
  312. spip->txptr = txbuf;
  313. spip->rxcnt = spip->txcnt = n;
  314. ssp_fifo_preload(spip);
  315. spip->ssp->IMSC = IMSC_ROR | IMSC_RT | IMSC_TX | IMSC_RX;
  316. }
  317. /**
  318. * @brief Receives data from the SPI bus.
  319. * @details This asynchronous function starts a receive operation.
  320. * @post At the end of the operation the configured callback is invoked.
  321. * @note The buffers are organized as uint8_t arrays for data sizes below or
  322. * equal to 8 bits else it is organized as uint16_t arrays.
  323. *
  324. * @param[in] spip pointer to the @p SPIDriver object
  325. * @param[in] n number of words to receive
  326. * @param[out] rxbuf the pointer to the receive buffer
  327. *
  328. * @notapi
  329. */
  330. void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf) {
  331. spip->rxptr = rxbuf;
  332. spip->txptr = NULL;
  333. spip->rxcnt = spip->txcnt = n;
  334. ssp_fifo_preload(spip);
  335. spip->ssp->IMSC = IMSC_ROR | IMSC_RT | IMSC_TX | IMSC_RX;
  336. }
  337. /**
  338. * @brief Exchanges one frame using a polled wait.
  339. * @details This synchronous function exchanges one frame using a polled
  340. * synchronization method. This function is useful when exchanging
  341. * small amount of data on high speed channels, usually in this
  342. * situation is much more efficient just wait for completion using
  343. * polling than suspending the thread waiting for an interrupt.
  344. *
  345. * @param[in] spip pointer to the @p SPIDriver object
  346. * @param[in] frame the data frame to send over the SPI bus
  347. * @return The received data frame from the SPI bus.
  348. */
  349. uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame) {
  350. spip->ssp->DR = (uint32_t)frame;
  351. while ((spip->ssp->SR & SR_RNE) == 0)
  352. ;
  353. return (uint16_t)spip->ssp->DR;
  354. }
  355. #endif /* HAL_USE_SPI */
  356. /** @} */