drv_spi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * File : drv_spi.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-08-30 tanek first implementation.
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include <rtdevice.h>
  27. #include <stdbool.h>
  28. #include "drv_spi.h"
  29. #include "drv_gpio.h"
  30. #include "drv_clock.h"
  31. #define SPI_BUS_MAX_CLK (30 * 1000 * 1000)
  32. #define DBG_ENABLE
  33. #define DBG_SECTION_NAME "[SPI]"
  34. #define DBG_LEVEL DBG_WARNING
  35. #define DBG_COLOR
  36. #include <rtdbg.h>
  37. #ifdef RT_USING_SPI
  38. //#define DEBUG
  39. #define ARR_LEN(__N) (sizeof(__N) / sizeof(__N[0]))
  40. #ifdef DEBUG
  41. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  42. #else
  43. #define DEBUG_PRINTF(...)
  44. #endif
  45. #define __SPI_STATIC_INLINE__ rt_inline
  46. /*
  47. * @brief Hardware Layer Interface
  48. */
  49. __SPI_STATIC_INLINE__
  50. rt_uint32_t SPI_GetVersion(SPI_T *spi)
  51. {
  52. return spi->VER;
  53. }
  54. /*
  55. * @brief
  56. */
  57. __SPI_STATIC_INLINE__
  58. void SPI_Reset(SPI_T *spi)
  59. {
  60. HAL_SET_BIT(spi->CTRL, SPI_CTRL_RST_MASK);
  61. }
  62. /*
  63. * @brief
  64. */
  65. __SPI_STATIC_INLINE__
  66. void SPI_SetMode(SPI_T *spi, SPI_CTRL_Mode mode)
  67. {
  68. HAL_MODIFY_REG(spi->CTRL, SPI_CTRL_MODE_MASK, mode);
  69. }
  70. /*
  71. * @brief
  72. */
  73. __SPI_STATIC_INLINE__
  74. void SPI_Enable(SPI_T *spi)
  75. {
  76. HAL_SET_BIT(spi->CTRL, SPI_CTRL_EN_MASK);
  77. }
  78. __SPI_STATIC_INLINE__
  79. void SPI_Disable(SPI_T *spi)
  80. {
  81. HAL_CLR_BIT(spi->CTRL, SPI_CTRL_EN_MASK);
  82. }
  83. /*
  84. * @brief
  85. */
  86. __SPI_STATIC_INLINE__
  87. void SPI_StartTransmit(SPI_T *spi)
  88. {
  89. HAL_SET_BIT(spi->TCTRL, SPI_TCTRL_XCH_MASK);
  90. }
  91. /*
  92. * @brief
  93. */
  94. __SPI_STATIC_INLINE__
  95. void SPI_SetFirstTransmitBit(SPI_T *spi, SPI_TCTRL_Fbs bit)
  96. {
  97. HAL_MODIFY_REG(spi->TCTRL, SPI_TCTRL_FBS_MASK, bit);
  98. }
  99. /*
  100. * @brief
  101. */
  102. __SPI_STATIC_INLINE__
  103. void SPI_EnableRapidsMode(SPI_T *spi, bool delay_sample)
  104. {
  105. HAL_SET_BIT(spi->TCTRL, SPI_TCTRL_RPSM_MASK);
  106. HAL_MODIFY_REG(spi->TCTRL, SPI_TCTRL_SDC_MASK, delay_sample << SPI_TCTRL_SDC_SHIFT);
  107. }
  108. /*
  109. * @brief
  110. */
  111. __SPI_STATIC_INLINE__
  112. void SPI_DisableRapidsMode(SPI_T *spi)
  113. {
  114. HAL_CLR_BIT(spi->TCTRL, SPI_TCTRL_RPSM_MASK);
  115. }
  116. /*
  117. * @brief
  118. */
  119. __SPI_STATIC_INLINE__
  120. void SPI_SetDuplex(SPI_T *spi, SPI_TCTRL_DHB_Duplex duplex)
  121. {
  122. HAL_MODIFY_REG(spi->TCTRL, SPI_TCTRL_DHB_MASK, duplex);
  123. }
  124. /*
  125. * @brief
  126. */
  127. __SPI_STATIC_INLINE__
  128. void SPI_SetCsLevel(SPI_T *spi, bool level)
  129. {
  130. HAL_MODIFY_REG(spi->TCTRL, SPI_TCTRL_SS_LEVEL_MASK, level << SPI_TCTRL_SS_LEVEL_SHIFT);
  131. }
  132. /*
  133. * @brief
  134. */
  135. __SPI_STATIC_INLINE__
  136. void SPI_ManualChipSelect(SPI_T *spi, SPI_TCTRL_SS_Sel cs)
  137. {
  138. HAL_SET_BIT(spi->TCTRL, SPI_TCTRL_SS_OWNER_MASK);
  139. HAL_MODIFY_REG(spi->TCTRL, SPI_TCTRL_SS_SEL_MASK, cs);
  140. }
  141. /*
  142. * @brief
  143. */
  144. __SPI_STATIC_INLINE__
  145. void SPI_AutoChipSelect(SPI_T *spi, SPI_TCTRL_SS_Sel cs, bool cs_remain)
  146. {
  147. HAL_MODIFY_REG(spi->TCTRL, SPI_TCTRL_SS_SEL_MASK, cs);
  148. HAL_CLR_BIT(spi->TCTRL, SPI_TCTRL_SS_OWNER_MASK);
  149. HAL_MODIFY_REG(spi->TCTRL, SPI_TCTRL_SS_CTL_MASK, (!cs_remain) << SPI_TCTRL_SS_CTL_SHIFT);
  150. }
  151. /*
  152. * @brief
  153. */
  154. __SPI_STATIC_INLINE__
  155. void SPI_SetCsIdle(SPI_T *spi, bool idle)
  156. {
  157. HAL_MODIFY_REG(spi->TCTRL, SPI_TCTRL_SPOL_MASK, (!!idle) << SPI_TCTRL_SPOL_SHIFT);
  158. }
  159. /*
  160. * @brief
  161. */
  162. __SPI_STATIC_INLINE__
  163. void SPI_SetSclkMode(SPI_T *spi, SPI_SCLK_Mode mode)
  164. {
  165. HAL_MODIFY_REG(spi->TCTRL, SPI_TCTRL_CPOL_MASK | SPI_TCTRL_CPHA_MASK, mode);
  166. }
  167. typedef enum
  168. {
  169. SPI_INT_CS_DESELECT = SPI_IER_SS_INT_EN_MASK,
  170. SPI_INT_TRANSFER_COMPLETE = SPI_IER_TC_INT_EN_MASK,
  171. SPI_INT_TXFIFO_UNDER_RUN = SPI_IER_TF_UDR_INT_EN_MASK,
  172. SPI_INT_TXFIFO_OVERFLOW = SPI_IER_TF_OVF_INT_EN_MASK,
  173. SPI_INT_RXFIFO_UNDER_RUN = SPI_IER_RF_UDR_INT_EN_MASK,
  174. SPI_INT_RXFIFO_OVERFLOW = SPI_IER_RF_OVF_INT_EN_MASK,
  175. SPI_INT_TXFIFO_FULL = SPI_IER_TF_FUL_INT_EN_MASK,
  176. SPI_INT_TXFIFO_EMPTY = SPI_IER_TX_EMP_INT_EN_MASK,
  177. SPI_INT_TXFIFO_READY = SPI_IER_TX_ERQ_INT_EN_MASK,
  178. SPI_INT_RXFIFO_FULL = SPI_IER_RF_FUL_INT_EN_MASK,
  179. SPI_INT_RXFIFO_EMPTY = SPI_IER_RX_EMP_INT_EN_MASK,
  180. SPI_INT_RXFIFO_READY = SPI_IER_RF_RDY_INT_EN_MASK
  181. } SPI_Int_Type;
  182. /*
  183. * @brief
  184. */
  185. __SPI_STATIC_INLINE__
  186. void SPI_EnableInt(SPI_T *spi, SPI_Int_Type type)
  187. {
  188. HAL_SET_BIT(spi->IER, type);
  189. }
  190. /*
  191. * @brief
  192. */
  193. __SPI_STATIC_INLINE__
  194. void SPI_DisableInt(SPI_T *spi, SPI_Int_Type type)
  195. {
  196. HAL_CLR_BIT(spi->IER, type);
  197. }
  198. /*
  199. * @brief
  200. */
  201. __SPI_STATIC_INLINE__
  202. bool SPI_IntState(SPI_T *spi, SPI_Int_Type type)
  203. {
  204. return !!HAL_GET_BIT(spi->STA, type);
  205. }
  206. /*
  207. * @brief
  208. */
  209. __SPI_STATIC_INLINE__
  210. bool SPI_ClearInt(SPI_T *spi, SPI_Int_Type type)
  211. {
  212. HAL_SET_BIT(spi->STA, type);
  213. return HAL_GET_BIT(spi->STA, type);
  214. }
  215. /*
  216. * @brief
  217. */
  218. __SPI_STATIC_INLINE__
  219. void SPI_DebugReadTx(SPI_T *spi, rt_uint32_t *data)
  220. {
  221. // tbc...
  222. }
  223. /*
  224. * @brief
  225. */
  226. __SPI_STATIC_INLINE__
  227. void SPI_DebugWriteRx(SPI_T *spi, rt_uint32_t *data)
  228. {
  229. // tbc...
  230. }
  231. /*
  232. * @brief
  233. */
  234. __SPI_STATIC_INLINE__
  235. void SPI_ResetTxFifo(SPI_T *spi)
  236. {
  237. HAL_SET_BIT(spi->FCTL, SPI_FCTL_TF_RST_MASK);
  238. while (HAL_GET_BIT(spi->FCTL, SPI_FCTL_TF_RST_MASK) != 0);
  239. }
  240. /*
  241. * @brief
  242. */
  243. __SPI_STATIC_INLINE__
  244. void SPI_ResetRxFifo(SPI_T *spi)
  245. {
  246. HAL_SET_BIT(spi->FCTL, SPI_FCTL_RF_RST_MASK);
  247. while (HAL_GET_BIT(spi->FCTL, SPI_FCTL_RF_RST_MASK) != 0);
  248. }
  249. /*
  250. * @brief
  251. */
  252. __SPI_STATIC_INLINE__
  253. void SPI_DMA(SPI_T *spi, bool txEn, bool rxEn)
  254. {
  255. HAL_MODIFY_REG(spi->FCTL,
  256. SPI_FCTL_TF_DRQ_EN_MASK | SPI_FCTL_RF_DRQ_EN_MASK,
  257. ((!!txEn) << SPI_FCTL_TF_DRQ_EN_SHIFT) | ((!!rxEn) << SPI_FCTL_RF_DRQ_EN_SHIFT));
  258. }
  259. /*
  260. * @brief
  261. */
  262. __SPI_STATIC_INLINE__
  263. void SPI_SetTxFifoThreshold(SPI_T *spi, uint8_t threshold)
  264. {
  265. HAL_MODIFY_REG(spi->FCTL, SPI_FCTL_TX_TRIG_LEVEL_MASK, threshold << SPI_FCTL_TX_TRIG_LEVEL_SHIFT);
  266. }
  267. /*
  268. * @brief
  269. */
  270. __SPI_STATIC_INLINE__
  271. void SPI_SetRxFifoThreshold(SPI_T *spi, uint8_t threshold)
  272. {
  273. HAL_MODIFY_REG(spi->FCTL, SPI_FCTL_RX_TRIG_LEVEL_MASK, threshold << SPI_FCTL_RX_TRIG_LEVEL_SHIFT);
  274. }
  275. /*
  276. * @brief
  277. */
  278. __SPI_STATIC_INLINE__
  279. uint8_t SPI_GetTxFifoCounter(SPI_T *spi)
  280. {
  281. return (uint8_t)((spi->FST & SPI_FST_TF_CNT_MASK) >> SPI_FST_TF_CNT_SHIFT);
  282. }
  283. /*
  284. * @brief
  285. */
  286. __SPI_STATIC_INLINE__
  287. uint8_t SPI_GetRxFifoCounter(SPI_T *spi)
  288. {
  289. return (uint8_t)((spi->FST & SPI_FST_RF_CNT_MASK) >> SPI_FST_RF_CNT_SHIFT);
  290. }
  291. /*
  292. * @brief
  293. */
  294. __SPI_STATIC_INLINE__
  295. void SPI_EnableDualMode(SPI_T *spi)
  296. {
  297. HAL_SET_BIT(spi->BCC, SPI_BCC_DRM_MASK);
  298. }
  299. /*
  300. * @brief
  301. */
  302. __SPI_STATIC_INLINE__
  303. void SPI_DisableDualMode(SPI_T *spi)
  304. {
  305. HAL_CLR_BIT(spi->BCC, SPI_BCC_DRM_MASK);
  306. }
  307. /*
  308. * @brief
  309. */
  310. __SPI_STATIC_INLINE__
  311. void SPI_SetInterval(SPI_T *spi, uint16_t nSCLK)
  312. {
  313. HAL_MODIFY_REG(spi->WAIT, SPI_WAIT_WCC_MASK, nSCLK << SPI_WAIT_WCC_SHIFT);
  314. }
  315. /*
  316. * @brief
  317. */
  318. static void SPI_SetClkDiv(SPI_T *spi, uint16_t div)
  319. {
  320. uint8_t n = 0;
  321. if (div < 1)
  322. {
  323. return;
  324. }
  325. if (div > 2 * (0xFF + 1))
  326. {
  327. HAL_CLR_BIT(spi->CCTR, SPI_CCTR_DRS_MASK);
  328. do
  329. {
  330. div = (div == 1) ? 0 : ((div + 1) / 2);
  331. n++;
  332. }
  333. while (div);
  334. HAL_MODIFY_REG(spi->CCTR, SPI_CCTR_CDR1_MASK, (n & 0x0F) << SPI_CCTR_CDR1_SHIFT);
  335. }
  336. else
  337. {
  338. HAL_SET_BIT(spi->CCTR, SPI_CCTR_DRS_MASK);
  339. n = ((div + 1) / 2) - 1;
  340. HAL_MODIFY_REG(spi->CCTR, SPI_CCTR_CDR2_MASK, (n & 0xFF) << SPI_CCTR_CDR2_SHIFT);
  341. }
  342. }
  343. /*
  344. * @brief
  345. */
  346. __SPI_STATIC_INLINE__
  347. void SPI_SetDataSize(SPI_T *spi, rt_uint32_t data_size, rt_uint32_t dummy_size)
  348. {
  349. HAL_MODIFY_REG(spi->BC, SPI_BC_MBC_MASK, data_size + dummy_size);
  350. HAL_MODIFY_REG(spi->TC, SPI_TC_MWTC_MASK, data_size);
  351. HAL_MODIFY_REG(spi->BCC, SPI_BCC_STC_MASK, data_size);
  352. }
  353. /*
  354. * @brief
  355. */
  356. __SPI_STATIC_INLINE__
  357. void SPI_Write(SPI_T *spi, uint8_t *data)
  358. {
  359. HAL_REG_8BIT(&spi->TXD) = *data;
  360. }
  361. /*
  362. * @brief
  363. */
  364. __SPI_STATIC_INLINE__
  365. void SPI_Read(SPI_T *spi, uint8_t *data)
  366. {
  367. *data = HAL_REG_8BIT(&spi->RXD);
  368. }
  369. /*
  370. * @brief
  371. */
  372. __SPI_STATIC_INLINE__
  373. uint8_t *SPI_TxAddress(SPI_T *spi)
  374. {
  375. return (uint8_t *)&spi->TXD;
  376. }
  377. /*
  378. * @brief
  379. */
  380. __SPI_STATIC_INLINE__
  381. uint8_t *SPI_RxAddress(SPI_T *spi)
  382. {
  383. return (uint8_t *)&spi->RXD;
  384. }
  385. /* private rt-thread spi ops function */
  386. static rt_err_t configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  387. static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message);
  388. static struct rt_spi_ops tina_spi_ops =
  389. {
  390. configure,
  391. xfer
  392. };
  393. static rt_err_t configure(struct rt_spi_device *device,
  394. struct rt_spi_configuration *configuration)
  395. {
  396. struct rt_spi_bus *spi_bus = (struct rt_spi_bus *)device->bus;
  397. struct tina_spi_cs *tina_spi_cs = device->parent.user_data;
  398. struct tina_spi *_spi_info = (struct tina_spi *)spi_bus->parent.user_data;
  399. SPI_T *spi = _spi_info->spi;
  400. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  401. RT_ASSERT(device != RT_NULL);
  402. RT_ASSERT(configuration != RT_NULL);
  403. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  404. DEBUG_PRINTF("spi address: %08X\n", (rt_uint32_t)spi);
  405. SPI_Disable(spi);
  406. SPI_Reset(spi);
  407. SPI_ResetRxFifo(spi);
  408. SPI_ResetTxFifo(spi);
  409. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  410. /* data_width */
  411. if (configuration->data_width != 8)
  412. {
  413. DEBUG_PRINTF("error: data_width is %d\n", configuration->data_width);
  414. return RT_EIO;
  415. }
  416. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  417. SPI_SetDuplex(spi, SPI_TCTRL_DHB_FULL_DUPLEX);
  418. SPI_SetMode(spi, SPI_CTRL_MODE_MASTER);
  419. /* MSB or LSB */
  420. if (configuration->mode & RT_SPI_MSB)
  421. {
  422. SPI_SetFirstTransmitBit(spi, SPI_TCTRL_FBS_MSB);
  423. }
  424. else
  425. {
  426. SPI_SetFirstTransmitBit(spi, SPI_TCTRL_FBS_LSB);
  427. }
  428. switch (configuration->mode)
  429. {
  430. case RT_SPI_MODE_0:
  431. SPI_SetSclkMode(spi, SPI_SCLK_Mode0);
  432. break;
  433. case RT_SPI_MODE_1:
  434. SPI_SetSclkMode(spi, SPI_SCLK_Mode1);
  435. break;
  436. case RT_SPI_MODE_2:
  437. SPI_SetSclkMode(spi, SPI_SCLK_Mode2);
  438. break;
  439. case RT_SPI_MODE_3:
  440. SPI_SetSclkMode(spi, SPI_SCLK_Mode3);
  441. break;
  442. }
  443. /* baudrate */
  444. {
  445. unsigned int spi_clock = 0;
  446. rt_uint32_t max_hz;
  447. rt_uint32_t div;
  448. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  449. max_hz = configuration->max_hz;
  450. if (max_hz > SPI_BUS_MAX_CLK)
  451. {
  452. max_hz = SPI_BUS_MAX_CLK;
  453. }
  454. spi_clock = ahb_get_clk();
  455. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  456. div = (spi_clock + max_hz - 1) / max_hz;
  457. dbg_log(DBG_LOG, "configuration->max_hz: %d\n", configuration->max_hz);
  458. dbg_log(DBG_LOG, "max freq: %d\n", max_hz);
  459. dbg_log(DBG_LOG, "spi_clock: %d\n", spi_clock);
  460. dbg_log(DBG_LOG, "div: %d\n", div);
  461. SPI_SetClkDiv(spi, div / 2);
  462. } /* baudrate */
  463. SPI_ManualChipSelect(spi, tina_spi_cs->cs);
  464. SPI_SetDataSize(spi, 0, 0);
  465. SPI_Enable(spi);
  466. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  467. return RT_EOK;
  468. };
  469. static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  470. {
  471. struct rt_spi_bus *r6_spi_bus = (struct rt_spi_bus *)device->bus;
  472. struct tina_spi *_spi_info = (struct tina_spi *)r6_spi_bus->parent.user_data;
  473. SPI_T *spi = _spi_info->spi;
  474. struct rt_spi_configuration *config = &device->config;
  475. struct tina_spi_cs *tina_spi_cs = device->parent.user_data;
  476. RT_ASSERT(device != NULL);
  477. RT_ASSERT(message != NULL);
  478. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  479. DEBUG_PRINTF("spi_info: %08X\n", (rt_uint32_t)_spi_info);
  480. DEBUG_PRINTF("spi address: %08X\n", (rt_uint32_t)spi);
  481. /* take CS */
  482. if (message->cs_take)
  483. {
  484. SPI_ManualChipSelect(spi, tina_spi_cs->cs);
  485. SPI_SetCsLevel(spi, false);
  486. DEBUG_PRINTF("spi take cs\n");
  487. }
  488. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  489. {
  490. if ((config->data_width <= 8) && (message->length > 0))
  491. {
  492. const rt_uint8_t *send_ptr = message->send_buf;
  493. rt_uint8_t *recv_ptr = message->recv_buf;
  494. rt_uint32_t tx_size = message->length;
  495. rt_uint32_t rx_size = message->length;
  496. DEBUG_PRINTF("spi poll transfer start: %d\n", tx_size);
  497. SPI_ResetTxFifo(spi);
  498. SPI_ResetRxFifo(spi);
  499. SPI_SetDataSize(spi, tx_size, 0);
  500. SPI_StartTransmit(spi);
  501. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  502. while (tx_size > 0 || rx_size > 0)
  503. {
  504. uint8_t tx_data = 0xFF;
  505. uint8_t rx_data = 0xFF;
  506. while ((SPI_GetTxFifoCounter(spi) < SPI_FIFO_SIZE) && (tx_size > 0))
  507. {
  508. if (send_ptr != RT_NULL)
  509. {
  510. tx_data = *send_ptr++;
  511. }
  512. SPI_Write(spi, &tx_data);
  513. tx_size--;
  514. }
  515. while (SPI_GetRxFifoCounter(spi) > 0)
  516. {
  517. rx_size--;
  518. SPI_Read(spi, &rx_data);
  519. if (recv_ptr != RT_NULL)
  520. {
  521. *recv_ptr++ = rx_data;
  522. }
  523. }
  524. }
  525. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  526. if ((tx_size != 0) || (rx_size != 0))
  527. {
  528. DEBUG_PRINTF("spi_tx_rx error with tx count = %d, rx count = %d.\n", tx_size, rx_size);
  529. return 0;
  530. }
  531. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  532. while (SPI_IntState(spi, SPI_INT_TRANSFER_COMPLETE) == 0);
  533. SPI_ClearInt(spi, SPI_INT_TRANSFER_COMPLETE);
  534. DEBUG_PRINTF("spi poll transfer finsh\n");
  535. }
  536. else if (config->data_width > 8)
  537. {
  538. DEBUG_PRINTF("data width: %d\n", config->data_width);
  539. RT_ASSERT(NULL);
  540. }
  541. }
  542. /* release CS */
  543. if (message->cs_release)
  544. {
  545. SPI_SetCsLevel(spi, true);
  546. DEBUG_PRINTF("spi release cs\n");
  547. }
  548. return message->length;
  549. };
  550. #ifdef TINA_USING_SPI0
  551. static struct rt_spi_bus spi_bus0;
  552. #endif
  553. #ifdef TINA_USING_SPI1
  554. static struct rt_spi_bus spi_bus1;
  555. #endif
  556. static const struct tina_spi spis[] =
  557. {
  558. #ifdef TINA_USING_SPI0
  559. {(SPI_T *)SPI0_BASE_ADDR, SPI0_GATING, &spi_bus0},
  560. #endif
  561. #ifdef TINA_USING_SPI1
  562. {(SPI_T *)SPI1_BASE_ADDR, SPI1_GATING, &spi_bus1},
  563. #endif
  564. };
  565. /** \brief init and register r6 spi bus.
  566. *
  567. * \param SPI: R6 SPI, e.g: SPI1,SPI2,SPI3.
  568. * \param spi_bus_name: spi bus name, e.g: "spi1"
  569. * \return
  570. *
  571. */
  572. rt_err_t tina_spi_bus_register(SPI_T *spi, const char *spi_bus_name)
  573. {
  574. int i;
  575. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  576. RT_ASSERT(spi_bus_name != RT_NULL);
  577. for (i = 0; i < ARR_LEN(spis); i++)
  578. {
  579. if (spi == spis[i].spi)
  580. {
  581. bus_software_reset_disalbe(spis[i].spi_gate);
  582. bus_gate_clk_enalbe(spis[i].spi_gate);
  583. spis[i].spi_bus->parent.user_data = (void *)&spis[i];
  584. DEBUG_PRINTF("bus addr: %08X\n", (rt_uint32_t)spis[i].spi_bus);
  585. DEBUG_PRINTF("user_data: %08X\n", (rt_uint32_t)spis[i].spi_bus->parent.user_data);
  586. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  587. rt_spi_bus_register(spis[i].spi_bus, spi_bus_name, &tina_spi_ops);
  588. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  589. return RT_EOK;
  590. }
  591. }
  592. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  593. return RT_ERROR;
  594. }
  595. int rt_hw_spi_init(void)
  596. {
  597. DEBUG_PRINTF("register spi bus\n");
  598. #ifdef TINA_USING_SPI0
  599. /* register spi bus */
  600. {
  601. rt_err_t result;
  602. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  603. gpio_set_func(GPIO_PORT_C, GPIO_PIN_0, IO_FUN_1);
  604. gpio_set_func(GPIO_PORT_C, GPIO_PIN_2, IO_FUN_1);
  605. gpio_set_func(GPIO_PORT_C, GPIO_PIN_3, IO_FUN_1);
  606. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  607. result = tina_spi_bus_register((SPI_T *)SPI0_BASE_ADDR, "spi0");
  608. if (result != RT_EOK)
  609. {
  610. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  611. return result;
  612. }
  613. }
  614. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  615. /* attach cs */
  616. {
  617. static struct rt_spi_device spi_device;
  618. static struct tina_spi_cs spi_cs;
  619. rt_err_t result;
  620. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  621. spi_cs.cs = SPI_TCTRL_SS_SEL_SS0;
  622. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  623. gpio_set_func(GPIO_PORT_C, GPIO_PIN_1, IO_FUN_1);
  624. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  625. result = rt_spi_bus_attach_device(&spi_device, "spi00", "spi0", (void *)&spi_cs);
  626. if (result != RT_EOK)
  627. {
  628. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  629. return result;
  630. }
  631. }
  632. DEBUG_PRINTF("%s -> %d\n", __FUNCTION__, __LINE__);
  633. #endif
  634. #ifdef TINA_USING_SPI1
  635. /* register spi bus */
  636. {
  637. rt_err_t result;
  638. gpio_set_func(GPIO_PORT_A, GPIO_PIN_1, IO_FUN_5);
  639. gpio_set_func(GPIO_PORT_A, GPIO_PIN_2, IO_FUN_5);
  640. gpio_set_func(GPIO_PORT_A, GPIO_PIN_3, IO_FUN_5);
  641. result = tina_spi_bus_register((SPI_T *)SPI1_BASE_ADDR, "spi1");
  642. if (result != RT_EOK)
  643. {
  644. DEBUG_PRINTF("register spi bus faild: %d\n", result);
  645. return result;
  646. }
  647. }
  648. DEBUG_PRINTF("attach cs\n");
  649. /* attach cs */
  650. {
  651. static struct rt_spi_device spi_device;
  652. static struct tina_spi_cs spi_cs;
  653. rt_err_t result;
  654. spi_cs.cs = SPI_TCTRL_SS_SEL_SS0;
  655. gpio_set_func(GPIO_PORT_A, GPIO_PIN_0, IO_FUN_5);
  656. result = rt_spi_bus_attach_device(&spi_device, "spi10", "spi1", (void *)&spi_cs);
  657. if (result != RT_EOK)
  658. {
  659. DEBUG_PRINTF("attach cs faild: %d\n", result);
  660. return result;
  661. }
  662. }
  663. #endif
  664. return RT_EOK;
  665. }
  666. INIT_BOARD_EXPORT(rt_hw_spi_init);
  667. #endif