drv_spi.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-04-28 CDT First version
  9. * 2023-09-30 CDT Delete dma transmit interrupt
  10. * 2024-02-20 CDT Support HC32F448
  11. * 2024-04-16 CDT Support HC32F472
  12. * 2025-04-09 CDT Support HC32F4A8
  13. * 2025-07-18 CDT Support HC32F334
  14. */
  15. /*******************************************************************************
  16. * Include files
  17. ******************************************************************************/
  18. #include <rtthread.h>
  19. #include <rtdevice.h>
  20. #if defined(RT_USING_SPI)
  21. #if defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) || \
  22. defined(BSP_USING_SPI4) || defined(BSP_USING_SPI5) || defined(BSP_USING_SPI6)
  23. #include "drv_spi.h"
  24. #include "board_config.h"
  25. /*******************************************************************************
  26. * Local type definitions ('typedef')
  27. ******************************************************************************/
  28. /*******************************************************************************
  29. * Local pre-processor symbols/macros ('#define')
  30. ******************************************************************************/
  31. // #define DRV_DEBUG
  32. #define LOG_TAG "drv.spi"
  33. #include <drv_log.h>
  34. /* SPI max division */
  35. #if defined(HC32F4A0) || defined(HC32F460)
  36. #define SPI_MAX_DIV_VAL (0x7U) /* Div256 */
  37. #elif defined(HC32F448) || defined(HC32F472) || defined(HC32F4A8) || defined (HC32F334)
  38. #define SPI_MAX_DIV_VAL (0x39U)
  39. #endif
  40. #ifdef BSP_SPI_USING_DMA
  41. #define DMA_CH_REG(reg_base, ch) (*(__IO uint32_t *)((uint32_t)(&(reg_base)) + ((ch) * 0x40UL)))
  42. #endif
  43. /*******************************************************************************
  44. * Global variable definitions (declared in header file with 'extern')
  45. ******************************************************************************/
  46. extern rt_err_t rt_hw_spi_board_init(CM_SPI_TypeDef *CM_SPIx);
  47. /*******************************************************************************
  48. * Local function prototypes ('static')
  49. ******************************************************************************/
  50. /*******************************************************************************
  51. * Local variable definitions ('static')
  52. ******************************************************************************/
  53. enum
  54. {
  55. #ifdef BSP_USING_SPI1
  56. SPI1_INDEX,
  57. #endif
  58. #ifdef BSP_USING_SPI2
  59. SPI2_INDEX,
  60. #endif
  61. #ifdef BSP_USING_SPI3
  62. SPI3_INDEX,
  63. #endif
  64. #ifdef BSP_USING_SPI4
  65. SPI4_INDEX,
  66. #endif
  67. #ifdef BSP_USING_SPI5
  68. SPI5_INDEX,
  69. #endif
  70. #ifdef BSP_USING_SPI6
  71. SPI6_INDEX,
  72. #endif
  73. };
  74. static struct hc32_spi_config spi_config[] =
  75. {
  76. #ifdef BSP_USING_SPI1
  77. SPI1_BUS_CONFIG,
  78. #endif
  79. #ifdef BSP_USING_SPI2
  80. SPI2_BUS_CONFIG,
  81. #endif
  82. #ifdef BSP_USING_SPI3
  83. SPI3_BUS_CONFIG,
  84. #endif
  85. #ifdef BSP_USING_SPI4
  86. SPI4_BUS_CONFIG,
  87. #endif
  88. #ifdef BSP_USING_SPI5
  89. SPI5_BUS_CONFIG,
  90. #endif
  91. #ifdef BSP_USING_SPI6
  92. SPI6_BUS_CONFIG,
  93. #endif
  94. };
  95. static struct hc32_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])] = {0};
  96. /*******************************************************************************
  97. * Function implementation - global ('extern') and local ('static')
  98. ******************************************************************************/
  99. static rt_err_t hc32_spi_init(struct hc32_spi *spi_drv, struct rt_spi_configuration *cfg)
  100. {
  101. RT_ASSERT(spi_drv != RT_NULL);
  102. RT_ASSERT(cfg != RT_NULL);
  103. uint32_t u32Cnt = 0;
  104. uint32_t u32BusFreq;
  105. stc_spi_init_t stcSpiInit;
  106. CM_SPI_TypeDef *spi_instance = spi_drv->config->Instance;
  107. /* Enable spi clock */
  108. FCG_Fcg1PeriphClockCmd(spi_drv->config->clock, ENABLE);
  109. /* Init spi struct as default value */
  110. SPI_StructInit(&stcSpiInit);
  111. if ((cfg->mode & RT_SPI_SLAVE) &&
  112. ((RT_SPI_MODE_0 == (cfg->mode & RT_SPI_MODE_3)) || (RT_SPI_MODE_2 == (cfg->mode & RT_SPI_MODE_3))))
  113. {
  114. return -RT_EINVAL;
  115. }
  116. /* Slave or master mode */
  117. if (cfg->mode & RT_SPI_SLAVE)
  118. {
  119. stcSpiInit.u32MasterSlave = SPI_SLAVE;
  120. stcSpiInit.u32ModeFaultDetect = SPI_MD_FAULT_DETECT_ENABLE;
  121. }
  122. else
  123. {
  124. stcSpiInit.u32MasterSlave = SPI_MASTER;
  125. }
  126. /* SI/SO pin shared */
  127. if (cfg->mode & RT_SPI_3WIRE)
  128. {
  129. return -RT_EINVAL;
  130. }
  131. else
  132. {
  133. stcSpiInit.u32TransMode = SPI_FULL_DUPLEX;
  134. }
  135. /* clock phase & polarity */
  136. if (RT_SPI_MODE_3 == (cfg->mode & RT_SPI_MODE_3))
  137. {
  138. stcSpiInit.u32SpiMode = SPI_MD_3;
  139. }
  140. else if (RT_SPI_MODE_2 == (cfg->mode & RT_SPI_MODE_3))
  141. {
  142. stcSpiInit.u32SpiMode = SPI_MD_2;
  143. }
  144. else if (RT_SPI_MODE_1 == (cfg->mode & RT_SPI_MODE_3))
  145. {
  146. stcSpiInit.u32SpiMode = SPI_MD_1;
  147. }
  148. else
  149. {
  150. stcSpiInit.u32SpiMode = SPI_MD_0;
  151. }
  152. /* No chipselect */
  153. if (cfg->mode & RT_SPI_NO_CS)
  154. {
  155. stcSpiInit.u32WireMode = SPI_4_WIRE;
  156. }
  157. else
  158. {
  159. stcSpiInit.u32WireMode = SPI_3_WIRE;
  160. }
  161. /* LSB or MSB */
  162. if (cfg->mode & RT_SPI_MSB)
  163. {
  164. stcSpiInit.u32FirstBit = SPI_FIRST_MSB;
  165. }
  166. else
  167. {
  168. stcSpiInit.u32FirstBit = SPI_FIRST_LSB;
  169. }
  170. /* config data width 8,16,32 */
  171. if (8 == cfg->data_width)
  172. {
  173. stcSpiInit.u32DataBits = SPI_DATA_SIZE_8BIT;
  174. }
  175. else if (16 == cfg->data_width)
  176. {
  177. stcSpiInit.u32DataBits = SPI_DATA_SIZE_16BIT;
  178. }
  179. else if (32 == cfg->data_width)
  180. {
  181. stcSpiInit.u32DataBits = SPI_DATA_SIZE_32BIT;
  182. }
  183. else
  184. {
  185. return -RT_EIO;
  186. }
  187. /* Get BUS clock */
  188. u32BusFreq = CLK_GetBusClockFreq(CLK_BUS_PCLK1);
  189. while (cfg->max_hz < u32BusFreq / (1UL << (u32Cnt + 1U)))
  190. {
  191. u32Cnt++;
  192. if (u32Cnt >= SPI_MAX_DIV_VAL) /* Div256 */
  193. {
  194. break;
  195. }
  196. }
  197. #if defined(HC32F4A0) || defined(HC32F460)
  198. stcSpiInit.u32BaudRatePrescaler = (u32Cnt << SPI_CFG2_MBR_POS);
  199. #elif defined(HC32F448) || defined(HC32F472) || defined(HC32F4A8) || defined (HC32F334)
  200. if (u32Cnt <= 15U)
  201. {
  202. stcSpiInit.u32BaudRatePrescaler = (u32Cnt << SPI_CFG1_CLKDIV_POS);
  203. }
  204. else
  205. {
  206. stcSpiInit.u32BaudRatePrescaler = (((7U + ((u32Cnt - 15U) & 0x07U)) << SPI_CFG1_CLKDIV_POS) | ((1U + ((u32Cnt - 15U) >> 3U)) << SPI_CFG2_MBR_POS));
  207. }
  208. #endif
  209. /* slave limit */
  210. if ((cfg->mode & RT_SPI_SLAVE) && (stcSpiInit.u32BaudRatePrescaler < SPI_BR_CLK_DIV8))
  211. {
  212. stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV8;
  213. }
  214. LOG_D("Bus freq: %d, SPI freq: %d, BaudRatePrescaler: %d, u32Cnt: %d", u32BusFreq, cfg->max_hz, stcSpiInit.u32BaudRatePrescaler, u32Cnt);
  215. /* spi port init */
  216. rt_hw_spi_board_init(spi_instance);
  217. if (LL_OK != SPI_Init(spi_instance, &stcSpiInit))
  218. {
  219. return -RT_EIO;
  220. }
  221. #ifdef BSP_SPI_USING_DMA
  222. /* DMA configuration */
  223. if (spi_drv->spi_dma_flag & RT_DEVICE_FLAG_DMA_RX)
  224. {
  225. struct dma_config *spi_dma;
  226. stc_dma_init_t stcDmaInit;
  227. /* Get spi dma_rx */
  228. spi_dma = spi_drv->config->dma_rx;
  229. /* Enable Dma clock */
  230. FCG_Fcg0PeriphClockCmd(spi_dma->clock, ENABLE);
  231. AOS_SetTriggerEventSrc(spi_dma->trigger_select, spi_dma->trigger_event);
  232. /* Config Dma */
  233. DMA_StructInit(&stcDmaInit);
  234. stcDmaInit.u32BlockSize = 1UL;
  235. stcDmaInit.u32SrcAddr = (uint32_t)(&spi_instance->DR);
  236. stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_FIX;
  237. if (8 == cfg->data_width)
  238. {
  239. stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
  240. }
  241. else if (16 == cfg->data_width)
  242. {
  243. stcDmaInit.u32DataWidth = DMA_DATAWIDTH_16BIT;
  244. }
  245. else
  246. {
  247. stcDmaInit.u32DataWidth = DMA_DATAWIDTH_32BIT;
  248. }
  249. /* Init Dma */
  250. if (LL_OK != DMA_Init(spi_dma->Instance, spi_dma->channel, &stcDmaInit))
  251. {
  252. return -RT_EIO;
  253. }
  254. /* Enable Dma */
  255. DMA_Cmd(spi_dma->Instance, ENABLE);
  256. }
  257. if (spi_drv->spi_dma_flag & RT_DEVICE_FLAG_DMA_TX)
  258. {
  259. struct dma_config *spi_dma;
  260. stc_dma_init_t stcDmaInit;
  261. /* Get spi dma_tx */
  262. spi_dma = spi_drv->config->dma_tx;
  263. FCG_Fcg0PeriphClockCmd(spi_dma->clock, ENABLE);
  264. AOS_SetTriggerEventSrc(spi_dma->trigger_select, spi_dma->trigger_event);
  265. /* Config Dma */
  266. DMA_StructInit(&stcDmaInit);
  267. stcDmaInit.u32BlockSize = 1UL;
  268. stcDmaInit.u32DestAddr = (uint32_t)(&spi_instance->DR);;
  269. stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_FIX;
  270. if (8 == cfg->data_width)
  271. {
  272. stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
  273. }
  274. else if (16 == cfg->data_width)
  275. {
  276. stcDmaInit.u32DataWidth = DMA_DATAWIDTH_16BIT;
  277. }
  278. else
  279. {
  280. stcDmaInit.u32DataWidth = DMA_DATAWIDTH_32BIT;
  281. }
  282. /* Init Dma */
  283. if (LL_OK != DMA_Init(spi_dma->Instance, spi_dma->channel, &stcDmaInit))
  284. {
  285. return -RT_EIO;
  286. }
  287. /* Enable Dma */
  288. DMA_Cmd(spi_dma->Instance, ENABLE);
  289. }
  290. #endif
  291. /* Enable error interrupt */
  292. #if defined (HC32F448) || defined (HC32F472)
  293. INTC_IntSrcCmd(spi_drv->config->err_irq.irq_config.int_src, ENABLE);
  294. #endif
  295. NVIC_EnableIRQ(spi_drv->config->err_irq.irq_config.irq_num);
  296. SPI_IntCmd(spi_instance, SPI_INT_ERR, ENABLE);
  297. LOG_D("%s init done", spi_drv->config->bus_name);
  298. return RT_EOK;
  299. }
  300. static void hc32_spi_enable(CM_SPI_TypeDef *SPIx)
  301. {
  302. /* Check if the SPI is already enabled */
  303. #if defined (HC32F460) || defined (HC32F4A0)
  304. if ((SPIx->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  305. {
  306. SPI_Cmd(SPIx, ENABLE);
  307. }
  308. #elif defined (HC32F448) || defined (HC32F472) || defined (HC32F4A8) || defined (HC32F334)
  309. if ((SPIx->CR & SPI_CR_SPE) != SPI_CR_SPE)
  310. {
  311. SPI_Cmd(SPIx, ENABLE);
  312. }
  313. #else
  314. #error "Please select first the target HC32xxxx device used in your application."
  315. #endif
  316. }
  317. static void hc32_spi_set_trans_mode(CM_SPI_TypeDef *SPIx, uint32_t u32Mode)
  318. {
  319. #if defined (HC32F460) || defined (HC32F4A0)
  320. if (SPI_SEND_ONLY == u32Mode)
  321. {
  322. SET_REG32_BIT(SPIx->CR1, SPI_CR1_TXMDS);
  323. }
  324. else
  325. {
  326. CLR_REG32_BIT(SPIx->CR1, SPI_CR1_TXMDS);
  327. }
  328. #elif defined (HC32F448) || defined (HC32F472) || defined (HC32F4A8) || defined (HC32F334)
  329. if (SPI_SEND_ONLY == u32Mode)
  330. {
  331. SET_REG32_BIT(SPIx->CR, SPI_CR_TXMDS);
  332. }
  333. else
  334. {
  335. CLR_REG32_BIT(SPIx->CR, SPI_CR_TXMDS);
  336. }
  337. #else
  338. #error "Please select first the target HC32xxxx device used in your application."
  339. #endif
  340. }
  341. #ifdef BSP_SPI_USING_DMA
  342. static uint32_t hc32_spi_get_trans_mode(CM_SPI_TypeDef *SPIx)
  343. {
  344. #if defined (HC32F460) || defined (HC32F4A0)
  345. return READ_REG32_BIT(SPIx->CR1, SPI_CR1_TXMDS);
  346. #elif defined (HC32F448) || defined (HC32F472) || defined (HC32F4A8) || defined (HC32F334)
  347. return READ_REG32_BIT(SPIx->CR, SPI_CR_TXMDS);
  348. #else
  349. #error "Please select first the target HC32xxxx device used in your application."
  350. #endif
  351. }
  352. /**
  353. * @brief Config DMA source address increment mode.
  354. * @param [in] DMAx DMA unit instance.
  355. * @param [in] u8Ch DMA channel.
  356. * @param [in] u32IncMode DMA source address increment mode @ref DMA_SrcAddr_Incremented_Mode
  357. * @retval None
  358. */
  359. void DMA_SetSrcAddrIncMode(CM_DMA_TypeDef *DMAx, uint8_t u8Ch, uint32_t u32IncMode)
  360. {
  361. __IO uint32_t *CHCTLx;
  362. CHCTLx = &DMA_CH_REG(DMAx->CHCTL0, u8Ch);
  363. MODIFY_REG32(*CHCTLx, DMA_CHCTL_SINC, u32IncMode);
  364. }
  365. /**
  366. * @brief Config DMA destination address increment mode.
  367. * @param [in] DMAx DMA unit instance.
  368. * @param [in] u8Ch DMA channel.
  369. * @param [in] u16Count DMA destination address increment mode @ref DMA_DesAddr_Incremented_Mode
  370. * @retval None
  371. */
  372. void DMA_SetDestAddrIncMode(CM_DMA_TypeDef *DMAx, uint8_t u8Ch, uint32_t u32IncMode)
  373. {
  374. __IO uint32_t *CHCTLx;
  375. CHCTLx = &DMA_CH_REG(DMAx->CHCTL0, u8Ch);
  376. MODIFY_REG32(*CHCTLx, DMA_CHCTL_DINC, u32IncMode);
  377. }
  378. #endif
  379. static rt_err_t hc32_spi_configure(struct rt_spi_device *device,
  380. struct rt_spi_configuration *configuration)
  381. {
  382. RT_ASSERT(device != RT_NULL);
  383. RT_ASSERT(configuration != RT_NULL);
  384. struct hc32_spi *spi_drv = rt_container_of(device->bus, struct hc32_spi, spi_bus);
  385. spi_drv->cfg = configuration;
  386. return hc32_spi_init(spi_drv, configuration);
  387. }
  388. static int32_t hc32_spi_dma_trans(struct hc32_spi_config *spi_config, const uint8_t *pvTxBuf, void *pvRxBuf, uint32_t u32Length)
  389. {
  390. int32_t i32Ret = LL_OK;
  391. #ifdef BSP_SPI_USING_DMA
  392. rt_uint32_t u32TimeoutCnt;
  393. CM_DMA_TypeDef *DmaInstance;
  394. rt_uint32_t DmaFlag;
  395. uint32_t u32TxTmp, u32RxTmp;
  396. if ((spi_config == RT_NULL) || ((pvTxBuf == RT_NULL) && (pvRxBuf == RT_NULL)))
  397. {
  398. return LL_ERR;
  399. }
  400. SPI_Cmd(spi_config->Instance, DISABLE);
  401. if (RT_NULL != pvTxBuf)
  402. {
  403. DMA_ClearTransCompleteStatus(spi_config->dma_tx->Instance, spi_config->dma_tx->flag);
  404. DMA_SetSrcAddr(spi_config->dma_tx->Instance, spi_config->dma_tx->channel, (uint32_t)pvTxBuf);
  405. DMA_SetSrcAddrIncMode(spi_config->dma_tx->Instance, spi_config->dma_tx->channel, DMA_SRC_ADDR_INC);
  406. DMA_SetTransCount(spi_config->dma_tx->Instance, spi_config->dma_tx->channel, u32Length);
  407. DMA_ChCmd(spi_config->dma_tx->Instance, spi_config->dma_tx->channel, ENABLE);
  408. }
  409. else
  410. {
  411. if (SPI_FULL_DUPLEX == hc32_spi_get_trans_mode(spi_config->Instance))
  412. {
  413. u32TxTmp = 0xFFFFFFFFUL;
  414. DMA_ClearTransCompleteStatus(spi_config->dma_tx->Instance, spi_config->dma_tx->flag);
  415. DMA_SetSrcAddr(spi_config->dma_tx->Instance, spi_config->dma_tx->channel, (uint32_t)&u32TxTmp);
  416. DMA_SetSrcAddrIncMode(spi_config->dma_tx->Instance, spi_config->dma_tx->channel, DMA_SRC_ADDR_FIX);
  417. DMA_SetTransCount(spi_config->dma_tx->Instance, spi_config->dma_tx->channel, u32Length);
  418. DMA_ChCmd(spi_config->dma_tx->Instance, spi_config->dma_tx->channel, ENABLE);
  419. }
  420. }
  421. if (RT_NULL != pvRxBuf)
  422. {
  423. DMA_ClearTransCompleteStatus(spi_config->dma_rx->Instance, spi_config->dma_rx->flag);
  424. DMA_SetDestAddr(spi_config->dma_rx->Instance, spi_config->dma_rx->channel, (uint32_t)pvRxBuf);
  425. DMA_SetDestAddrIncMode(spi_config->dma_rx->Instance, spi_config->dma_rx->channel, DMA_DEST_ADDR_INC);
  426. DMA_SetTransCount(spi_config->dma_rx->Instance, spi_config->dma_rx->channel, u32Length);
  427. DMA_ChCmd(spi_config->dma_rx->Instance, spi_config->dma_rx->channel, ENABLE);
  428. }
  429. else
  430. {
  431. if (SPI_FULL_DUPLEX == hc32_spi_get_trans_mode(spi_config->Instance))
  432. {
  433. DMA_ClearTransCompleteStatus(spi_config->dma_rx->Instance, spi_config->dma_rx->flag);
  434. DMA_SetDestAddr(spi_config->dma_rx->Instance, spi_config->dma_rx->channel, (uint32_t)&u32RxTmp);
  435. DMA_SetDestAddrIncMode(spi_config->dma_rx->Instance, spi_config->dma_rx->channel, DMA_DEST_ADDR_FIX);
  436. DMA_SetTransCount(spi_config->dma_rx->Instance, spi_config->dma_rx->channel, u32Length);
  437. DMA_ChCmd(spi_config->dma_rx->Instance, spi_config->dma_rx->channel, ENABLE);
  438. }
  439. }
  440. SPI_Cmd(spi_config->Instance, ENABLE);
  441. u32TimeoutCnt = 0U;
  442. /* Wait DMA transfer completed */
  443. if (RT_NULL != pvRxBuf)
  444. {
  445. DmaInstance = spi_config->dma_rx->Instance;
  446. DmaFlag = spi_config->dma_rx->flag;
  447. }
  448. else
  449. {
  450. DmaInstance = spi_config->dma_tx->Instance;
  451. DmaFlag = spi_config->dma_tx->flag;
  452. }
  453. while ((RESET == DMA_GetTransCompleteStatus(DmaInstance, DmaFlag)) &&
  454. (u32TimeoutCnt < spi_config->timeout))
  455. {
  456. rt_thread_mdelay(1);
  457. u32TimeoutCnt++;
  458. }
  459. if (u32TimeoutCnt >= spi_config->timeout)
  460. {
  461. i32Ret = LL_ERR_TIMEOUT;
  462. }
  463. #endif
  464. return i32Ret;
  465. }
  466. static rt_ssize_t hc32_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  467. {
  468. int32_t state;
  469. rt_size_t message_length, already_send_length;
  470. rt_uint16_t send_length;
  471. rt_uint8_t *recv_buf;
  472. const rt_uint8_t *send_buf;
  473. rt_uint32_t u32TimeoutCnt;
  474. RT_ASSERT(device != RT_NULL);
  475. RT_ASSERT(device->bus != RT_NULL);
  476. RT_ASSERT(message != RT_NULL);
  477. struct hc32_spi *spi_drv = rt_container_of(device->bus, struct hc32_spi, spi_bus);
  478. CM_SPI_TypeDef *spi_instance = spi_drv->config->Instance;
  479. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  480. {
  481. if (device->config.mode & RT_SPI_CS_HIGH)
  482. rt_pin_write(device->cs_pin, PIN_HIGH);
  483. else
  484. rt_pin_write(device->cs_pin, PIN_LOW);
  485. }
  486. LOG_D("%s transfer prepare and start", spi_drv->config->bus_name);
  487. LOG_D("%s sendbuf: %X, recvbuf: %X, length: %d", spi_drv->config->bus_name,
  488. (uint32_t)message->send_buf, (uint32_t)message->recv_buf, message->length);
  489. message_length = message->length;
  490. recv_buf = message->recv_buf;
  491. send_buf = message->send_buf;
  492. while (message_length)
  493. {
  494. if (message_length > 65535)
  495. {
  496. send_length = 65535;
  497. message_length = message_length - 65535;
  498. }
  499. else
  500. {
  501. send_length = message_length;
  502. message_length = 0;
  503. }
  504. /* calculate the start address */
  505. already_send_length = message->length - send_length - message_length;
  506. /* avoid null pointer problems */
  507. if (message->send_buf)
  508. {
  509. send_buf = (rt_uint8_t *)message->send_buf + already_send_length;
  510. }
  511. if (message->recv_buf)
  512. {
  513. recv_buf = (rt_uint8_t *)message->recv_buf + already_send_length;
  514. }
  515. if (message->send_buf && message->recv_buf)
  516. {
  517. hc32_spi_set_trans_mode(spi_instance, SPI_FULL_DUPLEX);
  518. if ((spi_drv->spi_dma_flag & RT_DEVICE_FLAG_DMA_TX) && (spi_drv->spi_dma_flag & RT_DEVICE_FLAG_DMA_RX))
  519. {
  520. state = hc32_spi_dma_trans(spi_drv->config, send_buf, recv_buf, send_length);
  521. }
  522. else
  523. {
  524. hc32_spi_enable(spi_instance);
  525. state = SPI_TransReceive(spi_instance, send_buf, recv_buf, send_length, spi_drv->config->timeout);
  526. }
  527. }
  528. else if (message->send_buf)
  529. {
  530. hc32_spi_set_trans_mode(spi_instance, SPI_SEND_ONLY);
  531. if (spi_drv->spi_dma_flag & RT_DEVICE_FLAG_DMA_TX)
  532. {
  533. state = hc32_spi_dma_trans(spi_drv->config, send_buf, RT_NULL, send_length);
  534. }
  535. else
  536. {
  537. hc32_spi_enable(spi_instance);
  538. state = SPI_Trans(spi_instance, send_buf, send_length, spi_drv->config->timeout);
  539. }
  540. }
  541. else
  542. {
  543. hc32_spi_set_trans_mode(spi_instance, SPI_FULL_DUPLEX);
  544. if ((spi_drv->spi_dma_flag & RT_DEVICE_FLAG_DMA_TX) && (spi_drv->spi_dma_flag & RT_DEVICE_FLAG_DMA_RX))
  545. {
  546. state = hc32_spi_dma_trans(spi_drv->config, RT_NULL, recv_buf, send_length);
  547. }
  548. else
  549. {
  550. hc32_spi_enable(spi_instance);
  551. state = SPI_Receive(spi_instance, recv_buf, send_length, spi_drv->config->timeout);
  552. }
  553. }
  554. if (state != LL_OK)
  555. {
  556. LOG_I("spi transfer error : %d", state);
  557. message->length = 0;
  558. break;
  559. }
  560. else
  561. {
  562. /* Wait for the spi transfer complete */
  563. if (spi_drv->spi_dma_flag & (RT_DEVICE_FLAG_DMA_TX | RT_DEVICE_FLAG_DMA_RX))
  564. {
  565. if (spi_drv->cfg->mode & RT_SPI_SLAVE)
  566. {
  567. rt_thread_mdelay(1);
  568. }
  569. else
  570. {
  571. u32TimeoutCnt = 0U;
  572. while ((RESET == SPI_GetStatus(spi_instance, SPI_FLAG_IDLE)) &&
  573. (u32TimeoutCnt < spi_drv->config->timeout))
  574. {
  575. rt_thread_mdelay(1);
  576. u32TimeoutCnt++;
  577. }
  578. if (u32TimeoutCnt >= spi_drv->config->timeout)
  579. {
  580. LOG_I("spi transfer timeout!");
  581. message->length = 0;
  582. break;
  583. }
  584. }
  585. }
  586. }
  587. }
  588. /* clear error flag */
  589. SPI_ClearStatus(spi_instance, SPI_FLAG_CLR_ALL);
  590. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  591. {
  592. if (device->config.mode & RT_SPI_CS_HIGH)
  593. rt_pin_write(device->cs_pin, PIN_LOW);
  594. else
  595. rt_pin_write(device->cs_pin, PIN_HIGH);
  596. }
  597. return message->length;
  598. }
  599. static const struct rt_spi_ops hc32_spi_ops =
  600. {
  601. .configure = hc32_spi_configure,
  602. .xfer = hc32_spi_xfer,
  603. };
  604. /**
  605. * Attach the spi device to SPI bus, this function must be used after initialization.
  606. */
  607. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
  608. {
  609. RT_ASSERT(bus_name != RT_NULL);
  610. RT_ASSERT(device_name != RT_NULL);
  611. rt_err_t result;
  612. struct rt_spi_device *spi_device;
  613. /* attach the device to spi bus*/
  614. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  615. RT_ASSERT(spi_device != RT_NULL);
  616. result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
  617. if (result != RT_EOK)
  618. {
  619. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  620. }
  621. return result;
  622. }
  623. static void hc32_spi_err_irq_handle(struct hc32_spi *spi)
  624. {
  625. __UNUSED uint32_t UnusedData;
  626. CM_SPI_TypeDef *spi_instance = spi->config->Instance;
  627. if (RESET != SPI_GetStatus(spi_instance, SPI_FLAG_OVERRUN))
  628. {
  629. UnusedData = SPI_ReadData(spi_instance);
  630. SPI_ClearStatus(spi_instance, SPI_FLAG_OVERRUN);
  631. }
  632. if (RESET != SPI_GetStatus(spi_instance, SPI_FLAG_UNDERRUN))
  633. {
  634. SPI_ClearStatus(spi_instance, SPI_FLAG_UNDERRUN);
  635. }
  636. if (RESET != SPI_GetStatus(spi_instance, SPI_FLAG_MD_FAULT))
  637. {
  638. SPI_ClearStatus(spi_instance, SPI_FLAG_MD_FAULT);
  639. }
  640. if (RESET != SPI_GetStatus(spi_instance, SPI_FLAG_PARITY_ERR))
  641. {
  642. SPI_ClearStatus(spi_instance, SPI_FLAG_PARITY_ERR);
  643. }
  644. }
  645. #if defined(BSP_USING_SPI1)
  646. static void hc32_spi1_err_irq_handler(void)
  647. {
  648. /* enter interrupt */
  649. rt_interrupt_enter();
  650. hc32_spi_err_irq_handle(&spi_bus_obj[SPI1_INDEX]);
  651. /* leave interrupt */
  652. rt_interrupt_leave();
  653. }
  654. #if defined (HC32F448) ||defined (HC32F472)
  655. void SPI1_Handler(void)
  656. {
  657. hc32_spi1_err_irq_handler();
  658. }
  659. #elif defined (HC32F334)
  660. void SPI_Handler(void)
  661. {
  662. hc32_spi1_err_irq_handler();
  663. }
  664. #endif /* HC32F334 */
  665. #endif /* BSP_USING_SPI1 */
  666. #if defined(BSP_USING_SPI2)
  667. static void hc32_spi2_err_irq_handler(void)
  668. {
  669. /* enter interrupt */
  670. rt_interrupt_enter();
  671. hc32_spi_err_irq_handle(&spi_bus_obj[SPI2_INDEX]);
  672. /* leave interrupt */
  673. rt_interrupt_leave();
  674. }
  675. #if defined (HC32F448) ||defined (HC32F472)
  676. void SPI2_Handler(void)
  677. {
  678. hc32_spi2_err_irq_handler();
  679. }
  680. #endif /* HC32F448, HC32F472 */
  681. #endif /* BSP_USING_SPI2 */
  682. #if defined(BSP_USING_SPI3)
  683. static void hc32_spi3_err_irq_handler(void)
  684. {
  685. /* enter interrupt */
  686. rt_interrupt_enter();
  687. hc32_spi_err_irq_handle(&spi_bus_obj[SPI3_INDEX]);
  688. /* leave interrupt */
  689. rt_interrupt_leave();
  690. }
  691. #if defined (HC32F448) ||defined (HC32F472)
  692. void SPI3_Handler(void)
  693. {
  694. hc32_spi3_err_irq_handler();
  695. }
  696. #endif /* HC32F448, HC32F472 */
  697. #endif /* BSP_USING_SPI3 */
  698. #if defined(BSP_USING_SPI4)
  699. static void hc32_spi4_err_irq_handler(void)
  700. {
  701. /* enter interrupt */
  702. rt_interrupt_enter();
  703. hc32_spi_err_irq_handle(&spi_bus_obj[SPI4_INDEX]);
  704. /* leave interrupt */
  705. rt_interrupt_leave();
  706. }
  707. #if defined (HC32F472)
  708. void SPI4_Handler(void)
  709. {
  710. hc32_spi4_err_irq_handler();
  711. }
  712. #endif /* HC32F472 */
  713. #endif /* BSP_USING_SPI4 */
  714. #if defined(BSP_USING_SPI5)
  715. static void hc32_spi5_err_irq_handler(void)
  716. {
  717. /* enter interrupt */
  718. rt_interrupt_enter();
  719. hc32_spi_err_irq_handle(&spi_bus_obj[SPI5_INDEX]);
  720. /* leave interrupt */
  721. rt_interrupt_leave();
  722. }
  723. #endif /* BSP_USING_SPI5 */
  724. #if defined(BSP_USING_SPI6)
  725. static void hc32_spi6_err_irq_handler(void)
  726. {
  727. /* enter interrupt */
  728. rt_interrupt_enter();
  729. hc32_spi_err_irq_handle(&spi_bus_obj[SPI6_INDEX]);
  730. /* leave interrupt */
  731. rt_interrupt_leave();
  732. }
  733. #endif /* BSP_USING_SPI6 */
  734. /**
  735. * @brief This function gets spi irq handle.
  736. * @param None
  737. * @retval None
  738. */
  739. static void hc32_get_spi_callback(void)
  740. {
  741. #ifdef BSP_USING_SPI1
  742. spi_config[SPI1_INDEX].err_irq.irq_callback = hc32_spi1_err_irq_handler;
  743. #endif
  744. #ifdef BSP_USING_SPI2
  745. spi_config[SPI2_INDEX].err_irq.irq_callback = hc32_spi2_err_irq_handler;
  746. #endif
  747. #ifdef BSP_USING_SPI3
  748. spi_config[SPI3_INDEX].err_irq.irq_callback = hc32_spi3_err_irq_handler;
  749. #endif
  750. #ifdef BSP_USING_SPI4
  751. spi_config[SPI4_INDEX].err_irq.irq_callback = hc32_spi4_err_irq_handler;
  752. #endif
  753. #ifdef BSP_USING_SPI5
  754. spi_config[SPI5_INDEX].err_irq.irq_callback = hc32_spi5_err_irq_handler;
  755. #endif
  756. #ifdef BSP_USING_SPI6
  757. spi_config[SPI6_INDEX].err_irq.irq_callback = hc32_spi6_err_irq_handler;
  758. #endif
  759. }
  760. /**
  761. * @brief This function gets dma witch spi used infomation include unit,
  762. * channel, interrupt etc.
  763. * @param None
  764. * @retval None
  765. */
  766. static void hc32_get_dma_info(void)
  767. {
  768. #ifdef BSP_SPI1_RX_USING_DMA
  769. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  770. static struct dma_config spi1_dma_rx = SPI1_RX_DMA_CONFIG;
  771. spi_config[SPI1_INDEX].dma_rx = &spi1_dma_rx;
  772. #endif
  773. #ifdef BSP_SPI1_TX_USING_DMA
  774. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  775. static struct dma_config spi1_dma_tx = SPI1_TX_DMA_CONFIG;
  776. spi_config[SPI1_INDEX].dma_tx = &spi1_dma_tx;
  777. #endif
  778. #ifdef BSP_SPI2_RX_USING_DMA
  779. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  780. static struct dma_config spi2_dma_rx = SPI2_RX_DMA_CONFIG;
  781. spi_config[SPI2_INDEX].dma_rx = &spi2_dma_rx;
  782. #endif
  783. #ifdef BSP_SPI2_TX_USING_DMA
  784. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  785. static struct dma_config spi2_dma_tx = SPI2_TX_DMA_CONFIG;
  786. spi_config[SPI2_INDEX].dma_tx = &spi2_dma_tx;
  787. #endif
  788. #ifdef BSP_SPI3_RX_USING_DMA
  789. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  790. static struct dma_config spi3_dma_rx = SPI3_RX_DMA_CONFIG;
  791. spi_config[SPI3_INDEX].dma_rx = &spi3_dma_rx;
  792. #endif
  793. #ifdef BSP_SPI3_TX_USING_DMA
  794. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  795. static struct dma_config spi3_dma_tx = SPI3_TX_DMA_CONFIG;
  796. spi_config[SPI3_INDEX].dma_tx = &spi3_dma_tx;
  797. #endif
  798. #ifdef BSP_SPI4_RX_USING_DMA
  799. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  800. static struct dma_config spi4_dma_rx = SPI4_RX_DMA_CONFIG;
  801. spi_config[SPI4_INDEX].dma_rx = &spi4_dma_rx;
  802. #endif
  803. #ifdef BSP_SPI4_TX_USING_DMA
  804. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  805. static struct dma_config spi4_dma_tx = SPI4_TX_DMA_CONFIG;
  806. spi_config[SPI4_INDEX].dma_tx = &spi4_dma_tx;
  807. #endif
  808. #ifdef BSP_SPI5_RX_USING_DMA
  809. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  810. static struct dma_config spi5_dma_rx = SPI5_RX_DMA_CONFIG;
  811. spi_config[SPI5_INDEX].dma_rx = &spi5_dma_rx;
  812. #endif
  813. #ifdef BSP_SPI5_TX_USING_DMA
  814. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  815. static struct dma_config spi5_dma_tx = SPI5_TX_DMA_CONFIG;
  816. spi_config[SPI5_INDEX].dma_tx = &spi5_dma_tx;
  817. #endif
  818. #ifdef BSP_SPI6_RX_USING_DMA
  819. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  820. static struct dma_config spi6_dma_rx = SPI6_RX_DMA_CONFIG;
  821. spi_config[SPI6_INDEX].dma_rx = &spi6_dma_rx;
  822. #endif
  823. #ifdef BSP_SPI6_TX_USING_DMA
  824. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  825. static struct dma_config spi6_dma_tx = SPI6_TX_DMA_CONFIG;
  826. spi_config[SPI6_INDEX].dma_tx = &spi6_dma_tx;
  827. #endif
  828. }
  829. static int hc32_hw_spi_bus_init(void)
  830. {
  831. rt_err_t result;
  832. hc32_get_spi_callback();
  833. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  834. {
  835. spi_bus_obj[i].config = &spi_config[i];
  836. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i];
  837. /* register the handle */
  838. #if defined (HC32F460) || defined (HC32F4A0) || defined (HC32F4A8)
  839. hc32_install_irq_handler(&spi_config[i].err_irq.irq_config, spi_config[i].err_irq.irq_callback, RT_FALSE);
  840. #elif defined (HC32F448) || defined (HC32F472) || defined (HC32F334)
  841. INTC_IntSrcCmd(spi_config[i].err_irq.irq_config.int_src, DISABLE);
  842. NVIC_DisableIRQ(spi_config[i].err_irq.irq_config.irq_num);
  843. #endif
  844. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &hc32_spi_ops);
  845. LOG_D("%s bus init done", spi_config[i].bus_name);
  846. }
  847. return result;
  848. }
  849. int hc32_hw_spi_init(void)
  850. {
  851. hc32_get_dma_info();
  852. return hc32_hw_spi_bus_init();
  853. }
  854. INIT_BOARD_EXPORT(hc32_hw_spi_init);
  855. #endif
  856. #endif /* BSP_USING_SPI */