nu_spi.h 26 KB

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