spi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file spi.c
  30. * @brief Contains all functions support for spi driver
  31. * @version 0.0
  32. * @date 18. Oct. 2017
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #include "spi.h"
  40. #include "error.h"
  41. #include <string.h>
  42. #include "pwrmgr.h"
  43. #include "clock.h"
  44. #include "log.h"
  45. typedef struct _spi_Context{
  46. spi_Cfg_t cfg;
  47. hal_spi_t* spi_info;
  48. bool is_slave_mode;
  49. spi_xmit_t transmit;
  50. }spi_Ctx_t;
  51. static spi_Ctx_t m_spiCtx[2];
  52. #define SPI_HDL_VALIDATE(hdl) {if((hdl == NULL) || (hdl->spi_index > 1))\
  53. return PPlus_ERR_INVALID_PARAM;\
  54. if((hdl != m_spiCtx[0].spi_info) && (hdl != m_spiCtx[1].spi_info))\
  55. return PPlus_ERR_NOT_REGISTED;}
  56. ////////////////// SPI /////////////////////////////////////////
  57. //this function is used just when you want to use two spi master module
  58. /*
  59. int hal_spi_module_select(hal_spi_t* spi_ptr)
  60. {
  61. SPI_HDL_VALIDATE(spi_ptr);
  62. if(spi_ptr->spi_index == 0)
  63. {
  64. AP_PERI_MASTER_SELECT &= ~0x33;
  65. AP_PERI_MASTER_SELECT |= 0x11;
  66. }
  67. else
  68. {
  69. AP_PERI_MASTER_SELECT &= ~0x33;
  70. AP_PERI_MASTER_SELECT |= 0x22;
  71. }
  72. return PPlus_SUCCESS;
  73. }
  74. */
  75. static void hal_spi_write_fifo(AP_SSI_TypeDef *Ssix,uint8_t len,uint8_t* tx_rx_ptr)
  76. {
  77. uint8_t i=0;
  78. HAL_ENTER_CRITICAL_SECTION();
  79. while(i<len)
  80. {
  81. Ssix->DataReg = *(tx_rx_ptr+i);
  82. i++;
  83. }
  84. HAL_EXIT_CRITICAL_SECTION();
  85. }
  86. static void hal_spi_read_fifo(AP_SSI_TypeDef *Ssix,uint8_t len,uint8_t* tx_rx_ptr)
  87. {
  88. uint8_t i=0;
  89. HAL_ENTER_CRITICAL_SECTION();
  90. while(i<len)
  91. {
  92. *(tx_rx_ptr+i) = Ssix->DataReg;
  93. i++;
  94. }
  95. HAL_EXIT_CRITICAL_SECTION();
  96. }
  97. void spi_int_enable(hal_spi_t* spi_ptr, uint32_t mask)
  98. {
  99. AP_SSI_TypeDef *Ssix = NULL;
  100. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  101. //Ssix->IMR = mask & 0x11;
  102. //HAL_ENTER_CRITICAL_SECTION();
  103. NVIC_EnableIRQ((IRQn_Type)(SPI0_IRQ + spi_ptr->spi_index));
  104. NVIC_SetPriority((IRQn_Type)(SPI0_IRQ + spi_ptr->spi_index), IRQ_PRIO_HAL);
  105. //HAL_EXIT_CRITICAL_SECTION();
  106. if(m_spiCtx[spi_ptr->spi_index].cfg.force_cs == true)
  107. Ssix->IMR = 0x11;
  108. else
  109. Ssix->IMR = 0x10;
  110. }
  111. static void spi_int_disable(hal_spi_t* spi_ptr)
  112. {
  113. AP_SSI_TypeDef *Ssix = NULL;
  114. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  115. NVIC_DisableIRQ((IRQn_Type)(SPI0_IRQ + spi_ptr->spi_index));
  116. Ssix->IMR = 0x00;
  117. }
  118. static void spi_int_handle(uint8_t id, spi_Ctx_t* pctx, AP_SSI_TypeDef * Ssix)
  119. {
  120. volatile uint8_t spi_irs_status;
  121. spi_xmit_t* trans_ptr;
  122. uint32_t rx_ftlr;
  123. spi_evt_t evt;
  124. bool complete_flag = false;
  125. uint16_t rem;
  126. uint8_t *rxBuf,*txBuf,i,len;
  127. trans_ptr = &pctx->transmit;
  128. spi_irs_status = Ssix->ISR;
  129. if(spi_irs_status & TRANSMIT_FIFO_EMPTY)
  130. {
  131. if(pctx->cfg.force_cs == true)
  132. {
  133. if(trans_ptr->tx_offset >= trans_ptr->xmit_len)
  134. Ssix->IMR = 0x10;
  135. }
  136. }
  137. if(spi_irs_status & RECEIVE_FIFO_FULL)
  138. {
  139. rx_ftlr = Ssix->RXFTLR;
  140. rxBuf = trans_ptr->rx_buf + trans_ptr->rx_offset;
  141. i=0;
  142. while(i<(rx_ftlr+1))
  143. {
  144. *(rxBuf+i) = Ssix->DataReg;
  145. i++;
  146. }
  147. trans_ptr->rx_offset += rx_ftlr+1;
  148. if(trans_ptr->rx_offset < trans_ptr->xmit_len)
  149. {
  150. rem = trans_ptr->xmit_len - trans_ptr->rx_offset;
  151. if(pctx->cfg.force_cs == true)
  152. {
  153. len = (rem>=4) ? 4 : rem;
  154. }
  155. else
  156. {
  157. len = (rem>=8) ? 8 : rem;
  158. }
  159. if(rx_ftlr != (len-1))
  160. {
  161. Ssix->RXFTLR = len-1;
  162. Ssix->TXFTLR = len-1;
  163. }
  164. i=0;
  165. txBuf = trans_ptr->tx_buf + trans_ptr->tx_offset;
  166. while(i<len)
  167. {
  168. Ssix->DataReg = *(txBuf+i);
  169. i++;
  170. }
  171. trans_ptr->tx_offset += len;
  172. }
  173. else
  174. {
  175. if(pctx->cfg.force_cs == true)
  176. hal_gpio_fmux(pctx->cfg.ssn_pin, Bit_ENABLE);
  177. complete_flag = true;
  178. }
  179. }
  180. if(complete_flag == true)
  181. {
  182. Ssix->IMR = 0x00;
  183. trans_ptr->busy = false;
  184. trans_ptr->tx_buf = NULL;
  185. trans_ptr->rx_buf = NULL;
  186. trans_ptr->tx_offset = 0;
  187. trans_ptr->rx_offset = 0;
  188. trans_ptr->xmit_len = 0;
  189. hal_pwrmgr_unlock((MODULE_e)(MOD_SPI0 + id));
  190. evt.id = id;
  191. evt.evt = SPI_TX_COMPLETED;
  192. pctx->cfg.evt_handler(&evt);
  193. }
  194. }
  195. static void spis_int_handle(uint8_t id, spi_Ctx_t* pctx, AP_SSI_TypeDef * Ssix)
  196. {
  197. volatile uint8_t spi_irs_status;
  198. spi_xmit_t* trans_ptr;
  199. spi_evt_t evt;
  200. uint16_t i, cnt;
  201. trans_ptr = &(pctx->transmit);
  202. spi_irs_status = Ssix->ISR;
  203. if(spi_irs_status & TRANSMIT_FIFO_EMPTY)
  204. {
  205. cnt = 8 - Ssix->TXFLR;
  206. for(i = 0; i< cnt; i++){
  207. if(trans_ptr->tx_offset == trans_ptr->xmit_len){
  208. Ssix->IMR = 0x10;
  209. break;
  210. }
  211. if(trans_ptr->tx_buf){
  212. Ssix->DataReg = trans_ptr->tx_buf[trans_ptr->tx_offset ++];
  213. }
  214. else
  215. {
  216. trans_ptr->tx_offset ++;
  217. Ssix->DataReg = 0;
  218. }
  219. }
  220. }
  221. if(spi_irs_status & RECEIVE_FIFO_FULL)
  222. {
  223. volatile uint32_t garbage;
  224. cnt = Ssix->RXFLR;
  225. if(trans_ptr->rx_buf){
  226. for(i = 0; i< cnt; i++)
  227. {
  228. if(trans_ptr->xmit_len > trans_ptr->rx_offset)
  229. trans_ptr->rx_buf[trans_ptr->rx_offset++] = Ssix->DataReg;
  230. else
  231. garbage = Ssix->DataReg;
  232. }
  233. }
  234. else
  235. {
  236. uint8_t rxbuf[16];
  237. if(trans_ptr->busy)
  238. trans_ptr->rx_offset += cnt;
  239. for(i = 0; i< cnt; i++)
  240. {
  241. *(rxbuf+i) = Ssix->DataReg;
  242. }
  243. evt.id = id;
  244. evt.evt = SPI_RX_DATA_S;
  245. evt.data = rxbuf;
  246. evt.len = cnt;
  247. pctx->cfg.evt_handler(&evt);
  248. }
  249. if(trans_ptr->busy && trans_ptr->rx_offset >= trans_ptr->xmit_len){
  250. memset(trans_ptr, 0, sizeof(spi_xmit_t));
  251. evt.id = id;
  252. evt.evt = SPI_RX_COMPLETED;
  253. evt.data = NULL;
  254. evt.len = cnt;
  255. pctx->cfg.evt_handler(&evt);
  256. evt.evt = SPI_TX_COMPLETED;
  257. pctx->cfg.evt_handler(&evt);
  258. }
  259. }
  260. }
  261. /**************************************************************************************
  262. * @fn hal_SPI0_IRQHandler
  263. *
  264. * @brief This function process for spi0 interrupt,when use int please consummate its callbackfunction
  265. *
  266. * input parameters
  267. *
  268. * @param None.
  269. *
  270. * output parameters
  271. *
  272. * @param None.
  273. *
  274. * @return None.
  275. **************************************************************************************/
  276. void __attribute__((used)) hal_SPI0_IRQHandler(void)
  277. {
  278. spi_Ctx_t* pctx = &m_spiCtx[0];
  279. if(pctx->spi_info == NULL)
  280. return;
  281. if(pctx->is_slave_mode)
  282. spis_int_handle(0, pctx, AP_SPI0);
  283. else
  284. spi_int_handle(0, pctx, AP_SPI0);
  285. }
  286. /**************************************************************************************
  287. * @fn hal_SPI1_IRQHandler
  288. *
  289. * @brief This function process for spi1 interrupt,when use int please consummate its callbackfunction
  290. *
  291. * input parameters
  292. *
  293. * @param None.
  294. *
  295. * output parameters
  296. *
  297. * @param None.
  298. *
  299. * @return None.
  300. **************************************************************************************/
  301. void __attribute__((used)) hal_SPI1_IRQHandler(void)
  302. {
  303. spi_Ctx_t* pctx = &m_spiCtx[1];
  304. if(pctx->spi_info == NULL)
  305. return;
  306. if(pctx->is_slave_mode)
  307. spis_int_handle(1, pctx, AP_SPI1);
  308. else
  309. spi_int_handle(1, pctx, AP_SPI1);
  310. }
  311. /**************************************************************************************
  312. * @fn hal_spi_pin_init
  313. *
  314. * @brief This function process for spi pin initial(4 lines);You can use two spi,spi0 and spi1,should programe by USE_AP_SPIX
  315. *
  316. * input parameters
  317. *
  318. * @param GPIO_Pin_e sck_pin: define sclk pin
  319. * GPIO_Pin_e ssn_pin: define ssn pin
  320. * GPIO_Pin_e tx_pin: define transmit pin;when use as master,it's mosi pin;corresponding,use as slave,it's miso
  321. * GPIO_Pin_e rx_pin: define receive pin;when use as master,it's miso pin;corresponding,use as slave,it's mosi
  322. *
  323. * output parameters
  324. *
  325. * @param None.
  326. *
  327. * @return None.
  328. **************************************************************************************/
  329. static void hal_spi_pin_init(hal_spi_t* spi_ptr,GPIO_Pin_e sck_pin,GPIO_Pin_e ssn_pin,GPIO_Pin_e tx_pin,GPIO_Pin_e rx_pin)
  330. {
  331. if(spi_ptr->spi_index == SPI0)
  332. {
  333. hal_gpio_fmux_set(sck_pin, SPI_0_SCK);
  334. hal_gpio_fmux_set(ssn_pin, SPI_0_SSN);
  335. hal_gpio_fmux_set(tx_pin, SPI_0_TX);
  336. hal_gpio_fmux_set(rx_pin, SPI_0_RX);
  337. }
  338. else if(spi_ptr->spi_index == SPI1)
  339. {
  340. hal_gpio_fmux_set(sck_pin, SPI_1_SCK);
  341. hal_gpio_fmux_set(ssn_pin, SPI_1_SSN);
  342. hal_gpio_fmux_set(tx_pin, SPI_1_TX);
  343. hal_gpio_fmux_set(rx_pin, SPI_1_RX);
  344. }
  345. }
  346. static void hal_spi_pin_deinit(GPIO_Pin_e sck_pin,GPIO_Pin_e ssn_pin,GPIO_Pin_e tx_pin,GPIO_Pin_e rx_pin)
  347. {
  348. hal_gpio_fmux(sck_pin, Bit_DISABLE);
  349. hal_gpio_fmux(ssn_pin, Bit_DISABLE);
  350. hal_gpio_fmux(tx_pin, Bit_DISABLE);
  351. hal_gpio_fmux(rx_pin, Bit_DISABLE);
  352. }
  353. /**************************************************************************************
  354. * @fn hal_spi_master_init
  355. *
  356. * @brief This function process for spi master initial
  357. *
  358. * input parameters
  359. *
  360. * @param uint32_t baud: baudrate select
  361. * SPI_SCMOD_e scmod: Serial Clock Polarity and Phase select; SPI_MODE0, //SCPOL=0,SCPH=0(default)
  362. * SPI_MODE1, //SCPOL=0,SCPH=1
  363. * SPI_MODE2, //SCPOL=1,SCPH=0
  364. * SPI_MODE3, //SCPOL=1,SCPH=1
  365. * SPI_TMOD_e tmod: Transfer Mode SPI_TRXD, //Transmit & Receive(default)
  366. * SPI_TXD, //Transmit Only
  367. * SPI_RXD, //Receive Only
  368. * SPI_EEPROM, //EEPROM Read
  369. *
  370. * output parameters
  371. *
  372. * @param None.
  373. *
  374. * @return None.
  375. **************************************************************************************/
  376. static void hal_spi_master_init(hal_spi_t* spi_ptr,uint32_t baud,SPI_SCMOD_e scmod,SPI_TMOD_e tmod)
  377. {
  378. uint8_t shift = 0;
  379. AP_SSI_TypeDef *Ssix = NULL;
  380. AP_COM_TypeDef *apcom = AP_COM;
  381. uint16_t baud_temp;
  382. int pclk = clk_ap_pclk();
  383. if(spi_ptr->spi_index == SPI1)
  384. {
  385. shift = 1;
  386. }
  387. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  388. Ssix->SSIEN = 0; //DISABLE_SPI;
  389. apcom->PERI_MASTER_SELECT |= (BIT(shift)|BIT(shift+4));
  390. Ssix->CR0= ((Ssix->CR0) & 0xfffffc3f)|(scmod<<6)|(tmod<<8);
  391. baud_temp = (pclk + (baud>>1)) / baud;
  392. if(baud_temp<2)
  393. {
  394. baud_temp = 2;
  395. }
  396. else if(baud_temp>65534)
  397. {
  398. baud_temp =65534;
  399. }
  400. Ssix->BAUDR= baud_temp; // set clock(round)
  401. Ssix->TXFTLR=4; // set fifo threshold to triggle interrupt
  402. Ssix->RXFTLR=1;
  403. Ssix->IMR = 0x00;
  404. Ssix->SER=1; //enable slave device
  405. Ssix->SSIEN = 1; //ENABLE_SPI;
  406. }
  407. /**************************************************************************************
  408. * @fn hal_spi_slave_init
  409. *
  410. * @brief This function process for spi slave initial
  411. *
  412. * input parameters
  413. *
  414. * @param uint32_t baud: baudrate select
  415. * SPI_SCMOD_e scmod: Serial Clock Polarity and Phase select; SPI_MODE0, //SCPOL=0,SCPH=0(default)
  416. * SPI_MODE1, //SCPOL=0,SCPH=1
  417. * SPI_MODE2, //SCPOL=1,SCPH=0
  418. * SPI_MODE3, //SCPOL=1,SCPH=1
  419. * SPI_TMOD_e tmod: Transfer Mode SPI_TRXD, //Transmit & Receive(default)
  420. * SPI_TXD, //Transmit Only
  421. * SPI_RXD, //Receive Only
  422. * SPI_EEPROM, //EEPROM Read
  423. *
  424. * output parameters
  425. *
  426. * @param None.
  427. *
  428. * @return None.
  429. **************************************************************************************/
  430. /*static*/ void hal_spi_slave_init(hal_spi_t* spi_ptr,uint32_t baud,SPI_SCMOD_e scmod,SPI_TMOD_e tmod)
  431. {
  432. uint8_t shift = 0;
  433. AP_SSI_TypeDef *Ssix = NULL;
  434. AP_COM_TypeDef *apcom = AP_COM;
  435. uint16_t baud_temp;
  436. int pclk = clk_ap_pclk();
  437. if(spi_ptr->spi_index == SPI1)
  438. {
  439. shift = 1;
  440. }
  441. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  442. Ssix->SSIEN = 0; //DISABLE_SPI;
  443. apcom->PERI_MASTER_SELECT &= ~(BIT(shift));
  444. Ssix->CR0= ((Ssix->CR0) & 0xfffffc3f)|(scmod<<6)|(tmod<<8)|0x400;
  445. baud_temp = (pclk + (baud>>1)) / baud;
  446. if(baud_temp<2)
  447. {
  448. baud_temp = 2;
  449. }
  450. else if(baud_temp>65534)
  451. {
  452. baud_temp =65534;
  453. }
  454. Ssix->BAUDR= baud_temp; // set clock(round)
  455. Ssix->TXFTLR=4; // set fifo threshold to triggle interrupt
  456. Ssix->RXFTLR=1; //threshold is 1
  457. Ssix->IMR=0x11; //enable tx and rx
  458. // Ssix->SER=1; //enable slave device
  459. Ssix->SSIEN = 1; //ENABLE_SPI;
  460. }
  461. static int hal_spi_tx_rx_single_frame(hal_spi_t* spi_ptr,uint8_t* tx_buf,uint8_t* rx_buf,uint8_t len)
  462. {
  463. int ret = PPlus_ERR_BUSY;;
  464. uint8_t dummy_buf[8] ={0,0,0,0,0,0,0,0};//padding data with your application
  465. uint8_t* tx_rx_ptr = NULL;
  466. AP_SSI_TypeDef *Ssix = NULL;
  467. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  468. if(tx_buf != NULL)
  469. tx_rx_ptr = tx_buf;
  470. else
  471. tx_rx_ptr = dummy_buf;
  472. //while(SPI_BUSY);
  473. if(Ssix->SR & TX_FIFO_NOT_FULL)
  474. {
  475. hal_spi_write_fifo(Ssix,len,tx_rx_ptr);
  476. SPI_INIT_TOUT(to);
  477. while(Ssix->SR & SPI_BUSY)
  478. {
  479. SPI_CHECK_TOUT(to,1000000,"timeout\n")
  480. }
  481. ret = PPlus_SUCCESS;
  482. }
  483. else
  484. {
  485. return ret;
  486. }
  487. if(rx_buf != NULL)
  488. tx_rx_ptr = rx_buf;
  489. else
  490. tx_rx_ptr = dummy_buf;
  491. //while(SPI_BUSY);
  492. len = NUMBER_DATA_RX_FIFO;
  493. hal_spi_read_fifo(Ssix,len,tx_rx_ptr);
  494. return ret;
  495. }
  496. static void spi0_sleep_handler(void)
  497. {
  498. if(m_spiCtx[0].spi_info != NULL)
  499. hal_spi_bus_deinit(m_spiCtx[0].spi_info);
  500. }
  501. static void spi1_sleep_handler(void)
  502. {
  503. if(m_spiCtx[1].spi_info != NULL)
  504. hal_spi_bus_deinit(m_spiCtx[1].spi_info);
  505. }
  506. static void spi0_wakeup_handler(void)
  507. {
  508. NVIC_SetPriority((IRQn_Type)SPI0_IRQ, IRQ_PRIO_HAL);
  509. }
  510. static void spi1_wakeup_handler(void)
  511. {
  512. NVIC_SetPriority((IRQn_Type)SPI1_IRQ, IRQ_PRIO_HAL);
  513. }
  514. int hal_spi_transmit(hal_spi_t* spi_ptr,uint8_t* tx_buf,uint8_t* rx_buf,uint16_t len)
  515. {
  516. spi_Ctx_t* pctx;
  517. AP_SSI_TypeDef *Ssix = NULL;
  518. spi_xmit_t* trans_ptr;
  519. uint8_t trans_len,remainder;
  520. uint16_t offset = 0;
  521. SPI_HDL_VALIDATE(spi_ptr);
  522. if((tx_buf == NULL) && (rx_buf == NULL))
  523. return PPlus_ERR_INVALID_PARAM;
  524. if(len == 0)
  525. return PPlus_ERR_INVALID_PARAM;
  526. pctx = &m_spiCtx[spi_ptr->spi_index];
  527. trans_ptr = &(pctx->transmit);
  528. if(pctx->transmit.busy == true)
  529. return PPlus_ERR_BUSY;
  530. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  531. if(pctx->cfg.force_cs == true /*&& pctx->is_slave_mode == FALSE*/)
  532. {
  533. hal_gpio_fmux(pctx->cfg.ssn_pin,Bit_DISABLE);
  534. hal_gpio_write(pctx->cfg.ssn_pin,0);
  535. }
  536. if(pctx->cfg.int_mode == false)
  537. {
  538. SPI_INIT_TOUT(to);
  539. while(len > 0)
  540. {
  541. trans_len =(len >= 8)?8:len;
  542. if(PPlus_SUCCESS == hal_spi_tx_rx_single_frame(spi_ptr,(tx_buf + offset),(rx_buf + offset),trans_len))
  543. {
  544. offset += trans_len;
  545. len -= trans_len;
  546. }
  547. SPI_CHECK_TOUT(to,1000000,"timeout2\n")
  548. }
  549. if(pctx->cfg.force_cs == true && pctx->is_slave_mode == FALSE)
  550. hal_gpio_fmux(pctx->cfg.ssn_pin,Bit_ENABLE);
  551. }
  552. else
  553. {
  554. spi_int_disable(spi_ptr);
  555. trans_ptr->busy = true;
  556. trans_ptr->tx_offset = 0;
  557. trans_ptr->rx_offset = 0;
  558. trans_ptr->rx_buf = rx_buf;
  559. trans_ptr->xmit_len = len;
  560. memcpy(trans_ptr->tx_buf,tx_buf,len);
  561. if(Ssix->SR & TX_FIFO_NOT_FULL)
  562. {
  563. hal_pwrmgr_lock((MODULE_e)(MOD_SPI0 + spi_ptr->spi_index));
  564. if(pctx->cfg.force_cs == true)
  565. trans_len = 4;
  566. else
  567. trans_len = 8;
  568. remainder = (len >= trans_len)?trans_len:len;
  569. m_spiCtx[spi_ptr->spi_index].transmit.tx_offset = remainder;
  570. Ssix->TXFTLR=remainder-1;
  571. Ssix->RXFTLR = remainder -1;
  572. hal_spi_write_fifo(Ssix,remainder,tx_buf);
  573. }
  574. spi_int_enable(spi_ptr,0x11);
  575. }
  576. return PPlus_SUCCESS;
  577. }
  578. int hal_spi_set_tx_buffer(hal_spi_t* spi_ptr,uint8_t* tx_buf,uint16_t len)
  579. {
  580. SPI_HDL_VALIDATE(spi_ptr);
  581. if((tx_buf == NULL) || (len == 0))
  582. return PPlus_ERR_INVALID_PARAM;
  583. m_spiCtx[spi_ptr->spi_index].transmit.tx_buf = tx_buf;//used when tx int
  584. m_spiCtx[spi_ptr->spi_index].transmit.buf_len = len;
  585. return PPlus_SUCCESS;
  586. }
  587. int hal_spi_set_int_mode(hal_spi_t* spi_ptr,bool en)
  588. {
  589. SPI_HDL_VALIDATE(spi_ptr);
  590. m_spiCtx[spi_ptr->spi_index].cfg.int_mode = en;
  591. if(en)
  592. {
  593. m_spiCtx[spi_ptr->spi_index].cfg.int_mode = true;
  594. spi_int_enable(spi_ptr, 0x10);
  595. }
  596. else
  597. {
  598. m_spiCtx[spi_ptr->spi_index].cfg.int_mode = false;
  599. spi_int_disable(spi_ptr);
  600. }
  601. return PPlus_SUCCESS;
  602. }
  603. int hal_spi_set_force_cs(hal_spi_t* spi_ptr,bool en)
  604. {
  605. SPI_HDL_VALIDATE(spi_ptr);
  606. m_spiCtx[spi_ptr->spi_index].cfg.force_cs = en;
  607. return PPlus_SUCCESS;
  608. }
  609. bool hal_spi_get_transmit_bus_state(hal_spi_t* spi_ptr)
  610. {
  611. return m_spiCtx[spi_ptr->spi_index].transmit.busy;
  612. }
  613. int hal_spi_TxComplete(hal_spi_t* spi_ptr)
  614. {
  615. AP_SSI_TypeDef *Ssix = NULL;
  616. SPI_HDL_VALIDATE(spi_ptr);
  617. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  618. SPI_INIT_TOUT(to);
  619. while(Ssix->SR & SPI_BUSY)
  620. {
  621. SPI_CHECK_TOUT(to, SPI_OP_TIMEOUT, "hal_spi_TxComplete TO\n");
  622. }
  623. return PPlus_SUCCESS;
  624. }
  625. int hal_spi_send_byte(hal_spi_t* spi_ptr,uint8_t data)
  626. {
  627. AP_SSI_TypeDef *Ssix = NULL;
  628. SPI_HDL_VALIDATE(spi_ptr);
  629. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  630. if(Ssix->SR & TX_FIFO_NOT_FULL){
  631. Ssix->DataReg = data & 0xff;
  632. SPI_INIT_TOUT(to);
  633. while(Ssix->SR & SPI_BUSY)
  634. {
  635. SPI_CHECK_TOUT(to, SPI_OP_TIMEOUT,"hal_spi_send_byte TO\n");
  636. }
  637. }
  638. return PPlus_SUCCESS;
  639. }
  640. int hal_spi_bus_init(hal_spi_t* spi_ptr,spi_Cfg_t cfg)
  641. {
  642. spi_Ctx_t* pctx = NULL;
  643. if((spi_ptr == NULL) || (spi_ptr->spi_index > 1))
  644. return PPlus_ERR_INVALID_PARAM;
  645. pctx = &m_spiCtx[spi_ptr->spi_index];
  646. if(pctx->spi_info != NULL)
  647. return PPlus_ERR_BUSY;
  648. clk_gate_enable((MODULE_e)(MOD_SPI0 + spi_ptr->spi_index));
  649. hal_spi_pin_init(spi_ptr,cfg.sclk_pin,cfg.ssn_pin,cfg.MOSI,cfg.MISO);
  650. hal_spi_master_init(spi_ptr,cfg.baudrate, cfg.spi_scmod, cfg.spi_tmod);
  651. pctx->cfg = cfg;
  652. pctx->transmit.busy = false;
  653. pctx->spi_info = spi_ptr;
  654. if(cfg.int_mode)
  655. spi_int_enable(spi_ptr, 0x10);
  656. else
  657. spi_int_disable(spi_ptr);
  658. pctx->is_slave_mode = false;
  659. return PPlus_SUCCESS;
  660. }
  661. //#define SPI_INIT_TOUT(to) int to = hal_systick()
  662. //#define PSI_CHECK_TOUT(to, timeout, loginfo) {if(hal_ms_intv(to) > timeout){LOG(loginfo);return PPlus_ERR_TIMEOUT;}}
  663. int hal_spis_clear_rx(hal_spi_t* spi_ptr)
  664. {
  665. AP_SSI_TypeDef *Ssix = NULL;
  666. volatile uint8_t rx;
  667. SPI_HDL_VALIDATE(spi_ptr);
  668. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  669. while(Ssix->RXFLR){
  670. rx = Ssix->DataReg;
  671. }
  672. return (int)rx;
  673. }
  674. uint32_t hal_spis_rx_len(hal_spi_t* spi_ptr)
  675. {
  676. AP_SSI_TypeDef *Ssix = NULL;
  677. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  678. return Ssix->RXFLR;
  679. }
  680. int hal_spis_read_rxn(hal_spi_t* spi_ptr, uint8_t* pbuf, uint16_t len)
  681. {
  682. AP_SSI_TypeDef *Ssix = NULL;
  683. Ssix = (spi_ptr->spi_index == SPI0) ? AP_SPI0 : AP_SPI1;
  684. while(len){
  685. *pbuf = Ssix->DataReg;
  686. pbuf ++;
  687. len --;
  688. }
  689. return PPlus_SUCCESS;
  690. }
  691. int hal_spis_bus_init(hal_spi_t* spi_ptr,spi_Cfg_t cfg)
  692. {
  693. spi_Ctx_t* pctx = NULL;
  694. if((spi_ptr == NULL) || (spi_ptr->spi_index > 1))
  695. return PPlus_ERR_INVALID_PARAM;
  696. pctx = &m_spiCtx[spi_ptr->spi_index];
  697. if(pctx->spi_info != NULL)
  698. return PPlus_ERR_BUSY;
  699. clk_gate_enable((MODULE_e)(MOD_SPI0 + spi_ptr->spi_index));
  700. hal_spi_pin_init(spi_ptr,cfg.sclk_pin,cfg.ssn_pin,cfg.MOSI,cfg.MISO);
  701. hal_spi_slave_init(spi_ptr,cfg.baudrate, cfg.spi_scmod, cfg.spi_tmod);
  702. pctx->cfg = cfg;
  703. memset(&(pctx->transmit), 0, sizeof(spi_xmit_t));
  704. pctx->spi_info = spi_ptr;
  705. pctx->is_slave_mode = true;
  706. if(cfg.int_mode)
  707. spi_int_enable(spi_ptr, 0x10);
  708. else
  709. spi_int_disable(spi_ptr);
  710. return PPlus_SUCCESS;
  711. }
  712. /**************************************************************************************
  713. * @fn hal_spi_deinit
  714. *
  715. * @brief This function will deinit the spi you select.
  716. *
  717. * input parameters
  718. *
  719. * @param hal_spi_t* spi_ptr: spi module handle.
  720. *
  721. * output parameters
  722. *
  723. * @param None.
  724. *
  725. * @return
  726. * PPlus_SUCCESS
  727. * PPlus_ERR_INVALID_PARAM
  728. **************************************************************************************/
  729. int hal_spi_bus_deinit(hal_spi_t* spi_ptr)
  730. {
  731. SPI_HDL_VALIDATE(spi_ptr);
  732. clk_reset((MODULE_e)(MOD_SPI0 + spi_ptr->spi_index));
  733. clk_gate_disable((MODULE_e)(MOD_SPI0 + spi_ptr->spi_index));
  734. //spi_int_disable(spi_ptr);
  735. hal_spi_pin_deinit(m_spiCtx[spi_ptr->spi_index].cfg.sclk_pin,m_spiCtx[spi_ptr->spi_index].cfg.ssn_pin,m_spiCtx[spi_ptr->spi_index].cfg.MOSI,m_spiCtx[spi_ptr->spi_index].cfg.MISO);
  736. memset(&(m_spiCtx[spi_ptr->spi_index]),0,sizeof(spi_Ctx_t));
  737. return PPlus_SUCCESS;
  738. }
  739. /**************************************************************************************
  740. * @fn hal_spi_init
  741. *
  742. * @brief it is used to init spi module.
  743. *
  744. * input parameters
  745. * @param None
  746. *
  747. * output parameters
  748. * @param None.
  749. *
  750. * @return None.
  751. **************************************************************************************/
  752. int hal_spi_init(SPI_INDEX_e channel)
  753. {
  754. int ret = 0;
  755. if(channel == SPI0){
  756. ret = hal_pwrmgr_register(MOD_SPI0,spi0_sleep_handler, spi0_wakeup_handler);
  757. if(ret == PPlus_SUCCESS)
  758. memset(&m_spiCtx[0],0,sizeof(spi_Ctx_t));
  759. return ret;
  760. }
  761. else if(channel == SPI1){
  762. ret = hal_pwrmgr_register(MOD_SPI1,spi1_sleep_handler, spi1_wakeup_handler);
  763. if(ret == PPlus_SUCCESS)
  764. memset(&m_spiCtx[1],0,sizeof(spi_Ctx_t));
  765. return ret;
  766. }
  767. return PPlus_ERR_INVALID_PARAM;
  768. }