fspim.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fspim.h
  15. * Date: 2022-02-10 14:53:42
  16. * LastEditTime: 2022-02-18 09:08:38
  17. * Description:  This files is for
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 zhugengyu 2021-12-3 init commit
  23. * 1.1 zhugengyu 2022-4-15 support test mode
  24. * 1.2 zhugengyu 2022-5-13 support spi dma
  25. */
  26. #ifndef DRIVERS_FSPI_M_H
  27. #define DRIVERS_FSPI_M_H
  28. #ifdef __cplusplus
  29. extern "C"
  30. {
  31. #endif
  32. /***************************** Include Files *********************************/
  33. #include "ftypes.h"
  34. #include "ferror_code.h"
  35. #include "fassert.h"
  36. #include "sdkconfig.h"
  37. /************************** Constant Definitions *****************************/
  38. #define FSPIM_SUCCESS FT_SUCCESS
  39. #define FSPIM_ERR_INVAL_STATE FT_MAKE_ERRCODE(ErrModBsp, ErrBspSpi, 0)
  40. #define FSPIM_ERR_NOT_READY FT_MAKE_ERRCODE(ErrModBsp, ErrBspSpi, 1)
  41. #define FSPIM_ERR_INVAL_PARAM FT_MAKE_ERRCODE(ErrModBsp, ErrBspSpi, 2)
  42. #define FSPIM_ERR_BUS_BUSY FT_MAKE_ERRCODE(ErrModBsp, ErrBspSpi, 3)
  43. #define FSPIM_ERR_NOT_SUPPORT FT_MAKE_ERRCODE(ErrModBsp, ErrBspSpi, 4)
  44. #define FSPIM_ERR_TIMEOUT FT_MAKE_ERRCODE(ErrModBsp, ErrBspSpi, 5)
  45. #define FSPIM_ERR_TRANS_FAIL FT_MAKE_ERRCODE(ErrModBsp, ErrBspSpi, 6)
  46. #define FSPIM_ERR_DMA_INIT FT_MAKE_ERRCODE(ErrModBsp, ErrBspSpi, 6)
  47. #if defined(CONFIG_TARGET_F2000_4) || defined(CONFIG_TARGET_D2000)
  48. #define FSPIM_VERSION_1 /* 用于FT2000/4和D2000平台的SPIM */
  49. #elif defined(CONFIG_TARGET_E2000)
  50. #define FSPIM_VERSION_2 /* 用于E2000平台的SPIM */
  51. #else
  52. #error "Invalid target board !!!"
  53. #endif
  54. /* add up new error code above and plust FSPIM_ERR_CODE_MAX by ONE*/
  55. #define FSPIM_ERR_CODE_PREFIX FSPIM_ERR_TRANS_FAIL & (FT_ERRCODE_SYS_MODULE_MASK | FT_ERRCODE_SUB_MODULE_MASK)
  56. #define FSPIM_NUM_OF_ERR_CODE 8
  57. typedef enum
  58. {
  59. FSPIM_SLAVE_DEV_0 = 0,
  60. FSPIM_SLAVE_DEV_1,
  61. FSPIM_SLAVE_DEV_2,
  62. FSPIM_SLAVE_DEV_3,
  63. FSPIM_NUM_OF_SLAVE_DEV
  64. } FSpimSlaveDevice;
  65. typedef enum
  66. {
  67. FSPIM_1_BYTE = 1,
  68. FSPIM_2_BYTE = 2,
  69. FSPIM_MAX_BYTES_NUM
  70. } FSpimTransByte;
  71. typedef enum
  72. {
  73. FSPIM_TRANS_MODE_RX_TX = 0x0,
  74. FSPIM_TRANS_MODE_TX_ONLY = 0x1,
  75. FSPIM_TRANS_MODE_RX_ONLY = 0x2,
  76. FSPIM_TRANS_MODE_READ_EEPROM = 0x3,
  77. FSPIM_TRANS_MODE_MAX
  78. } FSpimTransMode;
  79. /*
  80. CPOL = 0, CPHA = 0, sample at the first rising edge
  81. CPOL = 1, CPHA = 1, sample at the second rising edge
  82. CPOL = 1, CPHA = 0, sample at the second falling edge
  83. CPOL = 0, CPHA = 1, sample at the first falling edge
  84. */
  85. typedef enum
  86. {
  87. FSPIM_CPOL_LOW = 0, /* pharse 0 CPOL=0 */
  88. FSPIM_CPOL_HIGH /* pharse 1 CPOL=1 */
  89. } FSpimCpolType;
  90. typedef enum
  91. {
  92. FSPIM_CPHA_1_EDGE = 0, /* sample at the 1st edge, CPHA=0 */
  93. FSPIM_CPHA_2_EDGE /* sample at the 2nd edge, CPHA=1 */
  94. } FSpimCphaType;
  95. typedef enum
  96. {
  97. FSPIM_INTR_EVT_RX_DONE = 0, /* 接收完成事件 */
  98. FSPIM_INTR_EVT_TX_OVERFLOW, /* 发送FIFO上溢事件 */
  99. FSPIM_INTR_EVT_RX_UNDERFLOW, /* 接收FIFO下溢事件 */
  100. FSPIM_INTR_EVT_RX_OVERFLOW, /* 接收FIFO上溢事件 */
  101. FSPIM_INTR_EVT_NUM
  102. } FSpimIntrEvtType;
  103. /**************************** Type Definitions *******************************/
  104. /**
  105. * This typedef contains configuration information for the device.
  106. */
  107. typedef struct
  108. {
  109. u32 instance_id; /* Device instance id */
  110. uintptr base_addr; /* Device base address */
  111. u32 irq_num; /* Device intrrupt id */
  112. u32 irq_prority; /* Device intrrupt priority */
  113. FSpimSlaveDevice slave_dev_id; /* Slave device id */
  114. u32 max_freq_hz; /* Clock frequency in Hz */
  115. FSpimTransByte n_bytes; /* Bytes in transfer */
  116. FSpimCpolType cpol; /* Polarity of the clock */
  117. FSpimCphaType cpha; /* Phase of the clock */
  118. boolean en_test; /* Enable test mode */
  119. boolean en_dma; /* Enable DMA */
  120. } FSpimConfig;
  121. typedef void (*FSpimEvtHandler)(void *instance_p, void *param);
  122. /**
  123. * This typedef contains driver instance data. The user is required to allocate a
  124. * variable of this type for every device in the system. A pointer
  125. * to a variable of this type is then passed to the driver API functions.
  126. */
  127. typedef struct
  128. {
  129. FSpimConfig config; /* Current active configs */
  130. u32 is_ready; /* Device is initialized and ready */
  131. u32 length; /* Data length in transfer */
  132. const void *tx_buff; /* Tx buffer beg */
  133. void *rx_buff; /* Rx buffer beg */
  134. const void *tx_buff_end; /* Tx buffer end */
  135. void *rx_buff_end; /* Rx buffer end */
  136. u32 tx_fifo_len; /* Depth of tx fifo */
  137. u32 rx_fifo_len; /* Depth of rx fifo */
  138. FSpimEvtHandler evt_handler[FSPIM_INTR_EVT_NUM]; /* event handler for interrupt */
  139. void *evt_param[FSPIM_INTR_EVT_NUM]; /* parameters ptr of event handler */
  140. } FSpim;
  141. /************************** Variable Definitions *****************************/
  142. /***************** Macros (Inline Functions) Definitions *********************/
  143. /************************** Function Prototypes ******************************/
  144. /* fspim_sinit.c */
  145. /* 获取FSPIM驱动的默认配置参数 */
  146. const FSpimConfig *FSpimLookupConfig(u32 instance_id);
  147. /* fspim.c */
  148. /* 完成FSPIM驱动实例的初始化,使之可以使用*/
  149. FError FSpimCfgInitialize(FSpim *instance_p, const FSpimConfig *cofig_p);
  150. /* 完成I2C驱动实例去使能,清零实例数据 */
  151. void FSpimDeInitialize(FSpim *instance_p);
  152. /* 先发送后接收数据 (阻塞处理),利用Fifo进行处理 */
  153. FError FSpimTransferPollFifo(FSpim *instance_p, const void *tx_buf, void *rx_buf, fsize_t len);
  154. #ifdef FSPIM_VERSION_2 /* E2000 */
  155. /* 启动SPIM DMA数据传输 */
  156. FError FSpimTransferDMA(FSpim *instance_p, boolean tx, boolean rx);
  157. /* 设置片选信号 */
  158. void FSpimSetChipSelection(FSpim *instance_p, boolean on);
  159. #endif
  160. /* 获取FSPIM模块错误码对应的错误信息 */
  161. const char *FSpimErrorToMessage(FError error);
  162. /* fspim_intr.c */
  163. /* 先发送后接收数据 (中断处理),利用Fifo进行处理 */
  164. FError FSpimTransferByInterrupt(FSpim *instance_p, const void *tx_buf, void *rx_buf, fsize_t len);
  165. /* SPIM中断处理函数 */
  166. void FSpimInterruptHandler(s32 vector, void *param);
  167. /* 注册FSPIM中断事件处理函数 */
  168. void FSpimRegisterIntrruptHandler(FSpim *instance_p, FSpimIntrEvtType evt, FSpimEvtHandler handler, void *param);
  169. /* 打印SPIM控制寄存器信息 */
  170. void FSpimDumpRegister(uintptr base_addr);
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174. #endif