nu_spi.h 25 KB

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