drv_spi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2022-3-15 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(BSP_USING_SPI)
  14. #define LOG_TAG "drv.spi"
  15. #define DBG_ENABLE
  16. #define DBG_SECTION_NAME LOG_TAG
  17. #define DBG_LEVEL DBG_INFO
  18. #define DBG_COLOR
  19. #include <rtdbg.h>
  20. #include <rthw.h>
  21. #include <rtdevice.h>
  22. #include <rtdef.h>
  23. #include <drv_spi.h>
  24. /* Private define ---------------------------------------------------------------*/
  25. #ifndef NU_SPI_USE_PDMA_MIN_THRESHOLD
  26. #define NU_SPI_USE_PDMA_MIN_THRESHOLD (128)
  27. #endif
  28. enum
  29. {
  30. SPI_START = -1,
  31. #if defined(BSP_USING_SPI0)
  32. SPI0_IDX,
  33. #endif
  34. #if defined(BSP_USING_SPI1)
  35. SPI1_IDX,
  36. #endif
  37. #if defined(BSP_USING_SPI2)
  38. SPI2_IDX,
  39. #endif
  40. #if defined(BSP_USING_SPI3)
  41. SPI3_IDX,
  42. #endif
  43. #if defined(BSP_USING_SPI4)
  44. SPI4_IDX,
  45. #endif
  46. #if defined(BSP_USING_SPI5)
  47. SPI5_IDX,
  48. #endif
  49. #if defined(BSP_USING_SPI6)
  50. SPI6_IDX,
  51. #endif
  52. #if defined(BSP_USING_SPI7)
  53. SPI7_IDX,
  54. #endif
  55. #if defined(BSP_USING_SPI8)
  56. SPI8_IDX,
  57. #endif
  58. #if defined(BSP_USING_SPI9)
  59. SPI9_IDX,
  60. #endif
  61. #if defined(BSP_USING_SPI10)
  62. SPI10_IDX,
  63. #endif
  64. SPI_CNT
  65. };
  66. /* Private typedef --------------------------------------------------------------*/
  67. /* Private functions ------------------------------------------------------------*/
  68. static void nu_spi_transmission_with_poll(struct nu_spi *spi_bus,
  69. uint8_t *send_addr, uint8_t *recv_addr, int length, uint8_t bytes_per_word);
  70. static int nu_spi_register_bus(struct nu_spi *spi_bus, const char *name);
  71. static rt_ssize_t nu_spi_bus_xfer(struct rt_spi_device *device, struct rt_spi_message *message);
  72. static rt_err_t nu_spi_bus_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  73. #if defined(BSP_USING_SPI_PDMA)
  74. static void nu_pdma_spi_rx_cb_event(void *pvUserData, uint32_t u32EventFilter);
  75. static rt_err_t nu_pdma_spi_rx_config(struct nu_spi *spi_bus, uint8_t *pu8Buf, int32_t i32RcvLen, uint8_t bytes_per_word);
  76. static rt_err_t nu_pdma_spi_tx_config(struct nu_spi *spi_bus, const uint8_t *pu8Buf, int32_t i32SndLen, uint8_t bytes_per_word);
  77. static rt_ssize_t nu_spi_pdma_transmit(struct nu_spi *spi_bus, const uint8_t *send_addr, uint8_t *recv_addr, int length, uint8_t bytes_per_word);
  78. #endif
  79. /* Public functions -------------------------------------------------------------*/
  80. void nu_spi_transfer(struct nu_spi *spi_bus, uint8_t *tx, uint8_t *rx, int length, uint8_t bytes_per_word);
  81. void nu_spi_drain_rxfifo(SPI_T *spi_base);
  82. /* Private variables ------------------------------------------------------------*/
  83. static struct rt_spi_ops nu_spi_poll_ops =
  84. {
  85. .configure = nu_spi_bus_configure,
  86. .xfer = nu_spi_bus_xfer,
  87. };
  88. static struct nu_spi nu_spi_arr [] =
  89. {
  90. #if defined(BSP_USING_SPI0)
  91. {
  92. .name = "spi0",
  93. .spi_base = SPI0,
  94. .rstidx = SPI0_RST,
  95. #if defined(BSP_USING_SPI_PDMA)
  96. #if defined(BSP_USING_SPI0_PDMA)
  97. .pdma_perp_tx = PDMA_SPI0_TX,
  98. .pdma_perp_rx = PDMA_SPI0_RX,
  99. #else
  100. .pdma_perp_tx = NU_PDMA_UNUSED,
  101. .pdma_perp_rx = NU_PDMA_UNUSED,
  102. #endif
  103. #endif
  104. },
  105. #endif
  106. #if defined(BSP_USING_SPI1)
  107. {
  108. .name = "spi1",
  109. .spi_base = SPI1,
  110. .rstidx = SPI1_RST,
  111. #if defined(BSP_USING_SPI_PDMA)
  112. #if defined(BSP_USING_SPI1_PDMA)
  113. .pdma_perp_tx = PDMA_SPI1_TX,
  114. .pdma_perp_rx = PDMA_SPI1_RX,
  115. #else
  116. .pdma_perp_tx = NU_PDMA_UNUSED,
  117. .pdma_perp_rx = NU_PDMA_UNUSED,
  118. #endif
  119. #endif
  120. },
  121. #endif
  122. #if defined(BSP_USING_SPI2)
  123. {
  124. .name = "spi2",
  125. .spi_base = SPI2,
  126. .rstidx = SPI2_RST,
  127. #if defined(BSP_USING_SPI_PDMA)
  128. #if defined(BSP_USING_SPI2_PDMA)
  129. .pdma_perp_tx = PDMA_SPI2_TX,
  130. .pdma_perp_rx = PDMA_SPI2_RX,
  131. #else
  132. .pdma_perp_tx = NU_PDMA_UNUSED,
  133. .pdma_perp_rx = NU_PDMA_UNUSED,
  134. #endif
  135. #endif
  136. },
  137. #endif
  138. #if defined(BSP_USING_SPI3)
  139. {
  140. .name = "spi3",
  141. .spi_base = SPI3,
  142. .rstidx = SPI3_RST,
  143. #if defined(BSP_USING_SPI_PDMA)
  144. #if defined(BSP_USING_SPI3_PDMA)
  145. .pdma_perp_tx = PDMA_SPI3_TX,
  146. .pdma_perp_rx = PDMA_SPI3_RX,
  147. #else
  148. .pdma_perp_tx = NU_PDMA_UNUSED,
  149. .pdma_perp_rx = NU_PDMA_UNUSED,
  150. #endif
  151. #endif
  152. },
  153. #endif
  154. #if defined(BSP_USING_SPI4)
  155. {
  156. .name = "spi4",
  157. .spi_base = SPI4,
  158. .rstidx = SPI4_RST,
  159. #if defined(BSP_USING_SPI_PDMA)
  160. #if defined(BSP_USING_SPI4_PDMA)
  161. .pdma_perp_tx = PDMA_SPI4_TX,
  162. .pdma_perp_rx = PDMA_SPI4_RX,
  163. #else
  164. .pdma_perp_tx = NU_PDMA_UNUSED,
  165. .pdma_perp_rx = NU_PDMA_UNUSED,
  166. #endif
  167. #endif
  168. },
  169. #endif
  170. #if defined(BSP_USING_SPI5)
  171. {
  172. .name = "spi5",
  173. .spi_base = SPI5,
  174. .rstidx = SPI5_RST,
  175. #if defined(BSP_USING_SPI_PDMA)
  176. #if defined(BSP_USING_SPI5_PDMA)
  177. .pdma_perp_tx = PDMA_SPI5_TX,
  178. .pdma_perp_rx = PDMA_SPI5_RX,
  179. #else
  180. .pdma_perp_tx = NU_PDMA_UNUSED,
  181. .pdma_perp_rx = NU_PDMA_UNUSED,
  182. #endif
  183. #endif
  184. },
  185. #endif
  186. #if defined(BSP_USING_SPI6)
  187. {
  188. .name = "spi6",
  189. .spi_base = SPI6,
  190. .rstidx = SPI6_RST,
  191. #if defined(BSP_USING_SPI_PDMA)
  192. #if defined(BSP_USING_SPI6_PDMA)
  193. .pdma_perp_tx = PDMA_SPI6_TX,
  194. .pdma_perp_rx = PDMA_SPI6_RX,
  195. #else
  196. .pdma_perp_tx = NU_PDMA_UNUSED,
  197. .pdma_perp_rx = NU_PDMA_UNUSED,
  198. #endif
  199. #endif
  200. },
  201. #endif
  202. #if defined(BSP_USING_SPI7)
  203. {
  204. .name = "spi7",
  205. .spi_base = SPI7,
  206. .rstidx = SPI7_RST,
  207. #if defined(BSP_USING_SPI_PDMA)
  208. #if defined(BSP_USING_SPI7_PDMA)
  209. .pdma_perp_tx = PDMA_SPI7_TX,
  210. .pdma_perp_rx = PDMA_SPI7_RX,
  211. #else
  212. .pdma_perp_tx = NU_PDMA_UNUSED,
  213. .pdma_perp_rx = NU_PDMA_UNUSED,
  214. #endif
  215. #endif
  216. },
  217. #endif
  218. #if defined(BSP_USING_SPI8)
  219. {
  220. .name = "spi8",
  221. .spi_base = SPI8,
  222. .rstidx = SPI8_RST,
  223. #if defined(BSP_USING_SPI_PDMA)
  224. #if defined(BSP_USING_SPI8_PDMA)
  225. .pdma_perp_tx = PDMA_SPI8_TX,
  226. .pdma_perp_rx = PDMA_SPI8_RX,
  227. #else
  228. .pdma_perp_tx = NU_PDMA_UNUSED,
  229. .pdma_perp_rx = NU_PDMA_UNUSED,
  230. #endif
  231. #endif
  232. },
  233. #endif
  234. #if defined(BSP_USING_SPI9)
  235. {
  236. .name = "spi9",
  237. .spi_base = SPI9,
  238. .rstidx = SPI9_RST,
  239. #if defined(BSP_USING_SPI_PDMA)
  240. #if defined(BSP_USING_SPI9_PDMA)
  241. .pdma_perp_tx = PDMA_SPI9_TX,
  242. .pdma_perp_rx = PDMA_SPI9_RX,
  243. #else
  244. .pdma_perp_tx = NU_PDMA_UNUSED,
  245. .pdma_perp_rx = NU_PDMA_UNUSED,
  246. #endif
  247. #endif
  248. },
  249. #endif
  250. #if defined(BSP_USING_SPI10)
  251. {
  252. .name = "spi10",
  253. .spi_base = SPI10,
  254. .rstidx = SPI10_RST,
  255. #if defined(BSP_USING_SPI_PDMA)
  256. #if defined(BSP_USING_SPI10_PDMA)
  257. .pdma_perp_tx = PDMA_SPI10_TX,
  258. .pdma_perp_rx = PDMA_SPI10_RX,
  259. #else
  260. .pdma_perp_tx = NU_PDMA_UNUSED,
  261. .pdma_perp_rx = NU_PDMA_UNUSED,
  262. #endif
  263. #endif
  264. },
  265. #endif
  266. }; /* spi nu_spi */
  267. static rt_err_t nu_spi_bus_configure(struct rt_spi_device *device,
  268. struct rt_spi_configuration *configuration)
  269. {
  270. struct nu_spi *spi_bus;
  271. uint32_t u32SPIMode;
  272. uint32_t u32BusClock;
  273. rt_err_t ret = RT_EOK;
  274. void *pvUserData;
  275. RT_ASSERT(device);
  276. RT_ASSERT(configuration);
  277. spi_bus = (struct nu_spi *) device->bus;
  278. pvUserData = device->parent.user_data;
  279. /* Check mode */
  280. switch (configuration->mode & RT_SPI_MODE_3)
  281. {
  282. case RT_SPI_MODE_0:
  283. u32SPIMode = SPI_MODE_0;
  284. break;
  285. case RT_SPI_MODE_1:
  286. u32SPIMode = SPI_MODE_1;
  287. break;
  288. case RT_SPI_MODE_2:
  289. u32SPIMode = SPI_MODE_2;
  290. break;
  291. case RT_SPI_MODE_3:
  292. u32SPIMode = SPI_MODE_3;
  293. break;
  294. default:
  295. ret = -RT_EIO;
  296. goto exit_nu_spi_bus_configure;
  297. }
  298. /* Check data width */
  299. if (!(configuration->data_width == 8 ||
  300. configuration->data_width == 16 ||
  301. configuration->data_width == 24 ||
  302. configuration->data_width == 32))
  303. {
  304. ret = -RT_EINVAL;
  305. goto exit_nu_spi_bus_configure;
  306. }
  307. /* Try to set clock and get actual spi bus clock */
  308. u32BusClock = SPI_SetBusClock(spi_bus->spi_base, configuration->max_hz);
  309. if (configuration->max_hz > u32BusClock)
  310. {
  311. LOG_W("%s clock max frequency is %dHz (!= %dHz)\n", spi_bus->name, u32BusClock, configuration->max_hz);
  312. configuration->max_hz = u32BusClock;
  313. }
  314. /* Need to initialize new configuration? */
  315. if (rt_memcmp(configuration, &spi_bus->configuration, sizeof(*configuration)) != 0)
  316. {
  317. rt_memcpy(&spi_bus->configuration, configuration, sizeof(*configuration));
  318. SPI_Open(spi_bus->spi_base, SPI_MASTER, u32SPIMode, configuration->data_width, u32BusClock);
  319. if (configuration->mode & RT_SPI_CS_HIGH)
  320. {
  321. /* Set CS pin to LOW */
  322. if (pvUserData != RT_NULL)
  323. {
  324. // set to LOW */
  325. rt_pin_write(*((rt_base_t *)pvUserData), PIN_LOW);
  326. }
  327. else
  328. {
  329. SPI_SET_SS_LOW(spi_bus->spi_base);
  330. }
  331. }
  332. else
  333. {
  334. /* Set CS pin to HIGH */
  335. if (pvUserData != RT_NULL)
  336. {
  337. // set to HIGH */
  338. rt_pin_write(*((rt_base_t *)pvUserData), PIN_HIGH);
  339. }
  340. else
  341. {
  342. /* Set CS pin to HIGH */
  343. SPI_SET_SS_HIGH(spi_bus->spi_base);
  344. }
  345. }
  346. if (configuration->mode & RT_SPI_MSB)
  347. {
  348. /* Set sequence to MSB first */
  349. SPI_SET_MSB_FIRST(spi_bus->spi_base);
  350. }
  351. else
  352. {
  353. /* Set sequence to LSB first */
  354. SPI_SET_LSB_FIRST(spi_bus->spi_base);
  355. }
  356. }
  357. /* Clear SPI RX FIFO */
  358. nu_spi_drain_rxfifo(spi_bus->spi_base);
  359. exit_nu_spi_bus_configure:
  360. return -(ret);
  361. }
  362. #if defined(BSP_USING_SPI_PDMA)
  363. static void nu_pdma_spi_rx_cb_event(void *pvUserData, uint32_t u32EventFilter)
  364. {
  365. rt_err_t result;
  366. struct nu_spi *spi_bus = (struct nu_spi *)pvUserData;
  367. RT_ASSERT(spi_bus);
  368. result = rt_sem_release(spi_bus->m_psSemBus);
  369. RT_ASSERT(result == RT_EOK);
  370. }
  371. static void nu_pdma_spi_tx_cb_trigger(void *pvUserData, uint32_t u32UserData)
  372. {
  373. /* Get base address of spi register */
  374. SPI_T *spi_base = (SPI_T *)pvUserData;
  375. /* Trigger TX/RX PDMA transfer. */
  376. SPI_TRIGGER_TX_RX_PDMA(spi_base);
  377. }
  378. static void nu_pdma_spi_rx_cb_disable(void *pvUserData, uint32_t u32UserData)
  379. {
  380. /* Get base address of spi register */
  381. SPI_T *spi_base = (SPI_T *)pvUserData;
  382. /* Stop TX/RX DMA transfer. */
  383. SPI_DISABLE_TX_RX_PDMA(spi_base);
  384. }
  385. static rt_err_t nu_pdma_spi_rx_config(struct nu_spi *spi_bus, uint8_t *pu8Buf, int32_t i32RcvLen, uint8_t bytes_per_word)
  386. {
  387. struct nu_pdma_chn_cb sChnCB;
  388. rt_err_t result;
  389. rt_uint8_t *dst_addr = NULL;
  390. nu_pdma_memctrl_t memctrl = eMemCtl_Undefined;
  391. /* Get base address of spi register */
  392. SPI_T *spi_base = spi_bus->spi_base;
  393. rt_uint8_t spi_pdma_rx_chid = spi_bus->pdma_chanid_rx;
  394. nu_pdma_filtering_set(spi_pdma_rx_chid, NU_PDMA_EVENT_TRANSFER_DONE);
  395. /* Register ISR callback function */
  396. sChnCB.m_eCBType = eCBType_Event;
  397. sChnCB.m_pfnCBHandler = nu_pdma_spi_rx_cb_event;
  398. sChnCB.m_pvUserData = (void *)spi_bus;
  399. result = nu_pdma_callback_register(spi_pdma_rx_chid, &sChnCB);
  400. if (result != RT_EOK)
  401. {
  402. goto exit_nu_pdma_spi_rx_config;
  403. }
  404. /* Register Disable engine dma trigger callback function */
  405. sChnCB.m_eCBType = eCBType_Disable;
  406. sChnCB.m_pfnCBHandler = nu_pdma_spi_rx_cb_disable;
  407. sChnCB.m_pvUserData = (void *)spi_base;
  408. result = nu_pdma_callback_register(spi_pdma_rx_chid, &sChnCB);
  409. if (result != RT_EOK)
  410. {
  411. goto exit_nu_pdma_spi_rx_config;
  412. }
  413. if (pu8Buf == RT_NULL)
  414. {
  415. memctrl = eMemCtl_SrcFix_DstFix;
  416. dst_addr = (rt_uint8_t *) &spi_bus->dummy;
  417. }
  418. else
  419. {
  420. memctrl = eMemCtl_SrcFix_DstInc;
  421. dst_addr = pu8Buf;
  422. }
  423. result = nu_pdma_channel_memctrl_set(spi_pdma_rx_chid, memctrl);
  424. if (result != RT_EOK)
  425. {
  426. goto exit_nu_pdma_spi_rx_config;
  427. }
  428. result = nu_pdma_transfer(spi_pdma_rx_chid,
  429. bytes_per_word * 8,
  430. (uint32_t)&spi_base->RX,
  431. (uint32_t)dst_addr,
  432. i32RcvLen / bytes_per_word,
  433. 0);
  434. exit_nu_pdma_spi_rx_config:
  435. return result;
  436. }
  437. static rt_err_t nu_pdma_spi_tx_config(struct nu_spi *spi_bus, const uint8_t *pu8Buf, int32_t i32SndLen, uint8_t bytes_per_word)
  438. {
  439. struct nu_pdma_chn_cb sChnCB;
  440. rt_err_t result;
  441. rt_uint8_t *src_addr = NULL;
  442. nu_pdma_memctrl_t memctrl = eMemCtl_Undefined;
  443. /* Get base address of spi register */
  444. SPI_T *spi_base = spi_bus->spi_base;
  445. rt_uint8_t spi_pdma_tx_chid = spi_bus->pdma_chanid_tx;
  446. if (pu8Buf == RT_NULL)
  447. {
  448. spi_bus->dummy = 0;
  449. memctrl = eMemCtl_SrcFix_DstFix;
  450. src_addr = (rt_uint8_t *)&spi_bus->dummy;
  451. }
  452. else
  453. {
  454. memctrl = eMemCtl_SrcInc_DstFix;
  455. src_addr = (rt_uint8_t *)pu8Buf;
  456. }
  457. /* Register Disable engine dma trigger callback function */
  458. sChnCB.m_eCBType = eCBType_Trigger;
  459. sChnCB.m_pfnCBHandler = nu_pdma_spi_tx_cb_trigger;
  460. sChnCB.m_pvUserData = (void *)spi_base;
  461. result = nu_pdma_callback_register(spi_pdma_tx_chid, &sChnCB);
  462. if (result != RT_EOK)
  463. {
  464. goto exit_nu_pdma_spi_tx_config;
  465. }
  466. result = nu_pdma_channel_memctrl_set(spi_pdma_tx_chid, memctrl);
  467. if (result != RT_EOK)
  468. {
  469. goto exit_nu_pdma_spi_tx_config;
  470. }
  471. result = nu_pdma_transfer(spi_pdma_tx_chid,
  472. bytes_per_word * 8,
  473. (uint32_t)src_addr,
  474. (uint32_t)&spi_base->TX,
  475. i32SndLen / bytes_per_word,
  476. 0);
  477. exit_nu_pdma_spi_tx_config:
  478. return result;
  479. }
  480. /**
  481. * SPI PDMA transfer
  482. */
  483. static rt_ssize_t nu_spi_pdma_transmit(struct nu_spi *spi_bus, const uint8_t *send_addr, uint8_t *recv_addr, int length, uint8_t bytes_per_word)
  484. {
  485. rt_err_t result = RT_EOK;
  486. result = nu_pdma_spi_rx_config(spi_bus, recv_addr, length, bytes_per_word);
  487. RT_ASSERT(result == RT_EOK);
  488. result = nu_pdma_spi_tx_config(spi_bus, send_addr, length, bytes_per_word);
  489. RT_ASSERT(result == RT_EOK);
  490. /* Wait RX-PDMA transfer done */
  491. result = rt_sem_take(spi_bus->m_psSemBus, RT_WAITING_FOREVER);
  492. RT_ASSERT(result == RT_EOK);
  493. return length;
  494. }
  495. rt_err_t nu_hw_spi_pdma_allocate(struct nu_spi *spi_bus)
  496. {
  497. /* Allocate SPI_TX nu_dma channel */
  498. if ((spi_bus->pdma_chanid_tx = nu_pdma_channel_allocate(spi_bus->pdma_perp_tx)) < 0)
  499. {
  500. goto exit_nu_hw_spi_pdma_allocate;
  501. }
  502. /* Allocate SPI_RX nu_dma channel */
  503. else if ((spi_bus->pdma_chanid_rx = nu_pdma_channel_allocate(spi_bus->pdma_perp_rx)) < 0)
  504. {
  505. nu_pdma_channel_free(spi_bus->pdma_chanid_tx);
  506. goto exit_nu_hw_spi_pdma_allocate;
  507. }
  508. spi_bus->m_psSemBus = rt_sem_create("spibus_sem", 0, RT_IPC_FLAG_FIFO);
  509. RT_ASSERT(spi_bus->m_psSemBus != RT_NULL);
  510. return RT_EOK;
  511. exit_nu_hw_spi_pdma_allocate:
  512. return -(RT_ERROR);
  513. }
  514. #endif /* #if defined(BSP_USING_SPI_PDMA) */
  515. void nu_spi_drain_rxfifo(SPI_T *spi_base)
  516. {
  517. while (SPI_IS_BUSY(spi_base));
  518. // Drain SPI RX FIFO, make sure RX FIFO is empty
  519. while (!SPI_GET_RX_FIFO_EMPTY_FLAG(spi_base))
  520. {
  521. SPI_ClearRxFIFO(spi_base);
  522. }
  523. }
  524. static int nu_spi_read(SPI_T *spi_base, uint8_t *recv_addr, uint8_t bytes_per_word)
  525. {
  526. int size = 0;
  527. // Read RX data
  528. if (!SPI_GET_RX_FIFO_EMPTY_FLAG(spi_base))
  529. {
  530. uint32_t val;
  531. // Read data from SPI RX FIFO
  532. switch (bytes_per_word)
  533. {
  534. case 4:
  535. val = SPI_READ_RX(spi_base);
  536. nu_set32_le(recv_addr, val);
  537. break;
  538. case 3:
  539. val = SPI_READ_RX(spi_base);
  540. nu_set24_le(recv_addr, val);
  541. break;
  542. case 2:
  543. val = SPI_READ_RX(spi_base);
  544. nu_set16_le(recv_addr, val);
  545. break;
  546. case 1:
  547. *recv_addr = SPI_READ_RX(spi_base);
  548. break;
  549. default:
  550. LOG_E("Data length is not supported.\n");
  551. break;
  552. }
  553. size = bytes_per_word;
  554. }
  555. return size;
  556. }
  557. static int nu_spi_write(SPI_T *spi_base, const uint8_t *send_addr, uint8_t bytes_per_word)
  558. {
  559. // Wait SPI TX send data
  560. while (SPI_GET_TX_FIFO_FULL_FLAG(spi_base));
  561. // Input data to SPI TX
  562. switch (bytes_per_word)
  563. {
  564. case 4:
  565. SPI_WRITE_TX(spi_base, nu_get32_le(send_addr));
  566. break;
  567. case 3:
  568. SPI_WRITE_TX(spi_base, nu_get24_le(send_addr));
  569. break;
  570. case 2:
  571. SPI_WRITE_TX(spi_base, nu_get16_le(send_addr));
  572. break;
  573. case 1:
  574. SPI_WRITE_TX(spi_base, *((uint8_t *)send_addr));
  575. break;
  576. default:
  577. LOG_E("Data length is not supported.\n");
  578. break;
  579. }
  580. return bytes_per_word;
  581. }
  582. /**
  583. * @brief SPI bus polling
  584. * @param dev : The pointer of the specified SPI module.
  585. * @param send_addr : Source address
  586. * @param recv_addr : Destination address
  587. * @param length : Data length
  588. */
  589. static void nu_spi_transmission_with_poll(struct nu_spi *spi_bus,
  590. uint8_t *send_addr, uint8_t *recv_addr, int length, uint8_t bytes_per_word)
  591. {
  592. SPI_T *spi_base = spi_bus->spi_base;
  593. // Write-only
  594. if ((send_addr != RT_NULL) && (recv_addr == RT_NULL))
  595. {
  596. while (length > 0)
  597. {
  598. send_addr += nu_spi_write(spi_base, send_addr, bytes_per_word);
  599. length -= bytes_per_word;
  600. }
  601. } // if (send_addr != RT_NULL && recv_addr == RT_NULL)
  602. // Read-only
  603. else if ((send_addr == RT_NULL) && (recv_addr != RT_NULL))
  604. {
  605. spi_bus->dummy = 0;
  606. while (length > 0)
  607. {
  608. /* Input data to SPI TX FIFO */
  609. length -= nu_spi_write(spi_base, (const uint8_t *)&spi_bus->dummy, bytes_per_word);
  610. /* Read data from RX FIFO */
  611. recv_addr += nu_spi_read(spi_base, recv_addr, bytes_per_word);
  612. }
  613. } // else if (send_addr == RT_NULL && recv_addr != RT_NULL)
  614. // Read&Write
  615. else
  616. {
  617. while (length > 0)
  618. {
  619. /* Input data to SPI TX FIFO */
  620. send_addr += nu_spi_write(spi_base, send_addr, bytes_per_word);
  621. length -= bytes_per_word;
  622. /* Read data from RX FIFO */
  623. recv_addr += nu_spi_read(spi_base, recv_addr, bytes_per_word);
  624. }
  625. } // else
  626. /* Wait RX or drain RX-FIFO */
  627. if (recv_addr)
  628. {
  629. // Wait SPI transmission done
  630. while (SPI_IS_BUSY(spi_base))
  631. {
  632. while (!SPI_GET_RX_FIFO_EMPTY_FLAG(spi_base))
  633. {
  634. recv_addr += nu_spi_read(spi_base, recv_addr, bytes_per_word);
  635. }
  636. }
  637. while (!SPI_GET_RX_FIFO_EMPTY_FLAG(spi_base))
  638. {
  639. recv_addr += nu_spi_read(spi_base, recv_addr, bytes_per_word);
  640. }
  641. }
  642. else
  643. {
  644. /* Clear SPI RX FIFO */
  645. nu_spi_drain_rxfifo(spi_base);
  646. }
  647. }
  648. void nu_spi_transfer(struct nu_spi *spi_bus, uint8_t *tx, uint8_t *rx, int length, uint8_t bytes_per_word)
  649. {
  650. RT_ASSERT(spi_bus != RT_NULL);
  651. #if defined(BSP_USING_SPI_PDMA)
  652. /* DMA transfer constrains */
  653. if ((spi_bus->pdma_chanid_rx >= 0) &&
  654. !((uint32_t)tx % bytes_per_word) &&
  655. !((uint32_t)rx % bytes_per_word) &&
  656. (bytes_per_word != 3) &&
  657. (length >= NU_SPI_USE_PDMA_MIN_THRESHOLD))
  658. nu_spi_pdma_transmit(spi_bus, tx, rx, length, bytes_per_word);
  659. else
  660. nu_spi_transmission_with_poll(spi_bus, tx, rx, length, bytes_per_word);
  661. #else
  662. nu_spi_transmission_with_poll(spi_bus, tx, rx, length, bytes_per_word);
  663. #endif
  664. }
  665. static rt_ssize_t nu_spi_bus_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  666. {
  667. struct nu_spi *spi_bus;
  668. struct rt_spi_configuration *configuration;
  669. uint8_t bytes_per_word;
  670. void *pvUserData;
  671. RT_ASSERT(device != RT_NULL);
  672. RT_ASSERT(device->bus != RT_NULL);
  673. RT_ASSERT(message != RT_NULL);
  674. spi_bus = (struct nu_spi *) device->bus;
  675. configuration = (struct rt_spi_configuration *)&spi_bus->configuration;
  676. bytes_per_word = configuration->data_width / 8;
  677. pvUserData = device->parent.user_data;
  678. if ((message->length % bytes_per_word) != 0)
  679. {
  680. /* Say bye. */
  681. LOG_E("%s: error payload length(%d%%%d != 0).\n", spi_bus->name, message->length, bytes_per_word);
  682. return 0;
  683. }
  684. if (message->length > 0)
  685. {
  686. if (message->cs_take && !(configuration->mode & RT_SPI_NO_CS))
  687. {
  688. if (pvUserData != RT_NULL)
  689. {
  690. if (configuration->mode & RT_SPI_CS_HIGH)
  691. {
  692. // set to HIGH */
  693. rt_pin_write(*((rt_base_t *)pvUserData), PIN_HIGH);
  694. }
  695. else
  696. {
  697. // set to LOW */
  698. rt_pin_write(*((rt_base_t *)pvUserData), PIN_LOW);
  699. }
  700. }
  701. else
  702. {
  703. if (configuration->mode & RT_SPI_CS_HIGH)
  704. {
  705. SPI_SET_SS_HIGH(spi_bus->spi_base);
  706. }
  707. else
  708. {
  709. SPI_SET_SS_LOW(spi_bus->spi_base);
  710. }
  711. }
  712. }
  713. nu_spi_transfer(spi_bus, (uint8_t *)message->send_buf, (uint8_t *)message->recv_buf, message->length, bytes_per_word);
  714. if (message->cs_release && !(configuration->mode & RT_SPI_NO_CS))
  715. {
  716. if (pvUserData != RT_NULL)
  717. {
  718. if (configuration->mode & RT_SPI_CS_HIGH)
  719. {
  720. // set to LOW */
  721. rt_pin_write(*((rt_base_t *)pvUserData), PIN_LOW);
  722. }
  723. else
  724. {
  725. // set to HIGH */
  726. rt_pin_write(*((rt_base_t *)pvUserData), PIN_HIGH);
  727. }
  728. }
  729. else
  730. {
  731. if (configuration->mode & RT_SPI_CS_HIGH)
  732. {
  733. SPI_SET_SS_LOW(spi_bus->spi_base);
  734. }
  735. else
  736. {
  737. SPI_SET_SS_HIGH(spi_bus->spi_base);
  738. }
  739. }
  740. }
  741. }
  742. return message->length;
  743. }
  744. static int nu_spi_register_bus(struct nu_spi *spi_bus, const char *name)
  745. {
  746. return rt_spi_bus_register(&spi_bus->dev, name, &nu_spi_poll_ops);
  747. }
  748. /**
  749. * Hardware SPI Initial
  750. */
  751. static int rt_hw_spi_init(void)
  752. {
  753. int i;
  754. for (i = (SPI_START + 1); i < SPI_CNT; i++)
  755. {
  756. SYS_ResetModule(nu_spi_arr[i].rstidx);
  757. nu_spi_register_bus(&nu_spi_arr[i], nu_spi_arr[i].name);
  758. #if defined(BSP_USING_SPI_PDMA)
  759. nu_spi_arr[i].pdma_chanid_tx = -1;
  760. nu_spi_arr[i].pdma_chanid_rx = -1;
  761. if ((nu_spi_arr[i].pdma_perp_tx != NU_PDMA_UNUSED) && (nu_spi_arr[i].pdma_perp_rx != NU_PDMA_UNUSED))
  762. {
  763. if (nu_hw_spi_pdma_allocate(&nu_spi_arr[i]) != RT_EOK)
  764. {
  765. LOG_W("Failed to allocate DMA channels for %s. We will use poll-mode for this bus.\n", nu_spi_arr[i].name);
  766. }
  767. }
  768. #endif
  769. }
  770. return 0;
  771. }
  772. INIT_DEVICE_EXPORT(rt_hw_spi_init);
  773. #endif //#if defined(BSP_USING_SPI)