nu_spi.h 25 KB

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