usci_spi.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /****************************************************************************//**
  2. * @file usci_spi.h
  3. * @version V3.00
  4. * @brief M480 series USCI_SPI driver header file
  5. *
  6. * @copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #ifndef __USCI_SPI_H__
  9. #define __USCI_SPI_H__
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. /** @addtogroup Standard_Driver Standard Driver
  15. @{
  16. */
  17. /** @addtogroup USCI_SPI_Driver USCI_SPI Driver
  18. @{
  19. */
  20. /** @addtogroup USCI_SPI_EXPORTED_CONSTANTS USCI_SPI Exported Constants
  21. @{
  22. */
  23. #define USPI_MODE_0 (0x0 << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle low; data transmit with falling edge and receive with rising edge \hideinitializer */
  24. #define USPI_MODE_1 (0x1 << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle low; data transmit with rising edge and receive with falling edge \hideinitializer */
  25. #define USPI_MODE_2 (0x2 << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle high; data transmit with rising edge and receive with falling edge \hideinitializer */
  26. #define USPI_MODE_3 (0x3 << USPI_PROTCTL_SCLKMODE_Pos) /*!< SCLK idle high; data transmit with falling edge and receive with rising edge \hideinitializer */
  27. #define USPI_SLAVE (USPI_PROTCTL_SLAVE_Msk) /*!< Set as slave \hideinitializer */
  28. #define USPI_MASTER (0x0ul) /*!< Set as master \hideinitializer */
  29. #define USPI_SS (USPI_PROTCTL_SS_Msk) /*!< Set SS \hideinitializer */
  30. #define USPI_SS_ACTIVE_HIGH (0x0ul) /*!< SS active high \hideinitializer */
  31. #define USPI_SS_ACTIVE_LOW (USPI_LINECTL_CTLOINV_Msk) /*!< SS active low \hideinitializer */
  32. /* USCI_SPI Interrupt Mask */
  33. #define USPI_SSINACT_INT_MASK (0x001ul) /*!< Slave Slave Inactive interrupt mask \hideinitializer */
  34. #define USPI_SSACT_INT_MASK (0x002ul) /*!< Slave Slave Active interrupt mask \hideinitializer */
  35. #define USPI_SLVTO_INT_MASK (0x004ul) /*!< Slave Mode Time-out interrupt mask \hideinitializer */
  36. #define USPI_SLVBE_INT_MASK (0x008ul) /*!< Slave Mode Bit Count Error interrupt mask \hideinitializer */
  37. #define USPI_TXUDR_INT_MASK (0x010ul) /*!< Slave Transmit Under Run interrupt mask \hideinitializer */
  38. #define USPI_RXOV_INT_MASK (0x020ul) /*!< Receive Buffer Overrun interrupt mask \hideinitializer */
  39. #define USPI_TXST_INT_MASK (0x040ul) /*!< Transmit Start interrupt mask \hideinitializer */
  40. #define USPI_TXEND_INT_MASK (0x080ul) /*!< Transmit End interrupt mask \hideinitializer */
  41. #define USPI_RXST_INT_MASK (0x100ul) /*!< Receive Start interrupt mask \hideinitializer */
  42. #define USPI_RXEND_INT_MASK (0x200ul) /*!< Receive End interrupt mask \hideinitializer */
  43. /* USCI_SPI Status Mask */
  44. #define USPI_BUSY_MASK (0x01ul) /*!< Busy status mask \hideinitializer */
  45. #define USPI_RX_EMPTY_MASK (0x02ul) /*!< RX empty status mask \hideinitializer */
  46. #define USPI_RX_FULL_MASK (0x04ul) /*!< RX full status mask \hideinitializer */
  47. #define USPI_TX_EMPTY_MASK (0x08ul) /*!< TX empty status mask \hideinitializer */
  48. #define USPI_TX_FULL_MASK (0x10ul) /*!< TX full status mask \hideinitializer */
  49. #define USPI_SSLINE_STS_MASK (0x20ul) /*!< USCI_SPI_SS line status mask \hideinitializer */
  50. /*@}*/ /* end of group USCI_SPI_EXPORTED_CONSTANTS */
  51. /** @addtogroup USCI_SPI_EXPORTED_FUNCTIONS USCI_SPI Exported Functions
  52. @{
  53. */
  54. /**
  55. * @brief Disable slave 3-wire mode.
  56. * @param[in] uspi The pointer of the specified USCI_SPI module.
  57. * @return None
  58. * \hideinitializer
  59. */
  60. #define USPI_DISABLE_3WIRE_MODE(uspi) ( (uspi)->PROTCTL &= ~USPI_PROTCTL_SLV3WIRE_Msk )
  61. /**
  62. * @brief Enable slave 3-wire mode.
  63. * @param[in] uspi The pointer of the specified USCI_SPI module.
  64. * @return None
  65. * \hideinitializer
  66. */
  67. #define USPI_ENABLE_3WIRE_MODE(uspi) ( (uspi)->PROTCTL |= USPI_PROTCTL_SLV3WIRE_Msk )
  68. /**
  69. * @brief Get the Rx buffer empty flag.
  70. * @param[in] uspi The pointer of the specified USCI_SPI module.
  71. * @return Rx buffer flag
  72. * @retval 0: Rx buffer is not empty
  73. * @retval 1: Rx buffer is empty
  74. * \hideinitializer
  75. */
  76. #define USPI_GET_RX_EMPTY_FLAG(uspi) ( ((uspi)->BUFSTS & USPI_BUFSTS_RXEMPTY_Msk) == USPI_BUFSTS_RXEMPTY_Msk ? 1:0 )
  77. /**
  78. * @brief Get the Tx buffer empty flag.
  79. * @param[in] uspi The pointer of the specified USCI_SPI module.
  80. * @return Tx buffer flag
  81. * @retval 0: Tx buffer is not empty
  82. * @retval 1: Tx buffer is empty
  83. * \hideinitializer
  84. */
  85. #define USPI_GET_TX_EMPTY_FLAG(uspi) ( ((uspi)->BUFSTS & USPI_BUFSTS_TXEMPTY_Msk) == USPI_BUFSTS_TXEMPTY_Msk ? 1:0 )
  86. /**
  87. * @brief Get the Tx buffer full flag.
  88. * @param[in] uspi The pointer of the specified USCI_SPI module.
  89. * @return Tx buffer flag
  90. * @retval 0: Tx buffer is not full
  91. * @retval 1: Tx buffer is full
  92. * \hideinitializer
  93. */
  94. #define USPI_GET_TX_FULL_FLAG(uspi) ( ((uspi)->BUFSTS & USPI_BUFSTS_TXFULL_Msk) == USPI_BUFSTS_TXFULL_Msk ? 1:0 )
  95. /**
  96. * @brief Get the datum read from RX register.
  97. * @param[in] uspi The pointer of the specified USCI_SPI module.
  98. * @return data in Rx register
  99. * \hideinitializer
  100. */
  101. #define USPI_READ_RX(uspi) ((uspi)->RXDAT)
  102. /**
  103. * @brief Write datum to TX register.
  104. * @param[in] uspi The pointer of the specified USCI_SPI module.
  105. * @param[in] u32TxData The datum which user attempt to transfer through USCI_SPI bus.
  106. * @return None
  107. * \hideinitializer
  108. */
  109. #define USPI_WRITE_TX(uspi, u32TxData) ( (uspi)->TXDAT = (u32TxData) )
  110. /**
  111. * @brief Set USCI_SPI_SS pin to high state.
  112. * @param[in] uspi The pointer of the specified USCI_SPI module.
  113. * @return None.
  114. * @details Disable automatic slave selection function and set USCI_SPI_SS pin to high state. Only available in Master mode.
  115. * \hideinitializer
  116. */
  117. #define USPI_SET_SS_HIGH(uspi) \
  118. do{ \
  119. (uspi)->LINECTL |= (USPI_LINECTL_CTLOINV_Msk); \
  120. (uspi)->PROTCTL = ((uspi)->PROTCTL & ~(USPI_PROTCTL_AUTOSS_Msk | USPI_PROTCTL_SS_Msk)); \
  121. }while(0)
  122. /**
  123. * @brief Set USCI_SPI_SS pin to low state.
  124. * @param[in] uspi The pointer of the specified USCI_SPI module.
  125. * @return None.
  126. * @details Disable automatic slave selection function and set USCI_SPI_SS pin to low state. Only available in Master mode.
  127. * \hideinitializer
  128. */
  129. #define USPI_SET_SS_LOW(uspi) \
  130. do{ \
  131. (uspi)->LINECTL |= (USPI_LINECTL_CTLOINV_Msk); \
  132. (uspi)->PROTCTL = (((uspi)->PROTCTL & ~USPI_PROTCTL_AUTOSS_Msk) | USPI_PROTCTL_SS_Msk); \
  133. }while(0)
  134. /**
  135. * @brief Set the length of suspend interval.
  136. * @param[in] uspi The pointer of the specified USCI_SPI module.
  137. * @param[in] u32SuspCycle Decide the length of suspend interval.
  138. * @return None
  139. * \hideinitializer
  140. */
  141. #define USPI_SET_SUSPEND_CYCLE(uspi, u32SuspCycle) ( (uspi)->PROTCTL = ((uspi)->PROTCTL & ~USPI_PROTCTL_SUSPITV_Msk) | ((u32SuspCycle) << USPI_PROTCTL_SUSPITV_Pos) )
  142. /**
  143. * @brief Set the USCI_SPI transfer sequence with LSB first.
  144. * @param[in] uspi The pointer of the specified USCI_SPI module.
  145. * @return None
  146. * \hideinitializer
  147. */
  148. #define USPI_SET_LSB_FIRST(uspi) ( (uspi)->LINECTL |= USPI_LINECTL_LSB_Msk )
  149. /**
  150. * @brief Set the USCI_SPI transfer sequence with MSB first.
  151. * @param[in] uspi The pointer of the specified USCI_SPI module.
  152. * @return None
  153. * \hideinitializer
  154. */
  155. #define USPI_SET_MSB_FIRST(uspi) ( (uspi)->LINECTL &= ~USPI_LINECTL_LSB_Msk )
  156. /**
  157. * @brief Set the data width of a USCI_SPI transaction.
  158. * @param[in] uspi The pointer of the specified USCI_SPI module.
  159. * @param[in] u32Width The data width
  160. * @return None
  161. * \hideinitializer
  162. */
  163. #define USPI_SET_DATA_WIDTH(uspi,u32Width) \
  164. do{ \
  165. if((u32Width) == 16ul){ \
  166. (uspi)->LINECTL = ((uspi)->LINECTL & ~USPI_LINECTL_DWIDTH_Msk) | (0 << USPI_LINECTL_DWIDTH_Pos); \
  167. }else { \
  168. (uspi)->LINECTL = ((uspi)->LINECTL & ~USPI_LINECTL_DWIDTH_Msk) | ((u32Width) << USPI_LINECTL_DWIDTH_Pos); \
  169. } \
  170. }while(0)
  171. /**
  172. * @brief Get the USCI_SPI busy state.
  173. * @param[in] uspi The pointer of the specified USCI_SPI module.
  174. * @return USCI_SPI busy status
  175. * @retval 0: USCI_SPI module is not busy
  176. * @retval 1: USCI_SPI module is busy
  177. * \hideinitializer
  178. */
  179. #define USPI_IS_BUSY(uspi) ( ((uspi)->PROTSTS & USPI_PROTSTS_BUSY_Msk) == USPI_PROTSTS_BUSY_Msk ? 1:0 )
  180. /**
  181. * @brief Get the USCI_SPI wakeup flag.
  182. * @param[in] uspi The pointer of the specified USCI_SPI module.
  183. * @return Wakeup status.
  184. * @retval 0 Flag is not set.
  185. * @retval 1 Flag is set.
  186. * \hideinitializer
  187. */
  188. #define USPI_GET_WAKEUP_FLAG(uspi) ( ((uspi)->WKSTS & USPI_WKSTS_WKF_Msk) == USPI_WKSTS_WKF_Msk ? 1:0)
  189. /**
  190. * @brief Clear the USCI_SPI wakeup flag.
  191. * @param[in] uspi The pointer of the specified USCI_SPI module.
  192. * @return None
  193. * \hideinitializer
  194. */
  195. #define USPI_CLR_WAKEUP_FLAG(uspi) ( (uspi)->WKSTS |= USPI_WKSTS_WKF_Msk)
  196. /**
  197. * @brief Get protocol interrupt flag/status.
  198. * @param[in] uspi The pointer of the specified USCI_SPI module.
  199. * @return The interrupt flag/status of protocol status register.
  200. * \hideinitializer
  201. */
  202. #define USPI_GET_PROT_STATUS(uspi) ( (uspi)->PROTSTS)
  203. /**
  204. * @brief Clear specified protocol interrupt flag.
  205. * @param[in] uspi The pointer of the specified USCI_SPI module.
  206. * @param[in] u32IntTypeFlag Interrupt Type Flag, should be
  207. * - \ref USPI_PROTSTS_SSACTIF_Msk
  208. * - \ref USPI_PROTSTS_SSINAIF_Msk
  209. * - \ref USPI_PROTSTS_SLVBEIF_Msk
  210. * - \ref USPI_PROTSTS_SLVTOIF_Msk
  211. * - \ref USPI_PROTSTS_RXENDIF_Msk
  212. * - \ref USPI_PROTSTS_RXSTIF_Msk
  213. * - \ref USPI_PROTSTS_TXENDIF_Msk
  214. * - \ref USPI_PROTSTS_TXSTIF_Msk
  215. * @return None
  216. * \hideinitializer
  217. */
  218. #define USPI_CLR_PROT_INT_FLAG(uspi,u32IntTypeFlag) ( (uspi)->PROTSTS = (u32IntTypeFlag))
  219. /**
  220. * @brief Get buffer interrupt flag/status.
  221. * @param[in] uspi The pointer of the specified USCI_SPI module.
  222. * @return The interrupt flag/status of buffer status register.
  223. * \hideinitializer
  224. */
  225. #define USPI_GET_BUF_STATUS(uspi) ( (uspi)->BUFSTS)
  226. /**
  227. * @brief Clear specified buffer interrupt flag.
  228. * @param[in] uspi The pointer of the specified USCI_SPI module.
  229. * @param[in] u32IntTypeFlag Interrupt Type Flag, should be
  230. * - \ref USPI_BUFSTS_TXUDRIF_Msk
  231. * - \ref USPI_BUFSTS_RXOVIF_Msk
  232. * @return None
  233. * \hideinitializer
  234. */
  235. #define USPI_CLR_BUF_INT_FLAG(uspi,u32IntTypeFlag) ( (uspi)->BUFSTS = (u32IntTypeFlag))
  236. /**
  237. * @brief Enable specified protocol interrupt.
  238. * @param[in] uspi The pointer of the specified USCI_SPI module.
  239. * @param[in] u32IntSel Interrupt Type, should be
  240. * - \ref USPI_PROTIEN_SLVBEIEN_Msk
  241. * - \ref USPI_PROTIEN_SLVTOIEN_Msk
  242. * - \ref USPI_PROTIEN_SSACTIEN_Msk
  243. * - \ref USPI_PROTIEN_SSINAIEN_Msk
  244. * @return None
  245. * \hideinitializer
  246. */
  247. #define USPI_ENABLE_PROT_INT(uspi, u32IntSel) ((uspi)->PROTIEN |= (u32IntSel))
  248. /**
  249. * @brief Disable specified protocol interrupt.
  250. * @param[in] uspi The pointer of the specified USCI_SPI module.
  251. * @param[in] u32IntSel Interrupt Type, should be
  252. * - \ref USPI_PROTIEN_SLVBEIEN_Msk
  253. * - \ref USPI_PROTIEN_SLVTOIEN_Msk
  254. * - \ref USPI_PROTIEN_SSACTIEN_Msk
  255. * - \ref USPI_PROTIEN_SSINAIEN_Msk
  256. * @return None
  257. * \hideinitializer
  258. */
  259. #define USPI_DISABLE_PROT_INT(uspi, u32IntSel) ((uspi)->PROTIEN &= ~ (u32IntSel))
  260. /**
  261. * @brief Enable specified buffer interrupt.
  262. * @param[in] uspi The pointer of the specified USCI_SPI module.
  263. * @param[in] u32IntSel Interrupt Type, should be
  264. * - \ref USPI_BUFCTL_RXOVIEN_Msk
  265. * - \ref USPI_BUFCTL_TXUDRIEN_Msk
  266. * @return None
  267. * \hideinitializer
  268. */
  269. #define USPI_ENABLE_BUF_INT(uspi, u32IntSel) ((uspi)->BUFCTL |= (u32IntSel))
  270. /**
  271. * @brief Disable specified buffer interrupt.
  272. * @param[in] uspi The pointer of the specified USCI_SPI module.
  273. * @param[in] u32IntSel Interrupt Type, should be
  274. * - \ref USPI_BUFCTL_RXOVIEN_Msk
  275. * - \ref USPI_BUFCTL_TXUDRIEN_Msk
  276. * @return None
  277. * \hideinitializer
  278. */
  279. #define USPI_DISABLE_BUF_INT(uspi, u32IntSel) ((uspi)->BUFCTL &= ~ (u32IntSel))
  280. /**
  281. * @brief Enable specified transfer interrupt.
  282. * @param[in] uspi The pointer of the specified USCI_SPI module.
  283. * @param[in] u32IntSel Interrupt Type, should be
  284. * - \ref USPI_INTEN_RXENDIEN_Msk
  285. * - \ref USPI_INTEN_RXSTIEN_Msk
  286. * - \ref USPI_INTEN_TXENDIEN_Msk
  287. * - \ref USPI_INTEN_TXSTIEN_Msk
  288. * @return None
  289. * \hideinitializer
  290. */
  291. #define USPI_ENABLE_TRANS_INT(uspi, u32IntSel) ((uspi)->INTEN |= (u32IntSel))
  292. /**
  293. * @brief Disable specified transfer interrupt.
  294. * @param[in] uspi The pointer of the specified USCI_SPI module.
  295. * @param[in] u32IntSel Interrupt Type, should be
  296. * - \ref USPI_INTEN_RXENDIEN_Msk
  297. * - \ref USPI_INTEN_RXSTIEN_Msk
  298. * - \ref USPI_INTEN_TXENDIEN_Msk
  299. * - \ref USPI_INTEN_TXSTIEN_Msk
  300. * @return None
  301. * \hideinitializer
  302. */
  303. #define USPI_DISABLE_TRANS_INT(uspi, u32IntSel) ((uspi)->INTEN &= ~ (u32IntSel))
  304. /**
  305. * @brief Trigger RX PDMA function.
  306. * @param[in] uspi The pointer of the specified USCI_SPI module.
  307. * @return None.
  308. * @details Set RXPDMAEN bit of USPI_PDMACTL register to enable RX PDMA transfer function.
  309. * \hideinitializer
  310. */
  311. #define USPI_TRIGGER_RX_PDMA(uspi) ((uspi)->PDMACTL |= USPI_PDMACTL_RXPDMAEN_Msk|USPI_PDMACTL_PDMAEN_Msk)
  312. /**
  313. * @brief Trigger TX PDMA function.
  314. * @param[in] uspi The pointer of the specified USCI_SPI module.
  315. * @return None.
  316. * @details Set TXPDMAEN bit of USPI_PDMACTL register to enable TX PDMA transfer function.
  317. * \hideinitializer
  318. */
  319. #define USPI_TRIGGER_TX_PDMA(uspi) ((uspi)->PDMACTL |= USPI_PDMACTL_TXPDMAEN_Msk|USPI_PDMACTL_PDMAEN_Msk)
  320. /**
  321. * @brief Disable RX PDMA transfer.
  322. * @param[in] uspi The pointer of the specified USCI_SPI module.
  323. * @return None.
  324. * @details Clear RXPDMAEN bit of USPI_PDMACTL register to disable RX PDMA transfer function.
  325. * \hideinitializer
  326. */
  327. #define USPI_DISABLE_RX_PDMA(uspi) ( (uspi)->PDMACTL &= ~USPI_PDMACTL_RXPDMAEN_Msk )
  328. /**
  329. * @brief Disable TX PDMA transfer.
  330. * @param[in] uspi The pointer of the specified USCI_SPI module.
  331. * @return None.
  332. * @details Clear TXPDMAEN bit of USPI_PDMACTL register to disable TX PDMA transfer function.
  333. * \hideinitializer
  334. */
  335. #define USPI_DISABLE_TX_PDMA(uspi) ( (uspi)->PDMACTL &= ~USPI_PDMACTL_TXPDMAEN_Msk )
  336. uint32_t USPI_Open(USPI_T *uspi, uint32_t u32MasterSlave, uint32_t u32SPIMode, uint32_t u32DataWidth, uint32_t u32BusClock);
  337. void USPI_Close(USPI_T *uspi);
  338. void USPI_ClearRxBuf(USPI_T *uspi);
  339. void USPI_ClearTxBuf(USPI_T *uspi);
  340. void USPI_DisableAutoSS(USPI_T *uspi);
  341. void USPI_EnableAutoSS(USPI_T *uspi, uint32_t u32SSPinMask, uint32_t u32ActiveLevel);
  342. uint32_t USPI_SetBusClock(USPI_T *uspi, uint32_t u32BusClock);
  343. uint32_t USPI_GetBusClock(USPI_T *uspi);
  344. void USPI_EnableInt(USPI_T *uspi, uint32_t u32Mask);
  345. void USPI_DisableInt(USPI_T *uspi, uint32_t u32Mask);
  346. uint32_t USPI_GetIntFlag(USPI_T *uspi, uint32_t u32Mask);
  347. void USPI_ClearIntFlag(USPI_T *uspi, uint32_t u32Mask);
  348. uint32_t USPI_GetStatus(USPI_T *uspi, uint32_t u32Mask);
  349. void USPI_EnableWakeup(USPI_T *uspi);
  350. void USPI_DisableWakeup(USPI_T *uspi);
  351. /*@}*/ /* end of group USCI_SPI_EXPORTED_FUNCTIONS */
  352. /*@}*/ /* end of group USCI_SPI_Driver */
  353. /*@}*/ /* end of group Standard_Driver */
  354. #ifdef __cplusplus
  355. }
  356. #endif
  357. #endif /* __USCI_SPI_H__ */
  358. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/