drv_spi.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-5 SummerGift first version
  9. * 2018-12-11 greedyhao Porting for stm32f7xx
  10. * 2019-01-03 zylx modify DMA initialization and spixfer function
  11. * 2020-01-15 whj4674672 Porting for stm32h7xx
  12. * 2020-06-18 thread-liu Porting for stm32mp1xx
  13. * 2020-10-14 Dozingfiretruck Porting for stm32wbxx
  14. */
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #include "board.h"
  18. #ifdef BSP_USING_SPI
  19. #if defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) || defined(BSP_USING_SPI4) || defined(BSP_USING_SPI5) || defined(BSP_USING_SPI6)
  20. #include "drv_spi.h"
  21. #include "drv_config.h"
  22. #include <string.h>
  23. //#define DRV_DEBUG
  24. #define LOG_TAG "drv.spi"
  25. #include <drv_log.h>
  26. enum
  27. {
  28. #ifdef BSP_USING_SPI1
  29. SPI1_INDEX,
  30. #endif
  31. #ifdef BSP_USING_SPI2
  32. SPI2_INDEX,
  33. #endif
  34. #ifdef BSP_USING_SPI3
  35. SPI3_INDEX,
  36. #endif
  37. #ifdef BSP_USING_SPI4
  38. SPI4_INDEX,
  39. #endif
  40. #ifdef BSP_USING_SPI5
  41. SPI5_INDEX,
  42. #endif
  43. #ifdef BSP_USING_SPI6
  44. SPI6_INDEX,
  45. #endif
  46. };
  47. static struct stm32_spi_config spi_config[] =
  48. {
  49. #ifdef BSP_USING_SPI1
  50. SPI1_BUS_CONFIG,
  51. #endif
  52. #ifdef BSP_USING_SPI2
  53. SPI2_BUS_CONFIG,
  54. #endif
  55. #ifdef BSP_USING_SPI3
  56. SPI3_BUS_CONFIG,
  57. #endif
  58. #ifdef BSP_USING_SPI4
  59. SPI4_BUS_CONFIG,
  60. #endif
  61. #ifdef BSP_USING_SPI5
  62. SPI5_BUS_CONFIG,
  63. #endif
  64. #ifdef BSP_USING_SPI6
  65. SPI6_BUS_CONFIG,
  66. #endif
  67. };
  68. static struct stm32_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])] = {0};
  69. static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configuration *cfg)
  70. {
  71. RT_ASSERT(spi_drv != RT_NULL);
  72. RT_ASSERT(cfg != RT_NULL);
  73. SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
  74. if (cfg->mode & RT_SPI_SLAVE)
  75. {
  76. spi_handle->Init.Mode = SPI_MODE_SLAVE;
  77. }
  78. else
  79. {
  80. spi_handle->Init.Mode = SPI_MODE_MASTER;
  81. }
  82. if (cfg->mode & RT_SPI_3WIRE)
  83. {
  84. spi_handle->Init.Direction = SPI_DIRECTION_1LINE;
  85. }
  86. else
  87. {
  88. spi_handle->Init.Direction = SPI_DIRECTION_2LINES;
  89. }
  90. if (cfg->data_width == 8)
  91. {
  92. spi_handle->Init.DataSize = SPI_DATASIZE_8BIT;
  93. spi_handle->TxXferSize = 8;
  94. spi_handle->RxXferSize = 8;
  95. }
  96. else if (cfg->data_width == 16)
  97. {
  98. spi_handle->Init.DataSize = SPI_DATASIZE_16BIT;
  99. }
  100. else
  101. {
  102. return -RT_EIO;
  103. }
  104. if (cfg->mode & RT_SPI_CPHA)
  105. {
  106. spi_handle->Init.CLKPhase = SPI_PHASE_2EDGE;
  107. }
  108. else
  109. {
  110. spi_handle->Init.CLKPhase = SPI_PHASE_1EDGE;
  111. }
  112. if (cfg->mode & RT_SPI_CPOL)
  113. {
  114. spi_handle->Init.CLKPolarity = SPI_POLARITY_HIGH;
  115. }
  116. else
  117. {
  118. spi_handle->Init.CLKPolarity = SPI_POLARITY_LOW;
  119. }
  120. spi_handle->Init.NSS = SPI_NSS_SOFT;
  121. static uint32_t SPI_CLOCK;
  122. /* Some series may only have APBPERIPH_BASE, but don't have HAL_RCC_GetPCLK2Freq */
  123. #if defined(APBPERIPH_BASE)
  124. SPI_CLOCK = HAL_RCC_GetPCLK1Freq();
  125. #elif defined(APB1PERIPH_BASE) || defined(APB2PERIPH_BASE)
  126. /* The SPI clock for H7 cannot be configured with a peripheral bus clock, so it needs to be written separately */
  127. #if defined(SOC_SERIES_STM32H7)
  128. /* When the configuration is generated using CUBEMX, the configuration for the SPI clock is placed in the HAL_SPI_Init function.
  129. Therefore, it is necessary to initialize and configure the SPI clock to automatically configure the frequency division */
  130. HAL_SPI_Init(spi_handle);
  131. SPI_CLOCK = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI123);
  132. #else
  133. if ((rt_uint32_t)spi_drv->config->Instance >= APB2PERIPH_BASE)
  134. {
  135. SPI_CLOCK = HAL_RCC_GetPCLK2Freq();
  136. }
  137. else
  138. {
  139. SPI_CLOCK = HAL_RCC_GetPCLK1Freq();
  140. }
  141. #endif /* SOC_SERIES_STM32H7) */
  142. #endif /* APBPERIPH_BASE */
  143. if (cfg->max_hz >= SPI_CLOCK / 2)
  144. {
  145. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  146. }
  147. else if (cfg->max_hz >= SPI_CLOCK / 4)
  148. {
  149. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  150. }
  151. else if (cfg->max_hz >= SPI_CLOCK / 8)
  152. {
  153. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  154. }
  155. else if (cfg->max_hz >= SPI_CLOCK / 16)
  156. {
  157. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  158. }
  159. else if (cfg->max_hz >= SPI_CLOCK / 32)
  160. {
  161. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  162. }
  163. else if (cfg->max_hz >= SPI_CLOCK / 64)
  164. {
  165. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
  166. }
  167. else if (cfg->max_hz >= SPI_CLOCK / 128)
  168. {
  169. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
  170. }
  171. else
  172. {
  173. /* min prescaler 256 */
  174. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  175. }
  176. LOG_D("sys freq: %d, pclk freq: %d, SPI limiting freq: %d, SPI usage freq: %d",
  177. #if defined(SOC_SERIES_STM32MP1)
  178. HAL_RCC_GetSystemCoreClockFreq(),
  179. #else
  180. HAL_RCC_GetSysClockFreq(),
  181. #endif
  182. SPI_CLOCK,
  183. cfg->max_hz,
  184. SPI_CLOCK / (rt_size_t)pow(2,(spi_handle->Init.BaudRatePrescaler >> 28) + 1));
  185. if (cfg->mode & RT_SPI_MSB)
  186. {
  187. spi_handle->Init.FirstBit = SPI_FIRSTBIT_MSB;
  188. }
  189. else
  190. {
  191. spi_handle->Init.FirstBit = SPI_FIRSTBIT_LSB;
  192. }
  193. spi_handle->Init.TIMode = SPI_TIMODE_DISABLE;
  194. spi_handle->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  195. spi_handle->State = HAL_SPI_STATE_RESET;
  196. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32WB)
  197. spi_handle->Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  198. #elif defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32MP1)
  199. spi_handle->Init.Mode = SPI_MODE_MASTER;
  200. spi_handle->Init.NSS = SPI_NSS_SOFT;
  201. spi_handle->Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  202. spi_handle->Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
  203. spi_handle->Init.CRCPolynomial = 7;
  204. spi_handle->Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
  205. spi_handle->Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
  206. spi_handle->Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
  207. spi_handle->Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
  208. spi_handle->Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
  209. spi_handle->Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
  210. spi_handle->Init.IOSwap = SPI_IO_SWAP_DISABLE;
  211. spi_handle->Init.FifoThreshold = SPI_FIFO_THRESHOLD_08DATA;
  212. #endif
  213. if (HAL_SPI_Init(spi_handle) != HAL_OK)
  214. {
  215. return -RT_EIO;
  216. }
  217. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0) \
  218. || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32WB)
  219. SET_BIT(spi_handle->Instance->CR2, SPI_RXFIFO_THRESHOLD_HF);
  220. #endif
  221. /* DMA configuration */
  222. if (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  223. {
  224. HAL_DMA_Init(&spi_drv->dma.handle_rx);
  225. __HAL_LINKDMA(&spi_drv->handle, hdmarx, spi_drv->dma.handle_rx);
  226. /* NVIC configuration for DMA transfer complete interrupt */
  227. HAL_NVIC_SetPriority(spi_drv->config->dma_rx->dma_irq, 0, 0);
  228. HAL_NVIC_EnableIRQ(spi_drv->config->dma_rx->dma_irq);
  229. }
  230. if (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  231. {
  232. HAL_DMA_Init(&spi_drv->dma.handle_tx);
  233. __HAL_LINKDMA(&spi_drv->handle, hdmatx, spi_drv->dma.handle_tx);
  234. /* NVIC configuration for DMA transfer complete interrupt */
  235. HAL_NVIC_SetPriority(spi_drv->config->dma_tx->dma_irq, 1, 0);
  236. HAL_NVIC_EnableIRQ(spi_drv->config->dma_tx->dma_irq);
  237. }
  238. if(spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG || spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  239. {
  240. HAL_NVIC_SetPriority(spi_drv->config->irq_type, 2, 0);
  241. HAL_NVIC_EnableIRQ(spi_drv->config->irq_type);
  242. }
  243. LOG_D("%s init done", spi_drv->config->bus_name);
  244. return RT_EOK;
  245. }
  246. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  247. {
  248. HAL_StatusTypeDef state = HAL_OK;
  249. rt_size_t message_length, already_send_length;
  250. rt_uint16_t send_length;
  251. rt_uint8_t *recv_buf;
  252. const rt_uint8_t *send_buf;
  253. RT_ASSERT(device != RT_NULL);
  254. RT_ASSERT(device->bus != RT_NULL);
  255. RT_ASSERT(message != RT_NULL);
  256. struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
  257. SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
  258. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  259. {
  260. if (device->config.mode & RT_SPI_CS_HIGH)
  261. rt_pin_write(device->cs_pin, PIN_HIGH);
  262. else
  263. rt_pin_write(device->cs_pin, PIN_LOW);
  264. }
  265. LOG_D("%s transfer prepare and start", spi_drv->config->bus_name);
  266. LOG_D("%s sendbuf: %X, recvbuf: %X, length: %d",
  267. spi_drv->config->bus_name,
  268. (uint32_t)message->send_buf,
  269. (uint32_t)message->recv_buf, message->length);
  270. message_length = message->length;
  271. recv_buf = message->recv_buf;
  272. send_buf = message->send_buf;
  273. while (message_length)
  274. {
  275. /* the HAL library use uint16 to save the data length */
  276. if (message_length > 65535)
  277. {
  278. send_length = 65535;
  279. message_length = message_length - 65535;
  280. }
  281. else
  282. {
  283. send_length = message_length;
  284. message_length = 0;
  285. }
  286. /* calculate the start address */
  287. already_send_length = message->length - send_length - message_length;
  288. /* avoid null pointer problems */
  289. if (message->send_buf)
  290. {
  291. send_buf = (rt_uint8_t *)message->send_buf + already_send_length;
  292. }
  293. if (message->recv_buf)
  294. {
  295. recv_buf = (rt_uint8_t *)message->recv_buf + already_send_length;
  296. }
  297. #if defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32F7)
  298. rt_uint32_t* dma_buf = RT_NULL;
  299. if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG))
  300. {
  301. dma_buf = (rt_uint32_t *)rt_malloc_align(send_length,32);
  302. if(send_buf)
  303. {
  304. rt_memcpy(dma_buf, send_buf, send_length);
  305. }
  306. else
  307. {
  308. rt_memset(dma_buf, 0xFF, send_length);
  309. }
  310. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, dma_buf, send_length);
  311. state = HAL_SPI_TransmitReceive_DMA(spi_handle, (uint8_t *)dma_buf, (uint8_t *)dma_buf, send_length);
  312. }
  313. else
  314. #endif /* SOC_SERIES_STM32H7 || SOC_SERIES_STM32F7 */
  315. /* start once data exchange in DMA mode */
  316. if (message->send_buf && message->recv_buf)
  317. {
  318. if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG))
  319. {
  320. state = HAL_SPI_TransmitReceive_DMA(spi_handle, (uint8_t *)send_buf, (uint8_t *)recv_buf, send_length);
  321. }
  322. else if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG))
  323. {
  324. /* same as Tx ONLY. It will not receive SPI data any more. */
  325. rt_memset((uint8_t *)recv_buf, 0xff, send_length);
  326. state = HAL_SPI_Transmit_DMA(spi_handle, (uint8_t *)send_buf, send_length);
  327. }
  328. else if ((spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG))
  329. {
  330. state = HAL_ERROR;
  331. LOG_E("It shoule be enabled both BSP_SPIx_TX_USING_DMA and BSP_SPIx_TX_USING_DMA flag, if wants to use SPI DMA Rx singly.");
  332. break;
  333. }
  334. else
  335. {
  336. state = HAL_SPI_TransmitReceive(spi_handle, (uint8_t *)send_buf, (uint8_t *)recv_buf, send_length, 1000);
  337. }
  338. }
  339. else if (message->send_buf)
  340. {
  341. if (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  342. {
  343. state = HAL_SPI_Transmit_DMA(spi_handle, (uint8_t *)send_buf, send_length);
  344. }
  345. else
  346. {
  347. state = HAL_SPI_Transmit(spi_handle, (uint8_t *)send_buf, send_length, 1000);
  348. }
  349. if (message->cs_release && (device->config.mode & RT_SPI_3WIRE))
  350. {
  351. /* release the CS by disable SPI when using 3 wires SPI */
  352. __HAL_SPI_DISABLE(spi_handle);
  353. }
  354. }
  355. else if(message->recv_buf)
  356. {
  357. rt_memset((uint8_t *)recv_buf, 0xff, send_length);
  358. if (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  359. {
  360. state = HAL_SPI_Receive_DMA(spi_handle, (uint8_t *)recv_buf, send_length);
  361. }
  362. else
  363. {
  364. /* clear the old error flag */
  365. __HAL_SPI_CLEAR_OVRFLAG(spi_handle);
  366. state = HAL_SPI_Receive(spi_handle, (uint8_t *)recv_buf, send_length, 1000);
  367. }
  368. }
  369. else
  370. {
  371. state = HAL_ERROR;
  372. LOG_E("message->send_buf and message->recv_buf are both NULL!");
  373. }
  374. if (state != HAL_OK)
  375. {
  376. LOG_E("SPI transfer error: %d", state);
  377. message->length = 0;
  378. spi_handle->State = HAL_SPI_STATE_READY;
  379. break;
  380. }
  381. else
  382. {
  383. LOG_D("%s transfer done", spi_drv->config->bus_name);
  384. }
  385. /* For simplicity reasons, this example is just waiting till the end of the
  386. transfer, but application may perform other tasks while transfer operation
  387. is ongoing. */
  388. if (spi_drv->spi_dma_flag & (SPI_USING_TX_DMA_FLAG | SPI_USING_RX_DMA_FLAG))
  389. {
  390. /* blocking the thread,and the other tasks can run */
  391. if (rt_completion_wait(&spi_drv->cpt, 1000) != RT_EOK)
  392. {
  393. state = HAL_ERROR;
  394. LOG_E("wait for DMA interrupt overtime!");
  395. break;
  396. }
  397. }
  398. else
  399. {
  400. while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY);
  401. }
  402. #if defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32F7)
  403. if(dma_buf)
  404. {
  405. if(recv_buf)
  406. {
  407. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, dma_buf, send_length);
  408. rt_memcpy(recv_buf, dma_buf,send_length);
  409. }
  410. rt_free_align(dma_buf);
  411. }
  412. #endif /* SOC_SERIES_STM32H7 || SOC_SERIES_STM32F7 */
  413. }
  414. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  415. {
  416. if (device->config.mode & RT_SPI_CS_HIGH)
  417. rt_pin_write(device->cs_pin, PIN_LOW);
  418. else
  419. rt_pin_write(device->cs_pin, PIN_HIGH);
  420. }
  421. if(state != HAL_OK)
  422. {
  423. return -RT_ERROR;
  424. }
  425. return message->length;
  426. }
  427. static rt_err_t spi_configure(struct rt_spi_device *device,
  428. struct rt_spi_configuration *configuration)
  429. {
  430. RT_ASSERT(device != RT_NULL);
  431. RT_ASSERT(configuration != RT_NULL);
  432. struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
  433. spi_drv->cfg = configuration;
  434. return stm32_spi_init(spi_drv, configuration);
  435. }
  436. static const struct rt_spi_ops stm_spi_ops =
  437. {
  438. .configure = spi_configure,
  439. .xfer = spixfer,
  440. };
  441. static int rt_hw_spi_bus_init(void)
  442. {
  443. rt_err_t result;
  444. for (rt_size_t i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  445. {
  446. spi_bus_obj[i].config = &spi_config[i];
  447. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i];
  448. spi_bus_obj[i].handle.Instance = spi_config[i].Instance;
  449. if (spi_bus_obj[i].spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  450. {
  451. /* Configure the DMA handler for Transmission process */
  452. spi_bus_obj[i].dma.handle_rx.Instance = spi_config[i].dma_rx->Instance;
  453. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  454. spi_bus_obj[i].dma.handle_rx.Init.Channel = spi_config[i].dma_rx->channel;
  455. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32WB) || defined(SOC_SERIES_STM32H7)
  456. spi_bus_obj[i].dma.handle_rx.Init.Request = spi_config[i].dma_rx->request;
  457. #endif
  458. #ifndef SOC_SERIES_STM32U5
  459. spi_bus_obj[i].dma.handle_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  460. spi_bus_obj[i].dma.handle_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  461. spi_bus_obj[i].dma.handle_rx.Init.MemInc = DMA_MINC_ENABLE;
  462. spi_bus_obj[i].dma.handle_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  463. spi_bus_obj[i].dma.handle_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  464. spi_bus_obj[i].dma.handle_rx.Init.Mode = DMA_NORMAL;
  465. spi_bus_obj[i].dma.handle_rx.Init.Priority = DMA_PRIORITY_HIGH;
  466. #endif
  467. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32H7)
  468. spi_bus_obj[i].dma.handle_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  469. spi_bus_obj[i].dma.handle_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  470. spi_bus_obj[i].dma.handle_rx.Init.MemBurst = DMA_MBURST_INC4;
  471. spi_bus_obj[i].dma.handle_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  472. #endif
  473. {
  474. rt_uint32_t tmpreg = 0x00U;
  475. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32F0)
  476. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  477. SET_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  478. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  479. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WB) || defined(SOC_SERIES_STM32H7)
  480. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_rx->dma_rcc);
  481. /* Delay after an RCC peripheral clock enabling */
  482. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_rx->dma_rcc);
  483. #elif defined(SOC_SERIES_STM32MP1)
  484. __HAL_RCC_DMAMUX_CLK_ENABLE();
  485. SET_BIT(RCC->MP_AHB2ENSETR, spi_config[i].dma_rx->dma_rcc);
  486. tmpreg = READ_BIT(RCC->MP_AHB2ENSETR, spi_config[i].dma_rx->dma_rcc);
  487. #endif
  488. UNUSED(tmpreg); /* To avoid compiler warnings */
  489. }
  490. }
  491. if (spi_bus_obj[i].spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  492. {
  493. /* Configure the DMA handler for Transmission process */
  494. spi_bus_obj[i].dma.handle_tx.Instance = spi_config[i].dma_tx->Instance;
  495. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  496. spi_bus_obj[i].dma.handle_tx.Init.Channel = spi_config[i].dma_tx->channel;
  497. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32WB) || defined(SOC_SERIES_STM32H7)
  498. spi_bus_obj[i].dma.handle_tx.Init.Request = spi_config[i].dma_tx->request;
  499. #endif
  500. #ifndef SOC_SERIES_STM32U5
  501. spi_bus_obj[i].dma.handle_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  502. spi_bus_obj[i].dma.handle_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  503. spi_bus_obj[i].dma.handle_tx.Init.MemInc = DMA_MINC_ENABLE;
  504. spi_bus_obj[i].dma.handle_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  505. spi_bus_obj[i].dma.handle_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  506. spi_bus_obj[i].dma.handle_tx.Init.Mode = DMA_NORMAL;
  507. spi_bus_obj[i].dma.handle_tx.Init.Priority = DMA_PRIORITY_LOW;
  508. #endif
  509. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32H7)
  510. spi_bus_obj[i].dma.handle_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  511. spi_bus_obj[i].dma.handle_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  512. spi_bus_obj[i].dma.handle_tx.Init.MemBurst = DMA_MBURST_INC4;
  513. spi_bus_obj[i].dma.handle_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  514. #endif
  515. {
  516. rt_uint32_t tmpreg = 0x00U;
  517. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32F0)
  518. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  519. SET_BIT(RCC->AHBENR, spi_config[i].dma_tx->dma_rcc);
  520. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_tx->dma_rcc);
  521. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WB) || defined(SOC_SERIES_STM32H7)
  522. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_tx->dma_rcc);
  523. /* Delay after an RCC peripheral clock enabling */
  524. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_tx->dma_rcc);
  525. #elif defined(SOC_SERIES_STM32MP1)
  526. __HAL_RCC_DMAMUX_CLK_ENABLE();
  527. SET_BIT(RCC->MP_AHB2ENSETR, spi_config[i].dma_tx->dma_rcc);
  528. tmpreg = READ_BIT(RCC->MP_AHB2ENSETR, spi_config[i].dma_tx->dma_rcc);
  529. #endif
  530. UNUSED(tmpreg); /* To avoid compiler warnings */
  531. }
  532. }
  533. /* initialize completion object */
  534. rt_completion_init(&spi_bus_obj[i].cpt);
  535. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &stm_spi_ops);
  536. RT_ASSERT(result == RT_EOK);
  537. LOG_D("%s bus init done", spi_config[i].bus_name);
  538. }
  539. return result;
  540. }
  541. /**
  542. * Attach the spi device to SPI bus, this function must be used after initialization.
  543. */
  544. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
  545. {
  546. RT_ASSERT(bus_name != RT_NULL);
  547. RT_ASSERT(device_name != RT_NULL);
  548. rt_err_t result;
  549. struct rt_spi_device *spi_device;
  550. /* attach the device to spi bus*/
  551. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  552. RT_ASSERT(spi_device != RT_NULL);
  553. result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
  554. if (result != RT_EOK)
  555. {
  556. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  557. }
  558. RT_ASSERT(result == RT_EOK);
  559. LOG_D("%s attach to %s done", device_name, bus_name);
  560. return result;
  561. }
  562. #if defined(BSP_SPI1_TX_USING_DMA) || defined(BSP_SPI1_RX_USING_DMA)
  563. void SPI1_IRQHandler(void)
  564. {
  565. /* enter interrupt */
  566. rt_interrupt_enter();
  567. HAL_SPI_IRQHandler(&spi_bus_obj[SPI1_INDEX].handle);
  568. /* leave interrupt */
  569. rt_interrupt_leave();
  570. }
  571. #endif
  572. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_RX_USING_DMA)
  573. /**
  574. * @brief This function handles DMA Rx interrupt request.
  575. * @param None
  576. * @retval None
  577. */
  578. void SPI1_DMA_RX_IRQHandler(void)
  579. {
  580. /* enter interrupt */
  581. rt_interrupt_enter();
  582. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_rx);
  583. /* leave interrupt */
  584. rt_interrupt_leave();
  585. }
  586. #endif
  587. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_TX_USING_DMA)
  588. /**
  589. * @brief This function handles DMA Tx interrupt request.
  590. * @param None
  591. * @retval None
  592. */
  593. void SPI1_DMA_TX_IRQHandler(void)
  594. {
  595. /* enter interrupt */
  596. rt_interrupt_enter();
  597. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_tx);
  598. /* leave interrupt */
  599. rt_interrupt_leave();
  600. }
  601. #endif /* defined(BSP_USING_SPI1) && defined(BSP_SPI_USING_DMA) */
  602. #if defined(BSP_SPI2_TX_USING_DMA) || defined(BSP_SPI2_RX_USING_DMA)
  603. void SPI2_IRQHandler(void)
  604. {
  605. /* enter interrupt */
  606. rt_interrupt_enter();
  607. HAL_SPI_IRQHandler(&spi_bus_obj[SPI2_INDEX].handle);
  608. /* leave interrupt */
  609. rt_interrupt_leave();
  610. }
  611. #endif
  612. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_RX_USING_DMA)
  613. /**
  614. * @brief This function handles DMA Rx interrupt request.
  615. * @param None
  616. * @retval None
  617. */
  618. void SPI2_DMA_RX_IRQHandler(void)
  619. {
  620. /* enter interrupt */
  621. rt_interrupt_enter();
  622. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_rx);
  623. /* leave interrupt */
  624. rt_interrupt_leave();
  625. }
  626. #endif
  627. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_TX_USING_DMA)
  628. /**
  629. * @brief This function handles DMA Tx interrupt request.
  630. * @param None
  631. * @retval None
  632. */
  633. void SPI2_DMA_TX_IRQHandler(void)
  634. {
  635. /* enter interrupt */
  636. rt_interrupt_enter();
  637. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_tx);
  638. /* leave interrupt */
  639. rt_interrupt_leave();
  640. }
  641. #endif /* defined(BSP_USING_SPI2) && defined(BSP_SPI_USING_DMA) */
  642. #if defined(BSP_SPI3_TX_USING_DMA) || defined(BSP_SPI3_RX_USING_DMA)
  643. void SPI3_IRQHandler(void)
  644. {
  645. /* enter interrupt */
  646. rt_interrupt_enter();
  647. HAL_SPI_IRQHandler(&spi_bus_obj[SPI3_INDEX].handle);
  648. /* leave interrupt */
  649. rt_interrupt_leave();
  650. }
  651. #endif
  652. #if defined(BSP_USING_SPI3) && defined(BSP_SPI3_RX_USING_DMA)
  653. /**
  654. * @brief This function handles DMA Rx interrupt request.
  655. * @param None
  656. * @retval None
  657. */
  658. void SPI3_DMA_RX_IRQHandler(void)
  659. {
  660. /* enter interrupt */
  661. rt_interrupt_enter();
  662. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_rx);
  663. /* leave interrupt */
  664. rt_interrupt_leave();
  665. }
  666. #endif
  667. #if defined(BSP_USING_SPI3) && defined(BSP_SPI3_TX_USING_DMA)
  668. /**
  669. * @brief This function handles DMA Tx interrupt request.
  670. * @param None
  671. * @retval None
  672. */
  673. void SPI3_DMA_TX_IRQHandler(void)
  674. {
  675. /* enter interrupt */
  676. rt_interrupt_enter();
  677. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_tx);
  678. /* leave interrupt */
  679. rt_interrupt_leave();
  680. }
  681. #endif /* defined(BSP_USING_SPI3) && defined(BSP_SPI_USING_DMA) */
  682. #if defined(BSP_SPI4_TX_USING_DMA) || defined(BSP_SPI4_RX_USING_DMA)
  683. void SPI4_IRQHandler(void)
  684. {
  685. /* enter interrupt */
  686. rt_interrupt_enter();
  687. HAL_SPI_IRQHandler(&spi_bus_obj[SPI4_INDEX].handle);
  688. /* leave interrupt */
  689. rt_interrupt_leave();
  690. }
  691. #endif
  692. #if defined(BSP_USING_SPI4) && defined(BSP_SPI4_RX_USING_DMA)
  693. /**
  694. * @brief This function handles DMA Rx interrupt request.
  695. * @param None
  696. * @retval None
  697. */
  698. void SPI4_DMA_RX_IRQHandler(void)
  699. {
  700. /* enter interrupt */
  701. rt_interrupt_enter();
  702. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_rx);
  703. /* leave interrupt */
  704. rt_interrupt_leave();
  705. }
  706. #endif
  707. #if defined(BSP_USING_SPI4) && defined(BSP_SPI4_TX_USING_DMA)
  708. /**
  709. * @brief This function handles DMA Tx interrupt request.
  710. * @param None
  711. * @retval None
  712. */
  713. void SPI4_DMA_TX_IRQHandler(void)
  714. {
  715. /* enter interrupt */
  716. rt_interrupt_enter();
  717. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_tx);
  718. /* leave interrupt */
  719. rt_interrupt_leave();
  720. }
  721. #endif /* defined(BSP_USING_SPI4) && defined(BSP_SPI_USING_DMA) */
  722. #if defined(BSP_SPI5_TX_USING_DMA) || defined(BSP_SPI5_RX_USING_DMA)
  723. void SPI5_IRQHandler(void)
  724. {
  725. /* enter interrupt */
  726. rt_interrupt_enter();
  727. HAL_SPI_IRQHandler(&spi_bus_obj[SPI5_INDEX].handle);
  728. /* leave interrupt */
  729. rt_interrupt_leave();
  730. }
  731. #endif
  732. #if defined(BSP_USING_SPI5) && defined(BSP_SPI5_RX_USING_DMA)
  733. /**
  734. * @brief This function handles DMA Rx interrupt request.
  735. * @param None
  736. * @retval None
  737. */
  738. void SPI5_DMA_RX_IRQHandler(void)
  739. {
  740. /* enter interrupt */
  741. rt_interrupt_enter();
  742. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_rx);
  743. /* leave interrupt */
  744. rt_interrupt_leave();
  745. }
  746. #endif
  747. #if defined(BSP_USING_SPI5) && defined(BSP_SPI5_TX_USING_DMA)
  748. /**
  749. * @brief This function handles DMA Tx interrupt request.
  750. * @param None
  751. * @retval None
  752. */
  753. void SPI5_DMA_TX_IRQHandler(void)
  754. {
  755. /* enter interrupt */
  756. rt_interrupt_enter();
  757. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_tx);
  758. /* leave interrupt */
  759. rt_interrupt_leave();
  760. }
  761. #endif /* defined(BSP_USING_SPI5) && defined(BSP_SPI_USING_DMA) */
  762. #if defined(BSP_USING_SPI6) && defined(BSP_SPI6_RX_USING_DMA)
  763. /**
  764. * @brief This function handles DMA Rx interrupt request.
  765. * @param None
  766. * @retval None
  767. */
  768. void SPI6_DMA_RX_IRQHandler(void)
  769. {
  770. /* enter interrupt */
  771. rt_interrupt_enter();
  772. HAL_DMA_IRQHandler(&spi_bus_obj[SPI6_INDEX].dma.handle_rx);
  773. /* leave interrupt */
  774. rt_interrupt_leave();
  775. }
  776. #endif
  777. #if defined(BSP_USING_SPI6) && defined(BSP_SPI6_TX_USING_DMA)
  778. /**
  779. * @brief This function handles DMA Tx interrupt request.
  780. * @param None
  781. * @retval None
  782. */
  783. void SPI6_DMA_TX_IRQHandler(void)
  784. {
  785. /* enter interrupt */
  786. rt_interrupt_enter();
  787. HAL_DMA_IRQHandler(&spi_bus_obj[SPI6_INDEX].dma.handle_tx);
  788. /* leave interrupt */
  789. rt_interrupt_leave();
  790. }
  791. #endif /* defined(BSP_USING_SPI6) && defined(BSP_SPI_USING_DMA) */
  792. static void stm32_get_dma_info(void)
  793. {
  794. #ifdef BSP_SPI1_RX_USING_DMA
  795. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  796. static struct dma_config spi1_dma_rx = SPI1_RX_DMA_CONFIG;
  797. spi_config[SPI1_INDEX].dma_rx = &spi1_dma_rx;
  798. #endif
  799. #ifdef BSP_SPI1_TX_USING_DMA
  800. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  801. static struct dma_config spi1_dma_tx = SPI1_TX_DMA_CONFIG;
  802. spi_config[SPI1_INDEX].dma_tx = &spi1_dma_tx;
  803. #endif
  804. #ifdef BSP_SPI2_RX_USING_DMA
  805. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  806. static struct dma_config spi2_dma_rx = SPI2_RX_DMA_CONFIG;
  807. spi_config[SPI2_INDEX].dma_rx = &spi2_dma_rx;
  808. #endif
  809. #ifdef BSP_SPI2_TX_USING_DMA
  810. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  811. static struct dma_config spi2_dma_tx = SPI2_TX_DMA_CONFIG;
  812. spi_config[SPI2_INDEX].dma_tx = &spi2_dma_tx;
  813. #endif
  814. #ifdef BSP_SPI3_RX_USING_DMA
  815. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  816. static struct dma_config spi3_dma_rx = SPI3_RX_DMA_CONFIG;
  817. spi_config[SPI3_INDEX].dma_rx = &spi3_dma_rx;
  818. #endif
  819. #ifdef BSP_SPI3_TX_USING_DMA
  820. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  821. static struct dma_config spi3_dma_tx = SPI3_TX_DMA_CONFIG;
  822. spi_config[SPI3_INDEX].dma_tx = &spi3_dma_tx;
  823. #endif
  824. #ifdef BSP_SPI4_RX_USING_DMA
  825. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  826. static struct dma_config spi4_dma_rx = SPI4_RX_DMA_CONFIG;
  827. spi_config[SPI4_INDEX].dma_rx = &spi4_dma_rx;
  828. #endif
  829. #ifdef BSP_SPI4_TX_USING_DMA
  830. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  831. static struct dma_config spi4_dma_tx = SPI4_TX_DMA_CONFIG;
  832. spi_config[SPI4_INDEX].dma_tx = &spi4_dma_tx;
  833. #endif
  834. #ifdef BSP_SPI5_RX_USING_DMA
  835. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  836. static struct dma_config spi5_dma_rx = SPI5_RX_DMA_CONFIG;
  837. spi_config[SPI5_INDEX].dma_rx = &spi5_dma_rx;
  838. #endif
  839. #ifdef BSP_SPI5_TX_USING_DMA
  840. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  841. static struct dma_config spi5_dma_tx = SPI5_TX_DMA_CONFIG;
  842. spi_config[SPI5_INDEX].dma_tx = &spi5_dma_tx;
  843. #endif
  844. #ifdef BSP_SPI6_RX_USING_DMA
  845. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  846. static struct dma_config spi6_dma_rx = SPI6_RX_DMA_CONFIG;
  847. spi_config[SPI6_INDEX].dma_rx = &spi6_dma_rx;
  848. #endif
  849. #ifdef BSP_SPI6_TX_USING_DMA
  850. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  851. static struct dma_config spi6_dma_tx = SPI6_TX_DMA_CONFIG;
  852. spi_config[SPI6_INDEX].dma_tx = &spi6_dma_tx;
  853. #endif
  854. }
  855. void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
  856. {
  857. struct stm32_spi *spi_drv = rt_container_of(hspi, struct stm32_spi, handle);
  858. rt_completion_done(&spi_drv->cpt);
  859. }
  860. void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
  861. {
  862. struct stm32_spi *spi_drv = rt_container_of(hspi, struct stm32_spi, handle);
  863. rt_completion_done(&spi_drv->cpt);
  864. }
  865. void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
  866. {
  867. struct stm32_spi *spi_drv = rt_container_of(hspi, struct stm32_spi, handle);
  868. rt_completion_done(&spi_drv->cpt);
  869. }
  870. #if defined(SOC_SERIES_STM32F0)
  871. void SPI1_DMA_RX_TX_IRQHandler(void)
  872. {
  873. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_TX_USING_DMA)
  874. SPI1_DMA_TX_IRQHandler();
  875. #endif
  876. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_RX_USING_DMA)
  877. SPI1_DMA_RX_IRQHandler();
  878. #endif
  879. }
  880. void SPI2_DMA_RX_TX_IRQHandler(void)
  881. {
  882. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_TX_USING_DMA)
  883. SPI2_DMA_TX_IRQHandler();
  884. #endif
  885. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_RX_USING_DMA)
  886. SPI2_DMA_RX_IRQHandler();
  887. #endif
  888. }
  889. #endif /* SOC_SERIES_STM32F0 */
  890. int rt_hw_spi_init(void)
  891. {
  892. stm32_get_dma_info();
  893. return rt_hw_spi_bus_init();
  894. }
  895. INIT_BOARD_EXPORT(rt_hw_spi_init);
  896. #endif /* BSP_USING_SPI1 || BSP_USING_SPI2 || BSP_USING_SPI3 || BSP_USING_SPI4 || BSP_USING_SPI5 */
  897. #endif /* BSP_USING_SPI */