drv_spi.c 18 KB

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