spi.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /******************************************************************************
  2. * @file spi.h
  3. * @version V0.10
  4. * $Revision: 17 $
  5. * $Date: 15/08/11 10:26a $
  6. * @brief M451 series SPI driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __SPI_H__
  12. #define __SPI_H__
  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Include related headers */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. #include "M451Series.h"
  17. #ifdef __cplusplus
  18. extern "C"
  19. {
  20. #endif
  21. /** @addtogroup Standard_Driver Standard Driver
  22. @{
  23. */
  24. /** @addtogroup SPI_Driver SPI Driver
  25. @{
  26. */
  27. /** @addtogroup SPI_EXPORTED_CONSTANTS SPI Exported Constants
  28. @{
  29. */
  30. #define SPI_MODE_0 (SPI_CTL_TXNEG_Msk) /*!< CLKPOL=0; RXNEG=0; TXNEG=1 */
  31. #define SPI_MODE_1 (SPI_CTL_RXNEG_Msk) /*!< CLKPOL=0; RXNEG=1; TXNEG=0 */
  32. #define SPI_MODE_2 (SPI_CTL_CLKPOL_Msk | SPI_CTL_RXNEG_Msk) /*!< CLKPOL=1; RXNEG=1; TXNEG=0 */
  33. #define SPI_MODE_3 (SPI_CTL_CLKPOL_Msk | SPI_CTL_TXNEG_Msk) /*!< CLKPOL=1; RXNEG=0; TXNEG=1 */
  34. #define SPI_SLAVE (SPI_CTL_SLAVE_Msk) /*!< Set as slave */
  35. #define SPI_MASTER (0x0) /*!< Set as master */
  36. #define SPI_SS (SPI_SSCTL_SS_Msk) /*!< Set SS */
  37. #define SPI_SS_ACTIVE_HIGH (SPI_SSCTL_SSACTPOL_Msk) /*!< SS active high */
  38. #define SPI_SS_ACTIVE_LOW (0x0) /*!< SS active low */
  39. /* SPI Interrupt Mask */
  40. #define SPI_UNIT_INT_MASK (0x001) /*!< Unit transfer interrupt mask */
  41. #define SPI_SSACT_INT_MASK (0x002) /*!< Slave selection signal active interrupt mask */
  42. #define SPI_SSINACT_INT_MASK (0x004) /*!< Slave selection signal inactive interrupt mask */
  43. #define SPI_SLVUR_INT_MASK (0x008) /*!< Slave under run interrupt mask */
  44. #define SPI_SLVBE_INT_MASK (0x010) /*!< Slave bit count error interrupt mask */
  45. #define SPI_SLVTO_INT_MASK (0x020) /*!< Slave time-out interrupt mask */
  46. #define SPI_TXUF_INT_MASK (0x040) /*!< Slave TX underflow interrupt mask */
  47. #define SPI_FIFO_TXTH_INT_MASK (0x080) /*!< FIFO TX threshold interrupt mask */
  48. #define SPI_FIFO_RXTH_INT_MASK (0x100) /*!< FIFO RX threshold interrupt mask */
  49. #define SPI_FIFO_RXOV_INT_MASK (0x200) /*!< FIFO RX overrun interrupt mask */
  50. #define SPI_FIFO_RXTO_INT_MASK (0x400) /*!< FIFO RX time-out interrupt mask */
  51. /* SPI Status Mask */
  52. #define SPI_BUSY_MASK (0x01) /*!< Busy status mask */
  53. #define SPI_RX_EMPTY_MASK (0x02) /*!< RX empty status mask */
  54. #define SPI_RX_FULL_MASK (0x04) /*!< RX full status mask */
  55. #define SPI_TX_EMPTY_MASK (0x08) /*!< TX empty status mask */
  56. #define SPI_TX_FULL_MASK (0x10) /*!< TX full status mask */
  57. #define SPI_TXRX_RESET_MASK (0x20) /*!< TX or RX reset status mask */
  58. #define SPI_SPIEN_STS_MASK (0x40) /*!< SPIEN status mask */
  59. #define SPI_SSLINE_STS_MASK (0x80) /*!< SPIn_SS line status mask */
  60. /* I2S Data Width */
  61. #define I2S_DATABIT_8 (0 << SPI_I2SCTL_WDWIDTH_Pos) /*!< I2S data width is 8-bit */
  62. #define I2S_DATABIT_16 (1 << SPI_I2SCTL_WDWIDTH_Pos) /*!< I2S data width is 16-bit */
  63. #define I2S_DATABIT_24 (2 << SPI_I2SCTL_WDWIDTH_Pos) /*!< I2S data width is 24-bit */
  64. #define I2S_DATABIT_32 (3 << SPI_I2SCTL_WDWIDTH_Pos) /*!< I2S data width is 32-bit */
  65. /* I2S Audio Format */
  66. #define I2S_MONO SPI_I2SCTL_MONO_Msk /*!< Monaural channel */
  67. #define I2S_STEREO 0 /*!< Stereo channel */
  68. /* I2S Data Format */
  69. #define I2S_FORMAT_I2S (0<<SPI_I2SCTL_FORMAT_Pos) /*!< I2S data format */
  70. #define I2S_FORMAT_MSB (1<<SPI_I2SCTL_FORMAT_Pos) /*!< MSB justified data format */
  71. #define I2S_FORMAT_PCMA (2<<SPI_I2SCTL_FORMAT_Pos) /*!< PCM mode A data format */
  72. #define I2S_FORMAT_PCMB (3<<SPI_I2SCTL_FORMAT_Pos) /*!< PCM mode B data format */
  73. /* I2S Operation mode */
  74. #define I2S_MODE_SLAVE SPI_I2SCTL_SLAVE_Msk /*!< As slave mode */
  75. #define I2S_MODE_MASTER 0 /*!< As master mode */
  76. /* I2S TX FIFO Threshold */
  77. #define I2S_FIFO_TX_LEVEL_WORD_0 0 /*!< TX threshold is 0 word */
  78. #define I2S_FIFO_TX_LEVEL_WORD_1 (1 << SPI_FIFOCTL_TXTH_Pos) /*!< TX threshold is 1 word */
  79. #define I2S_FIFO_TX_LEVEL_WORD_2 (2 << SPI_FIFOCTL_TXTH_Pos) /*!< TX threshold is 2 words */
  80. #define I2S_FIFO_TX_LEVEL_WORD_3 (3 << SPI_FIFOCTL_TXTH_Pos) /*!< TX threshold is 3 words */
  81. /* I2S RX FIFO Threshold */
  82. #define I2S_FIFO_RX_LEVEL_WORD_1 0 /*!< RX threshold is 1 word */
  83. #define I2S_FIFO_RX_LEVEL_WORD_2 (1 << SPI_FIFOCTL_RXTH_Pos) /*!< RX threshold is 2 words */
  84. #define I2S_FIFO_RX_LEVEL_WORD_3 (2 << SPI_FIFOCTL_RXTH_Pos) /*!< RX threshold is 3 words */
  85. #define I2S_FIFO_RX_LEVEL_WORD_4 (3 << SPI_FIFOCTL_RXTH_Pos) /*!< RX threshold is 4 words */
  86. /* I2S Record Channel */
  87. #define I2S_MONO_RIGHT 0 /*!< Record mono right channel */
  88. #define I2S_MONO_LEFT SPI_I2SCTL_RXLCH_Msk /*!< Record mono left channel */
  89. /* I2S Channel */
  90. #define I2S_RIGHT 0 /*!< Select right channel */
  91. #define I2S_LEFT 1 /*!< Select left channel */
  92. /* I2S Interrupt Mask */
  93. #define I2S_FIFO_TXTH_INT_MASK (0x01) /*!< TX FIFO threshold interrupt mask */
  94. #define I2S_FIFO_RXTH_INT_MASK (0x02) /*!< RX FIFO threshold interrupt mask */
  95. #define I2S_FIFO_RXOV_INT_MASK (0x04) /*!< RX FIFO overrun interrupt mask */
  96. #define I2S_FIFO_RXTO_INT_MASK (0x08) /*!< RX FIFO time-out interrupt mask */
  97. #define I2S_TXUF_INT_MASK (0x10) /*!< TX FIFO underflow interrupt mask */
  98. #define I2S_RIGHT_ZC_INT_MASK (0x20) /*!< Right channel zero cross interrupt mask */
  99. #define I2S_LEFT_ZC_INT_MASK (0x40) /*!< Left channel zero cross interrupt mask */
  100. /*@}*/ /* end of group SPI_EXPORTED_CONSTANTS */
  101. /** @addtogroup SPI_EXPORTED_FUNCTIONS SPI Exported Functions
  102. @{
  103. */
  104. /**
  105. * @brief Clear the unit transfer interrupt flag.
  106. * @param[in] spi The pointer of the specified SPI module.
  107. * @return None.
  108. * @details Write 1 to UNITIF bit of SPI_STATUS register to clear the unit transfer interrupt flag.
  109. */
  110. #define SPI_CLR_UNIT_TRANS_INT_FLAG(spi) ((spi)->STATUS = SPI_STATUS_UNITIF_Msk)
  111. /**
  112. * @brief Disable 2-bit Transfer mode.
  113. * @param[in] spi The pointer of the specified SPI module.
  114. * @return None.
  115. * @details Clear TWOBIT bit of SPI_CTL register to disable 2-bit Transfer mode.
  116. */
  117. #define SPI_DISABLE_2BIT_MODE(spi) ((spi)->CTL &= ~SPI_CTL_TWOBIT_Msk)
  118. /**
  119. * @brief Disable Slave 3-wire mode.
  120. * @param[in] spi The pointer of the specified SPI module.
  121. * @return None.
  122. * @details Clear SLV3WIRE bit of SPI_SSCTL register to disable Slave 3-wire mode.
  123. */
  124. #define SPI_DISABLE_3WIRE_MODE(spi) ((spi)->SSCTL &= ~SPI_SSCTL_SLV3WIRE_Msk)
  125. /**
  126. * @brief Disable Dual I/O mode.
  127. * @param[in] spi The pointer of the specified SPI module.
  128. * @return None.
  129. * @details Clear DUALIOEN bit of SPI_CTL register to disable Dual I/O mode.
  130. */
  131. #define SPI_DISABLE_DUAL_MODE(spi) ((spi)->CTL &= ~SPI_CTL_DUALIOEN_Msk)
  132. /**
  133. * @brief Disable Quad I/O mode.
  134. * @param[in] spi The pointer of the specified SPI module.
  135. * @return None.
  136. * @details Clear QUADIOEN bit of SPI_CTL register to disable Quad I/O mode.
  137. */
  138. #define SPI_DISABLE_QUAD_MODE(spi) ((spi)->CTL &= ~SPI_CTL_QUADIOEN_Msk)
  139. /**
  140. * @brief Enable 2-bit Transfer mode.
  141. * @param[in] spi The pointer of the specified SPI module.
  142. * @return None.
  143. * @details Set TWOBIT bit of SPI_CTL register to enable 2-bit Transfer mode.
  144. */
  145. #define SPI_ENABLE_2BIT_MODE(spi) ((spi)->CTL |= SPI_CTL_TWOBIT_Msk)
  146. /**
  147. * @brief Enable Slave 3-wire mode.
  148. * @param[in] spi The pointer of the specified SPI module.
  149. * @return None.
  150. * @details Set SLV3WIRE bit of SPI_SSCTL register to enable Slave 3-wire mode.
  151. */
  152. #define SPI_ENABLE_3WIRE_MODE(spi) ((spi)->SSCTL |= SPI_SSCTL_SLV3WIRE_Msk)
  153. /**
  154. * @brief Enable Dual input mode.
  155. * @param[in] spi The pointer of the specified SPI module.
  156. * @return None.
  157. * @details Clear QDIODIR bit and set DUALIOEN bit of SPI_CTL register to enable Dual input mode.
  158. */
  159. #define SPI_ENABLE_DUAL_INPUT_MODE(spi) ((spi)->CTL = ((spi)->CTL & (~SPI_CTL_QDIODIR_Msk)) | SPI_CTL_DUALIOEN_Msk)
  160. /**
  161. * @brief Enable Dual output mode.
  162. * @param[in] spi The pointer of the specified SPI module.
  163. * @return None.
  164. * @details Set QDIODIR bit and DUALIOEN bit of SPI_CTL register to enable Dual output mode.
  165. */
  166. #define SPI_ENABLE_DUAL_OUTPUT_MODE(spi) ((spi)->CTL |= (SPI_CTL_QDIODIR_Msk | SPI_CTL_DUALIOEN_Msk))
  167. /**
  168. * @brief Enable Quad input mode.
  169. * @param[in] spi The pointer of the specified SPI module.
  170. * @return None.
  171. * @details Clear QDIODIR bit and set QUADIOEN bit of SPI_CTL register to enable Quad input mode.
  172. */
  173. #define SPI_ENABLE_QUAD_INPUT_MODE(spi) ((spi)->CTL = ((spi)->CTL & (~SPI_CTL_QDIODIR_Msk)) | SPI_CTL_QUADIOEN_Msk)
  174. /**
  175. * @brief Enable Quad output mode.
  176. * @param[in] spi The pointer of the specified SPI module.
  177. * @return None.
  178. * @details Set QDIODIR bit and QUADIOEN bit of SPI_CTL register to enable Quad output mode.
  179. */
  180. #define SPI_ENABLE_QUAD_OUTPUT_MODE(spi) ((spi)->CTL |= (SPI_CTL_QDIODIR_Msk | SPI_CTL_QUADIOEN_Msk))
  181. /**
  182. * @brief Trigger RX PDMA function.
  183. * @param[in] spi The pointer of the specified SPI module.
  184. * @return None.
  185. * @details Set RXPDMAEN bit of SPI_PDMACTL register to enable RX PDMA transfer function.
  186. */
  187. #define SPI_TRIGGER_RX_PDMA(spi) ((spi)->PDMACTL |= SPI_PDMACTL_RXPDMAEN_Msk)
  188. /**
  189. * @brief Trigger TX PDMA function.
  190. * @param[in] spi The pointer of the specified SPI module.
  191. * @return None.
  192. * @details Set TXPDMAEN bit of SPI_PDMACTL register to enable TX PDMA transfer function.
  193. */
  194. #define SPI_TRIGGER_TX_PDMA(spi) ((spi)->PDMACTL |= SPI_PDMACTL_TXPDMAEN_Msk)
  195. /**
  196. * @brief Disable RX PDMA transfer.
  197. * @param[in] spi The pointer of the specified SPI module.
  198. * @return None.
  199. * @details Clear RXPDMAEN bit of SPI_PDMACTL register to disable RX PDMA transfer function.
  200. */
  201. #define SPI_DISABLE_RX_PDMA(spi) ( (spi)->PDMACTL &= ~SPI_PDMACTL_RXPDMAEN_Msk )
  202. /**
  203. * @brief Disable TX PDMA transfer.
  204. * @param[in] spi The pointer of the specified SPI module.
  205. * @return None.
  206. * @details Clear TXPDMAEN bit of SPI_PDMACTL register to disable TX PDMA transfer function.
  207. */
  208. #define SPI_DISABLE_TX_PDMA(spi) ( (spi)->PDMACTL &= ~SPI_PDMACTL_TXPDMAEN_Msk )
  209. /**
  210. * @brief Get the count of available data in RX FIFO.
  211. * @param[in] spi The pointer of the specified SPI module.
  212. * @return The count of available data in RX FIFO.
  213. * @details Read RXCNT (SPI_STATUS[27:24]) to get the count of available data in RX FIFO.
  214. */
  215. #define SPI_GET_RX_FIFO_COUNT(spi) (((spi)->STATUS & SPI_STATUS_RXCNT_Msk) >> SPI_STATUS_RXCNT_Pos)
  216. /**
  217. * @brief Get the RX FIFO empty flag.
  218. * @param[in] spi The pointer of the specified SPI module.
  219. * @retval 0 RX FIFO is not empty.
  220. * @retval 1 RX FIFO is empty.
  221. * @details Read RXEMPTY bit of SPI_STATUS register to get the RX FIFO empty flag.
  222. */
  223. #define SPI_GET_RX_FIFO_EMPTY_FLAG(spi) (((spi)->STATUS & SPI_STATUS_RXEMPTY_Msk)>>SPI_STATUS_RXEMPTY_Pos)
  224. /**
  225. * @brief Get the TX FIFO empty flag.
  226. * @param[in] spi The pointer of the specified SPI module.
  227. * @retval 0 TX FIFO is not empty.
  228. * @retval 1 TX FIFO is empty.
  229. * @details Read TXEMPTY bit of SPI_STATUS register to get the TX FIFO empty flag.
  230. */
  231. #define SPI_GET_TX_FIFO_EMPTY_FLAG(spi) (((spi)->STATUS & SPI_STATUS_TXEMPTY_Msk)>>SPI_STATUS_TXEMPTY_Pos)
  232. /**
  233. * @brief Get the TX FIFO full flag.
  234. * @param[in] spi The pointer of the specified SPI module.
  235. * @retval 0 TX FIFO is not full.
  236. * @retval 1 TX FIFO is full.
  237. * @details Read TXFULL bit of SPI_STATUS register to get the TX FIFO full flag.
  238. */
  239. #define SPI_GET_TX_FIFO_FULL_FLAG(spi) (((spi)->STATUS & SPI_STATUS_TXFULL_Msk)>>SPI_STATUS_TXFULL_Pos)
  240. /**
  241. * @brief Get the datum read from RX register.
  242. * @param[in] spi The pointer of the specified SPI module.
  243. * @return Data in RX register.
  244. * @details Read SPI_RX register to get the received datum.
  245. */
  246. #define SPI_READ_RX(spi) ((spi)->RX)
  247. /**
  248. * @brief Write datum to TX register.
  249. * @param[in] spi The pointer of the specified SPI module.
  250. * @param[in] u32TxData The datum which user attempt to transfer through SPI bus.
  251. * @return None.
  252. * @details Write u32TxData to SPI_TX register.
  253. */
  254. #define SPI_WRITE_TX(spi, u32TxData) ((spi)->TX = (u32TxData))
  255. /**
  256. * @brief Set SPIn_SS pin to high state.
  257. * @param[in] spi The pointer of the specified SPI module.
  258. * @return None.
  259. * @details Disable automatic slave selection function and set SPIn_SS pin to high state.
  260. */
  261. #define SPI_SET_SS_HIGH(spi) ((spi)->SSCTL = ((spi)->SSCTL & (~SPI_SSCTL_AUTOSS_Msk)) | (SPI_SSCTL_SSACTPOL_Msk | SPI_SSCTL_SS_Msk))
  262. /**
  263. * @brief Set SPIn_SS pin to low state.
  264. * @param[in] spi The pointer of the specified SPI module.
  265. * @return None.
  266. * @details Disable automatic slave selection function and set SPIn_SS pin to low state.
  267. */
  268. #define SPI_SET_SS_LOW(spi) ((spi)->SSCTL = ((spi)->SSCTL & (~(SPI_SSCTL_AUTOSS_Msk | SPI_SSCTL_SSACTPOL_Msk))) | SPI_SSCTL_SS_Msk)
  269. /**
  270. * @brief Enable Byte Reorder function.
  271. * @param[in] spi The pointer of the specified SPI module.
  272. * @return None.
  273. * @details Enable Byte Reorder function. The suspend interval depends on the setting of SUSPITV (SPI_CTL[7:4]).
  274. */
  275. #define SPI_ENABLE_BYTE_REORDER(spi) ((spi)->CTL |= SPI_CTL_REORDER_Msk)
  276. /**
  277. * @brief Disable Byte Reorder function.
  278. * @param[in] spi The pointer of the specified SPI module.
  279. * @return None.
  280. * @details Clear REORDER bit field of SPI_CTL register to disable Byte Reorder function.
  281. */
  282. #define SPI_DISABLE_BYTE_REORDER(spi) ((spi)->CTL &= ~SPI_CTL_REORDER_Msk)
  283. /**
  284. * @brief Set the length of suspend interval.
  285. * @param[in] spi The pointer of the specified SPI module.
  286. * @param[in] u32SuspCycle Decides the length of suspend interval. It could be 0 ~ 15.
  287. * @return None.
  288. * @details Set the length of suspend interval according to u32SuspCycle.
  289. * The length of suspend interval is ((u32SuspCycle + 0.5) * the length of one SPI bus clock cycle).
  290. */
  291. #define SPI_SET_SUSPEND_CYCLE(spi, u32SuspCycle) ((spi)->CTL = ((spi)->CTL & ~SPI_CTL_SUSPITV_Msk) | ((u32SuspCycle) << SPI_CTL_SUSPITV_Pos))
  292. /**
  293. * @brief Set the SPI transfer sequence with LSB first.
  294. * @param[in] spi The pointer of the specified SPI module.
  295. * @return None.
  296. * @details Set LSB bit of SPI_CTL register to set the SPI transfer sequence with LSB first.
  297. */
  298. #define SPI_SET_LSB_FIRST(spi) ((spi)->CTL |= SPI_CTL_LSB_Msk)
  299. /**
  300. * @brief Set the SPI transfer sequence with MSB first.
  301. * @param[in] spi The pointer of the specified SPI module.
  302. * @return None.
  303. * @details Clear LSB bit of SPI_CTL register to set the SPI transfer sequence with MSB first.
  304. */
  305. #define SPI_SET_MSB_FIRST(spi) ((spi)->CTL &= ~SPI_CTL_LSB_Msk)
  306. /**
  307. * @brief Set the data width of a SPI transaction.
  308. * @param[in] spi The pointer of the specified SPI module.
  309. * @param[in] u32Width The bit width of one transaction.
  310. * @return None.
  311. * @details The data width can be 8 ~ 32 bits.
  312. */
  313. #define SPI_SET_DATA_WIDTH(spi, u32Width) ((spi)->CTL = ((spi)->CTL & ~SPI_CTL_DWIDTH_Msk) | (((u32Width)&0x1F) << SPI_CTL_DWIDTH_Pos))
  314. /**
  315. * @brief Get the SPI busy state.
  316. * @param[in] spi The pointer of the specified SPI module.
  317. * @retval 0 SPI controller is not busy.
  318. * @retval 1 SPI controller is busy.
  319. * @details This macro will return the busy state of SPI controller.
  320. */
  321. #define SPI_IS_BUSY(spi) ( ((spi)->STATUS & SPI_STATUS_BUSY_Msk)>>SPI_STATUS_BUSY_Pos )
  322. /**
  323. * @brief Enable SPI controller.
  324. * @param[in] spi The pointer of the specified SPI module.
  325. * @return None.
  326. * @details Set SPIEN (SPI_CTL[0]) to enable SPI controller.
  327. */
  328. #define SPI_ENABLE(spi) ((spi)->CTL |= SPI_CTL_SPIEN_Msk)
  329. /**
  330. * @brief Disable SPI controller.
  331. * @param[in] spi The pointer of the specified SPI module.
  332. * @return None.
  333. * @details Clear SPIEN (SPI_CTL[0]) to disable SPI controller.
  334. */
  335. #define SPI_DISABLE(spi) ((spi)->CTL &= ~SPI_CTL_SPIEN_Msk)
  336. /**
  337. * @brief Enable zero cross detection function.
  338. * @param[in] i2s The pointer of the specified I2S module.
  339. * @param[in] u32ChMask The mask for left or right channel. Valid values are:
  340. * - \ref I2S_RIGHT
  341. * - \ref I2S_LEFT
  342. * @return None
  343. * @details This function will set RZCEN or LZCEN bit of SPI_I2SCTL register to enable zero cross detection function.
  344. */
  345. static __INLINE void I2S_ENABLE_TX_ZCD(SPI_T *i2s, uint32_t u32ChMask)
  346. {
  347. if(u32ChMask == I2S_RIGHT)
  348. i2s->I2SCTL |= SPI_I2SCTL_RZCEN_Msk;
  349. else
  350. i2s->I2SCTL |= SPI_I2SCTL_LZCEN_Msk;
  351. }
  352. /**
  353. * @brief Disable zero cross detection function.
  354. * @param[in] i2s The pointer of the specified I2S module.
  355. * @param[in] u32ChMask The mask for left or right channel. Valid values are:
  356. * - \ref I2S_RIGHT
  357. * - \ref I2S_LEFT
  358. * @return None
  359. * @details This function will clear RZCEN or LZCEN bit of SPI_I2SCTL register to disable zero cross detection function.
  360. */
  361. static __INLINE void I2S_DISABLE_TX_ZCD(SPI_T *i2s, uint32_t u32ChMask)
  362. {
  363. if(u32ChMask == I2S_RIGHT)
  364. i2s->I2SCTL &= ~SPI_I2SCTL_RZCEN_Msk;
  365. else
  366. i2s->I2SCTL &= ~SPI_I2SCTL_LZCEN_Msk;
  367. }
  368. /**
  369. * @brief Enable I2S TX DMA function.
  370. * @param[in] i2s The pointer of the specified I2S module.
  371. * @return None
  372. * @details This macro will set TXPDMAEN bit of SPI_PDMACTL register to transmit data with PDMA.
  373. */
  374. #define I2S_ENABLE_TXDMA(i2s) ( (i2s)->PDMACTL |= SPI_PDMACTL_TXPDMAEN_Msk )
  375. /**
  376. * @brief Disable I2S TX DMA function.
  377. * @param[in] i2s The pointer of the specified I2S module.
  378. * @return None
  379. * @details This macro will clear TXPDMAEN bit of SPI_PDMACTL register to disable TX DMA function.
  380. */
  381. #define I2S_DISABLE_TXDMA(i2s) ( (i2s)->PDMACTL &= ~SPI_PDMACTL_TXPDMAEN_Msk )
  382. /**
  383. * @brief Enable I2S RX DMA function.
  384. * @param[in] i2s The pointer of the specified I2S module.
  385. * @return None
  386. * @details This macro will set RXPDMAEN bit of SPI_PDMACTL register to receive data with PDMA.
  387. */
  388. #define I2S_ENABLE_RXDMA(i2s) ( (i2s)->PDMACTL |= SPI_PDMACTL_RXPDMAEN_Msk )
  389. /**
  390. * @brief Disable I2S RX DMA function.
  391. * @param[in] i2s The pointer of the specified I2S module.
  392. * @return None
  393. * @details This macro will clear RXPDMAEN bit of SPI_PDMACTL register to disable RX DMA function.
  394. */
  395. #define I2S_DISABLE_RXDMA(i2s) ( (i2s)->PDMACTL &= ~SPI_PDMACTL_RXPDMAEN_Msk )
  396. /**
  397. * @brief Enable I2S TX function.
  398. * @param[in] i2s The pointer of the specified I2S module.
  399. * @return None
  400. * @details This macro will set TXEN bit of SPI_I2SCTL register to enable I2S TX function.
  401. */
  402. #define I2S_ENABLE_TX(i2s) ( (i2s)->I2SCTL |= SPI_I2SCTL_TXEN_Msk )
  403. /**
  404. * @brief Disable I2S TX function.
  405. * @param[in] i2s The pointer of the specified I2S module.
  406. * @return None
  407. * @details This macro will clear TXEN bit of SPI_I2SCTL register to disable I2S TX function.
  408. */
  409. #define I2S_DISABLE_TX(i2s) ( (i2s)->I2SCTL &= ~SPI_I2SCTL_TXEN_Msk )
  410. /**
  411. * @brief Enable I2S RX function.
  412. * @param[in] i2s The pointer of the specified I2S module.
  413. * @return None
  414. * @details This macro will set RXEN bit of SPI_I2SCTL register to enable I2S RX function.
  415. */
  416. #define I2S_ENABLE_RX(i2s) ( (i2s)->I2SCTL |= SPI_I2SCTL_RXEN_Msk )
  417. /**
  418. * @brief Disable I2S RX function.
  419. * @param[in] i2s The pointer of the specified I2S module.
  420. * @return None
  421. * @details This macro will clear RXEN bit of SPI_I2SCTL register to disable I2S RX function.
  422. */
  423. #define I2S_DISABLE_RX(i2s) ( (i2s)->I2SCTL &= ~SPI_I2SCTL_RXEN_Msk )
  424. /**
  425. * @brief Enable TX Mute function.
  426. * @param[in] i2s The pointer of the specified I2S module.
  427. * @return None
  428. * @details This macro will set MUTE bit of SPI_I2SCTL register to enable I2S TX mute function.
  429. */
  430. #define I2S_ENABLE_TX_MUTE(i2s) ( (i2s)->I2SCTL |= SPI_I2SCTL_MUTE_Msk )
  431. /**
  432. * @brief Disable TX Mute function.
  433. * @param[in] i2s The pointer of the specified I2S module.
  434. * @return None
  435. * @details This macro will clear MUTE bit of SPI_I2SCTL register to disable I2S TX mute function.
  436. */
  437. #define I2S_DISABLE_TX_MUTE(i2s) ( (i2s)->I2SCTL &= ~SPI_I2SCTL_MUTE_Msk )
  438. /**
  439. * @brief Clear TX FIFO.
  440. * @param[in] i2s The pointer of the specified I2S module.
  441. * @return None
  442. * @details This macro will clear TX FIFO. The internal TX FIFO pointer will be reset to FIFO start point.
  443. */
  444. #define I2S_CLR_TX_FIFO(i2s) ( (i2s)->FIFOCTL |= SPI_FIFOCTL_TXFBCLR_Msk )
  445. /**
  446. * @brief Clear RX FIFO.
  447. * @param[in] i2s The pointer of the specified I2S module.
  448. * @return None
  449. * @details This macro will clear RX FIFO. The internal RX FIFO pointer will be reset to FIFO start point.
  450. */
  451. #define I2S_CLR_RX_FIFO(i2s) ( (i2s)->FIFOCTL |= SPI_FIFOCTL_RXFBCLR_Msk )
  452. /**
  453. * @brief This function sets the recording source channel when mono mode is used.
  454. * @param[in] i2s The pointer of the specified I2S module.
  455. * @param[in] u32Ch left or right channel. Valid values are:
  456. * - \ref I2S_MONO_LEFT
  457. * - \ref I2S_MONO_RIGHT
  458. * @return None
  459. * @details This function selects the recording source channel of monaural mode.
  460. */
  461. static __INLINE void I2S_SET_MONO_RX_CHANNEL(SPI_T *i2s, uint32_t u32Ch)
  462. {
  463. u32Ch == I2S_MONO_LEFT ?
  464. (i2s->I2SCTL |= SPI_I2SCTL_RXLCH_Msk) :
  465. (i2s->I2SCTL &= ~SPI_I2SCTL_RXLCH_Msk);
  466. }
  467. /**
  468. * @brief Write data to I2S TX FIFO.
  469. * @param[in] i2s The pointer of the specified I2S module.
  470. * @param[in] u32Data The value written to TX FIFO.
  471. * @return None
  472. * @details This macro will write a value to TX FIFO.
  473. */
  474. #define I2S_WRITE_TX_FIFO(i2s, u32Data) ( (i2s)->TX = (u32Data) )
  475. /**
  476. * @brief Read RX FIFO.
  477. * @param[in] i2s The pointer of the specified I2S module.
  478. * @return The value read from RX FIFO.
  479. * @details This function will return a value read from RX FIFO.
  480. */
  481. #define I2S_READ_RX_FIFO(i2s) ( (i2s)->RX )
  482. /**
  483. * @brief Get the interrupt flag.
  484. * @param[in] i2s The pointer of the specified I2S module.
  485. * @param[in] u32Mask The mask value for all interrupt flags.
  486. * @return The interrupt flags specified by the u32mask parameter.
  487. * @details This macro will return the combination interrupt flags of SPI_I2SSTS register. The flags are specified by the u32mask parameter.
  488. */
  489. #define I2S_GET_INT_FLAG(i2s, u32Mask) ( (i2s)->I2SSTS & (u32Mask) )
  490. /**
  491. * @brief Clear the interrupt flag.
  492. * @param[in] i2s The pointer of the specified I2S module.
  493. * @param[in] u32Mask The mask value for all interrupt flags.
  494. * @return None
  495. * @details This macro will clear the interrupt flags specified by the u32mask parameter.
  496. * @note Except TX and RX FIFO threshold interrupt flags, the other interrupt flags can be cleared by writing 1 to itself.
  497. */
  498. #define I2S_CLR_INT_FLAG(i2s, u32Mask) ( (i2s)->I2SSTS = (u32Mask) )
  499. /**
  500. * @brief Get transmit FIFO level
  501. * @param[in] i2s The pointer of the specified I2S module.
  502. * @return TX FIFO level
  503. * @details This macro will return the number of available words in TX FIFO.
  504. */
  505. #define I2S_GET_TX_FIFO_LEVEL(i2s) ( ((i2s)->I2SSTS & SPI_I2SSTS_TXCNT_Msk) >> SPI_I2SSTS_TXCNT_Pos )
  506. /**
  507. * @brief Get receive FIFO level
  508. * @param[in] i2s The pointer of the specified I2S module.
  509. * @return RX FIFO level
  510. * @details This macro will return the number of available words in RX FIFO.
  511. */
  512. #define I2S_GET_RX_FIFO_LEVEL(i2s) ( ((i2s)->I2SSTS & SPI_I2SSTS_RXCNT_Msk) >> SPI_I2SSTS_RXCNT_Pos )
  513. /* Function prototype declaration */
  514. uint32_t SPI_Open(SPI_T *spi, uint32_t u32MasterSlave, uint32_t u32SPIMode, uint32_t u32DataWidth, uint32_t u32BusClock);
  515. void SPI_Close(SPI_T *spi);
  516. void SPI_ClearRxFIFO(SPI_T *spi);
  517. void SPI_ClearTxFIFO(SPI_T *spi);
  518. void SPI_DisableAutoSS(SPI_T *spi);
  519. void SPI_EnableAutoSS(SPI_T *spi, uint32_t u32SSPinMask, uint32_t u32ActiveLevel);
  520. uint32_t SPI_SetBusClock(SPI_T *spi, uint32_t u32BusClock);
  521. void SPI_SetFIFO(SPI_T *spi, uint32_t u32TxThreshold, uint32_t u32RxThreshold);
  522. uint32_t SPI_GetBusClock(SPI_T *spi);
  523. void SPI_EnableInt(SPI_T *spi, uint32_t u32Mask);
  524. void SPI_DisableInt(SPI_T *spi, uint32_t u32Mask);
  525. uint32_t SPI_GetIntFlag(SPI_T *spi, uint32_t u32Mask);
  526. void SPI_ClearIntFlag(SPI_T *spi, uint32_t u32Mask);
  527. uint32_t SPI_GetStatus(SPI_T *spi, uint32_t u32Mask);
  528. uint32_t I2S_Open(SPI_T *i2s, uint32_t u32MasterSlave, uint32_t u32SampleRate, uint32_t u32WordWidth, uint32_t u32Channels, uint32_t u32DataFormat);
  529. void I2S_Close(SPI_T *i2s);
  530. void I2S_EnableInt(SPI_T *i2s, uint32_t u32Mask);
  531. void I2S_DisableInt(SPI_T *i2s, uint32_t u32Mask);
  532. uint32_t I2S_EnableMCLK(SPI_T *i2s, uint32_t u32BusClock);
  533. void I2S_DisableMCLK(SPI_T *i2s);
  534. void I2S_SetFIFO(SPI_T *i2s, uint32_t u32TxThreshold, uint32_t u32RxThreshold);
  535. /*@}*/ /* end of group SPI_EXPORTED_FUNCTIONS */
  536. /*@}*/ /* end of group SPI_Driver */
  537. /*@}*/ /* end of group Standard_Driver */
  538. #ifdef __cplusplus
  539. }
  540. #endif
  541. #endif //__SPI_H__
  542. /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/