hv_drv_Spi.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * @file hv_drv_Spi.h
  3. * @brief Header file of SPI module.
  4. *
  5. * @verbatim
  6. * ==============================================================================
  7. * ##### How to use #####
  8. * ==============================================================================
  9. * (+) Use ()
  10. *
  11. * @endverbatim
  12. * @author HiView SoC Software Team
  13. * @version 1.0.0
  14. * @date 2023-06-19
  15. */
  16. #ifndef _HV_DRV_SPI_H
  17. #define _HV_DRV_SPI_H
  18. #include "Common/hv_comm_DataType.h"
  19. #include "hv_cal_Dma.h"
  20. /** @defgroup DRV_SPI DRV_SPI
  21. * drv spi functions
  22. * @{
  23. */
  24. typedef enum _SpiPort
  25. {
  26. SPI_PORT_0 = 0,
  27. SPI_PORT_1,
  28. SPI_PORT_MAX
  29. } SpiPort;
  30. typedef enum _SpiMode
  31. {
  32. SPI_MODE_MASTER = 0,
  33. SPI_MODE_SLAVE,
  34. SPI_MODE_MAX
  35. } SpiMode;
  36. typedef enum _SpiBandWidth
  37. {
  38. SPI_BITWIDTH_8 = 0x07, /* The DataSize for transmition is 8bits */
  39. SPI_BITWIDTH_16 = 0x0F, /* The DataSize for transmition is 16bits */
  40. SPI_BITWIDTH_32 = 0x1F, /* The DataSize for transmition is 32bits */
  41. SPI_BITWIDTH_MAX,
  42. } SpiBandWidth;
  43. typedef enum _SpiDirection
  44. {
  45. SPI_DIRECTION_TXRX = 0, //SPI_DIRECTION_2LINES
  46. SPI_DIRECTION_TX, //SPI_DIRECTION_1LINE
  47. SPI_DIRECTION_RX, //SPI_DIRECTION_2LINES_RXONLY
  48. SPI_DIRECTION_MAX
  49. } SpiDirection;
  50. typedef enum _SpiClkPolarity
  51. {
  52. SPI_CLKPOLARITY_LOW = 0,
  53. SPI_CLKPOLARITY_HIGH,
  54. SPI_CLKPOLARITY_MAX
  55. } SpiClkPolarity;
  56. typedef enum _SpiClkPhase
  57. {
  58. SPI_CLKPHASE_1EDGE = 0,
  59. SPI_CLKPHASE_2EDGE,
  60. SPI_CLKPHASE_MAX
  61. } SpiClkPhase;
  62. typedef enum _SPiDivRatio
  63. {
  64. SPI_DIVRATIO_2 = 2,
  65. SPI_DIVRATIO_4 = 4,
  66. SPI_DIVRATIO_8 = 8,
  67. SPI_DIVRATIO_16 = 16,
  68. SPI_DIVRATIO_32 = 32,
  69. SPI_DIVRATIO_64 = 64,
  70. SPI_DIVRATIO_128 = 128,
  71. SPI_DIVRATIO_256 = 256,
  72. SPI_DIVRATIO_512 = 512,
  73. SPI_DIVRATIO_MAX,
  74. } SPiDivRatio;
  75. typedef enum _SpiEnableState
  76. {
  77. SPI_ENABLESTATE_RESET = 0,
  78. SPI_ENABLESTATE_SET,
  79. SPI_ENABLESTATE_MAX
  80. } SpiEnableState;
  81. typedef enum _SpiCsMode
  82. {
  83. SPI_CSMODE_HARDWARE = 0,
  84. SPI_CSMODE_SOFTWARE,
  85. SPI_CSMODE_ALWAYS_LOW,
  86. SPI_CSMODE_MAX
  87. } SpiCsMode;
  88. typedef enum _SpiState
  89. {
  90. SPI_STATE_RESET = 0, /*!< SPI not yet initialized or disabled*/
  91. SPI_STATE_READY, /*!< SPI initialized and ready for use*/
  92. SPI_STATE_BUSY, /*!< SPI process is ongoing*/
  93. SPI_STATE_BUSY_TX, /*!< Data Transmission process is ongoing */
  94. SPI_STATE_BUSY_RX, /*!< Data Reception process is ongoing*/
  95. SPI_STATE_BUSY_TXRX, /*!< Data Transmission and Reception process is ongoing */
  96. SPI_STATE_ERROR /*!< SPI error state */
  97. } SpiState;
  98. typedef enum _SpiError
  99. {
  100. SPI_ERROR_NONE = 0, /*!< No error*/
  101. SPI_ERROR_MODF, /*!< MODF error*/
  102. SPI_ERROR_TFEMPTY, /*!< Tx fifo empty when transmit start as slave*/
  103. SPI_ERROR_OVR, /*!< OVR error*/
  104. SPI_ERROR_DMA, /*!< DMA transfer error */
  105. SPI_ERROR_FLAG, /*!< Flag: RXNE,TXE, BSY */
  106. SPI_ERROR_TIMEOUT, /*!< SPI Timeout erro */
  107. } SpiError;
  108. //SR
  109. typedef enum _SpiFlag
  110. {
  111. SPI_FLAG_BUSY = (UINT32)(1 << 0),
  112. SPI_FLAG_TFNF = (UINT32)(1 << 1),
  113. SPI_FLAG_TFE = (UINT32)(1 << 2),
  114. SPI_FLAG_RFNE = (UINT32)(1 << 3),
  115. SPI_FLAG_RFF = (UINT32)(1 << 4),
  116. SPI_FLAG_TXE = (UINT32)(1 << 5),
  117. SPI_FLAG_DCOL = (UINT32)(1 << 6),
  118. SPI_FLAG_MAX,
  119. } SpiFlag;
  120. //ISR
  121. typedef enum _SpiInterrupt
  122. {
  123. SPI_INTERRUPT_TXEIR = (UINT32)(1 << 0),
  124. SPI_INTERRUPT_TXOIR = (UINT32)(1 << 1),
  125. SPI_INTERRUPT_RXUIR = (UINT32)(1 << 2),
  126. SPI_INTERRUPT_RXOIR = (UINT32)(1 << 3),
  127. SPI_INTERRUPT_RXFIR = (UINT32)(1 << 4),
  128. SPI_INTERRUPT_MSTIR = (UINT32)(1 << 5),
  129. SPI_INTERRUPT_MAX,
  130. } SpiInterrupt;
  131. typedef enum _SpiTransState
  132. {
  133. SPI_TRANS_IN_PROCESS = 0,
  134. SPI_TRANS_END,
  135. } SpiTransState;
  136. typedef enum _SpiTransType
  137. {
  138. SPI_TRANSMIT = 0,
  139. SPI_RECEIVE,
  140. } SpiTransType;
  141. typedef enum _SpiDmaTxEn
  142. {
  143. SPI_DMATX_DISABLE = 0,
  144. SPI_DMATX_ENABLE,
  145. } SpiDmaTxEn;
  146. typedef enum _SpiDmaRxEn
  147. {
  148. SPI_DMARX_DISABLE = 0,
  149. SPI_DMARX_ENABLE,
  150. } SpiDmaRxEn;
  151. typedef Status (*SpiCpltCallback)(SpiTransType enTransType, VOID *pArg);
  152. typedef struct _SpiInitParam
  153. {
  154. SpiMode enMode; /* Specifies the SPI operating mode: Master or Slave*/
  155. SpiDirection enDirection; /* Specifies the SPI Directional mode state.*/
  156. SpiBandWidth enDataSize; /* Specifies the SPI data size. */
  157. SpiClkPolarity enClkPolarity; /* Specifies the serial clock steady state. */
  158. SpiClkPhase enClkPhase; /* Specifies the clock active edge for the bit capture.*/
  159. SPiDivRatio enBaudRatePrescaler; /* Specifies the Baud Rate prescaler value. */
  160. SpiCsMode enCsCtlMode; /* Specifies CS is controlled by controller or gpio */
  161. SpiCpltCallback pfSpiCpltCallback;
  162. } SpiInitParam;
  163. typedef struct _SpiSelf SpiSelf;
  164. #define HV_SPI_FIFO_DEPTH (16)
  165. #define HV_SPI_TIMEOUT (5000)
  166. #define HV_SPI_DR_ADDR_RANGE_TOTAL (34 * 4) /* Data Register Number */
  167. #define HV_SPI_NO_DUMMY (0)
  168. #if 1
  169. #define HV_SPI_DMA_TX_THRESHOLD 1
  170. #define HV_SPI_DMA_RX_THRESHOLD 0
  171. #else
  172. #define HV_SPI_DMA_TX_THRESHOLD 8
  173. #define HV_SPI_DMA_RX_THRESHOLD 7
  174. #endif
  175. /**@brief Set the spi direction
  176. * @param self pointer to spi structure.
  177. * @param enDirection
  178. * @retval none
  179. */
  180. VOID Hv_Drv_Spi_SetDirection(SpiSelf *pstSelf, SpiDirection enDirection);
  181. /**@brief Set data frame size
  182. * @param self pointer to spi structure.
  183. * @param FrameWidth Indicates how many bits of data are serially transmitted in a frame.
  184. * @retval none
  185. */
  186. VOID Hv_Drv_Spi_SetBitsWidth(SpiSelf *pstSelf, SpiBandWidth enFrameWidth);
  187. /**@brief Set spi the baudrate
  188. * @param self pointer to spi structure.
  189. * @param BaudRate baudrate which will set as SCKDV.
  190. * @retval none
  191. */
  192. VOID Hv_Drv_Spi_SetBaudRate(SpiSelf *pstSelf, SPiDivRatio enDivRatio);
  193. /**@brief Set spi transmit DMA
  194. * @param self pointer to spi structure.
  195. * @param transmit DMA structure
  196. * @retval none
  197. */
  198. VOID Hv_Drv_Spi_SetDmaTx(SpiSelf *pstSelf, DmaSelf *pstDmaTx);
  199. /**@brief Set spi receive DMA
  200. * @param self pointer to spi structure.
  201. * @param receive DMA structure
  202. * @retval none
  203. */
  204. VOID Hv_Drv_Spi_SetDmaRx(SpiSelf *pstSelf, DmaSelf *pstDmaRx);
  205. /**@brief Bond the complete callback
  206. * @param self pointer to spi structure.
  207. * @param CallbackFunc callback defined by USER
  208. * @retval none
  209. */
  210. VOID Hv_Drv_Spi_SetCpltCallBack(SpiSelf *pstSelf, SpiCpltCallback pfCallbackFunc);
  211. /**@brief Get spi state
  212. * @param self pointer to spi structure
  213. * @retval spi state
  214. */
  215. UCHAR8 Hv_Drv_Spi_GetState(const SpiSelf *self);
  216. /**@brief Initializes the SPI according to the specified parameters and create the associated handle.
  217. * @param initParam pointer to the configuration information for SPI module.
  218. * @return spi handler
  219. */
  220. SpiSelf* Hv_Drv_Spi_Init(SpiInitParam *pstInitParam);
  221. /**@brief De-initializes the SPI
  222. * @param self pointer to spi structure
  223. * @retval result
  224. */
  225. Status Hv_Drv_Spi_Cleanup(SpiSelf *pstSelf);
  226. /**@brief Transmit an amount of data in blocking mode
  227. * @param self pointer to spi structure
  228. * @param pucCmdAddr pointer to command buffer
  229. * @param pucTxData pointer to data buffer
  230. * @param usDataSize amount of data to be sent
  231. * @param uiTimeout Timeout duration
  232. * @retval result
  233. */
  234. Status Hv_Drv_Spi_PollingTransmit(SpiSelf *pstSelf, UCHAR8 *pucCmdAddr, USHORT16 usCmdAddrSize,
  235. UCHAR8 *pucTxData, USHORT16 usDataSize, UINT32 uiTimeout);
  236. /**@brief Transmit an amount of data in no-blocking mode with interrupt
  237. * @param self pointer to spi structure
  238. * @param pucCmdAddr pointer to command buffer
  239. * @param pucTxData pointer to data buffer
  240. * @param usDataSize amount of data to be sent
  241. * @retval result
  242. */
  243. Status Hv_Drv_Spi_IntTransmit(SpiSelf *pstSelf, UCHAR8 *pucCmdAddr, USHORT16 usCmdAddrSize,
  244. UCHAR8 *pucTxData,USHORT16 usDataSize);
  245. /**@brief Transmit an amount of data in no-blocking mode with DMA
  246. * @param self pointer to spi structure
  247. * @param pucData pointer to data buffer
  248. * @param usDataSize amount of data to be sent
  249. * @retval result
  250. */
  251. Status Hv_Drv_Spi_DmaTransmit(SpiSelf *pstSelf, UCHAR8* pucData, USHORT16 usDataSize);
  252. /**@brief Receive an amount of data in blocking mode
  253. * @param self pointer to spi structure
  254. * @param pucCmdAddr pointer to command buffer
  255. * @param pucRxData pointer to data buffer
  256. * @param usDataSize amount of data to be sent
  257. * @param uiTimeout Timeout duration
  258. * @retval result
  259. */
  260. Status Hv_Drv_Spi_PollingReceive(SpiSelf *pstSelf, UCHAR8 *pucCmdAddr, USHORT16 usCmdAddrSize,
  261. UCHAR8 *pucRxData, USHORT16 usDataSize, UINT32 uiTimeout);
  262. /**@brief Receive an amount of data in no-blocking mode with interrupt
  263. * @param self pointer to spi structure
  264. * @param pCmd pointer to command buffer
  265. * @param pRxData pointer to data buffer
  266. * @param size amount of data to be sent
  267. * @retval result
  268. */
  269. Status Hv_Drv_Spi_IntReceive(SpiSelf *pstSelf, UCHAR8 *pucCmdAddr, USHORT16 usCmdAddrSize,
  270. UCHAR8 *pucRxData,USHORT16 usDataSize);
  271. /**@brief Receive an amount of data in no-blocking mode with DMA
  272. * @param self pointer to spi structure
  273. * @param pCmd pointer to command buffer
  274. * @param pData pointer to data buffer
  275. * @param size amount of data to be sent
  276. * @retval result
  277. */
  278. Status Hv_Drv_Spi_DmaReceive(SpiSelf *pstSelf, UCHAR8 *pucCmd, UCHAR8 *pucRxData, USHORT16 usDataSize);
  279. /**@brief check spi transfer complete yes or not
  280. * @param self pointer to spi structure
  281. * @retval HV_TRUE/HV_FALSE
  282. */
  283. BOOL Hv_Drv_Spi_TransferIsComplete(SpiSelf *pstSelf);
  284. /** @} */
  285. #endif//_HV_DRV_SPI_H