spi.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**************************************************************************//**
  2. * @file spi.h
  3. * @version V3.00
  4. * $Revision: 4 $
  5. * $Date: 14/01/28 10:49a $
  6. * @brief M051 series 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 M051_Device_Driver M051 Device Driver
  18. @{
  19. */
  20. /** @addtogroup M051_SPI_Driver SPI Driver
  21. @{
  22. */
  23. /** @addtogroup M051_SPI_EXPORTED_CONSTANTS SPI Exported Constants
  24. @{
  25. */
  26. #define SPI_MODE_0 (SPI_CNTRL_TX_NEG_Msk) /*!< CLKP=0; RX_NEG=0; TX_NEG=1 */
  27. #define SPI_MODE_1 (SPI_CNTRL_RX_NEG_Msk) /*!< CLKP=0; RX_NEG=1; TX_NEG=0 */
  28. #define SPI_MODE_2 (SPI_CNTRL_CLKP_Msk | SPI_CNTRL_RX_NEG_Msk) /*!< CLKP=1; RX_NEG=1; TX_NEG=0 */
  29. #define SPI_MODE_3 (SPI_CNTRL_CLKP_Msk | SPI_CNTRL_TX_NEG_Msk) /*!< CLKP=1; RX_NEG=0; TX_NEG=1 */
  30. #define SPI_SLAVE (SPI_CNTRL_SLAVE_Msk) /*!< Set as slave */
  31. #define SPI_MASTER (0x0) /*!< Set as master */
  32. #define SPI_SS (SPI_SSR_SSR_Msk) /*!< Set SS */
  33. #define SPI_SS_ACTIVE_HIGH (SPI_SSR_SS_LVL_Msk) /*!< SS active high */
  34. #define SPI_SS_ACTIVE_LOW (0x0) /*!< SS active low */
  35. #define SPI_UNIT_INT_MASK (0x01) /*!< Unit transfer interrupt mask */
  36. #define SPI_SSTA_INT_MASK (0x02) /*!< Slave 3-Wire mode start interrupt mask */
  37. #define SPI_FIFO_TX_INT_MASK (0x04) /*!< FIFO TX interrupt mask */
  38. #define SPI_FIFO_RX_INT_MASK (0x08) /*!< FIFO RX interrupt mask */
  39. #define SPI_FIFO_RXOV_INT_MASK (0x10) /*!< FIFO RX overrun interrupt mask */
  40. #define SPI_FIFO_TIMEOUT_INT_MASK (0x20) /*!< FIFO RX timeout interrupt mask */
  41. #define SPI_BUSY_MASK (0x01) /*!< Busy status mask */
  42. #define SPI_RX_EMPTY_MASK (0x02) /*!< RX empty status mask */
  43. #define SPI_RX_FULL_MASK (0x04) /*!< RX full status mask */
  44. #define SPI_TX_EMPTY_MASK (0x08) /*!< TX empty status mask */
  45. #define SPI_TX_FULL_MASK (0x10) /*!< TX full status mask */
  46. /*@}*/ /* end of group M051_SPI_EXPORTED_CONSTANTS */
  47. /** @addtogroup M051_SPI_EXPORTED_FUNCTIONS SPI Exported Functions
  48. @{
  49. */
  50. /**
  51. * @brief Abort the current transfer in slave 3-wire mode.
  52. * @param[in] spi is the base address of SPI module.
  53. * @return None
  54. */
  55. #define SPI_ABORT_3WIRE_TRANSFER(spi) ((spi)->CNTRL2 |= SPI_CNTRL2_SLV_ABORT_Msk)
  56. /**
  57. * @brief Clear the slave 3-wire mode start interrupt flag.
  58. * @param[in] spi is the base address of SPI module.
  59. * @return None
  60. */
  61. #define SPI_CLR_3WIRE_START_INT_FLAG(spi) ((spi)->CNTRL2 |= SPI_CNTRL2_SLV_START_INTSTS_Msk)
  62. /**
  63. * @brief Clear the unit transfer interrupt flag.
  64. * @param[in] spi is the base address of SPI module.
  65. * @return None
  66. */
  67. #define SPI_CLR_UNIT_TRANS_INT_FLAG(spi) ((spi)->CNTRL |= SPI_CNTRL_IF_Msk))
  68. /**
  69. * @brief Disable slave 3-wire mode.
  70. * @param[in] spi is the base address of SPI module.
  71. * @return None
  72. */
  73. #define SPI_DISABLE_3WIRE_MODE(spi) ((spi)->CNTRL2 &= ~SPI_CNTRL2_NOSLVSEL_Msk)
  74. /**
  75. * @brief Enable slave 3-wire mode.
  76. * @param[in] spi is the base address of SPI module.
  77. * @return None
  78. */
  79. #define SPI_ENABLE_3WIRE_MODE(spi) ((spi)->CNTRL2 |= SPI_CNTRL2_NOSLVSEL_Msk)
  80. /**
  81. * @brief Get the count of available data in RX FIFO.
  82. * @param[in] spi is the base address of SPI module.
  83. * @return The count of available data in RX FIFO.
  84. */
  85. #define SPI_GET_RX_FIFO_COUNT(spi) (((spi)->STATUS & SPI_STATUS_RX_FIFO_COUNT_Msk) >> SPI_STATUS_RX_FIFO_COUNT_Pos)
  86. /**
  87. * @brief Get the RX FIFO empty flag.
  88. * @param[in] spi is the base address of SPI module.
  89. * @return RX FIFO empty flag
  90. * @retval 0: RX FIFO is not empty
  91. * @retval 1: RX FIFO is empty
  92. */
  93. #define SPI_GET_RX_FIFO_EMPTY_FLAG(spi) (((spi)->STATUS & SPI_STATUS_RX_EMPTY_Msk)>>SPI_STATUS_RX_EMPTY_Pos)
  94. /**
  95. * @brief Get the TX FIFO empty flag.
  96. * @param[in] spi is the base address of SPI module.
  97. * @return Tx FIFO empty flag
  98. * @retval 0: TX FIFO is not empty
  99. * @retval 1: TX FIFO is empty
  100. */
  101. #define SPI_GET_TX_FIFO_EMPTY_FLAG(spi) (((spi)->STATUS & SPI_STATUS_TX_EMPTY_Msk)>>SPI_STATUS_TX_EMPTY_Pos)
  102. /**
  103. * @brief Get the TX FIFO full flag.
  104. * @param[in] spi is the base address of SPI module.
  105. * @return TX FIFO full flag
  106. * @retval 0: TX FIFO is not full
  107. * @retval 1: TX FIFO is full
  108. */
  109. #define SPI_GET_TX_FIFO_FULL_FLAG(spi) (((spi)->STATUS & SPI_STATUS_TX_FULL_Msk)>>SPI_STATUS_TX_FULL_Pos)
  110. /**
  111. * @brief Get the datum read from RX0 register.
  112. * @param[in] spi is the base address of SPI module.
  113. * @return Data in RX0 register
  114. */
  115. #define SPI_READ_RX0(spi) ((spi)->RX0)
  116. /**
  117. * @brief Get the datum read from RX1 register.
  118. * @param[in] spi is the base address of SPI module.
  119. * @return Data in RX1 register
  120. */
  121. #define SPI_READ_RX1(spi) ((spi)->RX1)
  122. /**
  123. * @brief Write datum to TX0 register.
  124. * @param[in] spi is the base address of SPI module.
  125. * @param[in] u32TxData is the datum which user attempt to transfer through SPI bus.
  126. * @return None
  127. */
  128. #define SPI_WRITE_TX0(spi, u32TxData) ((spi)->TX0 = (u32TxData))
  129. /**
  130. * @brief Write datum to TX1 register.
  131. * @param[in] spi is the base address of SPI module.
  132. * @param[in] u32TxData is the datum which user attempt to transfer through SPI bus.
  133. * @return None
  134. */
  135. #define SPI_WRITE_TX1(spi, u32TxData) ((spi)->TX1 = (u32TxData))
  136. /**
  137. * @brief Disable automatic slave select function and set SPI_SS pin to high state.
  138. * @param[in] spi is the base address of SPI module.
  139. * @return None
  140. */
  141. #define SPI_SET_SS_HIGH(spi) ((spi)->SSR = ((spi)->SSR & (~SPI_SSR_AUTOSS_Msk)) | (SPI_SSR_SS_LVL_Msk | SPI_SSR_SSR_Msk))
  142. /**
  143. * @brief Disable automatic slave select function and set SPI_SS pin to low state.
  144. * @param[in] spi is the base address of SPI module.
  145. * @return None
  146. */
  147. #define SPI_SET_SS_LOW(spi) ((spi)->SSR = ((spi)->SSR & (~(SPI_SSR_AUTOSS_Msk|SPI_SSR_SS_LVL_Msk))) | SPI_SSR_SSR_Msk)
  148. /**
  149. * @brief Enable byte reorder function.
  150. * @param[in] spi is the base address of SPI module.
  151. * @return None
  152. */
  153. #define SPI_ENABLE_BYTE_REORDER(spi) ((spi)->CNTRL = ((spi)->CNTRL & (~SPI_CNTRL_REORDER_Msk)) | (1<<SPI_CNTRL_REORDER_Pos))
  154. /**
  155. * @brief Disable byte reorder function.
  156. * @param[in] spi is the base address of SPI module.
  157. * @return None
  158. */
  159. #define SPI_DISABLE_BYTE_REORDER(spi) ((spi)->CNTRL &= ~SPI_CNTRL_REORDER_Msk)
  160. /**
  161. * @brief Set the length of suspend interval.
  162. * @param[in] spi is the base address of SPI module.
  163. * @param[in] u32SuspCycle decides the length of suspend interval.
  164. * @return None
  165. */
  166. #define SPI_SET_SUSPEND_CYCLE(spi, u32SuspCycle) ((spi)->CNTRL = ((spi)->CNTRL & ~SPI_CNTRL_SP_CYCLE_Msk) | ((u32SuspCycle) << SPI_CNTRL_SP_CYCLE_Pos))
  167. /**
  168. * @brief Set the SPI transfer sequence with LSB first.
  169. * @param[in] spi is the base address of SPI module.
  170. * @return None
  171. */
  172. #define SPI_SET_LSB_FIRST(spi) ((spi)->CNTRL |= SPI_CNTRL_LSB_Msk)
  173. /**
  174. * @brief Set the SPI transfer sequence with MSB first.
  175. * @param[in] spi is the base address of SPI module.
  176. * @return None
  177. */
  178. #define SPI_SET_MSB_FIRST(spi) ((spi)->CNTRL &= ~SPI_CNTRL_LSB_Msk)
  179. /**
  180. * @brief Set the data width of a SPI transaction.
  181. * @param[in] spi is the base address of SPI module.
  182. * @param[in] u32Width is the bit width of transfer data.
  183. * @return None
  184. */
  185. #define SPI_SET_DATA_WIDTH(spi, u32Width) ((spi)->CNTRL = ((spi)->CNTRL & ~SPI_CNTRL_TX_BIT_LEN_Msk) | (((u32Width)&0x1F) << SPI_CNTRL_TX_BIT_LEN_Pos))
  186. /**
  187. * @brief Get the SPI busy state.
  188. * @param[in] spi is the base address of SPI module.
  189. * @return SPI busy status
  190. * @retval 0: SPI module is not busy
  191. * @retval 1: SPI module is busy
  192. */
  193. #define SPI_IS_BUSY(spi) ( ((spi)->CNTRL & SPI_CNTRL_GO_BUSY_Msk)>>SPI_CNTRL_GO_BUSY_Pos )
  194. /**
  195. * @brief Set the GO_BUSY bit to trigger SPI transfer.
  196. * @param[in] spi is the base address of SPI module.
  197. * @return None
  198. */
  199. #define SPI_TRIGGER(spi) ((spi)->CNTRL |= SPI_CNTRL_GO_BUSY_Msk)
  200. /* Function prototype declaration */
  201. uint32_t SPI_Open(SPI_T *spi, uint32_t u32MasterSlave, uint32_t u32SPIMode, uint32_t u32DataWidth, uint32_t u32BusClock);
  202. void SPI_Close(SPI_T *spi);
  203. void SPI_ClearRxFIFO(SPI_T *spi);
  204. void SPI_ClearTxFIFO(SPI_T *spi);
  205. void SPI_DisableAutoSS(SPI_T *spi);
  206. void SPI_EnableAutoSS(SPI_T *spi, uint32_t u32SSPinMask, uint32_t u32ActiveLevel);
  207. uint32_t SPI_SetBusClock(SPI_T *spi, uint32_t u32BusClock);
  208. void SPI_EnableFIFO(SPI_T *spi, uint32_t u32TxThreshold, uint32_t u32RxThreshold);
  209. void SPI_DisableFIFO(SPI_T *spi);
  210. uint32_t SPI_GetBusClock(SPI_T *spi);
  211. void SPI_EnableInt(SPI_T *spi, uint32_t u32Mask);
  212. void SPI_DisableInt(SPI_T *spi, uint32_t u32Mask);
  213. uint32_t SPI_GetIntFlag(SPI_T *spi, uint32_t u32Mask);
  214. void SPI_ClearIntFlag(SPI_T *spi, uint32_t u32Mask);
  215. uint32_t SPI_GetStatus(SPI_T *spi, uint32_t u32Mask);
  216. /*@}*/ /* end of group M051_SPI_EXPORTED_FUNCTIONS */
  217. /*@}*/ /* end of group M051_SPI_Driver */
  218. /*@}*/ /* end of group M051_Device_Driver */
  219. #ifdef __cplusplus
  220. }
  221. #endif
  222. #endif //__SPI_H__
  223. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/