drv_spi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _DRV_SPI_H_
  13. #define _DRV_SPI_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include "quec_cust_patch.h"
  18. #include "quec_proj_config.h"
  19. #include "hal_config.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #define CONFIG_GENERAL_SPI_FLASH_MOUNT_POINT "/extn"
  24. #define FLASH_START_ADDR 0x00000000
  25. #define FLASH_MOUNT_SIZE (0x00800000 - 0x1000)
  26. #define FLASH_EB_SIZE 0x1000
  27. #define FLASH_PB_SIZE 0x200
  28. typedef enum
  29. {
  30. SPI_I2C_CS0 = 0,
  31. SPI_I2C_CS1,
  32. SPI_I2C_CS2,
  33. SPI_I2C_CS3,
  34. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SPI
  35. SPI_I2C_GPIO, //使用GPIO的方式来设置GPIO
  36. #endif
  37. } drvSpiCsSel;
  38. typedef enum
  39. {
  40. SPI_CPOL_LOW = 0,
  41. SPI_CPOL_HIGH,
  42. } drvSpiCpolPol;
  43. typedef enum
  44. {
  45. SPI_CPHA_1Edge,
  46. SPI_CPHA_2Edge,
  47. } drvSpiCphaPol;
  48. typedef enum
  49. {
  50. SPI_CS_ACTIVE_HIGH,
  51. SPI_CS_ACTIVE_LOW,
  52. } drvSpiCsPol;
  53. typedef enum
  54. {
  55. SPI_DI_0 = 0,
  56. SPI_DI_1,
  57. SPI_DI_2,
  58. } drvSpiInputSel;
  59. typedef enum
  60. {
  61. RX_TRIGGER_1_BYTE,
  62. RX_TRIGGER_4_BYTE,
  63. RX_TRIGGER_8_BYTE,
  64. RX_TRIGGER_12_BYTE,
  65. } drvSpiRxTrigger;
  66. typedef enum
  67. {
  68. TX_TRIGGER_1_EMPTY,
  69. TX_TRIGGER_4_EMPTY,
  70. TX_TRIGGER_8_EMPTY,
  71. TX_TRIGGER_12_EMPTY,
  72. } drvSpiTxTrigger;
  73. typedef enum
  74. {
  75. SPI_CLK_CTRL,
  76. SPI_DO_CTRL,
  77. SPI_CS0_CTRL,
  78. SPI_CS1_CTRL,
  79. SPI_CS2_CTRL,
  80. } drvSpiPinCtrl;
  81. typedef enum
  82. {
  83. SPI_TRIGGER_1_DATA,
  84. SPI_TRIGGER_4_DATA,
  85. SPI_TRIGGER_8_DATA,
  86. SPI_TRIGGER_12_DATA,
  87. } drvSpithreshold;
  88. #if defined(CONFIG_SOC_8910) || defined(CONFIG_SOC_8811)
  89. typedef struct
  90. {
  91. uint32_t rxOvf : 1;
  92. uint32_t txTh : 1;
  93. uint32_t txDmaDone : 1;
  94. uint32_t rxTh : 1;
  95. uint32_t rxDmaDone : 1;
  96. drvSpithreshold Tx_Rthreshold;
  97. drvSpithreshold Rx_Tthreshold;
  98. } drvSpiIrq;
  99. #elif defined(CONFIG_SOC_8850)
  100. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SPI
  101. //为了兼容8910的定义
  102. typedef struct
  103. {
  104. uint32_t rxOvf : 1;
  105. uint32_t txTh : 1;
  106. uint32_t txDmaDone : 1;
  107. uint32_t rxTh : 1;
  108. uint32_t rxDmaDone : 1;
  109. //drvSpithreshold Tx_Rthreshold;
  110. //drvSpithreshold Rx_Tthreshold;
  111. unsigned int Tx_Rthreshold : 5; //5bit:[0-31]对应0到31档的阈值
  112. unsigned int Rx_Tthreshold : 5; //5bit:[0-31]对应0到31档的阈值
  113. } drvSpiIrq;
  114. #else
  115. typedef struct
  116. {
  117. uint32_t rxf_full_raw_sts : 1; // [0]
  118. uint32_t rxf_empty_raw_sts : 1; // [1]
  119. uint32_t txf_full_raw_sts : 1; // [2]
  120. uint32_t tx_fifo_empty_w : 1; // [3]
  121. uint32_t rx_ovf_raw_sts : 1; // [4]
  122. uint32_t time_out_raw_sts : 1; // [5]
  123. uint32_t rxf_full_r : 1; // [6]
  124. uint32_t txf_empty_w : 1; // [7]
  125. uint32_t tx_end_irq : 1; // [8]
  126. uint32_t rx_end_irq : 1; // [9]
  127. } drvSpiIrq;
  128. #endif
  129. #endif
  130. typedef enum
  131. {
  132. SPI_CTRL,
  133. INPUT_CTRL,
  134. FORCE_0_CTRL,
  135. FROCE_1_CTRL,
  136. } drvSpiPinState;
  137. typedef enum
  138. {
  139. SPI_DIRECT_POLLING = 0,
  140. SPI_DIRECT_IRQ,
  141. SPI_DMA_POLLING,
  142. SPI_DMA_IRQ,
  143. } drvSpiTransferMode;
  144. #ifdef CONFIG_SOC_8850
  145. typedef enum
  146. {
  147. CLK_SOURCE_RTC32,
  148. CLK_SOURCE_XTAL26,
  149. CLK_SOURCE_RC26_26,
  150. CLK_SOURCE_GNSSPLL_133m,
  151. CLK_SOURCE_APLL_167,
  152. } drvClkSource_t;
  153. typedef enum
  154. {
  155. SPI_WORK_STANDARD,
  156. SPI_WORK_SYNC,
  157. SPI_WORK_S8,
  158. SPI_WORK_3_WIRE,
  159. SPI_WORK_SLAVE,
  160. } drvSpiWorkMode_t;
  161. #endif
  162. typedef struct
  163. {
  164. #ifdef CONFIG_SOC_8811
  165. bool clk_sel;
  166. #endif
  167. bool inputEn;
  168. #ifdef CONFIG_SOC_8850
  169. bool lsb;
  170. #endif
  171. uint8_t oe_delay;
  172. uint32_t name;
  173. uint32_t framesize;
  174. uint32_t baud;
  175. #ifdef CONFIG_SOC_8850
  176. drvSpiWorkMode_t work_mode;
  177. drvClkSource_t clk_source;
  178. #endif
  179. drvSpiCsPol cs_polarity0;
  180. drvSpiCsPol cs_polarity1;
  181. drvSpiCpolPol cpol;
  182. drvSpiCphaPol cpha;
  183. drvSpiInputSel input_sel;
  184. drvSpiTransferMode transmode;
  185. #ifdef CONFIG_SOC_8850
  186. uint8_t rxf_full_thrhld;
  187. uint8_t rxf_empty_thrhld;
  188. uint8_t txf_full_thrhld;
  189. uint8_t txf_empty_thrhld;
  190. uint32_t spi_tx_data_len;
  191. uint32_t spi_rx_data_len;
  192. #endif
  193. } drvSpiConfig_t;
  194. struct drvSpiMaster;
  195. typedef void (*callback)(drvSpiIrq cause);
  196. typedef struct drvSpiMaster drvSpiMaster_t;
  197. #if !((defined CONFIG_QUEC_PROJECT_FEATURE_SPI4_EXT_NOR_SFFS) || (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_SPI4_EXTNSFFS))
  198. typedef struct drvGeneralSpiFlash drvGeneralSpiFlash_t;
  199. #endif
  200. /**
  201. * @brief acquire the spi master
  202. *
  203. * @param cfg : drvSpiConfig_t structure that contains the configuration
  204. infomation for specified SPI peripheral
  205. * @return
  206. * - (NULL) fail
  207. * - otherwise the spi master instance
  208. */
  209. drvSpiMaster_t *drvSpiMasterAcquire(drvSpiConfig_t cfg);
  210. /**
  211. * @brief Transmits datas with specific length throuth the SPI peripheral.
  212. *
  213. * @param d : point to spi instance
  214. * @param cs : cs choice of spi
  215. * @param sendaddr : point to senddata
  216. * for uis8910
  217. since the data will not be spliced, when the frame lenght is greater than 8, the specified data structure must be used
  218. * In direct mode :
  219. framesize <= 8: sendaddr MUST be of type uint8
  220. framesize > 8 framesize <= 16: sendaddr MUST be of type uint16
  221. framesize > 16: sendaddr MUST be of type uint32
  222. * in DMA mode:
  223. framesize <= 8: sendaddr MUST be of type uint8
  224. framesize > 8 : sendaddr MUST be of type uint32
  225. * @param len : data len to be transmitted,len = allbytesize / sizeof(dataformat),
  226. eg : uint8[10],len = 10, uint16[10],len = 10.
  227. * for uis8811
  228. all data will be processed as 8 bits, and the data will be spliced according the frame lenght,the redundant data will be discarded.
  229. * @param len: data len is all bytes.
  230. * @return
  231. * - (false) fail
  232. * - (true) success
  233. */
  234. #if defined(CONFIG_SOC_8910)
  235. bool drvSpiWrite(drvSpiMaster_t *d, drvSpiCsSel cs, const void *sendaddr, uint32_t len);
  236. #elif defined(CONFIG_SOC_8811)
  237. bool drvSpiWrite(drvSpiMaster_t *d, drvSpiCsSel cs, void *sendaddr, uint32_t len);
  238. #elif defined(CONFIG_SOC_8850)
  239. uint32_t drvSpiWrite(drvSpiMaster_t *d, const void *sendaddr, uint32_t len);
  240. #else
  241. uint32_t drvSpiWrite(drvSpiMaster_t *d, void *sendaddr, uint32_t len);
  242. #endif
  243. #if defined(CONFIG_SOC_8850)
  244. /**
  245. * @brief config spi cs
  246. * @param d : point to spi instance
  247. * csnum: cs number
  248. * cschoose: cs status
  249. * @return :
  250. * -true
  251. * -false
  252. */
  253. bool drvSpiCsConfig(drvSpiMaster_t *d, drvSpiCsSel csnum, bool cschoose);
  254. /**
  255. * @brief config spi regs
  256. * @param d : point to spi instance
  257. * @return : None
  258. */
  259. void drvSpiRegsConfig(drvSpiMaster_t *d);
  260. #endif
  261. /**
  262. * @brief Receive the most recent received datas by the SPI peripheral.
  263. *
  264. * @param d : point to spi instance
  265. * @param cs : cs choice of spi
  266. * @param sendaddr : point to senddata address.in this case, you can specify the content of datas.
  267. * @param readaddr : point to readaddr address.
  268. * for uis8910
  269. since the data will not be spliced, when the frame lenght is greater than 8, the specified data structure must be used
  270. * In direct mode :
  271. framesize <= 8: sendaddr MUST be of type uint8
  272. framesize > 8 framesize <= 16: sendaddr MUST be of type uint16
  273. framesize > 16: sendaddr MUST be of type uint32
  274. * in DMA mode:
  275. framesize <= 8: sendaddr MUST be of type uint8
  276. framesize > 8 : sendaddr MUST be of type uint32
  277. * @param len : data len to be transmitted,len = allbytesize / sizeof(dataformat),
  278. eg : uint8[10],len = 10, uint16[10],len = 10.
  279. * for uis8811
  280. all data will be processed as 8 bits, and the data will be spliced according the frame lenght,the redundant data will be discarded.
  281. * @param len: data len is all bytes.
  282. * @return
  283. * - (false) fail
  284. * - (true) success
  285. */
  286. #ifndef CONFIG_SOC_8850
  287. bool drvSpiRead(drvSpiMaster_t *d, drvSpiCsSel cs, void *sendaddr, void *readaddr, uint32_t len);
  288. #else
  289. uint32_t drvSpiRead(drvSpiMaster_t *d, void *readaddr, uint32_t len);
  290. uint32_t drvSpiReadWrite(drvSpiMaster_t *d, void *sendaddr, void *readaddr, uint32_t len);
  291. uint32_t drvSpiRWDirectPolling(drvSpiMaster_t *d, void *sendaddr, void *readaddr, uint32_t len);
  292. uint32_t drvSpiRWDmaPolling(drvSpiMaster_t *d, void *sendaddr, void *readaddr, uint32_t len);
  293. #endif
  294. #ifndef CONFIG_SOC_8850
  295. /**
  296. * @brief get irq mask of the SPI peripheral
  297. * @param d : point to spi instance
  298. * @return : None
  299. */
  300. void prvSpiGetIrqMask(drvSpiMaster_t *d);
  301. #endif
  302. /**
  303. * @brief config irq of the SPI peripheral
  304. * @param d : point to spi instance
  305. * @param mask : point to the irq mask cofig
  306. * @param callfunc :call back function .
  307. * @return : None
  308. */
  309. void drvSpiSetIrqHandle(drvSpiMaster_t *d, callback callfunc);
  310. /**
  311. * @brief config irq of the SPI peripheral
  312. * @param d : point to spi instance
  313. * @param mask : point to the irq mask cofig
  314. * @param callfunc :call back function .
  315. * @return : None
  316. */
  317. void drvSpiSetIrqMask(drvSpiMaster_t *d, drvSpiIrq *mask);
  318. /**
  319. * @brief clear irq of the SPI peripheral
  320. * @param d : point to spi instance
  321. * @return : None
  322. */
  323. void drvSpiClrIrqMask(drvSpiMaster_t *d);
  324. /**
  325. * @brief Enable the Tx stream mode,Used for SD/MMC SPI mode.
  326. * @param d : point to spi instance
  327. * @param stream_bit : value of stream mode
  328. * @param on : enable or disable stream mode
  329. * - true enable stream mode, this mode provide infinite bit stream for sending after fifo is empty,
  330. all generated data have the same value, the vaule is in stream bit)
  331. - false disable stream mode
  332. * @return : None
  333. */
  334. void drvSpiEnableStreamMode(drvSpiMaster_t *d, uint8_t stream_bit, bool on);
  335. /**
  336. * @brief get the Number of data in spi rx FIFO
  337. * @param d : point to spi instance
  338. * @return : Number of data in spi rx FIFO
  339. */
  340. uint8_t drvSpiGetRxLevel(drvSpiMaster_t *d);
  341. /**
  342. * @brief get the Number of empty spot in spi tx FIFO
  343. * @param d : point to spi instance
  344. * @return : Number of empty spot in spi tx FIFO
  345. */
  346. uint8_t drvSpiGetTxFree(drvSpiMaster_t *d);
  347. /**
  348. * @brief config pin state of the spi peripheral
  349. * @param d : point to spi instance
  350. * @param pinctrl : where it can be
  351. * - SPI_CLK_CTRL SPI_DO_CTRL SPI_CS0_CTRL SPI_CS1_CTRL SPI_CS2_CTRL
  352. * @param pinstate : pin can be config as :
  353. * - SPI_CTRL (ctrl by spi peripheral automaticly)
  354. * - INPUT_CTRL (ctrl pin in input mode)
  355. * - FORCE_0_CTRL (froced pull down)
  356. * - FROCE_1_CTRL (froced pull up)
  357. * @return : None
  358. */
  359. #ifndef CONFIG_SOC_8850
  360. void drvSpiPinControl(drvSpiMaster_t *d, drvSpiPinCtrl pinctrl, drvSpiPinState pinstate);
  361. #else
  362. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  363. bool drvSpiCsConfig(drvSpiMaster_t *d, drvSpiCsSel csnum, bool cschoose);
  364. #endif
  365. #endif
  366. /**
  367. * @brief release the spi peripheral
  368. * @param d : point to spi instance
  369. * @return : None
  370. */
  371. void drvSpiMasterRelease(drvSpiMaster_t *d);
  372. #if !((defined CONFIG_QUEC_PROJECT_FEATURE_SPI4_EXT_NOR_SFFS) || (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_SPI4_EXTNSFFS))
  373. /**
  374. * @brief open spi flash device
  375. *
  376. * @param name: DRV_NAME_SPI1, spi device name
  377. * @return
  378. * - (NULL) fail
  379. * - otherwise the spi flash instance
  380. */
  381. drvGeneralSpiFlash_t *drvGeneralSpiFlashOpen(uint32_t name);
  382. /**
  383. * @brief config iomux of spi port, config SPI, read spi flash ID
  384. *
  385. * @param name: DRV_NAME_SPI1, spi device name
  386. * @return
  387. * - none
  388. */
  389. void drvGeneralSpiFlashInit(uint32_t name);
  390. /**
  391. * @brief spi write enable
  392. *
  393. * @param null
  394. * @return
  395. * - none
  396. */
  397. void drvGeneralSpiFlashEnable(void);
  398. /**
  399. * @brief wait for spi flash to complete the last operation
  400. *
  401. * @param null
  402. * @return
  403. * - false: time out
  404. * - true: normal
  405. */
  406. int drvGeneralSpiFlashFininsh(void);
  407. /**
  408. * @brief spi flash read
  409. *
  410. * @param offset : absolute read address of spi flash
  411. * size : read size
  412. * pbuf : read buffer address
  413. * @return
  414. * - true
  415. */
  416. int drvGeneralSpiFlashRead(uint32_t offset, uint32_t size, void *pbuf);
  417. /**
  418. * @brief spi flash write
  419. *
  420. * @param d : drvGeneralSpiFlash_t strunture of spi flash device
  421. * offset : absolute write address of spi flash
  422. * data : write data buffer address
  423. * size : write size
  424. * @return
  425. * - false: fail
  426. * - true: success
  427. */
  428. bool drvGeneralSpiFlashWrite(drvGeneralSpiFlash_t *d, uint32_t offset, const void *data, size_t size);
  429. /**
  430. * @brief spi flash erase
  431. *
  432. * @param d : drvGeneralSpiFlash_t strunture of spi flash device
  433. * offset : absolute erase address of spi flash
  434. * size : erase size
  435. * @return
  436. * - false: fail
  437. * - true: success
  438. */
  439. bool drvGeneralSpiFlashErase(drvGeneralSpiFlash_t *d, uint32_t offset, size_t size);
  440. /**
  441. * @brief spi flash write enable test
  442. *
  443. * @param null
  444. * @return
  445. * - none
  446. */
  447. bool drvGeneralSpiFlashWriteEnableTest(void);
  448. /**
  449. * @brief spi flash write test
  450. *
  451. * @param offset : absolute write address of spi flash
  452. * size : write size
  453. * buf : write buffer address
  454. * @return
  455. * - false: fail
  456. * - true: success
  457. */
  458. int drvGeneralSpiFlashWriteTest(uint32_t offset, uint32_t size, const void *buf);
  459. /**
  460. * @brief spi flash erase test
  461. *
  462. * @param offset : absolute erase address of spi flash
  463. * size : erase size
  464. * @return
  465. * - false: fail
  466. * - true: success
  467. */
  468. int drvGeneralSpiFlashEraseTest(uint32_t offset, uint32_t size);
  469. #endif
  470. #ifdef __cplusplus
  471. }
  472. #endif
  473. #endif