nu_spi.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /**************************************************************************//**
  2. * @file nu_spi.h
  3. * @brief SPI driver header file
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #ifndef __NU_SPI_H__
  9. #define __NU_SPI_H__
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. /** @addtogroup Standard_Driver Standard Driver
  15. @{
  16. */
  17. /** @addtogroup SPI_Driver SPI Driver
  18. @{
  19. */
  20. /** @addtogroup SPI_EXPORTED_CONSTANTS SPI Exported Constants
  21. @{
  22. */
  23. #define SPI_MODE_0 (SPI_CTL_TXNEG_Msk) /*!< CLKPOL=0; RXNEG=0; TXNEG=1 \hideinitializer */
  24. #define SPI_MODE_1 (SPI_CTL_RXNEG_Msk) /*!< CLKPOL=0; RXNEG=1; TXNEG=0 \hideinitializer */
  25. #define SPI_MODE_2 (SPI_CTL_CLKPOL_Msk | SPI_CTL_RXNEG_Msk) /*!< CLKPOL=1; RXNEG=1; TXNEG=0 \hideinitializer */
  26. #define SPI_MODE_3 (SPI_CTL_CLKPOL_Msk | SPI_CTL_TXNEG_Msk) /*!< CLKPOL=1; RXNEG=0; TXNEG=1 \hideinitializer */
  27. #define SPI_SLAVE (SPI_CTL_SLAVE_Msk) /*!< Set as slave \hideinitializer */
  28. #define SPI_MASTER (0x0U) /*!< Set as master \hideinitializer */
  29. #define SPI_SS (SPI_SSCTL_SS_Msk) /*!< Set SS \hideinitializer */
  30. #define SPI_SS_ACTIVE_HIGH (SPI_SSCTL_SSACTPOL_Msk) /*!< SS active high \hideinitializer */
  31. #define SPI_SS_ACTIVE_LOW (0x0U) /*!< SS active low \hideinitializer */
  32. /* SPI Interrupt Mask */
  33. #define SPI_UNIT_INT_MASK (0x001U) /*!< Unit transfer interrupt mask \hideinitializer */
  34. #define SPI_SSACT_INT_MASK (0x002U) /*!< Slave selection signal active interrupt mask \hideinitializer */
  35. #define SPI_SSINACT_INT_MASK (0x004U) /*!< Slave selection signal inactive interrupt mask \hideinitializer */
  36. #define SPI_SLVUR_INT_MASK (0x008U) /*!< Slave under run interrupt mask \hideinitializer */
  37. #define SPI_SLVBE_INT_MASK (0x010U) /*!< Slave bit count error interrupt mask \hideinitializer */
  38. #define SPI_TXUF_INT_MASK (0x040U) /*!< Slave TX underflow interrupt mask \hideinitializer */
  39. #define SPI_FIFO_TXTH_INT_MASK (0x080U) /*!< FIFO TX threshold interrupt mask \hideinitializer */
  40. #define SPI_FIFO_RXTH_INT_MASK (0x100U) /*!< FIFO RX threshold interrupt mask \hideinitializer */
  41. #define SPI_FIFO_RXOV_INT_MASK (0x200U) /*!< FIFO RX overrun interrupt mask \hideinitializer */
  42. #define SPI_FIFO_RXTO_INT_MASK (0x400U) /*!< FIFO RX time-out interrupt mask \hideinitializer */
  43. /* SPI Status Mask */
  44. #define SPI_BUSY_MASK (0x01U) /*!< Busy status mask \hideinitializer */
  45. #define SPI_RX_EMPTY_MASK (0x02U) /*!< RX empty status mask \hideinitializer */
  46. #define SPI_RX_FULL_MASK (0x04U) /*!< RX full status mask \hideinitializer */
  47. #define SPI_TX_EMPTY_MASK (0x08U) /*!< TX empty status mask \hideinitializer */
  48. #define SPI_TX_FULL_MASK (0x10U) /*!< TX full status mask \hideinitializer */
  49. #define SPI_TXRX_RESET_MASK (0x20U) /*!< TX or RX reset status mask \hideinitializer */
  50. #define SPI_SPIEN_STS_MASK (0x40U) /*!< SPIEN status mask \hideinitializer */
  51. #define SPI_SSLINE_STS_MASK (0x80U) /*!< SPIx_SS line status mask \hideinitializer */
  52. /* I2S Data Width */
  53. #define SPII2S_DATABIT_8 (0U << SPI_I2SCTL_WDWIDTH_Pos) /*!< I2S data width is 8-bit \hideinitializer */
  54. #define SPII2S_DATABIT_16 (1U << SPI_I2SCTL_WDWIDTH_Pos) /*!< I2S data width is 16-bit \hideinitializer */
  55. #define SPII2S_DATABIT_24 (2U << SPI_I2SCTL_WDWIDTH_Pos) /*!< I2S data width is 24-bit \hideinitializer */
  56. #define SPII2S_DATABIT_32 (3U << SPI_I2SCTL_WDWIDTH_Pos) /*!< I2S data width is 32-bit \hideinitializer */
  57. /* I2S Audio Format */
  58. #define SPII2S_MONO SPI_I2SCTL_MONO_Msk /*!< Monaural channel \hideinitializer */
  59. #define SPII2S_STEREO (0U) /*!< Stereo channel \hideinitializer */
  60. /* I2S Data Format */
  61. #define SPII2S_FORMAT_I2S (0U<<SPI_I2SCTL_FORMAT_Pos) /*!< I2S data format \hideinitializer */
  62. #define SPII2S_FORMAT_MSB (1U<<SPI_I2SCTL_FORMAT_Pos) /*!< MSB justified data format \hideinitializer */
  63. #define SPII2S_FORMAT_PCMA (2U<<SPI_I2SCTL_FORMAT_Pos) /*!< PCM mode A data format \hideinitializer */
  64. #define SPII2S_FORMAT_PCMB (3U<<SPI_I2SCTL_FORMAT_Pos) /*!< PCM mode B data format \hideinitializer */
  65. /* I2S Operation mode */
  66. #define SPII2S_MODE_SLAVE SPI_I2SCTL_SLAVE_Msk /*!< As slave mode \hideinitializer */
  67. #define SPII2S_MODE_MASTER (0U) /*!< As master mode \hideinitializer */
  68. /* I2S Record Channel */
  69. #define SPII2S_MONO_RIGHT (0U) /*!< Record mono right channel \hideinitializer */
  70. #define SPII2S_MONO_LEFT SPI_I2SCTL_RXLCH_Msk /*!< Record mono left channel \hideinitializer */
  71. /* I2S Channel */
  72. #define SPII2S_RIGHT (0U) /*!< Select right channel \hideinitializer */
  73. #define SPII2S_LEFT (1U) /*!< Select left channel \hideinitializer */
  74. /* I2S Interrupt Mask */
  75. #define SPII2S_FIFO_TXTH_INT_MASK (0x01U) /*!< TX FIFO threshold interrupt mask \hideinitializer */
  76. #define SPII2S_FIFO_RXTH_INT_MASK (0x02U) /*!< RX FIFO threshold interrupt mask \hideinitializer */
  77. #define SPII2S_FIFO_RXOV_INT_MASK (0x04U) /*!< RX FIFO overrun interrupt mask \hideinitializer */
  78. #define SPII2S_FIFO_RXTO_INT_MASK (0x08U) /*!< RX FIFO time-out interrupt mask \hideinitializer */
  79. #define SPII2S_TXUF_INT_MASK (0x10U) /*!< TX FIFO underflow interrupt mask \hideinitializer */
  80. #define SPII2S_RIGHT_ZC_INT_MASK (0x20U) /*!< Right channel zero cross interrupt mask \hideinitializer */
  81. #define SPII2S_LEFT_ZC_INT_MASK (0x40U) /*!< Left channel zero cross interrupt mask \hideinitializer */
  82. /*@}*/ /* end of group SPI_EXPORTED_CONSTANTS */
  83. /** @addtogroup SPI_EXPORTED_FUNCTIONS SPI Exported Functions
  84. @{
  85. */
  86. /**
  87. * @brief Clear the unit transfer interrupt flag.
  88. * @param[in] spi The pointer of the specified SPI module.
  89. * @return None.
  90. * @details Write 1 to UNITIF bit of SPI_STATUS register to clear the unit transfer interrupt flag.
  91. * \hideinitializer
  92. */
  93. #define SPI_CLR_UNIT_TRANS_INT_FLAG(spi) ((spi)->STATUS = SPI_STATUS_UNITIF_Msk)
  94. /**
  95. * @brief Trigger RX PDMA function.
  96. * @param[in] spi The pointer of the specified SPI module.
  97. * @return None.
  98. * @details Set RXPDMAEN bit of SPI_PDMACTL register to enable RX PDMA transfer function.
  99. * \hideinitializer
  100. */
  101. #define SPI_TRIGGER_RX_PDMA(spi) ((spi)->PDMACTL |= SPI_PDMACTL_RXPDMAEN_Msk)
  102. /**
  103. * @brief Trigger TX PDMA function.
  104. * @param[in] spi The pointer of the specified SPI module.
  105. * @return None.
  106. * @details Set TXPDMAEN bit of SPI_PDMACTL register to enable TX PDMA transfer function.
  107. * \hideinitializer
  108. */
  109. #define SPI_TRIGGER_TX_PDMA(spi) ((spi)->PDMACTL |= SPI_PDMACTL_TXPDMAEN_Msk)
  110. /**
  111. * @brief Trigger TX and RX PDMA function.
  112. * @param[in] spi The pointer of the specified SPI module.
  113. * @return None.
  114. * @details Set TXPDMAEN bit and RXPDMAEN bit of SPI_PDMACTL register to enable TX and RX PDMA transfer function.
  115. * \hideinitializer
  116. */
  117. #define SPI_TRIGGER_TX_RX_PDMA(spi) ((spi)->PDMACTL |= (SPI_PDMACTL_TXPDMAEN_Msk | SPI_PDMACTL_RXPDMAEN_Msk))
  118. /**
  119. * @brief Disable RX PDMA transfer.
  120. * @param[in] spi The pointer of the specified SPI module.
  121. * @return None.
  122. * @details Clear RXPDMAEN bit of SPI_PDMACTL register to disable RX PDMA transfer function.
  123. * \hideinitializer
  124. */
  125. #define SPI_DISABLE_RX_PDMA(spi) ( (spi)->PDMACTL &= ~SPI_PDMACTL_RXPDMAEN_Msk )
  126. /**
  127. * @brief Disable TX PDMA transfer.
  128. * @param[in] spi The pointer of the specified SPI module.
  129. * @return None.
  130. * @details Clear TXPDMAEN bit of SPI_PDMACTL register to disable TX PDMA transfer function.
  131. * \hideinitializer
  132. */
  133. #define SPI_DISABLE_TX_PDMA(spi) ( (spi)->PDMACTL &= ~SPI_PDMACTL_TXPDMAEN_Msk )
  134. /**
  135. * @brief Disable TX and RX PDMA transfer.
  136. * @param[in] spi The pointer of the specified SPI module.
  137. * @return None.
  138. * @details Clear TXPDMAEN bit and RXPDMAEN bit of SPI_PDMACTL register to disable TX and RX PDMA transfer function.
  139. * \hideinitializer
  140. */
  141. #define SPI_DISABLE_TX_RX_PDMA(spi) ( (spi)->PDMACTL &= ~(SPI_PDMACTL_TXPDMAEN_Msk | SPI_PDMACTL_RXPDMAEN_Msk) )
  142. /**
  143. * @brief Get the count of available data in RX FIFO.
  144. * @param[in] spi The pointer of the specified SPI module.
  145. * @return The count of available data in RX FIFO.
  146. * @details Read RXCNT (SPI_STATUS[27:24]) to get the count of available data in RX FIFO.
  147. * \hideinitializer
  148. */
  149. #define SPI_GET_RX_FIFO_COUNT(spi) (((spi)->STATUS & SPI_STATUS_RXCNT_Msk) >> SPI_STATUS_RXCNT_Pos)
  150. /**
  151. * @brief Get the RX FIFO empty flag.
  152. * @param[in] spi The pointer of the specified SPI module.
  153. * @retval 0 RX FIFO is not empty.
  154. * @retval 1 RX FIFO is empty.
  155. * @details Read RXEMPTY bit of SPI_STATUS register to get the RX FIFO empty flag.
  156. * \hideinitializer
  157. */
  158. #define SPI_GET_RX_FIFO_EMPTY_FLAG(spi) (((spi)->STATUS & SPI_STATUS_RXEMPTY_Msk)>>SPI_STATUS_RXEMPTY_Pos)
  159. /**
  160. * @brief Get the TX FIFO empty flag.
  161. * @param[in] spi The pointer of the specified SPI module.
  162. * @retval 0 TX FIFO is not empty.
  163. * @retval 1 TX FIFO is empty.
  164. * @details Read TXEMPTY bit of SPI_STATUS register to get the TX FIFO empty flag.
  165. * \hideinitializer
  166. */
  167. #define SPI_GET_TX_FIFO_EMPTY_FLAG(spi) (((spi)->STATUS & SPI_STATUS_TXEMPTY_Msk)>>SPI_STATUS_TXEMPTY_Pos)
  168. /**
  169. * @brief Get the TX FIFO full flag.
  170. * @param[in] spi The pointer of the specified SPI module.
  171. * @retval 0 TX FIFO is not full.
  172. * @retval 1 TX FIFO is full.
  173. * @details Read TXFULL bit of SPI_STATUS register to get the TX FIFO full flag.
  174. * \hideinitializer
  175. */
  176. #define SPI_GET_TX_FIFO_FULL_FLAG(spi) (((spi)->STATUS & SPI_STATUS_TXFULL_Msk)>>SPI_STATUS_TXFULL_Pos)
  177. /**
  178. * @brief Get the datum read from RX register.
  179. * @param[in] spi The pointer of the specified SPI module.
  180. * @return Data in RX register.
  181. * @details Read SPI_RX register to get the received datum.
  182. * \hideinitializer
  183. */
  184. #define SPI_READ_RX(spi) ((spi)->RX)
  185. /**
  186. * @brief Write datum to TX register.
  187. * @param[in] spi The pointer of the specified SPI module.
  188. * @param[in] u32TxData The datum which user attempt to transfer through SPI bus.
  189. * @return None.
  190. * @details Write u32TxData to SPI_TX register.
  191. * \hideinitializer
  192. */
  193. #define SPI_WRITE_TX(spi, u32TxData) ((spi)->TX = (u32TxData))
  194. /**
  195. * @brief Set SPIx_SS pin to high state.
  196. * @param[in] spi The pointer of the specified SPI module.
  197. * @return None.
  198. * @details Disable automatic slave selection function and set SPIx_SS pin to high state.
  199. * \hideinitializer
  200. */
  201. #define SPI_SET_SS_HIGH(spi) ((spi)->SSCTL = ((spi)->SSCTL & (~SPI_SSCTL_AUTOSS_Msk)) | (SPI_SSCTL_SSACTPOL_Msk | SPI_SSCTL_SS_Msk))
  202. /**
  203. * @brief Set SPIx_SS pin to low state.
  204. * @param[in] spi The pointer of the specified SPI module.
  205. * @return None.
  206. * @details Disable automatic slave selection function and set SPIx_SS pin to low state.
  207. * \hideinitializer
  208. */
  209. #define SPI_SET_SS_LOW(spi) ((spi)->SSCTL = ((spi)->SSCTL & (~(SPI_SSCTL_AUTOSS_Msk | SPI_SSCTL_SSACTPOL_Msk))) | SPI_SSCTL_SS_Msk)
  210. /**
  211. * @brief Enable Byte Reorder function.
  212. * @param[in] spi The pointer of the specified SPI module.
  213. * @return None.
  214. * @details Enable Byte Reorder function. The suspend interval depends on the setting of SUSPITV (SPI_CTL[7:4]).
  215. * \hideinitializer
  216. */
  217. #define SPI_ENABLE_BYTE_REORDER(spi) ((spi)->CTL |= SPI_CTL_REORDER_Msk)
  218. /**
  219. * @brief Disable Byte Reorder function.
  220. * @param[in] spi The pointer of the specified SPI module.
  221. * @return None.
  222. * @details Clear REORDER bit field of SPI_CTL register to disable Byte Reorder function.
  223. * \hideinitializer
  224. */
  225. #define SPI_DISABLE_BYTE_REORDER(spi) ((spi)->CTL &= ~SPI_CTL_REORDER_Msk)
  226. /**
  227. * @brief Set the length of suspend interval.
  228. * @param[in] spi The pointer of the specified SPI module.
  229. * @param[in] u32SuspCycle Decides the length of suspend interval. It could be 0 ~ 15.
  230. * @return None.
  231. * @details Set the length of suspend interval according to u32SuspCycle.
  232. * The length of suspend interval is ((u32SuspCycle + 0.5) * the length of one SPI bus clock cycle).
  233. * \hideinitializer
  234. */
  235. #define SPI_SET_SUSPEND_CYCLE(spi, u32SuspCycle) ((spi)->CTL = ((spi)->CTL & ~SPI_CTL_SUSPITV_Msk) | ((u32SuspCycle) << SPI_CTL_SUSPITV_Pos))
  236. /**
  237. * @brief Set the SPI transfer sequence with LSB first.
  238. * @param[in] spi The pointer of the specified SPI module.
  239. * @return None.
  240. * @details Set LSB bit of SPI_CTL register to set the SPI transfer sequence with LSB first.
  241. * \hideinitializer
  242. */
  243. #define SPI_SET_LSB_FIRST(spi) ((spi)->CTL |= SPI_CTL_LSB_Msk)
  244. /**
  245. * @brief Set the SPI transfer sequence with MSB first.
  246. * @param[in] spi The pointer of the specified SPI module.
  247. * @return None.
  248. * @details Clear LSB bit of SPI_CTL register to set the SPI transfer sequence with MSB first.
  249. * \hideinitializer
  250. */
  251. #define SPI_SET_MSB_FIRST(spi) ((spi)->CTL &= ~SPI_CTL_LSB_Msk)
  252. /**
  253. * @brief Set the data width of a SPI transaction.
  254. * @param[in] spi The pointer of the specified SPI module.
  255. * @param[in] u32Width The bit width of one transaction.
  256. * @return None.
  257. * @details The data width can be 8 ~ 32 bits.
  258. * \hideinitializer
  259. */
  260. #define SPI_SET_DATA_WIDTH(spi, u32Width) ((spi)->CTL = ((spi)->CTL & ~SPI_CTL_DWIDTH_Msk) | (((u32Width)&0x1F) << SPI_CTL_DWIDTH_Pos))
  261. /**
  262. * @brief Get the SPI busy state.
  263. * @param[in] spi The pointer of the specified SPI module.
  264. * @retval 0 SPI controller is not busy.
  265. * @retval 1 SPI controller is busy.
  266. * @details This macro will return the busy state of SPI controller.
  267. * \hideinitializer
  268. */
  269. #define SPI_IS_BUSY(spi) ( ((spi)->STATUS & SPI_STATUS_BUSY_Msk)>>SPI_STATUS_BUSY_Pos )
  270. /**
  271. * @brief Enable SPI controller.
  272. * @param[in] spi The pointer of the specified SPI module.
  273. * @return None.
  274. * @details Set SPIEN (SPI_CTL[0]) to enable SPI controller.
  275. * \hideinitializer
  276. */
  277. #define SPI_ENABLE(spi) ((spi)->CTL |= SPI_CTL_SPIEN_Msk)
  278. /**
  279. * @brief Disable SPI controller.
  280. * @param[in] spi The pointer of the specified SPI module.
  281. * @return None.
  282. * @details Clear SPIEN (SPI_CTL[0]) to disable SPI controller.
  283. * \hideinitializer
  284. */
  285. #define SPI_DISABLE(spi) ((spi)->CTL &= ~SPI_CTL_SPIEN_Msk)
  286. /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
  287. __STATIC_INLINE void SPII2S_ENABLE_TX_ZCD(SPI_T *i2s, uint32_t u32ChMask);
  288. __STATIC_INLINE void SPII2S_DISABLE_TX_ZCD(SPI_T *i2s, uint32_t u32ChMask);
  289. __STATIC_INLINE void SPII2S_SET_MONO_RX_CHANNEL(SPI_T *i2s, uint32_t u32Ch);
  290. /**
  291. * @brief Enable zero cross detection function.
  292. * @param[in] i2s The pointer of the specified I2S module.
  293. * @param[in] u32ChMask The mask for left or right channel. Valid values are:
  294. * - \ref SPII2S_RIGHT
  295. * - \ref SPII2S_LEFT
  296. * @return None
  297. * @details This function will set RZCEN or LZCEN bit of SPI_I2SCTL register to enable zero cross detection function.
  298. */
  299. __STATIC_INLINE void SPII2S_ENABLE_TX_ZCD(SPI_T *i2s, uint32_t u32ChMask)
  300. {
  301. if (u32ChMask == SPII2S_RIGHT)
  302. {
  303. i2s->I2SCTL |= SPI_I2SCTL_RZCEN_Msk;
  304. }
  305. else
  306. {
  307. i2s->I2SCTL |= SPI_I2SCTL_LZCEN_Msk;
  308. }
  309. }
  310. /**
  311. * @brief Disable zero cross detection function.
  312. * @param[in] i2s The pointer of the specified I2S module.
  313. * @param[in] u32ChMask The mask for left or right channel. Valid values are:
  314. * - \ref SPII2S_RIGHT
  315. * - \ref SPII2S_LEFT
  316. * @return None
  317. * @details This function will clear RZCEN or LZCEN bit of SPI_I2SCTL register to disable zero cross detection function.
  318. */
  319. __STATIC_INLINE void SPII2S_DISABLE_TX_ZCD(SPI_T *i2s, uint32_t u32ChMask)
  320. {
  321. if (u32ChMask == SPII2S_RIGHT)
  322. {
  323. i2s->I2SCTL &= ~SPI_I2SCTL_RZCEN_Msk;
  324. }
  325. else
  326. {
  327. i2s->I2SCTL &= ~SPI_I2SCTL_LZCEN_Msk;
  328. }
  329. }
  330. /**
  331. * @brief Enable I2S TX DMA function.
  332. * @param[in] i2s The pointer of the specified I2S module.
  333. * @return None
  334. * @details This macro will set TXPDMAEN bit of SPI_PDMACTL register to transmit data with PDMA.
  335. * \hideinitializer
  336. */
  337. #define SPII2S_ENABLE_TXDMA(i2s) ( (i2s)->PDMACTL |= SPI_PDMACTL_TXPDMAEN_Msk )
  338. /**
  339. * @brief Disable I2S TX DMA function.
  340. * @param[in] i2s The pointer of the specified I2S module.
  341. * @return None
  342. * @details This macro will clear TXPDMAEN bit of SPI_PDMACTL register to disable TX DMA function.
  343. * \hideinitializer
  344. */
  345. #define SPII2S_DISABLE_TXDMA(i2s) ( (i2s)->PDMACTL &= ~SPI_PDMACTL_TXPDMAEN_Msk )
  346. /**
  347. * @brief Enable I2S RX DMA function.
  348. * @param[in] i2s The pointer of the specified I2S module.
  349. * @return None
  350. * @details This macro will set RXPDMAEN bit of SPI_PDMACTL register to receive data with PDMA.
  351. * \hideinitializer
  352. */
  353. #define SPII2S_ENABLE_RXDMA(i2s) ( (i2s)->PDMACTL |= SPI_PDMACTL_RXPDMAEN_Msk )
  354. /**
  355. * @brief Disable I2S RX DMA function.
  356. * @param[in] i2s The pointer of the specified I2S module.
  357. * @return None
  358. * @details This macro will clear RXPDMAEN bit of SPI_PDMACTL register to disable RX DMA function.
  359. * \hideinitializer
  360. */
  361. #define SPII2S_DISABLE_RXDMA(i2s) ( (i2s)->PDMACTL &= ~SPI_PDMACTL_RXPDMAEN_Msk )
  362. /**
  363. * @brief Enable I2S TX function.
  364. * @param[in] i2s The pointer of the specified I2S module.
  365. * @return None
  366. * @details This macro will set TXEN bit of SPI_I2SCTL register to enable I2S TX function.
  367. * \hideinitializer
  368. */
  369. #define SPII2S_ENABLE_TX(i2s) ( (i2s)->I2SCTL |= SPI_I2SCTL_TXEN_Msk )
  370. /**
  371. * @brief Disable I2S TX function.
  372. * @param[in] i2s The pointer of the specified I2S module.
  373. * @return None
  374. * @details This macro will clear TXEN bit of SPI_I2SCTL register to disable I2S TX function.
  375. * \hideinitializer
  376. */
  377. #define SPII2S_DISABLE_TX(i2s) ( (i2s)->I2SCTL &= ~SPI_I2SCTL_TXEN_Msk )
  378. /**
  379. * @brief Enable I2S RX function.
  380. * @param[in] i2s The pointer of the specified I2S module.
  381. * @return None
  382. * @details This macro will set RXEN bit of SPI_I2SCTL register to enable I2S RX function.
  383. * \hideinitializer
  384. */
  385. #define SPII2S_ENABLE_RX(i2s) ( (i2s)->I2SCTL |= SPI_I2SCTL_RXEN_Msk )
  386. /**
  387. * @brief Disable I2S RX function.
  388. * @param[in] i2s The pointer of the specified I2S module.
  389. * @return None
  390. * @details This macro will clear RXEN bit of SPI_I2SCTL register to disable I2S RX function.
  391. * \hideinitializer
  392. */
  393. #define SPII2S_DISABLE_RX(i2s) ( (i2s)->I2SCTL &= ~SPI_I2SCTL_RXEN_Msk )
  394. /**
  395. * @brief Enable TX Mute function.
  396. * @param[in] i2s The pointer of the specified I2S module.
  397. * @return None
  398. * @details This macro will set MUTE bit of SPI_I2SCTL register to enable I2S TX mute function.
  399. * \hideinitializer
  400. */
  401. #define SPII2S_ENABLE_TX_MUTE(i2s) ( (i2s)->I2SCTL |= SPI_I2SCTL_MUTE_Msk )
  402. /**
  403. * @brief Disable TX Mute function.
  404. * @param[in] i2s The pointer of the specified I2S module.
  405. * @return None
  406. * @details This macro will clear MUTE bit of SPI_I2SCTL register to disable I2S TX mute function.
  407. * \hideinitializer
  408. */
  409. #define SPII2S_DISABLE_TX_MUTE(i2s) ( (i2s)->I2SCTL &= ~SPI_I2SCTL_MUTE_Msk )
  410. /**
  411. * @brief Clear TX FIFO.
  412. * @param[in] i2s The pointer of the specified I2S module.
  413. * @return None
  414. * @details This macro will clear TX FIFO. The internal TX FIFO pointer will be reset to FIFO start point.
  415. * \hideinitializer
  416. */
  417. #define SPII2S_CLR_TX_FIFO(i2s) ( (i2s)->FIFOCTL |= SPI_FIFOCTL_TXFBCLR_Msk )
  418. /**
  419. * @brief Clear RX FIFO.
  420. * @param[in] i2s The pointer of the specified I2S module.
  421. * @return None
  422. * @details This macro will clear RX FIFO. The internal RX FIFO pointer will be reset to FIFO start point.
  423. * \hideinitializer
  424. */
  425. #define SPII2S_CLR_RX_FIFO(i2s) ( (i2s)->FIFOCTL |= SPI_FIFOCTL_RXFBCLR_Msk )
  426. /**
  427. * @brief This function sets the recording source channel when mono mode is used.
  428. * @param[in] i2s The pointer of the specified I2S module.
  429. * @param[in] u32Ch left or right channel. Valid values are:
  430. * - \ref SPII2S_MONO_LEFT
  431. * - \ref SPII2S_MONO_RIGHT
  432. * @return None
  433. * @details This function selects the recording source channel of monaural mode.
  434. * \hideinitializer
  435. */
  436. __STATIC_INLINE void SPII2S_SET_MONO_RX_CHANNEL(SPI_T *i2s, uint32_t u32Ch)
  437. {
  438. u32Ch == SPII2S_MONO_LEFT ?
  439. (i2s->I2SCTL |= SPI_I2SCTL_RXLCH_Msk) :
  440. (i2s->I2SCTL &= ~SPI_I2SCTL_RXLCH_Msk);
  441. }
  442. /**
  443. * @brief Write data to I2S TX FIFO.
  444. * @param[in] i2s The pointer of the specified I2S module.
  445. * @param[in] u32Data The value written to TX FIFO.
  446. * @return None
  447. * @details This macro will write a value to TX FIFO.
  448. * \hideinitializer
  449. */
  450. #define SPII2S_WRITE_TX_FIFO(i2s, u32Data) ( (i2s)->TX = (u32Data) )
  451. /**
  452. * @brief Read RX FIFO.
  453. * @param[in] i2s The pointer of the specified I2S module.
  454. * @return The value read from RX FIFO.
  455. * @details This function will return a value read from RX FIFO.
  456. * \hideinitializer
  457. */
  458. #define SPII2S_READ_RX_FIFO(i2s) ( (i2s)->RX )
  459. /**
  460. * @brief Get the interrupt flag.
  461. * @param[in] i2s The pointer of the specified I2S module.
  462. * @param[in] u32Mask The mask value for all interrupt flags.
  463. * @return The interrupt flags specified by the u32mask parameter.
  464. * @details This macro will return the combination interrupt flags of SPI_I2SSTS register. The flags are specified by the u32mask parameter.
  465. * \hideinitializer
  466. */
  467. #define SPII2S_GET_INT_FLAG(i2s, u32Mask) ( (i2s)->I2SSTS & (u32Mask) )
  468. /**
  469. * @brief Clear the interrupt flag.
  470. * @param[in] i2s The pointer of the specified I2S module.
  471. * @param[in] u32Mask The mask value for all interrupt flags.
  472. * @return None
  473. * @details This macro will clear the interrupt flags specified by the u32mask parameter.
  474. * @note Except TX and RX FIFO threshold interrupt flags, the other interrupt flags can be cleared by writing 1 to itself.
  475. * \hideinitializer
  476. */
  477. #define SPII2S_CLR_INT_FLAG(i2s, u32Mask) ( (i2s)->I2SSTS = (u32Mask) )
  478. /**
  479. * @brief Get transmit FIFO level
  480. * @param[in] i2s The pointer of the specified I2S module.
  481. * @return TX FIFO level
  482. * @details This macro will return the number of available words in TX FIFO.
  483. * \hideinitializer
  484. */
  485. #define SPII2S_GET_TX_FIFO_LEVEL(i2s) ( ((i2s)->I2SSTS & SPI_I2SSTS_TXCNT_Msk) >> SPI_I2SSTS_TXCNT_Pos )
  486. /**
  487. * @brief Get receive FIFO level
  488. * @param[in] i2s The pointer of the specified I2S module.
  489. * @return RX FIFO level
  490. * @details This macro will return the number of available words in RX FIFO.
  491. * \hideinitializer
  492. */
  493. #define SPII2S_GET_RX_FIFO_LEVEL(i2s) ( ((i2s)->I2SSTS & SPI_I2SSTS_RXCNT_Msk) >> SPI_I2SSTS_RXCNT_Pos )
  494. /**
  495. * @brief Set SPI Master Receive Phase.
  496. * @param[in] spi is the base address of SPI module.
  497. * @param[in] rxdly is the clock cycle of delay for rx phase.
  498. * @return none
  499. * \hideinitializer
  500. */
  501. #define SPI_SET_MRXPHASE(spi, rxdly) ( (spi)->INTERNAL = ((spi)->INTERNAL & ~SPI_INTERNAL_MRXPHASE_Msk) | (rxdly<<SPI_INTERNAL_MRXPHASE_Pos) )
  502. /* Function prototype declaration */
  503. uint32_t SPI_Open(SPI_T *spi, uint32_t u32MasterSlave, uint32_t u32SPIMode, uint32_t u32DataWidth, uint32_t u32BusClock);
  504. void SPI_Close(SPI_T *spi);
  505. void SPI_ClearRxFIFO(SPI_T *spi);
  506. void SPI_ClearTxFIFO(SPI_T *spi);
  507. void SPI_DisableAutoSS(SPI_T *spi);
  508. void SPI_EnableAutoSS(SPI_T *spi, uint32_t u32SSPinMask, uint32_t u32ActiveLevel);
  509. uint32_t SPI_SetBusClock(SPI_T *spi, uint32_t u32BusClock);
  510. void SPI_SetFIFO(SPI_T *spi, uint32_t u32TxThreshold, uint32_t u32RxThreshold);
  511. uint32_t SPI_GetBusClock(SPI_T *spi);
  512. void SPI_EnableInt(SPI_T *spi, uint32_t u32Mask);
  513. void SPI_DisableInt(SPI_T *spi, uint32_t u32Mask);
  514. uint32_t SPI_GetIntFlag(SPI_T *spi, uint32_t u32Mask);
  515. void SPI_ClearIntFlag(SPI_T *spi, uint32_t u32Mask);
  516. uint32_t SPI_GetStatus(SPI_T *spi, uint32_t u32Mask);
  517. uint32_t SPII2S_Open(SPI_T *i2s, uint32_t u32MasterSlave, uint32_t u32SampleRate, uint32_t u32WordWidth, uint32_t u32Channels, uint32_t u32DataFormat);
  518. void SPII2S_Close(SPI_T *i2s);
  519. void SPII2S_EnableInt(SPI_T *i2s, uint32_t u32Mask);
  520. void SPII2S_DisableInt(SPI_T *i2s, uint32_t u32Mask);
  521. uint32_t SPII2S_EnableMCLK(SPI_T *i2s, uint32_t u32BusClock);
  522. void SPII2S_DisableMCLK(SPI_T *i2s);
  523. void SPII2S_SetFIFO(SPI_T *i2s, uint32_t u32TxThreshold, uint32_t u32RxThreshold);
  524. /*@}*/ /* end of group SPI_EXPORTED_FUNCTIONS */
  525. /*@}*/ /* end of group SPI_Driver */
  526. /*@}*/ /* end of group Standard_Driver */
  527. #ifdef __cplusplus
  528. }
  529. #endif
  530. #endif