spi_8xx.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /*
  2. * @brief LPC8XX SPI common functions and definitions
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #ifndef __SPI_8XX_H__
  32. #define __SPI_8XX_H__
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup SPI_COMMON_8XX CHIP: LPC8XX SPI common functions and definitions
  37. * @ingroup CHIP_8XX_Drivers
  38. * @{
  39. */
  40. /**
  41. * @brief SPI register block structure
  42. */
  43. typedef struct { /*!< SPI Structure */
  44. __IO uint32_t CFG; /*!< SPI Configuration register */
  45. __IO uint32_t DLY; /*!< SPI Delay register */
  46. __IO uint32_t STAT; /*!< SPI Status register */
  47. __IO uint32_t INTENSET; /*!< SPI Interrupt Enable Set register */
  48. __O uint32_t INTENCLR; /*!< SPI Interrupt Enable Clear register */
  49. __I uint32_t RXDAT; /*!< SPI Receive Data register */
  50. __IO uint32_t TXDATCTL; /*!< SPI Transmit Data with Control register */
  51. __IO uint32_t TXDAT; /*!< SPI Transmit Data register */
  52. __IO uint32_t TXCTRL; /*!< SPI Transmit Control register */
  53. __IO uint32_t DIV; /*!< SPI clock Divider register */
  54. __I uint32_t INTSTAT; /*!< SPI Interrupt Status register */
  55. } LPC_SPI_T;
  56. /* Reserved bits masks for registers */
  57. #define SPI_CFG_RESERVED ((1<<1)|(1<<6)|0xfffffe00)
  58. #define SPI_DLY_RESERVED 0xffff0000
  59. #define SPI_STAT_RESERVED (~0x1ff)
  60. #define SPI_INTENSET_RESERVED (~0x3f)
  61. #define SPI_INTENCLR_RESERVED (~0x3f)
  62. #define SPI_RXDAT_RESERVED ((7<<17)|(0x7ffu<<21))
  63. #define SPI_TXDATCTL_RESERVED ((7<<17)|(1<<23)|(0xfu<<28))
  64. #define SPI_TXDAT_RESERVED 0xffff0000
  65. #define SPI_TXCTRL_RESERVED (0xffff|(7<<17)|(1<<23)|(0xfu<<28))
  66. #define SPI_DIV_RESERVED 0xffff0000
  67. #define SPI_INTSTAT_RESERVED (~0x3f)
  68. /**
  69. * Macro defines for SPI Configuration register
  70. */
  71. #define SPI_CFG_BITMASK (0x1BD) /** SPI register bit mask */
  72. #define SPI_CFG_SPI_EN (1 << 0) /** SPI Slave Mode Select */
  73. #define SPI_CFG_SLAVE_EN (0 << 0) /** SPI Master Mode Select */
  74. #define SPI_CFG_MASTER_EN (1 << 2) /** SPI MSB First mode enable */
  75. #define SPI_CFG_MSB_FIRST_EN (0 << 3) /** SPI LSB First mode enable */
  76. #define SPI_CFG_LSB_FIRST_EN (1 << 3) /** SPI Clock Phase Select */
  77. #define SPI_CFG_CPHA_FIRST (0 << 4) /** Capture data on the first edge, Change data on the following edge */
  78. #define SPI_CFG_CPHA_SECOND (1 << 4) /** SPI Clock Polarity Select */
  79. #define SPI_CFG_CPOL_LO (0 << 5) /** The rest state of the clock (between frames) is low. */
  80. #define SPI_CFG_CPOL_HI (1 << 5) /** The rest state of the clock (between frames) is high. */
  81. #define SPI_CFG_LBM_EN (1 << 7) /** SPI control 1 loopback mode enable */
  82. #define SPI_CFG_SPOL_LO (0 << 8) /** SPI SSEL0 Polarity Select */
  83. #define SPI_CFG_SPOL_HI (1 << 8) /** SSEL0 is active High */
  84. #define SPI_CFG_SPOLNUM_HI(n) (1 << ((n) + 8)) /** SSELN is active High, selects 0 - 3 */
  85. /**
  86. * Macro defines for SPI Delay register
  87. */
  88. #define SPI_DLY_BITMASK (0xFFFF) /** SPI DLY Register Mask */
  89. #define SPI_DLY_PRE_DELAY(n) (((n) & 0x0F) << 0) /** Time in SPI clocks between SSEL assertion and the beginning of a data frame */
  90. #define SPI_DLY_POST_DELAY(n) (((n) & 0x0F) << 4) /** Time in SPI clocks between the end of a data frame and SSEL deassertion. */
  91. #define SPI_DLY_FRAME_DELAY(n) (((n) & 0x0F) << 8) /** Minimum time in SPI clocks between adjacent data frames. */
  92. #define SPI_DLY_TRANSFER_DELAY(n) (((n) & 0x0F) << 12) /** Minimum time in SPI clocks that the SSEL is deasserted between transfers. */
  93. /**
  94. * Macro defines for SPI Status register
  95. */
  96. #define SPI_STAT_BITMASK (0x1FF) /** SPI STAT Register BitMask */
  97. #define SPI_STAT_RXRDY (1 << 0) /** Receiver Ready Flag */
  98. #define SPI_STAT_TXRDY (1 << 1) /** Transmitter Ready Flag */
  99. #define SPI_STAT_RXOV (1 << 2) /** Receiver Overrun interrupt flag */
  100. #define SPI_STAT_TXUR (1 << 3) /** Transmitter Underrun interrupt flag (In Slave Mode only) */
  101. #define SPI_STAT_SSA (1 << 4) /** Slave Select Assert */
  102. #define SPI_STAT_SSD (1 << 5) /** Slave Select Deassert */
  103. #define SPI_STAT_STALLED (1 << 6) /** Stalled status flag */
  104. #define SPI_STAT_EOT (1 << 7) /** End Transfer flag */
  105. #define SPI_STAT_MSTIDLE (1 << 8) /** Idle status flag */
  106. /**
  107. * Macro defines for SPI Interrupt Enable read and Set register
  108. */
  109. #define SPI_INTENSET_BITMASK (0x3F) /** SPI INTENSET Register BitMask */
  110. #define SPI_INTENSET_RXDYEN (1 << 0) /** Enable Interrupt when receiver data is available */
  111. #define SPI_INTENSET_TXDYEN (1 << 1) /** Enable Interrupt when the transmitter holding register is available. */
  112. #define SPI_INTENSET_RXOVEN (1 << 2) /** Enable Interrupt when a receiver overrun occurs */
  113. #define SPI_INTENSET_TXUREN (1 << 3) /** Enable Interrupt when a transmitter underrun occurs (In Slave Mode Only)*/
  114. #define SPI_INTENSET_SSAEN (1 << 4) /** Enable Interrupt when the Slave Select is asserted.*/
  115. #define SPI_INTENSET_SSDEN (1 << 5) /** Enable Interrupt when the Slave Select is deasserted..*/
  116. /**
  117. * Macro defines for SPI Interrupt Enable Clear register
  118. */
  119. #define SPI_INTENCLR_BITMASK (0x3F) /** SPI INTENCLR Register BitMask */
  120. #define SPI_INTENCLR_RXDYEN (1 << 0) /** Disable Interrupt when receiver data is available */
  121. #define SPI_INTENCLR_TXDYEN (1 << 1) /** Disable Interrupt when the transmitter holding register is available. */
  122. #define SPI_INTENCLR_RXOVEN (1 << 2) /** Disable Interrupt when a receiver overrun occurs */
  123. #define SPI_INTENCLR_TXUREN (1 << 3) /** Disable Interrupt when a transmitter underrun occurs (In Slave Mode Only) */
  124. #define SPI_INTENCLR_SSAEN (1 << 4) /** Disable Interrupt when the Slave Select is asserted. */
  125. #define SPI_INTENCLR_SSDEN (1 << 5) /** Disable Interrupt when the Slave Select is deasserted.. */
  126. /**
  127. * Macro defines for SPI Receiver Data register
  128. */
  129. #define SPI_RXDAT_BITMASK (0x1FFFFF) /** SPI RXDAT Register BitMask */
  130. #define SPI_RXDAT_DATA(n) ((n) & 0xFFFF) /** Receiver Data */
  131. #define SPI_RXDAT_RXSSELN_ACTIVE (0 << 16) /** The state of SSEL pin is active */
  132. #define SPI_RXDAT_RXSSELN_INACTIVE ((1 << 16) /** The state of SSEL pin is inactive */
  133. #define SPI_RXDAT_RXSSELNUM_INACTIVE(n) (1 << ((n) + 16)) /** The state of SSELN pin is inactive */
  134. #define SPI_RXDAT_SOT (1 << 20) /** Start of Transfer flag */
  135. /**
  136. * Macro defines for SPI Transmitter Data and Control register
  137. */
  138. #define SPI_TXDATCTL_BITMASK (0xF71FFFF) /** SPI TXDATCTL Register BitMask */
  139. #define SPI_TXDATCTL_DATA(n) ((n) & 0xFFFF) /** SPI Transmit Data */
  140. #define SPI_TXDATCTL_CTRLMASK (0xF710000) /** SPI TXDATCTL Register BitMask for control bits only */
  141. #define SPI_TXDATCTL_ASSERT_SSEL (0 << 16) /** Assert SSEL0 pin */
  142. #define SPI_TXDATCTL_DEASSERT_SSEL (1 << 16) /** Deassert SSEL0 pin */
  143. #define SPI_TXDATCTL_DEASSERTNUM_SSEL(n) (1 << ((n) + 16)) /** Deassert SSELN pin */
  144. #define SPI_TXDATCTL_DEASSERT_ALL (0xF << 16) /** Deassert all SSEL pins */
  145. #define SPI_TXDATCTL_EOT (1 << 20) /** End of Transfer flag (TRANSFER_DELAY is applied after sending the current frame) */
  146. #define SPI_TXDATCTL_EOF (1 << 21) /** End of Frame flag (FRAME_DELAY is applied after sending the current part) */
  147. #define SPI_TXDATCTL_RXIGNORE (1 << 22) /** Receive Ignore Flag */
  148. #define SPI_TXDATCTL_FLEN(n) (((n) & 0x0F) << 24) /** Frame length - 1 */
  149. /**
  150. * Macro defines for SPI Transmitter Data Register
  151. */
  152. #define SPI_TXDAT_DATA(n) ((n) & 0xFFFF) /** SPI Transmit Data */
  153. /**
  154. * Macro defines for SPI Transmitter Control register
  155. */
  156. #define SPI_TXCTL_BITMASK (0xF7F0000) /** SPI TXDATCTL Register BitMask */
  157. #define SPI_TXCTL_ASSERT_SSEL (0 << 16) /** Assert SSEL0 pin */
  158. #define SPI_TXCTL_DEASSERT_SSEL (1 << 16) /** Deassert SSEL0 pin */
  159. #define SPI_TXCTL_DEASSERTNUM_SSEL(n) (1 << ((n) + 16)) /** Deassert SSELN pin */
  160. #define SPI_TXDATCTL_DEASSERT_ALL (0xF << 16) /** Deassert all SSEL pins */
  161. #define SPI_TXCTL_EOT (1 << 20) /** End of Transfer flag (TRANSFER_DELAY is applied after sending the current frame) */
  162. #define SPI_TXCTL_EOF (1 << 21) /** End of Frame flag (FRAME_DELAY is applied after sending the current part) */
  163. #define SPI_TXCTL_RXIGNORE (1 << 22) /** Receive Ignore Flag */
  164. #define SPI_TXCTL_FLEN(n) ((((n) - 1) & 0x0F) << 24) /** Frame length, 0 - 16 */
  165. #define SPI_TXCTL_FLENMASK (0xF << 24) /** Frame length mask */
  166. /**
  167. * Macro defines for SPI Divider register
  168. */
  169. #define SPI_DIV_VAL(n) ((n) & 0xFFFF) /** Rate divider value mask (In Master Mode only)*/
  170. /**
  171. * Macro defines for SPI Interrupt Status register
  172. */
  173. #define SPI_INTSTAT_BITMASK (0x3F) /** SPI INTSTAT Register Bitmask */
  174. #define SPI_INTSTAT_RXRDY (1 << 0) /** Receiver Ready Flag */
  175. #define SPI_INTSTAT_TXRDY (1 << 1) /** Transmitter Ready Flag */
  176. #define SPI_INTSTAT_RXOV (1 << 2) /** Receiver Overrun interrupt flag */
  177. #define SPI_INTSTAT_TXUR (1 << 3) /** Transmitter Underrun interrupt flag (In Slave Mode only) */
  178. #define SPI_INTSTAT_SSA (1 << 4) /** Slave Select Assert */
  179. #define SPI_INTSTAT_SSD (1 << 5) /** Slave Select Deassert */
  180. /** @brief SPI Clock Mode*/
  181. #define SPI_CLOCK_CPHA0_CPOL0 (SPI_CFG_CPOL_LO | SPI_CFG_CPHA_FIRST) /**< CPHA = 0, CPOL = 0 */
  182. #define SPI_CLOCK_MODE0 SPI_CLOCK_CPHA0_CPOL0 /**< Alias for CPHA = 0, CPOL = 0 */
  183. #define SPI_CLOCK_CPHA1_CPOL0 SPI_CFG_CPOL_LO | SPI_CFG_CPHA_SECOND /**< CPHA = 0, CPOL = 1 */
  184. #define SPI_CLOCK_MODE1 SPI_CLOCK_CPHA1_CPOL0 /**< Alias for CPHA = 0, CPOL = 1 */
  185. #define SPI_CLOCK_CPHA0_CPOL1 SPI_CFG_CPOL_HI | SPI_CFG_CPHA_FIRST /**< CPHA = 1, CPOL = 0 */
  186. #define SPI_CLOCK_MODE2 SPI_CLOCK_CPHA0_CPOL1 /**< Alias for CPHA = 1, CPOL = 0 */
  187. #define SPI_CLOCK_CPHA1_CPOL1 SPI_CFG_CPOL_HI | SPI_CFG_CPHA_SECOND /**< CPHA = 1, CPOL = 1 */
  188. #define SPI_CLOCK_MODE3 SPI_CLOCK_CPHA1_CPOL1 /**< Alias for CPHA = 1, CPOL = 1 */
  189. /**
  190. * @brief Set SPI CFG register values
  191. * @param pSPI : The base of SPI peripheral on the chip
  192. * @param bits : CFG register bits to set, amd OR'ed value of SPI_CFG_* definitions
  193. * @return Nothing
  194. * @note This function safely sets only the selected bits in the SPI CFG register.
  195. * It can be used to enable multiple bits at once.
  196. */
  197. STATIC INLINE void Chip_SPI_SetCFGRegBits(LPC_SPI_T *pSPI, uint32_t bits)
  198. {
  199. /* Update CFG register with only selected bits disabled */
  200. pSPI->CFG = bits | (pSPI->CFG & SPI_CFG_BITMASK);
  201. }
  202. /**
  203. * @brief Clear SPI CFG register values
  204. * @param pSPI : The base of SPI peripheral on the chip
  205. * @param bits : CFG register bits to clear, amd OR'ed value of SPI_CFG_* definitions
  206. * @return Nothing
  207. * @note This function safely clears only the selected bits in the SPI CFG register.
  208. * It can be used to disable multiple bits at once.
  209. */
  210. STATIC INLINE void Chip_SPI_ClearCFGRegBits(LPC_SPI_T *pSPI, uint32_t bits)
  211. {
  212. /* Update CFG register with only selected bits disabled */
  213. pSPI->CFG = ~bits & (pSPI->CFG & SPI_CFG_BITMASK);
  214. }
  215. /**
  216. * @brief Initialize the SPI
  217. * @param pSPI : The base SPI peripheral on the chip
  218. * @return Nothing
  219. */
  220. STATIC INLINE void Chip_SPI_Init(LPC_SPI_T *pSPI)
  221. {
  222. /* Enable SPI clock and reset IP */
  223. if (pSPI == LPC_SPI1) {
  224. Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SPI1);
  225. Chip_SYSCTL_PeriphReset(RESET_SPI1);
  226. } else {
  227. Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SPI0);
  228. Chip_SYSCTL_PeriphReset(RESET_SPI0);
  229. }
  230. }
  231. /**
  232. * @brief Disable SPI peripheral
  233. * @param pSPI : The base of SPI peripheral on the chip
  234. * @return Nothing
  235. */
  236. STATIC INLINE void Chip_SPI_Disable(LPC_SPI_T *pSPI)
  237. {
  238. Chip_SPI_ClearCFGRegBits(pSPI, SPI_CFG_SPI_EN);
  239. }
  240. /**
  241. * @brief Disable SPI operation
  242. * @param pSPI : The base of SPI peripheral on the chip
  243. * @return Nothing
  244. * @note The SPI controller is disabled.
  245. */
  246. STATIC INLINE void Chip_SPI_DeInit(LPC_SPI_T *pSPI)
  247. {
  248. Chip_SPI_Disable(pSPI);
  249. if (pSPI == LPC_SPI1) {
  250. Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SPI1);
  251. } else {
  252. Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SPI0);
  253. }
  254. }
  255. /**
  256. * @brief Enable SPI peripheral
  257. * @param pSPI : The base of SPI peripheral on the chip
  258. * @return Nothing
  259. */
  260. STATIC INLINE void Chip_SPI_Enable(LPC_SPI_T *pSPI)
  261. {
  262. Chip_SPI_SetCFGRegBits(pSPI, SPI_CFG_SPI_EN);
  263. }
  264. /**
  265. * @brief Enable SPI master mode
  266. * @param pSPI : The base of SPI peripheral on the chip
  267. * @return Nothing
  268. * @note SPI slave mode will be disabled with this call. All SPI SSEL
  269. * lines will also be deasserted.
  270. */
  271. STATIC INLINE void Chip_SPI_EnableMasterMode(LPC_SPI_T *pSPI)
  272. {
  273. Chip_SPI_SetCFGRegBits(pSPI, SPI_CFG_MASTER_EN);
  274. /* Deassert all chip selects, only in master mode */
  275. pSPI->TXCTRL = SPI_TXDATCTL_DEASSERT_ALL;
  276. }
  277. /**
  278. * @brief Enable SPI slave mode
  279. * @param pSPI : The base of SPI peripheral on the chip
  280. * @return Nothing
  281. * @note SPI master mode will be disabled with this call.
  282. */
  283. STATIC INLINE void Chip_SPI_EnableSlaveMode(LPC_SPI_T *pSPI)
  284. {
  285. Chip_SPI_ClearCFGRegBits(pSPI, SPI_CFG_MASTER_EN);
  286. }
  287. /**
  288. * @brief Enable LSB First transfers
  289. * @param pSPI : The base of SPI peripheral on the chip
  290. * @return Nothing
  291. */
  292. STATIC INLINE void Chip_SPI_EnableLSBFirst(LPC_SPI_T *pSPI)
  293. {
  294. Chip_SPI_SetCFGRegBits(pSPI, SPI_CFG_LSB_FIRST_EN);
  295. }
  296. /**
  297. * @brief Enable MSB First transfers
  298. * @param pSPI : The base of SPI peripheral on the chip
  299. * @return Nothing
  300. */
  301. STATIC INLINE void Chip_SPI_EnableMSBFirst(LPC_SPI_T *pSPI)
  302. {
  303. Chip_SPI_ClearCFGRegBits(pSPI, SPI_CFG_LSB_FIRST_EN);
  304. }
  305. /**
  306. * @brief Set SPI mode
  307. * @param pSPI : The base of SPI peripheral on the chip
  308. * @param mode : SPI mode to set the SPI interface to
  309. * @return Nothing
  310. */
  311. STATIC INLINE void Chip_SPI_SetSPIMode(LPC_SPI_T *pSPI, uint32_t mode)
  312. {
  313. Chip_SPI_ClearCFGRegBits(pSPI, (SPI_CFG_CPOL_HI | SPI_CFG_CPHA_SECOND));
  314. Chip_SPI_SetCFGRegBits(pSPI, (uint32_t) mode);
  315. }
  316. /**
  317. * @brief Set polarity on the SPI chip select high
  318. * @param pSPI : The base of SPI peripheral on the chip
  319. * @param csNum : Chip select number, 0 - 3
  320. * @return Nothing
  321. * @note SPI chip select polarity is active high.
  322. */
  323. STATIC INLINE void Chip_SPI_SetCSPolHigh(LPC_SPI_T *pSPI, uint8_t csNum)
  324. {
  325. Chip_SPI_SetCFGRegBits(pSPI, SPI_CFG_SPOLNUM_HI(csNum));
  326. }
  327. /**
  328. * @brief Set polarity on the SPI chip select low
  329. * @param pSPI : The base of SPI peripheral on the chip
  330. * @param csNum : Chip select number, 0 - 3
  331. * @return Nothing
  332. * @note SPI chip select polarity is active low.
  333. */
  334. STATIC INLINE void Chip_SPI_SetCSPolLow(LPC_SPI_T *pSPI, uint8_t csNum)
  335. {
  336. Chip_SPI_ClearCFGRegBits(pSPI, SPI_CFG_SPOLNUM_HI(csNum));
  337. }
  338. /**
  339. * @brief Setup SPI configuration
  340. * @param pSPI : The base of SPI peripheral on the chip
  341. * @param config : ORed spi configuration flags
  342. * @return Nothing
  343. * @note Possible values that can be ORed in @a config are
  344. * SPI_CLOCK_* (example: #SPI_CLOCK_CPHA0_CPOL0) along with
  345. * SPI_CFG_* (example: #SPI_CFG_SPI_EN).
  346. */
  347. STATIC INLINE void Chip_SPI_ConfigureSPI(LPC_SPI_T *pSPI, uint32_t config)
  348. {
  349. Chip_SPI_ClearCFGRegBits(pSPI, SPI_CFG_MASTER_EN | SPI_CFG_LSB_FIRST_EN |
  350. SPI_CFG_CPHA_SECOND | SPI_CFG_CPOL_HI);
  351. Chip_SPI_SetCFGRegBits(pSPI, config);
  352. /* Deassert all chip selects, only in master mode */
  353. pSPI->TXCTRL = SPI_TXDATCTL_DEASSERT_ALL;
  354. }
  355. /**
  356. * @brief Get the current status of SPI controller
  357. * @param pSPI : The base of SPI peripheral on the chip
  358. * @return SPI Status (Or-ed bit value of SPI_STAT_*)
  359. * @note Mask the return value with a value of type SPI_STAT_* to determine
  360. * if that status is active.
  361. */
  362. STATIC INLINE uint32_t Chip_SPI_GetStatus(LPC_SPI_T *pSPI)
  363. {
  364. return pSPI->STAT & ~SPI_STAT_RESERVED;
  365. }
  366. /**
  367. * @brief Clear SPI status
  368. * @param pSPI : The base of SPI peripheral on the chip
  369. * @param Flag : Clear Flag (Or-ed bit value of SPI_STAT_*)
  370. * @return Nothing
  371. * @note Only SPI_STAT_RXOV, SPI_STAT_TXUR, SPI_STAT_SSA, and
  372. * SPI_STAT_SSD statuses can be cleared.
  373. */
  374. STATIC INLINE void Chip_SPI_ClearStatus(LPC_SPI_T *pSPI, uint32_t Flag)
  375. {
  376. pSPI->STAT = Flag;
  377. }
  378. /**
  379. * @brief Enable a SPI interrupt
  380. * @param pSPI : The base of SPI peripheral on the chip
  381. * @param Flag : Or'ed value of SPI_INTENSET_* values to enable
  382. * @return Nothing
  383. */
  384. STATIC INLINE void Chip_SPI_EnableInts(LPC_SPI_T *pSPI, uint32_t Flag)
  385. {
  386. pSPI->INTENSET = Flag;
  387. }
  388. /**
  389. * @brief Disable a SPI interrupt
  390. * @param pSPI : The base of SPI peripheral on the chip
  391. * @param Flag : Or'ed value of SPI_INTENCLR_* values to disable
  392. * @return Nothing
  393. */
  394. STATIC INLINE void Chip_SPI_DisableInts(LPC_SPI_T *pSPI, uint32_t Flag)
  395. {
  396. pSPI->INTENCLR = Flag;
  397. }
  398. /**
  399. * @brief Return enabled SPI interrupts
  400. * @param pSPI : The base of SPI peripheral on the chip
  401. * @return An Or'ed value of SPI_INTENSET_* values
  402. * @note Mask the return value with a SPI_INTENSET_* value to determine
  403. * if the interrupt is enabled.
  404. */
  405. STATIC INLINE uint32_t Chip_SPI_GetEnabledInts(LPC_SPI_T *pSPI)
  406. {
  407. return pSPI->INTENSET & ~SPI_INTENSET_RESERVED;
  408. }
  409. /**
  410. * @brief Return pending SPI interrupts
  411. * @param pSPI : The base of SPI peripheral on the chip
  412. * @return An Or'ed value of SPI_INTSTAT_* values
  413. * @note Mask the return value with a SPI_INTSTAT_* value to determine
  414. * if the interrupt is pending.
  415. */
  416. STATIC INLINE uint32_t Chip_SPI_GetPendingInts(LPC_SPI_T *pSPI)
  417. {
  418. return pSPI->INTSTAT & ~SPI_INTSTAT_RESERVED;
  419. }
  420. /**
  421. * @brief Flush FIFOs
  422. * @param pSPI : The base of SPI peripheral on the chip
  423. * @return Nothing
  424. */
  425. STATIC INLINE void Chip_SPI_FlushFifos(LPC_SPI_T *pSPI)
  426. {
  427. Chip_SPI_Disable(pSPI);
  428. Chip_SPI_Enable(pSPI);
  429. }
  430. /**
  431. * @brief Read raw data from receive FIFO with status bits
  432. * @param pSPI : The base of SPI peripheral on the chip
  433. * @return Current value in receive data FIFO plus status bits
  434. */
  435. STATIC INLINE uint32_t Chip_SPI_ReadRawRXFifo(LPC_SPI_T *pSPI)
  436. {
  437. return pSPI->RXDAT & ~SPI_RXDAT_RESERVED;
  438. }
  439. /**
  440. * @brief Read data from receive FIFO masking off status bits
  441. * @param pSPI : The base of SPI peripheral on the chip
  442. * @return Current value in receive data FIFO
  443. * @note The return value is masked with 0xFFFF to not exceed 16-bits. All
  444. * other status bits are thrown away. This register should only be read if it
  445. * has data in it. This function is useful for systems that don't need SPI
  446. * select (SSEL) monitoring.
  447. */
  448. STATIC INLINE uint32_t Chip_SPI_ReadRXData(LPC_SPI_T *pSPI)
  449. {
  450. return pSPI->RXDAT & 0xFFFF;
  451. }
  452. /**
  453. * @brief Write data to transmit FIFO
  454. * @param pSPI : The base of SPI peripheral on the chip
  455. * @param data : Data to write
  456. * @return Nothing
  457. */
  458. STATIC INLINE void Chip_SPI_WriteTXData(LPC_SPI_T *pSPI, uint16_t data)
  459. {
  460. pSPI->TXDAT = (uint32_t) data;
  461. }
  462. /**
  463. * @brief Set SPI TXCTRL register control options
  464. * @param pSPI : The base of SPI peripheral on the chip
  465. * @param bits : TXCTRL register bits to set, amd OR'ed value of SPI_TXDATCTL_* definitions
  466. * @return Nothing
  467. * @note This function safely sets only the selected bits in the SPI TXCTRL register.
  468. * It can be used to enable multiple bits at once.
  469. */
  470. STATIC INLINE void Chip_SPI_SetTXCTRLRegBits(LPC_SPI_T *pSPI, uint32_t bits)
  471. {
  472. pSPI->TXCTRL = bits | (pSPI->TXCTRL & SPI_TXDATCTL_CTRLMASK);
  473. }
  474. /**
  475. * @brief Clear SPI TXCTRL register control options
  476. * @param pSPI : The base of SPI peripheral on the chip
  477. * @param bits : TXCTRL register bits to clear, amd OR'ed value of SPI_TXDATCTL_* definitions
  478. * @return Nothing
  479. * @note This function safely clears only the selected bits in the SPI TXCTRL register.
  480. * It can be used to disable multiple bits at once.
  481. */
  482. STATIC INLINE void Chip_SPI_ClearTXCTRLRegBits(LPC_SPI_T *pSPI, uint32_t bits)
  483. {
  484. pSPI->TXCTRL = ~bits & (pSPI->TXCTRL & SPI_TXDATCTL_CTRLMASK);
  485. }
  486. /**
  487. * @brief Set TX control options (safe)
  488. * @param pSPI : The base of SPI peripheral on the chip
  489. * @param ctrlBits : Or'ed control bits to set
  490. * @return Nothing
  491. * @note Selectable control states include SPI_TXCTL_DEASSERTNUM_SSEL(0/1/2/3),
  492. * SPI_TXCTL_EOT, SPI_TXCTL_EOF, SPI_TXCTL_RXIGNORE, and SPI_TXCTL_FLEN(bits).
  493. */
  494. STATIC INLINE void Chip_SPI_SetTXCtl(LPC_SPI_T *pSPI, uint32_t ctrlBits)
  495. {
  496. Chip_SPI_SetTXCTRLRegBits(pSPI, ctrlBits);
  497. }
  498. /**
  499. * @brief Clear TX control options (safe)
  500. * @param pSPI : The base of SPI peripheral on the chip
  501. * @param ctrlBits : Or'ed control bits to clear
  502. * @return Nothing
  503. * @note Selectable control states include SPI_TXCTL_DEASSERTNUM_SSEL(0/1/2/3),
  504. * SPI_TXCTL_EOT, SPI_TXCTL_EOF, SPI_TXCTL_RXIGNORE, and SPI_TXCTL_FLEN(bits).
  505. */
  506. STATIC INLINE void Chip_SPI_ClearTXCtl(LPC_SPI_T *pSPI, uint32_t ctrlBits)
  507. {
  508. Chip_SPI_ClearTXCTRLRegBits(pSPI, ctrlBits);
  509. }
  510. /**
  511. * @brief Set TX data transfer size in bits
  512. * @param pSPI : The base of SPI peripheral on the chip
  513. * @param ctrlBits : Number of bits to transmit and receive, must be 1 to 16
  514. * @return Nothing
  515. */
  516. STATIC INLINE void Chip_SPI_SetXferSize(LPC_SPI_T *pSPI, uint32_t ctrlBits)
  517. {
  518. Chip_SPI_ClearTXCTRLRegBits(pSPI, SPI_TXCTL_FLENMASK);
  519. Chip_SPI_SetTXCTRLRegBits(pSPI, SPI_TXCTL_FLEN(ctrlBits));
  520. }
  521. /**
  522. * @}
  523. */
  524. /** @defgroup SPI_8XX CHIP: LPC8xx SPI driver
  525. * @ingroup CHIP_8XX_Drivers
  526. * @{
  527. */
  528. /** @brief SPI Mode*/
  529. typedef enum {
  530. SPI_MODE_MASTER = SPI_CFG_MASTER_EN, /* Master Mode */
  531. SPI_MODE_SLAVE = SPI_CFG_SLAVE_EN, /* Slave Mode */
  532. } SPI_MODE_T;
  533. /** @brief SPI Data Order Mode*/
  534. typedef enum IP_SPI_DATA_ORDER {
  535. SPI_DATA_MSB_FIRST = SPI_CFG_MSB_FIRST_EN, /* Standard Order */
  536. SPI_DATA_LSB_FIRST = SPI_CFG_LSB_FIRST_EN, /* Reverse Order */
  537. } SPI_DATA_ORDER_T;
  538. /** @brief SPI SSEL Polarity definition*/
  539. typedef enum IP_SPI_SSEL_POL {
  540. SPI_SSEL_ACTIVE_LO = SPI_CFG_SPOL_LO, /* SSEL is active Low*/
  541. SPI_SSEL_ACTIVE_HI = SPI_CFG_SPOL_HI, /* SSEL is active High */
  542. } SPI_SSEL_POL_T;
  543. /**
  544. * @brief SPI Configure Struct
  545. */
  546. typedef struct {
  547. SPI_MODE_T Mode; /* Mode Select */
  548. uint32_t ClockMode; /* CPHA CPOL Select */
  549. SPI_DATA_ORDER_T DataOrder; /* MSB/LSB First */
  550. SPI_SSEL_POL_T SSELPol; /* SSEL Polarity Select */
  551. uint16_t ClkDiv; /* SPI Clock Divider Value */
  552. } SPI_CONFIG_T;
  553. /**
  554. * @brief SPI Delay Configure Struct
  555. */
  556. typedef struct {
  557. uint8_t PreDelay; /* Pre-delay value in SPI clock time */
  558. uint8_t PostDelay; /* Post-delay value in SPI clock time */
  559. uint8_t FrameDelay; /* Delay value between frames of a transfer in SPI clock time */
  560. uint8_t TransferDelay; /* Delay value between transfers in SPI clock time */
  561. } SPI_DELAY_CONFIG_T;
  562. /**
  563. * @brief SPI data setup structure
  564. */
  565. typedef struct {
  566. uint16_t *pTx; /**< Pointer to data buffer*/
  567. uint32_t TxCnt;/* Transmit Counter */
  568. uint16_t *pRx; /**< Pointer to data buffer*/
  569. uint32_t RxCnt;/* Transmit Counter */
  570. uint32_t Length; /**< Data Length*/
  571. uint16_t DataSize; /** < The size of a frame (1-16)*/
  572. } SPI_DATA_SETUP_T;
  573. /**
  574. * @brief Calculate the divider for SPI clock
  575. * @param pSPI : The base of SPI peripheral on the chip
  576. * @param bitRate : Expected clock rate
  577. * @return Divider value
  578. */
  579. uint32_t Chip_SPI_CalClkRateDivider(LPC_SPI_T *pSPI, uint32_t bitRate);
  580. /**
  581. * @brief Config SPI Delay parameters
  582. * @param pSPI : The base of SPI peripheral on the chip
  583. * @param pConfig : SPI Delay Configure Struct
  584. * @return Nothing
  585. * @note The SPI controller is disabled
  586. */
  587. void Chip_SPI_DelayConfig(LPC_SPI_T *pSPI, SPI_DELAY_CONFIG_T *pConfig);
  588. /**
  589. * @brief Enable/Disable SPI interrupt
  590. * @param pSPI : The base SPI peripheral on the chip
  591. * @param IntMask : Interrupt mask
  592. * @param NewState : ENABLE or DISABLE interrupt
  593. * @return Nothing
  594. */
  595. void Chip_SPI_Int_Cmd(LPC_SPI_T *pSPI, uint32_t IntMask, FunctionalState NewState);
  596. /**
  597. * @brief Enable SPI peripheral
  598. * @param pSPI : The base of SPI peripheral on the chip
  599. * @return Nothing
  600. */
  601. /**
  602. * @brief Enable loopback mode
  603. * @param pSPI : The base of SPI peripheral on the chip
  604. * @return Nothing
  605. * @note Serial input is taken from the serial output (MOSI or MISO) rather
  606. * than the serial input pin
  607. */
  608. STATIC INLINE void Chip_SPI_EnableLoopBack(LPC_SPI_T *pSPI)
  609. {
  610. pSPI->CFG = SPI_CFG_LBM_EN | (pSPI->CFG & ~SPI_CFG_RESERVED);
  611. }
  612. /**
  613. * @brief Disable loopback mode
  614. * @param pSPI : The base of SPI peripheral on the chip
  615. * @return Nothing
  616. * @note Serial input is taken from the serial output (MOSI or MISO) rather
  617. * than the serial input pin
  618. */
  619. STATIC INLINE void Chip_SPI_DisableLoopBack(LPC_SPI_T *pSPI)
  620. {
  621. pSPI->CFG &= (~SPI_CFG_LBM_EN) & SPI_CFG_BITMASK;
  622. }
  623. /**
  624. * @brief Set control information including SSEL, EOT, EOF RXIGNORE and FLEN
  625. * @param pSPI : The base of SPI peripheral on the chip
  626. * @param Flen : Data size (1-16)
  627. * @param Flag : Flag control (Or-ed values of SPI_TXCTL_*)
  628. * @note The control information has no effect unless data is later written to TXDAT
  629. * @return Nothing
  630. */
  631. STATIC INLINE void Chip_SPI_SetControlInfo(LPC_SPI_T *pSPI, uint8_t Flen, uint32_t Flag)
  632. {
  633. pSPI->TXCTRL = Flag | SPI_TXDATCTL_FLEN(Flen - 1);
  634. }
  635. /**
  636. * @brief Send the first Frame of a transfer (Rx Ignore)
  637. * @param pSPI : The base of SPI peripheral on the chip
  638. * @param Data : Transmit data
  639. * @param DataSize : Data Size (1-16)
  640. * @return Nothing
  641. */
  642. STATIC INLINE void Chip_SPI_SendFirstFrame_RxIgnore(LPC_SPI_T *pSPI, uint16_t Data, uint8_t DataSize)
  643. {
  644. pSPI->TXDATCTL = SPI_TXDATCTL_ASSERT_SSEL | SPI_TXDATCTL_EOF | SPI_TXDATCTL_RXIGNORE | SPI_TXDATCTL_FLEN(
  645. DataSize - 1) | SPI_TXDATCTL_DATA(Data);
  646. }
  647. /**
  648. * @brief Send the first Frame of a transfer
  649. * @param pSPI : The base of SPI peripheral on the chip
  650. * @param Data : Transmit data
  651. * @param DataSize : Data Size (1-16)
  652. * @return Nothing
  653. */
  654. STATIC INLINE void Chip_SPI_SendFirstFrame(LPC_SPI_T *pSPI, uint16_t Data, uint8_t DataSize)
  655. {
  656. pSPI->TXDATCTL = SPI_TXDATCTL_ASSERT_SSEL | SPI_TXDATCTL_EOF | SPI_TXDATCTL_FLEN(DataSize - 1) | SPI_TXDATCTL_DATA(
  657. Data);
  658. }
  659. /**
  660. * @brief Send the middle Frame of a transfer
  661. * @param pSPI : The base of SPI peripheral on the chip
  662. * @param Data : Transmit data
  663. * @return Nothing
  664. */
  665. STATIC INLINE void Chip_SPI_SendMidFrame(LPC_SPI_T *pSPI, uint16_t Data)
  666. {
  667. pSPI->TXDAT = SPI_TXDAT_DATA(Data);
  668. }
  669. /**
  670. * @brief Send the last Frame of a transfer (Rx Ignore)
  671. * @param pSPI : The base of SPI peripheral on the chip
  672. * @param Data : Transmit data
  673. * @param DataSize : Data Size (1-16)
  674. * @return Nothing
  675. */
  676. STATIC INLINE void Chip_SPI_SendLastFrame_RxIgnore(LPC_SPI_T *pSPI, uint16_t Data, uint8_t DataSize)
  677. {
  678. pSPI->TXDATCTL = SPI_TXDATCTL_ASSERT_SSEL | SPI_TXDATCTL_EOF | SPI_TXDATCTL_EOT | SPI_TXDATCTL_RXIGNORE |
  679. SPI_TXDATCTL_FLEN(DataSize - 1) | SPI_TXDATCTL_DATA(Data);
  680. }
  681. /**
  682. * @brief Send the last Frame of a transfer
  683. * @param pSPI : The base of SPI peripheral on the chip
  684. * @param Data : Transmit data
  685. * @param DataSize : Data Size (1-16)
  686. * @return Nothing
  687. */
  688. STATIC INLINE void Chip_SPI_SendLastFrame(LPC_SPI_T *pSPI, uint16_t Data, uint8_t DataSize)
  689. {
  690. pSPI->TXDATCTL = SPI_TXDATCTL_ASSERT_SSEL | SPI_TXDATCTL_EOF | SPI_TXDATCTL_EOT |
  691. SPI_TXDATCTL_FLEN(DataSize - 1) | SPI_TXDATCTL_DATA(Data);
  692. }
  693. /**
  694. * @brief Read data received
  695. * @param pSPI : The base of SPI peripheral on the chip
  696. * @return Receive data
  697. */
  698. STATIC INLINE uint16_t Chip_SPI_ReceiveFrame(LPC_SPI_T *pSPI)
  699. {
  700. return SPI_RXDAT_DATA(pSPI->RXDAT);
  701. }
  702. /**
  703. * @brief SPI Interrupt Read/Write
  704. * @param pSPI : The base SPI peripheral on the chip
  705. * @param xf_setup : Pointer to a SPI_DATA_SETUP_T structure that contains specified
  706. * information about transmit/receive data configuration
  707. * @return SUCCESS or ERROR
  708. */
  709. Status Chip_SPI_Int_RWFrames(LPC_SPI_T *pSPI, SPI_DATA_SETUP_T *xf_setup);
  710. /**
  711. * @brief SPI Polling Read/Write in blocking mode
  712. * @param pSPI : The base SPI peripheral on the chip
  713. * @param pXfSetup : Pointer to a SPI_DATA_SETUP_T structure that contains specified
  714. * information about transmit/receive data configuration
  715. * @return Actual data length has been transferred
  716. * @note
  717. * This function can be used in both master and slave mode. It starts with writing phase and after that,
  718. * a reading phase is generated to read any data available in RX_FIFO. All needed information is prepared
  719. * through xf_setup param.
  720. */
  721. uint32_t Chip_SPI_RWFrames_Blocking(LPC_SPI_T *pSPI, SPI_DATA_SETUP_T *pXfSetup);
  722. /**
  723. * @brief SPI Polling Write in blocking mode
  724. * @param pSPI : The base SPI peripheral on the chip
  725. * @param pXfSetup :Pointer to a SPI_DATA_SETUP_T structure that contains specified
  726. * information about transmit/receive data configuration
  727. * @return Actual data length has been transferred
  728. * @note
  729. * This function can be used in both master and slave mode. First, a writing operation will send
  730. * the needed data. After that, a dummy reading operation is generated to clear data buffer
  731. */
  732. uint32_t Chip_SPI_WriteFrames_Blocking(LPC_SPI_T *pSPI, SPI_DATA_SETUP_T *pXfSetup);
  733. /**
  734. * @brief SPI Polling Read in blocking mode
  735. * @param pSPI : The base SPI peripheral on the chip
  736. * @param pXfSetup :Pointer to a SPI_DATA_SETUP_T structure that contains specified
  737. * information about transmit/receive data configuration
  738. * @return Actual data length has been read
  739. * @note
  740. * This function can be used in both master and slave mode. First, a writing operation will send
  741. * the needed data. After that, a dummy reading operation is generated to clear data buffer
  742. */
  743. uint32_t Chip_SPI_ReadFrames_Blocking(LPC_SPI_T *pSPI, SPI_DATA_SETUP_T *pXfSetup);
  744. /**
  745. * @}
  746. */
  747. /** @defgroup SPI_MASTER_8XX CHIP: LPC8XX SPI master driver
  748. * @ingroup SPI_COMMON_8XX
  749. * @{
  750. */
  751. /**
  752. * @brief Get SPI master bit rate
  753. * @param pSPI : The base of SPI peripheral on the chip
  754. * @return The actual SPI clock bit rate
  755. */
  756. uint32_t Chip_SPIM_GetClockRate(LPC_SPI_T *pSPI);
  757. /**
  758. * @brief Set SPI master bit rate
  759. * @param pSPI : The base of SPI peripheral on the chip
  760. * @param rate : Desired clock bit rate for the SPI interface
  761. * @return The actual SPI clock bit rate
  762. * @note This function will set the SPI clock divider to get closest
  763. * to the desired rate as possible.
  764. */
  765. uint32_t Chip_SPIM_SetClockRate(LPC_SPI_T *pSPI, uint32_t rate);
  766. /**
  767. * @brief SPI Delay Configure Struct
  768. */
  769. typedef struct {
  770. uint8_t PreDelay; /** Pre-delay value in SPI clocks, 0 - 15 */
  771. uint8_t PostDelay; /** Post-delay value in SPI clocks, 0 - 15 */
  772. uint8_t FrameDelay; /** Delay value between frames of a transfer in SPI clocks, 0 - 15 */
  773. uint8_t TransferDelay; /** Delay value between transfers in SPI clocks, 1 - 16 */
  774. } SPIM_DELAY_CONFIG_T;
  775. /**
  776. * @brief Config SPI Delay parameters
  777. * @param pSPI : The base of SPI peripheral on the chip
  778. * @param pConfig : SPI Delay Configure Struct
  779. * @return Nothing
  780. */
  781. void Chip_SPIM_DelayConfig(LPC_SPI_T *pSPI, SPIM_DELAY_CONFIG_T *pConfig);
  782. /**
  783. * @brief Forces an end of transfer for the current master transfer
  784. * @param pSPI : The base of SPI peripheral on the chip
  785. * @return Nothing
  786. * @note Use this function to perform an immediate end of trasnfer for the
  787. * current master operation. If the master is currently transferring data started
  788. * with the Chip_SPIM_Xfer function, this terminates the transfer after the
  789. * current byte completes and completes the transfer.
  790. */
  791. STATIC INLINE void Chip_SPIM_ForceEndOfTransfer(LPC_SPI_T *pSPI)
  792. {
  793. pSPI->STAT = SPI_STAT_EOT;
  794. }
  795. /**
  796. * @brief Assert a SPI select
  797. * @param pSPI : The base of SPI peripheral on the chip
  798. * @param sselNum : SPI select to assert, 0 - 3
  799. * @return Nothing
  800. */
  801. void Chip_SPIM_AssertSSEL(LPC_SPI_T *pSPI, uint8_t sselNum);
  802. /**
  803. * @brief Deassert a SPI select
  804. * @param pSPI : The base of SPI peripheral on the chip
  805. * @param sselNum : SPI select to deassert, 0 - 3
  806. * @return Nothing
  807. */
  808. void Chip_SPIM_DeAssertSSEL(LPC_SPI_T *pSPI, uint8_t sselNum);
  809. /**
  810. * @brief Enable loopback mode
  811. * @param pSPI : The base of SPI peripheral on the chip
  812. * @return Nothing
  813. * @note Serial input is taken from the serial output (MOSI or MISO) rather
  814. * than the serial input pin.
  815. */
  816. STATIC INLINE void Chip_SPIM_EnableLoopBack(LPC_SPI_T *pSPI)
  817. {
  818. Chip_SPI_SetCFGRegBits(pSPI, SPI_CFG_LBM_EN);
  819. }
  820. /**
  821. * @brief Disable loopback mode
  822. * @param pSPI : The base of SPI peripheral on the chip
  823. * @return Nothing
  824. */
  825. STATIC INLINE void Chip_SPIM_DisableLoopBack(LPC_SPI_T *pSPI)
  826. {
  827. Chip_SPI_ClearCFGRegBits(pSPI, SPI_CFG_LBM_EN);
  828. }
  829. struct SPIM_XFER;
  830. /** @brief SPI master select assert callback
  831. * This callback is called from the SPI master handler when the SPI master
  832. * selects the slave (asserts SSEL).
  833. */
  834. typedef void (*SPIMasterXferCSAssert)(struct SPIM_XFER *pMasterXfer);
  835. /** @brief SPI master send data callback
  836. * This callback is called from the SPI master handler when the SPI master
  837. * needs a data buffer to send.
  838. */
  839. typedef void (*SPIMasterXferSend)(struct SPIM_XFER *pMasterXfer);
  840. /** @brief SPI master receive data callback
  841. * This callback is called from the SPI master handler when the SPI master
  842. * needs a buffer to place data into.
  843. */
  844. typedef void (*SPIMasterXferRecv)(struct SPIM_XFER *pMasterXfer);
  845. /** @brief SPI master transfer select deassert data callback
  846. * This callback is called from the SPI master handler when the SPI master
  847. * deasserts the slave select.
  848. */
  849. typedef void (*SPIMMasterXferCSDeAssert)(struct SPIM_XFER *pMasterXfer);
  850. /** @brief SPI master transfer done data callback
  851. * This callback is called from the SPI master handler when the SPI master
  852. * has completed the transfer and becomes idle.
  853. */
  854. typedef void (*SPIMMasterXferDone)(struct SPIM_XFER *pMasterXfer);
  855. /** SPI slave callback functions */
  856. typedef struct {
  857. SPIMasterXferCSAssert masterXferCSAssert; /** SPI transfer CS assert, called when a slave select is asserted */
  858. SPIMasterXferSend masterXferSend; /** SPI transfer data receive buffer callback, called when a send buffer is needed */
  859. SPIMasterXferRecv masterXferRecv; /** SPI transfer send buffer callback, called when send buffer is needed (and SPI_TXCTL_RXIGNORE option is not set) */
  860. SPIMMasterXferCSDeAssert mMasterXferCSDeAssert; /** SPI transfer CS deassert, called when a slave select is deasserted */
  861. SPIMMasterXferDone mMasterXferDone; /** SPI transfer done callback, called when transfer is complete */
  862. } SPIM_CALLBACKS_T;
  863. /** Slave transfer data context */
  864. typedef struct SPIM_XFER {
  865. const SPIM_CALLBACKS_T *pCB; /** Pointer to SPI master data callback functions */
  866. union { /** Pointer to receive buffer, set to NULL to toss receeive data */
  867. uint8_t *pRXData8; /** Receive buffer used with data transfer size <= 8-bits, modified by driver */
  868. uint16_t *pRXData16; /** Receive buffer used with data transfer size > 8-bits, modified by driver */
  869. };
  870. union { /** Pointer to transmit buffer, set to NULL to transmit 0x0 */
  871. uint8_t *pTXData8; /** Send buffer used with data transfer size <= 8-bits, modified by driver */
  872. uint16_t *pTXData16; /** Send buffer used with data transfer size > 8-bits, modified by driver */
  873. };
  874. uint32_t options; /** Master transfer options, an OR'ed value of SPI_TXCTL_EOT, SPI_TXCTL_EOF, SPI_TXCTL_RXIGNORE, and SPI_TXCTL_FLEN(bits) */
  875. uint16_t rxCount; /** Size of the pRXData buffer in items (not bytes), modified by driver */
  876. uint16_t txCount; /** Number of items (not bytes) to send in pTXData buffer, modified by driver */
  877. uint16_t dataRXferred; /** Total items (not bytes) received, modified by driver */
  878. uint16_t dataTXferred; /** Total items (not bytes) transmitted, modified by driver */
  879. uint8_t sselNum; /** Slave number assigned to this transfer, 0 - 3, used by driver to select slave */
  880. bool terminate; /** Transfer will terminate when txCount goes to 0 and master goes idle, must be set before last byte is sent */
  881. } SPIM_XFER_T;
  882. /**
  883. * @brief SPI master transfer state change handler
  884. * @param pSPI : The base of SPI peripheral on the chip
  885. * @param xfer : Pointer to a SPIM_XFER_T structure see notes below
  886. * @return Nothing
  887. * @note See @ref SPIM_XFER_T for more information on this function. When using
  888. * this function, the SPI master interrupts should be enabled and setup in the SPI
  889. * interrupt handler to call this function when they fire. This function is meant
  890. * to be called from the interrupt handler.
  891. */
  892. void Chip_SPIM_XferHandler(LPC_SPI_T *pSPI, SPIM_XFER_T *xfer);
  893. /**
  894. * @brief Start non-blocking SPI master transfer
  895. * @param pSPI : The base of SPI peripheral on the chip
  896. * @param xfer : Pointer to a SPIM_XFER_T structure see notes below
  897. * @return Nothing
  898. * @note This function starts a non-blocking SPI master transfer with the
  899. * parameters setup in the passed @ref SPIM_XFER_T structure. Once the transfer is
  900. * started, the interrupt handler must call Chip_SPIM_XferHandler to keep the
  901. * transfer going and fed with data. This function should only be called when
  902. * the master is idle.<br>
  903. *
  904. * This function must be called with the options and sselNum fields correctly
  905. * setup. Initial data buffers and the callback pointer must also be setup. No
  906. * sanity checks are performed on the passed data.<br>
  907. *
  908. * Example call:<br>
  909. * SPIM_XFER_T mxfer;
  910. * mxfer.pCB = &masterCallbacks;
  911. * mxfer.sselNum = 2; // Use chip select 2
  912. * mxfer.options = SPI_TXCTL_FLEN(8); // 8 data bits, supports 1 - 16 bits
  913. * mxfer.options |= SPI_TXCTL_EOT | SPI_TXCTL_EOF; // Apply frame and transfer delays to master transfer
  914. * mxfer.options |= SPI_TXCTL_RXIGNORE; // Ignore RX data, will toss receive data regardless of pRXData8 or pRXData16 buffer
  915. * mxfer.pTXData8 = SendBuffer;
  916. * mxfer.txCount = 16; // Number of bytes to send before SPIMasterXferSend callback is called
  917. * mxfer.pRXData8 = RecvBuffer; // Will not receive data if pRXData8/pRXData16 is NULL or SPI_TXCTL_RXIGNORE option is set
  918. * mxfer.rxCount = 16; // Number of bytes to receive before SPIMasterXferRecv callback is called
  919. * Chip_SPIM_Xfer(LPC_SPI0, &mxfer); // Start transfer
  920. *
  921. * Note that the transfer, once started, needs to be constantly fed by the callbacks.
  922. * The txCount and rxCount field only indicate the buffer size before the callbacks are called.
  923. * To terminate the transfer, the SPIMasterXferSend callback must set the terminate field.
  924. */
  925. void Chip_SPIM_Xfer(LPC_SPI_T *pSPI, SPIM_XFER_T *xfer);
  926. /**
  927. * @brief Perform blocking SPI master transfer
  928. * @param pSPI : The base of SPI peripheral on the chip
  929. * @param xfer : Pointer to a SPIM_XFER_T structure see notes below
  930. * @return Nothing
  931. * @note This function starts a blocking SPI master transfer with the
  932. * parameters setup in the passed @ref SPIM_XFER_T structure. Once the transfer is
  933. * started, the callbacks in Chip_SPIM_XferHandler may be called to keep the
  934. * transfer going and fed with data. SPI interrupts must be disabled prior to
  935. * calling this function. It is not recommended to use this function.<br>
  936. */
  937. void Chip_SPIM_XferBlocking(LPC_SPI_T *pSPI, SPIM_XFER_T *xfer);
  938. /**
  939. * @}
  940. */
  941. /** @defgroup SPI_SLAVE_8XX CHIP: LPC8XX SPI slave driver
  942. * @ingroup SPI_COMMON_8XX
  943. * @{
  944. */
  945. /**
  946. * Macro defines for SPI Status register
  947. */
  948. /* Clear RXOV Flag */
  949. #define SPI_STAT_CLR_RXOV ((uint32_t) (1 << 2))
  950. /* Clear TXUR Flag */
  951. #define SPI_STAT_CLR_TXUR ((uint32_t) (1 << 3))
  952. /* Clear SSA Flag */
  953. #define SPI_STAT_CLR_SSA ((uint32_t) (1 << 4))
  954. /* Clear SSD Flag */
  955. #define SPI_STAT_CLR_SSD ((uint32_t) (1 << 5))
  956. struct SPIS_XFER;
  957. /** @brief SPI slave select assertion callback
  958. * This callback is called from the SPI slave handler when an SPI slave select (SSEL)
  959. * is initially asserted. It is used to indicate the start of a slave transfer that
  960. * will happen on the bus.
  961. */
  962. typedef void (*SPISlaveXferCSAssert)(struct SPIS_XFER *pSlaveXfer);
  963. /** @brief SPI slave send data callback
  964. * This callback is called from the SPI slave handler when an SPI slave select (SSEL)
  965. * needs a data buffer to send.
  966. */
  967. typedef void (*SPISlaveXferSend)(struct SPIS_XFER *pSlaveXfer);
  968. /** @brief SPI slave receive data callback
  969. * This callback is called from the SPI slave handler when an SPI slave select (SSEL)
  970. * needs a buffer to place data.
  971. */
  972. typedef void (*SPISlaveXferRecv)(struct SPIS_XFER *pSlaveXfer);
  973. /** @brief SPI slave select de-assertion callback
  974. * This callback is called from the SPI slave handler when an SPI slave select (SSEL)
  975. * is de-asserted. It can be used to indicate the end of a transfer.
  976. */
  977. typedef void (*SPISlaveXferCSDeAssert)(struct SPIS_XFER *pSlaveXfer);
  978. /** SPI slave callback functions */
  979. typedef struct {
  980. SPISlaveXferCSAssert slaveXferCSAssert; /** SPI transfer start callback, called on SPI CS assertion */
  981. SPISlaveXferSend slaveXferSend; /** SPI transfer data receive buffer callback, called when a receive buffer is needed */
  982. SPISlaveXferRecv slaveXferRecv; /** SPI transfer send buffer callback, called when data is needed */
  983. SPISlaveXferCSDeAssert slaveXferCSDeAssert; /** SPI transfer completion callback, called on SPI CS deassertion */
  984. } SPIS_CALLBACKS_T;
  985. /** Slave transfer data context */
  986. typedef struct SPIS_XFER {
  987. const SPIS_CALLBACKS_T *pCB; /** Pointer to SPI slave callback functions */
  988. union { /** Pointer to receive buffer, set to NULL to toss receeive data */
  989. uint8_t *pRXData8; /** Receive buffer used with data transfer size <= 8-bits, modified by driver */
  990. uint16_t *pRXData16; /** Receive buffer used with data transfer size > 8-bits, modified by driver */
  991. };
  992. union { /** Pointer to transmit buffer, set to NULL to transmit 0x0 */
  993. uint8_t *pTXData8; /** Send buffer used with data transfer size <= 8-bits, modified by driver */
  994. uint16_t *pTXData16; /** Send buffer used with data transfer size > 8-bits, modified by driver */
  995. };
  996. uint16_t rxCount; /** Size of the pRXData buffer in items (not bytes), modified by driver */
  997. uint16_t txCount; /** Number of items (not bytes) to send in pTXData buffer, modified by driver */
  998. uint16_t dataRXferred; /** Total items (not bytes) received, modified by driver */
  999. uint16_t dataTXferred; /** Total items (not bytes) transmitted, modified by driver */
  1000. uint8_t sselNum; /** Slave number assigned to this transfer, 0 - 3, modified by driver */
  1001. } SPIS_XFER_T;
  1002. /**
  1003. * @brief SPI slave transfer state change handler
  1004. * @param pSPI : The base of SPI peripheral on the chip
  1005. * @param xfer : Pointer to a SPIS_XFER_T structure see notes below
  1006. * @return returns 0 on success, or SPI_STAT_RXOV and/or SPI_STAT_TXUR on an error
  1007. * @note See @ref SPIS_XFER_T for more information on this function. When using
  1008. * this function, the SPI slave interrupts should be enabled and setup in the SPI
  1009. * interrupt handler to call this function when they fire. This function is meant
  1010. * to be called from the interrupt handler. The @ref SPIS_XFER_T data does not need
  1011. * to be setup prior to the call and should be setup by the callbacks instead.<br>
  1012. *
  1013. * The callbacks are handled in the interrupt handler. If you are getting overflow
  1014. * or underflow errors, you might need to lower the speed of the master clock or
  1015. * extend the master's select assetion time.<br>
  1016. */
  1017. uint32_t Chip_SPIS_XferHandler(LPC_SPI_T *pSPI, SPIS_XFER_T *xfer);
  1018. /**
  1019. * @brief Pre-buffers slave transmit data
  1020. * @param pSPI : The base of SPI peripheral on the chip
  1021. * @param xfer : Pointer to a SPIS_XFER_T structure see notes below
  1022. * @return Nothing
  1023. * @note Pre-buffering allows the slave to prime the transmit FIFO with data prior to
  1024. * the master starting a transfer. If data is not pre-buffered, the initial slave
  1025. * transmit data will always be 0x0 with a slave transmit underflow status.
  1026. * Pre-buffering is best used when only a single slave select is used by an
  1027. * application.
  1028. */
  1029. STATIC INLINE void Chip_SPIS_PreBuffSlave(LPC_SPI_T *pSPI, SPIS_XFER_T *xfer)
  1030. {
  1031. Chip_SPIS_XferHandler(pSPI, xfer);
  1032. }
  1033. /**
  1034. * @brief SPI slave transfer blocking function
  1035. * @param pSPI : The base of SPI peripheral on the chip
  1036. * @param xfer : Pointer to a SPIS_XFER_T structure
  1037. * @return returns 0 on success, or SPI_STAT_RXOV and/or SPI_STAT_TXUR on an error
  1038. * @note This function performs a blocking transfer on the SPI slave interface.
  1039. * It is not recommended to use this function. Once this function is called, it
  1040. * will block forever until a slave transfer consisting of a slave SSEL assertion,
  1041. * and de-assertion occur. The callbacks are still used for slave data buffer
  1042. * management. SPI interrupts must be disabled prior to calling this function.
  1043. */
  1044. uint32_t Chip_SPIS_XferBlocking(LPC_SPI_T *pSPI, SPIS_XFER_T *xfer);
  1045. /**
  1046. * @}
  1047. */
  1048. #ifdef __cplusplus
  1049. }
  1050. #endif
  1051. #endif /* __SPI_8XX_H__ */