gd_spi.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*!
  2. *******************************************************************************
  3. **
  4. ** \file gd_spi.h
  5. **
  6. ** \brief Serail Peripheral Interface driver.
  7. **
  8. ** (C) Goke Microelectronics China 2007 - 2010
  9. **
  10. ** \attention THIS SAMPLE CODE IS PROVIDED AS IS. HUNAN GOFORTUNE SEMICONDUCTOR
  11. ** ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
  12. ** OMMISSIONS.
  13. **
  14. ** \version \$Id: gd_spi.h,v 1.1 2013/11/21 01:27:52
  15. **
  16. *******************************************************************************
  17. */
  18. #ifndef GD_SPI_H
  19. #define GD_SPI_H
  20. #include <gtypes.h>
  21. #include <gmodids.h>
  22. /*!
  23. *******************************************************************************
  24. **
  25. ** \anchor gd_spi_error_base
  26. ** \brief The base error code for the serial peripheral interface driver.
  27. **
  28. *******************************************************************************
  29. */
  30. /*@{*/
  31. #define GD_SPI_ERR_BASE ( GD_SPI_MODULE_ID << 16)
  32. /*@}*/
  33. typedef enum
  34. {
  35. GD_SPI_CHANNEL_0 = 0,
  36. GD_SPI_CHANNEL_1,
  37. GD_SPI_CHANNEL_NUM
  38. }GD_SPI_CHANNEL_E;
  39. typedef enum
  40. {
  41. GD_SPI_SLAVE0 = 0,
  42. GD_SPI_SLAVE1,
  43. GD_SPI_SLAVE2,
  44. GD_SPI_SLAVE3,
  45. GD_SPI_SLAVE4,
  46. GD_SPI_SLAVE5,
  47. GD_SPI_SLAVE_NUM
  48. }GD_SPI_SLAVE_E;
  49. // TODO: to be confirmed for each clock value
  50. typedef enum
  51. {
  52. GD_SPI_FREQ_81M = 81000000,
  53. GD_SPI_FREQ_40M = 40000000,
  54. GD_SPI_FREQ_30M = 30000000,
  55. GD_SPI_FREQ_27M = 27000000,
  56. GD_SPI_FREQ_20M = 20000000,
  57. GD_SPI_FREQ_16M = 16000000,
  58. GD_SPI_FREQ_13M = 13500000,
  59. GD_SPI_FREQ_10M = 10000000,
  60. GD_SPI_FREQ_9M = 9000000,
  61. GD_SPI_FREQ_6M = 6000000,
  62. GD_SPI_FREQ_3M = 3000000,
  63. GD_SPI_FREQ_1M = 1000000,
  64. }GD_SPI_SPEED_MODE;
  65. typedef enum
  66. {
  67. SPI_POLARITY_MODE0 = 0,//!< Sclk low level,fisrt edge get data.
  68. SPI_POLARITY_MODE1,//!< Sclk low level,second edge get data.
  69. SPI_POLARITY_MODE2,//!< Sclk high level,first edge get data.
  70. SPI_POLARITY_MODE3,//!< Sclk high level,second edge get data.
  71. }GD_SPI_POLARITY_MODE;
  72. /*!
  73. *******************************************************************************
  74. **
  75. ** \brief SPI driver error codes.
  76. **
  77. *******************************************************************************
  78. */
  79. enum
  80. {
  81. GD_ERR_SPI_TYPE_NOT_SUPPORTED = GD_SPI_ERR_BASE, //!< Device not supported.
  82. GD_ERR_SPI_IN_USE, //!< Read error.
  83. GD_ERR_SPI_NOT_OPEN,
  84. GD_ERR_SPI_READ, //!< Read error.
  85. GD_ERR_SPI_WRITE, //!< Write error.
  86. GD_ERR_SPI_ERASE,
  87. GD_ERR_SPI_UNLOCK_FAIL,
  88. GD_ERR_SPI_INI_ERR,
  89. GD_ERR_SPI_BUSY,
  90. };
  91. /*!
  92. *******************************************************************************
  93. **
  94. ** \brief SPI open parameter.
  95. **
  96. ******************************************************************************/
  97. typedef enum
  98. {
  99. GD_SPI_UNUSED,
  100. GD_SPI_WRITE_ONLY,
  101. GD_SPI_READ_ONLY,
  102. GD_SPI_WRITE_READ,
  103. }GD_SPI_RW;
  104. typedef struct
  105. {
  106. /*! The connection parameters to be applied when open an instance of the
  107. driver.
  108. */
  109. U8 spi;
  110. U8 slave;
  111. U8 csgpio;
  112. U8 polarity; //!< send and recive data mode
  113. U32 baudrate;
  114. GBOOL used_irq;
  115. } GD_SPI_OPEN_PARAMS_S;
  116. /*!
  117. *******************************************************************************
  118. **
  119. ** \brief SPI ISR parameter.
  120. **
  121. ******************************************************************************/
  122. typedef struct
  123. {
  124. /*! The connection parameters to be applied when used irq mode for the
  125. driver.
  126. */
  127. U8 spi;
  128. U32 write_len;
  129. U32 read_len;
  130. U32 all_len;
  131. U32 wf_len;
  132. U32 rf_len;
  133. U32 w_xfer;
  134. U32 r_xfer;
  135. U8 bitpw;
  136. U8 finished;
  137. GD_SPI_RW rwmode;
  138. void* wbuf;
  139. void* rbuf;
  140. GD_HANDLE spihandle;
  141. GD_HANDLE spiIrqHandle;
  142. } GD_SPI_ISR_PARAMS_S;
  143. /*!
  144. *******************************************************************************
  145. **
  146. ** \brief SPI global parameter.
  147. **
  148. ******************************************************************************/
  149. typedef struct
  150. {
  151. U8 spi;
  152. U8 slave;
  153. U8 using;
  154. U8 polarity; //!< send and recive data mode
  155. U32 baudrate;
  156. GD_HANDLE cs;
  157. U8 datwidth;
  158. GBOOL used_irq;
  159. } GD_SPI_STATUS_PARAMS_S;
  160. /*!
  161. *******************************************************************************
  162. **
  163. ** \brief SPI write parameter.
  164. **
  165. ******************************************************************************/
  166. typedef struct
  167. {
  168. U32 wBitSet;
  169. U32 sBitSet;
  170. U32 eBitSet;
  171. U32 wipMask;
  172. U32 address;
  173. } GD_SPI_WR_PARAMS_S;
  174. typedef struct
  175. {
  176. U8 readID; // command to read the chip identification
  177. U8 writeEnable; // command to enable a write/erase sequence
  178. U8 writeDisable; // command to disable a write/erase sequence
  179. U8 readStatus; // command to read from status register
  180. U8 writeStatus; // command to write to status register
  181. U8 readData; // command to read data
  182. U8 readDataFast; // command to read data in fast mode
  183. U8 eraseSector; // command to erase a single sector
  184. U8 eraseChip; // command to erase the entire chip
  185. U8 programPage; // command to program a sector page
  186. U32 statusMaskWIP; // status register mask for bit write-in-progress
  187. U32 statusMaskWEL; // status register mask for bit write-enable-latch
  188. U8 readIO2; // command to read data by IO2
  189. U8 readIO4; // command to read data by IO4
  190. U8 programPage2; // command to program a sector page by IO2
  191. U8 programPage4; // command to program a sector page by IO4
  192. }
  193. GD_SPI_CMD_S;
  194. /*
  195. *******************************************************************
  196. * sflash cmd attribute
  197. * sflash cmd register bit definition
  198. * bit 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17
  199. | rsrd | hold time | transfer data bytes |data cycle|adr and dummy cycle | cmd cycle |
  200. 00-100ns 11111 -- 4 bytes 00 -x1
  201. 01-3us other(e.g.,n) -- n bytes 0-3 cycle 0-3 cycle 01 -x2
  202. 10-100us 10 -x4
  203. bit 16 15 14 13 12 11 10 9 8 [7: 0]
  204. | RWN | dummy cycle number | adr byte num | | cmd to DF |
  205. 00 - rd data for SF 0 - 7 bytes cycle 0-7 bytes 1 - send cmd
  206. 01 - wr data to SF 0 - no send cmd see specific flash cmd
  207. 11 - nothing to do
  208. ********************************************************************
  209. */
  210. /* send cmd or not [8]*/
  211. #define SPI_SEND_CMD 0x00000100
  212. #define SPI_NO_SEND_CMD 0x00000000
  213. /* byte number of address to send [11:9]*/
  214. #define SPI_SEND_ADDR_BYTE_NUM_0 0x00000000
  215. #define SPI_SEND_ADDR_BYTE_NUM_1 0x00000200
  216. #define SPI_SEND_ADDR_BYTE_NUM_2 0x00000400
  217. #define SPI_SEND_ADDR_BYTE_NUM_3 0x00000600
  218. #define SPI_SEND_ADDR_BYTE_NUM_4 0x00000800
  219. #define SPI_SEND_ADDR_BYTE_NUM_5 0x00000a00
  220. #define SPI_SEND_ADDR_BYTE_NUM_6 0x00000c00
  221. #define SPI_SEND_ADDR_BYTE_NUM_7 0x00000e00
  222. /*Byte number of dummy cycle to send [14:12]*/
  223. #define SPI_SEND_DUMMY_BYTE_NUM_0 0x00000000
  224. #define SPI_SEND_DUMMY_BYTE_NUM_1 0x00001000
  225. #define SPI_SEND_DUMMY_BYTE_NUM_2 0x00002000
  226. #define SPI_SEND_DUMMY_BYTE_NUM_3 0x00003000
  227. #define SPI_SEND_DUMMY_BYTE_NUM_4 0x00004000
  228. #define SPI_SEND_DUMMY_BYTE_NUM_5 0x00005000
  229. #define SPI_SEND_DUMMY_BYTE_NUM_6 0x00006000
  230. #define SPI_SEND_DUMMY_BYTE_NUM_7 0x00007000
  231. /* Command operation[16:15]: 00 for read data from SPI; 01 for write data to SPI; 11 for nothing to do */
  232. #define SPI_RWN_READ 0x00000000
  233. #define SPI_RWN_WRITE 0x00008000
  234. #define SPI_RWN_NOTHING 0x00018000
  235. /*I/O mode of command cycle to SF[18:17]: 00 for x1; 01 for x2; 10 for x4*/
  236. #define SPI_CMD_MODE_1X 0x00000000
  237. #define SPI_CMD_MODE_2X 0x00020000
  238. #define SPI_CMD_MODE_4X 0x00040000
  239. /* I/O mode of address and dummy cycle to SF[20:19]*/
  240. #define SPI_ADDR_DUMMY_CYCLE_NUM_0 0x00000000
  241. #define SPI_ADDR_DUMMY_CYCLE_NUM_1 0x00080000
  242. #define SPI_ADDR_DUMMY_CYCLE_NUM_2 0x00100000
  243. #define SPI_ADDR_DUMMY_CYCLE_NUM_3 0x00180000
  244. /*I/O mode of data cycle to or from SF [22:21] */
  245. #define SPI_DATA_CYCLE_NUM_0 0x00000000
  246. #define SPI_DATA_CYCLE_NUM_1 0x00200000
  247. #define SPI_DATA_CYCLE_NUM_2 0x00400000
  248. #define SPI_DATA_CYCLE_NUM_3 0x00600000
  249. /*Transfer data byte number to or from SF[27:23]. For 11111 case, transfer 4bytes per request. For other case, transfer number bytes.*/
  250. #define SPI_TRANSFER_BYTE_NUM_4 0x0f800000
  251. #define SPI_TRANSFER_BYTE_LOC 23
  252. #define SPI_HOLD_TIME_100ns 0x00000000
  253. #define SPI_HOLD_TIME_3us 0x10000000
  254. #define SPI_HOLD_TIME_100us 0x20000000
  255. /*
  256. *******************************************************************************
  257. *******************************************************************************
  258. **
  259. ** Generic serial flash specific API functions
  260. **
  261. *******************************************************************************
  262. *******************************************************************************
  263. */
  264. #ifdef __cplusplus
  265. extern "C" {
  266. #endif
  267. GERR GD_SPI_Init( void );
  268. GERR GD_SPI_Exit( void );
  269. GERR GD_SPI_Open( GD_SPI_OPEN_PARAMS_S *openParamsP, GD_HANDLE* pHandle );
  270. GERR GD_SPI_Close( GD_HANDLE* pHandle );
  271. GERR GD_SPI_WriteWords( GD_HANDLE handle, U16* wbuffer, U32 w_words );
  272. GERR GD_SPI_WriteThenReadWords( GD_HANDLE handle, U16* wbuffer, U32 w_words, U16* rbuffer, U32 r_words );
  273. GERR GD_SPI_WriteBytes( GD_HANDLE handle, U8* wbuffer, U32 w_size );
  274. GERR GD_SPI_WriteThenReadBytes( GD_HANDLE handle, U8* wbuffer, U32 w_size, U8* rbuffer, U32 r_size );
  275. GERR GD_SPI_GetDevice(GD_HANDLE handle);
  276. GERR GD_SPI_ReleaseDevice(GD_HANDLE handle);
  277. GERR GD_SPI_SetDatFormat(GD_HANDLE handle, U8 dat_width);
  278. #ifdef __cplusplus
  279. }
  280. #endif
  281. #endif