spi.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /****************************************************************************//**
  2. * @file spi.h
  3. * @version V1.00
  4. * $Revision: 21 $
  5. * $Date: 15/06/18 4:12p $
  6. * @brief NUC472/NUC442 SPI driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __SPI_H__
  12. #define __SPI_H__
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  18. @{
  19. */
  20. /** @addtogroup NUC472_442_SPI_Driver SPI Driver
  21. @{
  22. */
  23. /** @addtogroup NUC472_442_SPI_EXPORTED_CONSTANTS SPI Exported Constants
  24. @{
  25. */
  26. #define SPI_MODE_0 (SPI_CTL_TXNEG_Msk) /*!< CLKP=0; RX_NEG=0; TX_NEG=1 \hideinitializer */
  27. #define SPI_MODE_1 (SPI_CTL_RXNEG_Msk) /*!< CLKP=0; RX_NEG=1; TX_NEG=0 \hideinitializer */
  28. #define SPI_MODE_2 (SPI_CTL_CLKPOL_Msk | SPI_CTL_RXNEG_Msk) /*!< CLKP=1; RX_NEG=1; TX_NEG=0 \hideinitializer */
  29. #define SPI_MODE_3 (SPI_CTL_CLKPOL_Msk | SPI_CTL_TXNEG_Msk) /*!< CLKP=1; RX_NEG=0; TX_NEG=1 \hideinitializer */
  30. #define SPI_SLAVE (SPI_CTL_SLAVE_Msk) /*!< Set as slave \hideinitializer */
  31. #define SPI_MASTER (0x0) /*!< Set as master \hideinitializer */
  32. #define SPI_SS0 (0x1) /*!< Set SS0 \hideinitializer */
  33. #define SPI_SS1 (0x2) /*!< Set SS1 \hideinitializer */
  34. #define SPI_SS_ACTIVE_HIGH (SPI_SSCTL_SSACTPOL_Msk) /*!< SS active high \hideinitializer */
  35. #define SPI_SS_ACTIVE_LOW (0x0) /*!< SS active low \hideinitializer */
  36. #define SPI_UNITIEN_MASK (0x001) /*!< Interrupt enable mask \hideinitializer */
  37. #define SPI_SSINAIEN_MASK (0x002) /*!< Slave Slave Inactive interrupt enable mask \hideinitializer */
  38. #define SPI_SSACTIEN_MASK (0x004) /*!< Slave Slave Active interrupt enable mask \hideinitializer */
  39. #define SPI_SLVURIEN_MASK (0x008) /*!< Slave Mode Error 1 interrupt enable mask \hideinitializer */
  40. #define SPI_SLVBEIEN_MASK (0x010) /*!< Slave Mode Error 0 interrupt enable mask \hideinitializer */
  41. #define SPI_SLVTOIEN_MASK (0x020) /*!< Slave Mode Time-out interrupt enable mask \hideinitializer */
  42. #define SPI_FIFO_TXTHIEN_MASK (0x040) /*!< Transmit FIFO Threshold interrupt enable mask \hideinitializer */
  43. #define SPI_FIFO_RXTHIEN_MASK (0x080) /*!< Receive FIFO Threshold interrupt enable mask \hideinitializer */
  44. #define SPI_FIFO_RXOVIEN_MASK (0x100) /*!< Receive FIFO Overrun interrupt enable mask \hideinitializer */
  45. #define SPI_FIFO_TXUFIEN_MASK (0x200) /*!< Slave Transmit Under Run interrupt enable mask \hideinitializer */
  46. #define SPI_FIFO_RXTOIEN_MASK (0x400) /*!< Slave Receive Time-out interrupt enable mask \hideinitializer */
  47. /*@}*/ /* end of group NUC472_442_SPI_EXPORTED_CONSTANTS */
  48. /** @addtogroup NUC472_442_SPI_EXPORTED_FUNCTIONS SPI Exported Functions
  49. @{
  50. */
  51. /**
  52. * @brief Set time out period for slave.
  53. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  54. * @param[in] spi is the base address of SPI module.
  55. * @param[in] u32TimeoutPeriod is the period of time out.
  56. * @return none
  57. * \hideinitializer
  58. */
  59. #define SPI_SET_SLAVE_TIMEOUT_PERIOD(spi, u32TimeoutPeriod) ( (spi)->SSCTL = ((spi)->SSCTL & ~SPI_SSCTL_SLVTOCNT_Msk) | (((uint32_t)u32TimeoutPeriod & 0xFFFF) << SPI_SSCTL_SLVTOCNT_Pos) )
  60. /**
  61. * @brief Enable time out clear function for FIFO mode.
  62. * @param[in] spi is the base address of SPI module.
  63. * @return none
  64. * \hideinitializer
  65. */
  66. #define SPI_ENABLE_TIMEOUT_FIFO_CLEAR(spi) ( (spi)->SSCTL |= SPI_SSCTL_SLVTORST_Msk )
  67. /**
  68. * @brief Disable time out clear function for FIFO mode.
  69. * @param[in] spi is the base address of SPI module.
  70. * @return none
  71. * \hideinitializer
  72. */
  73. #define SPI_DISABLE_TIMEOUT_FIFO_CLEAR(spi) ( (spi)->SSCTL &= ~SPI_SSCTL_SLVTORST_Msk )
  74. /**
  75. * @brief Set data out signal to low (0) if transmit under-run occurs.
  76. * @param[in] spi is the base address of SPI module.
  77. * @return none
  78. * \hideinitializer
  79. */
  80. #define SPI_SET_TX_UNDERRUN_DATA_LOW(spi) ( (spi)->FIFOCTL &= ~SPI_FIFOCTL_TXUFPOL_Msk )
  81. /**
  82. * @brief Set data out signal to high (1) if transmit under-run occurs.
  83. * @param[in] spi is the base address of SPI module.
  84. * @return none
  85. * \hideinitializer
  86. */
  87. #define SPI_SET_TX_UNDERRUN_DATA_HIGH(spi) ( (spi)->FIFOCTL |= SPI_FIFOCTL_TXUFPOL_Msk )
  88. /**
  89. * @brief Get the status flags.
  90. * @param spi is the base address of SPI module.
  91. * @return status flags
  92. * \hideinitializer
  93. */
  94. #define SPI_GET_STATUS(spi) ( (spi)->STATUS )
  95. /**
  96. * @brief Clear the unit transfer interrupt flag.
  97. * @param[in] spi is the base address of SPI module.
  98. * @return none
  99. * \hideinitializer
  100. */
  101. #define SPI_CLR_UNIT_TRANS_INT_FLAG(spi) ( (spi)->STATUS = SPI_STATUS_UNITIF_Msk )
  102. /**
  103. * @brief Disable slave 3-wire mode.
  104. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  105. * @param[in] spi is the base address of SPI module.
  106. * @return none
  107. * \hideinitializer
  108. */
  109. #define SPI_DISABLE_3WIRE_MODE(spi) ( (spi)->SSCTL &= ~SPI_SSCTL_SLV3WIRE_Msk )
  110. /**
  111. * @brief Enable slave 3-wire mode.
  112. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  113. * @param[in] spi is the base address of SPI module.
  114. * @return none
  115. * \hideinitializer
  116. */
  117. #define SPI_ENABLE_3WIRE_MODE(spi) ( (spi)->SSCTL |= SPI_SSCTL_SLV3WIRE_Msk )
  118. /**
  119. * @brief Get the count of available data in RX FIFO.
  120. * @param[in] spi is the base address of SPI module.
  121. * @return The count of available data in RX FIFO.
  122. * \hideinitializer
  123. */
  124. #define SPI_GET_RX_FIFO_COUNT(spi) ( (((spi)->STATUS & SPI_STATUS_RXCNT_Msk) >> SPI_STATUS_RXCNT_Pos) & 0xf )
  125. /**
  126. * @brief Get the Rx FIFO empty flag.
  127. * @param[in] spi is the base address of SPI module.
  128. * @return Rx FIFO flag
  129. * @retval 0: Rx FIFO is not empty
  130. * @retval 1: Rx FIFO is empty
  131. * \hideinitializer
  132. */
  133. #define SPI_GET_RX_FIFO_EMPTY_FLAG(spi) ( ((spi)->STATUS & SPI_STATUS_RXEMPTY_Msk) == SPI_STATUS_RXEMPTY_Msk ? 1:0 )
  134. /**
  135. * @brief Get the Tx FIFO empty flag.
  136. * @param[in] spi is the base address of SPI module.
  137. * @return Tx FIFO flag
  138. * @retval 0: Tx FIFO is not empty
  139. * @retval 1: Tx FIFO is empty
  140. * \hideinitializer
  141. */
  142. #define SPI_GET_TX_FIFO_EMPTY_FLAG(spi) ( ((spi)->STATUS & SPI_STATUS_TXEMPTY_Msk) == SPI_STATUS_TXEMPTY_Msk ? 1:0 )
  143. /**
  144. * @brief Get the Tx FIFO full flag.
  145. * @param[in] spi is the base address of SPI module.
  146. * @return Tx FIFO flag
  147. * @retval 0: Tx FIFO is not full
  148. * @retval 1: Tx FIFO is full
  149. * \hideinitializer
  150. */
  151. #define SPI_GET_TX_FIFO_FULL_FLAG(spi) ( ((spi)->STATUS & SPI_STATUS_TXFULL_Msk) == SPI_STATUS_TXFULL_Msk ? 1:0 )
  152. /**
  153. * @brief Get the datum read from R0 FIFO.
  154. * @param[in] spi is the base address of SPI module.
  155. * @return data in Rx register
  156. * \hideinitializer
  157. */
  158. #define SPI_READ_RX(spi) ((spi)->RX)
  159. /**
  160. * @brief Write datum to TX register.
  161. * @param[in] spi is the base address of SPI module.
  162. * @param[in] u32TxData is the datum which user attempt to transfer through SPI bus.
  163. * @return none
  164. * \hideinitializer
  165. */
  166. #define SPI_WRITE_TX(spi, u32TxData) ( (spi)->TX = u32TxData )
  167. /**
  168. * @brief Set SPIn_SS0 pin to high state.
  169. * @param[in] spi The pointer of the specified SPI module.
  170. * @return None.
  171. * @details Disable automatic slave selection function and set SPIn_SS0 pin to high state. Only available in Master mode.
  172. * \hideinitializer
  173. */
  174. #define SPI_SET_SS0_HIGH(spi) ((spi)->SSCTL = ((spi)->SSCTL & ~(SPI_SSCTL_AUTOSS_Msk|SPI_SSCTL_SSACTPOL_Msk|SPI_SS0)))
  175. /**
  176. * @brief Set SPIn_SS0 pin to low state.
  177. * @param[in] spi The pointer of the specified SPI module.
  178. * @return None.
  179. * @details Disable automatic slave selection function and set SPIn_SS0 pin to low state. Only available in Master mode.
  180. * \hideinitializer
  181. */
  182. #define SPI_SET_SS0_LOW(spi) ((spi)->SSCTL = ((spi)->SSCTL & ~(SPI_SSCTL_AUTOSS_Msk|SPI_SSCTL_SSACTPOL_Msk|SPI_SS0)) | SPI_SS0)
  183. /**
  184. * @brief Set SPIn_SS1 pin to high state.
  185. * @param[in] spi The pointer of the specified SPI module.
  186. * @return None.
  187. * @details Disable automatic slave selection function and set SPIn_SS1 pin to high state. Only available in Master mode.
  188. * \hideinitializer
  189. */
  190. #define SPI_SET_SS1_HIGH(spi) ((spi)->SSCTL = ((spi)->SSCTL & ~(SPI_SSCTL_AUTOSS_Msk|SPI_SSCTL_SSACTPOL_Msk|SPI_SS1)))
  191. /**
  192. * @brief Set SPIn_SS1 pin to low state.
  193. * @param[in] spi The pointer of the specified SPI module.
  194. * @return None.
  195. * @details Disable automatic slave selection function and set SPIn_SS1 pin to low state. Only available in Master mode.
  196. * \hideinitializer
  197. */
  198. #define SPI_SET_SS1_LOW(spi) ((spi)->SSCTL = ((spi)->SSCTL & ~(SPI_SSCTL_AUTOSS_Msk|SPI_SSCTL_SSACTPOL_Msk|SPI_SS1)) | SPI_SS1)
  199. /**
  200. * @brief Enable byte reorder function.
  201. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  202. * @param[in] spi is the base address of SPI module.
  203. * @return none
  204. * \hideinitializer
  205. */
  206. #define SPI_ENABLE_BYTE_REORDER(spi) ( (spi)->CTL |= SPI_CTL_REORDER_Msk )
  207. /**
  208. * @brief Disable byte reorder function.
  209. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  210. * @param [in] spi is the base address of SPI module.
  211. * @return none
  212. * \hideinitializer
  213. */
  214. #define SPI_DISABLE_BYTE_REORDER(spi) ( (spi)->CTL &= ~SPI_CTL_REORDER_Msk )
  215. /**
  216. * @brief Set the length of suspend interval.
  217. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  218. * @param[in] spi is the base address of SPI module.
  219. * @param[in] u32SuspCycle decides the length of suspend interval.
  220. * @return none
  221. * \hideinitializer
  222. */
  223. #define SPI_SET_SUSPEND_CYCLE(spi, u32SuspCycle) ( (spi)->CTL = ((spi)->CTL & ~SPI_CTL_SUSPITV_Msk) | (u32SuspCycle << SPI_CTL_SUSPITV_Pos) )
  224. /**
  225. * @brief Set the SPI transfer sequence with LSB first.
  226. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  227. * @param[in] spi is the base address of SPI module.
  228. * @return none
  229. * \hideinitializer
  230. */
  231. #define SPI_SET_LSB_FIRST(spi) ( (spi)->CTL |= SPI_CTL_LSB_Msk )
  232. /**
  233. * @brief Set the SPI transfer sequence with MSB first.
  234. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  235. * @param[in] spi is the base address of SPI module.
  236. * @return none
  237. * \hideinitializer
  238. */
  239. #define SPI_SET_MSB_FIRST(spi) ( (spi)->CTL &= ~SPI_CTL_LSB_Msk )
  240. /**
  241. * @brief Set the data width of a SPI transaction.
  242. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  243. * @param[in] spi is the base address of SPI module.
  244. * @param[in] u32Width data width
  245. * @return none
  246. * \hideinitializer
  247. */
  248. static __INLINE void SPI_SET_DATA_WIDTH(SPI_T *spi, uint32_t u32Width)
  249. {
  250. if(u32Width == 32)
  251. u32Width = 0;
  252. spi->CTL = (spi->CTL & ~SPI_CTL_DWIDTH_Msk) | (u32Width << SPI_CTL_DWIDTH_Pos);
  253. }
  254. /**
  255. * @brief Get the SPI busy state.
  256. * @param[in] spi is the base address of SPI module.
  257. * @return SPI busy status
  258. * @retval 0: SPI module is not busy
  259. * @retval 1: SPI module is busy
  260. * \hideinitializer
  261. */
  262. #define SPI_IS_BUSY(spi) ( ((spi)->STATUS & SPI_STATUS_BUSY_Msk) == SPI_STATUS_BUSY_Msk ? 1:0 )
  263. /**
  264. * @brief Set the SPIEN bit to trigger SPI transfer.
  265. * @param[in] spi is the base address of SPI module.
  266. * @return none
  267. * \hideinitializer
  268. */
  269. #define SPI_TRIGGER(spi) ( (spi)->CTL |= SPI_CTL_SPIEN_Msk )
  270. /**
  271. * @brief Set the SPIEN bit to trigger SPI transfer.
  272. * @param[in] spi is the base address of SPI module.
  273. * @return none
  274. * \hideinitializer
  275. */
  276. #define SPI_ENABLE(spi) ( (spi)->CTL |= SPI_CTL_SPIEN_Msk )
  277. /**
  278. * @brief Disable SPI function.
  279. * @param[in] spi is the base address of SPI module.
  280. * @return none
  281. * \hideinitializer
  282. */
  283. #define SPI_DISABLE(spi) ( (spi)->CTL &= ~SPI_CTL_SPIEN_Msk )
  284. /**
  285. * @brief Disable SPI Dual IO function.
  286. * @param[in] spi is the base address of SPI module.
  287. * @return none
  288. * \hideinitializer
  289. */
  290. #define SPI_DISABLE_DUAL_MODE(spi) ( (spi)->CTL &= ~SPI_CTL_DUALIOEN_Msk )
  291. /**
  292. * @brief Enable Dual IO function and set SPI Dual IO direction to input.
  293. * @param[in] spi is the base address of SPI module.
  294. * @return none
  295. * \hideinitializer
  296. */
  297. #define SPI_ENABLE_DUAL_INPUT_MODE(spi) ( (spi)->CTL = ((spi)->CTL & ~SPI_CTL_QDIODIR_Msk) | SPI_CTL_DUALIOEN_Msk )
  298. /**
  299. * @brief Enable Dual IO function and set SPI Dual IO direction to output.
  300. * @param[in] spi is the base address of SPI module.
  301. * @return none
  302. * \hideinitializer
  303. */
  304. #define SPI_ENABLE_DUAL_OUTPUT_MODE(spi) ( (spi)->CTL |= SPI_CTL_QDIODIR_Msk | SPI_CTL_DUALIOEN_Msk )
  305. /**
  306. * @brief Disable SPI Dual IO function.
  307. * @param[in] spi is the base address of SPI module.
  308. * @return none
  309. * \hideinitializer
  310. */
  311. #define SPI_DISABLE_QUAD_MODE(spi) ( (spi)->CTL &= ~SPI_CTL_QUADIOEN_Msk )
  312. /**
  313. * @brief Set SPI Quad IO direction to input.
  314. * @param[in] spi is the base address of SPI module.
  315. * @return none
  316. * \hideinitializer
  317. */
  318. #define SPI_ENABLE_QUAD_INPUT_MODE(spi) ( (spi)->CTL = ((spi)->CTL & ~SPI_CTL_QDIODIR_Msk) | SPI_CTL_QUADIOEN_Msk )
  319. /**
  320. * @brief Set SPI Quad IO direction to output.
  321. * @param[in] spi is the base address of SPI module.
  322. * @return none
  323. * \hideinitializer
  324. */
  325. #define SPI_ENABLE_QUAD_OUTPUT_MODE(spi) ( (spi)->CTL |= SPI_CTL_QDIODIR_Msk | SPI_CTL_QUADIOEN_Msk )
  326. /**
  327. * @brief Trigger RX PDMA transfer.
  328. * @param[in] spi is the base address of SPI module.
  329. * @return none
  330. * \hideinitializer
  331. */
  332. #define SPI_TRIGGER_RX_PDMA(spi) ( (spi)->PDMACTL |= SPI_PDMACTL_RXPDMAEN_Msk )
  333. /**
  334. * @brief Trigger TX PDMA transfer.
  335. * @param[in] spi is the base address of SPI module.
  336. * @return none
  337. * \hideinitializer
  338. */
  339. #define SPI_TRIGGER_TX_PDMA(spi) ( (spi)->PDMACTL |= SPI_PDMACTL_TXPDMAEN_Msk )
  340. /**
  341. * @brief Trigger TX/RX PDMA transfer at the same time.
  342. * @param[in] spi is the base address of SPI module.
  343. * @return none
  344. * \hideinitializer
  345. */
  346. #define SPI_TRIGGER_TXRX_PDMA(spi) ( (spi)->PDMACTL |= (SPI_PDMACTL_TXPDMAEN_Msk | SPI_PDMACTL_RXPDMAEN_Msk) )
  347. /**
  348. * @brief Disable RX PDMA transfer.
  349. * @param[in] spi is the base address of SPI module.
  350. * @return none
  351. * \hideinitializer
  352. */
  353. #define SPI_DISABLE_RX_PDMA(spi) ( (spi)->PDMACTL &= ~SPI_PDMACTL_RXPDMAEN_Msk )
  354. /**
  355. * @brief Trigger TX PDMA transfer.
  356. * @param[in] spi is the base address of SPI module.
  357. * @return none
  358. * \hideinitializer
  359. */
  360. #define SPI_DISABLE_TX_PDMA(spi) ( (spi)->PDMACTL &= ~SPI_PDMACTL_TXPDMAEN_Msk )
  361. /**
  362. * @brief Enable 2-bit transfer mode.
  363. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  364. * @param[in] spi is the base address of SPI module.
  365. * @return none
  366. * \hideinitializer
  367. */
  368. #define SPI_ENABLE_2BIT_MODE(spi) ( (spi)->CTL |= SPI_CTL_TWOBIT_Msk )
  369. /**
  370. * @brief Disable 2-bit transfer mode.
  371. * @note Before calling this function, SPI must be stopped first. \ref SPI_DISABLE must be called.
  372. * @param[in] spi is the base address of SPI module.
  373. * @return none
  374. * \hideinitializer
  375. */
  376. #define SPI_DISABLE_2BIT_MODE(spi) ( (spi)->CTL &= ~SPI_CTL_TWOBIT_Msk )
  377. uint32_t SPI_Open(SPI_T *spi,uint32_t u32MasterSlave, uint32_t u32SPIMode, uint32_t u32DataWidth, uint32_t u32BusClock);
  378. void SPI_Close(SPI_T *spi);
  379. void SPI_ClearRxFIFO(SPI_T *spi);
  380. void SPI_ClearTxFIFO(SPI_T *spi);
  381. void SPI_DisableAutoSS(SPI_T *spi);
  382. void SPI_EnableAutoSS(SPI_T *spi, uint32_t u32SSPinMask, uint32_t u32ActiveLevel);
  383. uint32_t SPI_SetBusClock(SPI_T *spi, uint32_t u32BusClock);
  384. void SPI_SetFIFOThreshold(SPI_T *spi, uint32_t u32TxThreshold, uint32_t u32RxThreshold);
  385. uint32_t SPI_GetBusClock(SPI_T *spi);
  386. void SPI_EnableInt(SPI_T *spi, uint32_t u32Mask);
  387. void SPI_DisableInt(SPI_T *spi, uint32_t u32Mask);
  388. /*@}*/ /* end of group NUC472_442_SPI_EXPORTED_FUNCTIONS */
  389. /*@}*/ /* end of group NUC472_442_SPI_Driver */
  390. /*@}*/ /* end of group NUC472_442_Device_Driver */
  391. #ifdef __cplusplus
  392. }
  393. #endif
  394. #endif //__SPI_H__
  395. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/