drv_spi.c 18 KB

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