drv_spi.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. state = HAL_SPI_Transmit_DMA(spi_handle, (uint8_t *)send_buf, send_length);
  326. }
  327. else if ((spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG))
  328. {
  329. state = HAL_ERROR;
  330. 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.");
  331. break;
  332. }
  333. else
  334. {
  335. state = HAL_SPI_TransmitReceive(spi_handle, (uint8_t *)send_buf, (uint8_t *)recv_buf, send_length, 1000);
  336. }
  337. }
  338. else if (message->send_buf)
  339. {
  340. if (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  341. {
  342. state = HAL_SPI_Transmit_DMA(spi_handle, (uint8_t *)send_buf, send_length);
  343. }
  344. else
  345. {
  346. state = HAL_SPI_Transmit(spi_handle, (uint8_t *)send_buf, send_length, 1000);
  347. }
  348. if (message->cs_release && (device->config.mode & RT_SPI_3WIRE))
  349. {
  350. /* release the CS by disable SPI when using 3 wires SPI */
  351. __HAL_SPI_DISABLE(spi_handle);
  352. }
  353. }
  354. else if(message->recv_buf)
  355. {
  356. rt_memset((uint8_t *)recv_buf, 0xff, send_length);
  357. if (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  358. {
  359. state = HAL_SPI_Receive_DMA(spi_handle, (uint8_t *)recv_buf, send_length);
  360. }
  361. else
  362. {
  363. /* clear the old error flag */
  364. __HAL_SPI_CLEAR_OVRFLAG(spi_handle);
  365. state = HAL_SPI_Receive(spi_handle, (uint8_t *)recv_buf, send_length, 1000);
  366. }
  367. }
  368. else
  369. {
  370. state = HAL_ERROR;
  371. LOG_E("message->send_buf and message->recv_buf are both NULL!");
  372. }
  373. if (state != HAL_OK)
  374. {
  375. LOG_E("SPI transfer error: %d", state);
  376. message->length = 0;
  377. spi_handle->State = HAL_SPI_STATE_READY;
  378. break;
  379. }
  380. else
  381. {
  382. LOG_D("%s transfer done", spi_drv->config->bus_name);
  383. }
  384. /* For simplicity reasons, this example is just waiting till the end of the
  385. transfer, but application may perform other tasks while transfer operation
  386. is ongoing. */
  387. if (spi_drv->spi_dma_flag & (SPI_USING_TX_DMA_FLAG | SPI_USING_RX_DMA_FLAG))
  388. {
  389. /* blocking the thread,and the other tasks can run */
  390. if (rt_completion_wait(&spi_drv->cpt, 1000) != RT_EOK)
  391. {
  392. state = HAL_ERROR;
  393. LOG_E("wait for DMA interrupt overtime!");
  394. break;
  395. }
  396. }
  397. else
  398. {
  399. while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY);
  400. }
  401. #if defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32F7)
  402. if(dma_buf)
  403. {
  404. if(recv_buf)
  405. {
  406. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, dma_buf, send_length);
  407. rt_memcpy(recv_buf, dma_buf,send_length);
  408. }
  409. rt_free_align(dma_buf);
  410. }
  411. #endif /* SOC_SERIES_STM32H7 || SOC_SERIES_STM32F7 */
  412. }
  413. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  414. {
  415. if (device->config.mode & RT_SPI_CS_HIGH)
  416. rt_pin_write(device->cs_pin, PIN_LOW);
  417. else
  418. rt_pin_write(device->cs_pin, PIN_HIGH);
  419. }
  420. if(state != HAL_OK)
  421. {
  422. return -RT_ERROR;
  423. }
  424. return message->length;
  425. }
  426. static rt_err_t spi_configure(struct rt_spi_device *device,
  427. struct rt_spi_configuration *configuration)
  428. {
  429. RT_ASSERT(device != RT_NULL);
  430. RT_ASSERT(configuration != RT_NULL);
  431. struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
  432. spi_drv->cfg = configuration;
  433. return stm32_spi_init(spi_drv, configuration);
  434. }
  435. static const struct rt_spi_ops stm_spi_ops =
  436. {
  437. .configure = spi_configure,
  438. .xfer = spixfer,
  439. };
  440. static int rt_hw_spi_bus_init(void)
  441. {
  442. rt_err_t result;
  443. for (rt_size_t i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  444. {
  445. spi_bus_obj[i].config = &spi_config[i];
  446. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i];
  447. spi_bus_obj[i].handle.Instance = spi_config[i].Instance;
  448. if (spi_bus_obj[i].spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  449. {
  450. /* Configure the DMA handler for Transmission process */
  451. spi_bus_obj[i].dma.handle_rx.Instance = spi_config[i].dma_rx->Instance;
  452. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  453. spi_bus_obj[i].dma.handle_rx.Init.Channel = spi_config[i].dma_rx->channel;
  454. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32WB) || defined(SOC_SERIES_STM32H7)
  455. spi_bus_obj[i].dma.handle_rx.Init.Request = spi_config[i].dma_rx->request;
  456. #endif
  457. #ifndef SOC_SERIES_STM32U5
  458. spi_bus_obj[i].dma.handle_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  459. spi_bus_obj[i].dma.handle_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  460. spi_bus_obj[i].dma.handle_rx.Init.MemInc = DMA_MINC_ENABLE;
  461. spi_bus_obj[i].dma.handle_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  462. spi_bus_obj[i].dma.handle_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  463. spi_bus_obj[i].dma.handle_rx.Init.Mode = DMA_NORMAL;
  464. spi_bus_obj[i].dma.handle_rx.Init.Priority = DMA_PRIORITY_HIGH;
  465. #endif
  466. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32H7)
  467. spi_bus_obj[i].dma.handle_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  468. spi_bus_obj[i].dma.handle_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  469. spi_bus_obj[i].dma.handle_rx.Init.MemBurst = DMA_MBURST_INC4;
  470. spi_bus_obj[i].dma.handle_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  471. #endif
  472. {
  473. rt_uint32_t tmpreg = 0x00U;
  474. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32F0)
  475. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  476. SET_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  477. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  478. #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)
  479. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_rx->dma_rcc);
  480. /* Delay after an RCC peripheral clock enabling */
  481. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_rx->dma_rcc);
  482. #elif defined(SOC_SERIES_STM32MP1)
  483. __HAL_RCC_DMAMUX_CLK_ENABLE();
  484. SET_BIT(RCC->MP_AHB2ENSETR, spi_config[i].dma_rx->dma_rcc);
  485. tmpreg = READ_BIT(RCC->MP_AHB2ENSETR, spi_config[i].dma_rx->dma_rcc);
  486. #endif
  487. UNUSED(tmpreg); /* To avoid compiler warnings */
  488. }
  489. }
  490. if (spi_bus_obj[i].spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  491. {
  492. /* Configure the DMA handler for Transmission process */
  493. spi_bus_obj[i].dma.handle_tx.Instance = spi_config[i].dma_tx->Instance;
  494. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  495. spi_bus_obj[i].dma.handle_tx.Init.Channel = spi_config[i].dma_tx->channel;
  496. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32WB) || defined(SOC_SERIES_STM32H7)
  497. spi_bus_obj[i].dma.handle_tx.Init.Request = spi_config[i].dma_tx->request;
  498. #endif
  499. #ifndef SOC_SERIES_STM32U5
  500. spi_bus_obj[i].dma.handle_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  501. spi_bus_obj[i].dma.handle_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  502. spi_bus_obj[i].dma.handle_tx.Init.MemInc = DMA_MINC_ENABLE;
  503. spi_bus_obj[i].dma.handle_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  504. spi_bus_obj[i].dma.handle_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  505. spi_bus_obj[i].dma.handle_tx.Init.Mode = DMA_NORMAL;
  506. spi_bus_obj[i].dma.handle_tx.Init.Priority = DMA_PRIORITY_LOW;
  507. #endif
  508. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32H7)
  509. spi_bus_obj[i].dma.handle_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  510. spi_bus_obj[i].dma.handle_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  511. spi_bus_obj[i].dma.handle_tx.Init.MemBurst = DMA_MBURST_INC4;
  512. spi_bus_obj[i].dma.handle_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  513. #endif
  514. {
  515. rt_uint32_t tmpreg = 0x00U;
  516. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32F0)
  517. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  518. SET_BIT(RCC->AHBENR, spi_config[i].dma_tx->dma_rcc);
  519. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_tx->dma_rcc);
  520. #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)
  521. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_tx->dma_rcc);
  522. /* Delay after an RCC peripheral clock enabling */
  523. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_tx->dma_rcc);
  524. #elif defined(SOC_SERIES_STM32MP1)
  525. __HAL_RCC_DMAMUX_CLK_ENABLE();
  526. SET_BIT(RCC->MP_AHB2ENSETR, spi_config[i].dma_tx->dma_rcc);
  527. tmpreg = READ_BIT(RCC->MP_AHB2ENSETR, spi_config[i].dma_tx->dma_rcc);
  528. #endif
  529. UNUSED(tmpreg); /* To avoid compiler warnings */
  530. }
  531. }
  532. /* initialize completion object */
  533. rt_completion_init(&spi_bus_obj[i].cpt);
  534. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &stm_spi_ops);
  535. RT_ASSERT(result == RT_EOK);
  536. LOG_D("%s bus init done", spi_config[i].bus_name);
  537. }
  538. return result;
  539. }
  540. /**
  541. * Attach the spi device to SPI bus, this function must be used after initialization.
  542. */
  543. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
  544. {
  545. RT_ASSERT(bus_name != RT_NULL);
  546. RT_ASSERT(device_name != RT_NULL);
  547. rt_err_t result;
  548. struct rt_spi_device *spi_device;
  549. /* attach the device to spi bus*/
  550. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  551. RT_ASSERT(spi_device != RT_NULL);
  552. result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
  553. if (result != RT_EOK)
  554. {
  555. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  556. }
  557. RT_ASSERT(result == RT_EOK);
  558. LOG_D("%s attach to %s done", device_name, bus_name);
  559. return result;
  560. }
  561. #if defined(BSP_SPI1_TX_USING_DMA) || defined(BSP_SPI1_RX_USING_DMA)
  562. void SPI1_IRQHandler(void)
  563. {
  564. /* enter interrupt */
  565. rt_interrupt_enter();
  566. HAL_SPI_IRQHandler(&spi_bus_obj[SPI1_INDEX].handle);
  567. /* leave interrupt */
  568. rt_interrupt_leave();
  569. }
  570. #endif
  571. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_RX_USING_DMA)
  572. /**
  573. * @brief This function handles DMA Rx interrupt request.
  574. * @param None
  575. * @retval None
  576. */
  577. void SPI1_DMA_RX_IRQHandler(void)
  578. {
  579. /* enter interrupt */
  580. rt_interrupt_enter();
  581. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_rx);
  582. /* leave interrupt */
  583. rt_interrupt_leave();
  584. }
  585. #endif
  586. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_TX_USING_DMA)
  587. /**
  588. * @brief This function handles DMA Tx interrupt request.
  589. * @param None
  590. * @retval None
  591. */
  592. void SPI1_DMA_TX_IRQHandler(void)
  593. {
  594. /* enter interrupt */
  595. rt_interrupt_enter();
  596. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_tx);
  597. /* leave interrupt */
  598. rt_interrupt_leave();
  599. }
  600. #endif /* defined(BSP_USING_SPI1) && defined(BSP_SPI_USING_DMA) */
  601. #if defined(BSP_SPI2_TX_USING_DMA) || defined(BSP_SPI2_RX_USING_DMA)
  602. void SPI2_IRQHandler(void)
  603. {
  604. /* enter interrupt */
  605. rt_interrupt_enter();
  606. HAL_SPI_IRQHandler(&spi_bus_obj[SPI2_INDEX].handle);
  607. /* leave interrupt */
  608. rt_interrupt_leave();
  609. }
  610. #endif
  611. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_RX_USING_DMA)
  612. /**
  613. * @brief This function handles DMA Rx interrupt request.
  614. * @param None
  615. * @retval None
  616. */
  617. void SPI2_DMA_RX_IRQHandler(void)
  618. {
  619. /* enter interrupt */
  620. rt_interrupt_enter();
  621. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_rx);
  622. /* leave interrupt */
  623. rt_interrupt_leave();
  624. }
  625. #endif
  626. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_TX_USING_DMA)
  627. /**
  628. * @brief This function handles DMA Tx interrupt request.
  629. * @param None
  630. * @retval None
  631. */
  632. void SPI2_DMA_TX_IRQHandler(void)
  633. {
  634. /* enter interrupt */
  635. rt_interrupt_enter();
  636. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_tx);
  637. /* leave interrupt */
  638. rt_interrupt_leave();
  639. }
  640. #endif /* defined(BSP_USING_SPI2) && defined(BSP_SPI_USING_DMA) */
  641. #if defined(BSP_SPI3_TX_USING_DMA) || defined(BSP_SPI3_RX_USING_DMA)
  642. void SPI3_IRQHandler(void)
  643. {
  644. /* enter interrupt */
  645. rt_interrupt_enter();
  646. HAL_SPI_IRQHandler(&spi_bus_obj[SPI3_INDEX].handle);
  647. /* leave interrupt */
  648. rt_interrupt_leave();
  649. }
  650. #endif
  651. #if defined(BSP_USING_SPI3) && defined(BSP_SPI3_RX_USING_DMA)
  652. /**
  653. * @brief This function handles DMA Rx interrupt request.
  654. * @param None
  655. * @retval None
  656. */
  657. void SPI3_DMA_RX_IRQHandler(void)
  658. {
  659. /* enter interrupt */
  660. rt_interrupt_enter();
  661. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_rx);
  662. /* leave interrupt */
  663. rt_interrupt_leave();
  664. }
  665. #endif
  666. #if defined(BSP_USING_SPI3) && defined(BSP_SPI3_TX_USING_DMA)
  667. /**
  668. * @brief This function handles DMA Tx interrupt request.
  669. * @param None
  670. * @retval None
  671. */
  672. void SPI3_DMA_TX_IRQHandler(void)
  673. {
  674. /* enter interrupt */
  675. rt_interrupt_enter();
  676. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_tx);
  677. /* leave interrupt */
  678. rt_interrupt_leave();
  679. }
  680. #endif /* defined(BSP_USING_SPI3) && defined(BSP_SPI_USING_DMA) */
  681. #if defined(BSP_SPI4_TX_USING_DMA) || defined(BSP_SPI4_RX_USING_DMA)
  682. void SPI4_IRQHandler(void)
  683. {
  684. /* enter interrupt */
  685. rt_interrupt_enter();
  686. HAL_SPI_IRQHandler(&spi_bus_obj[SPI4_INDEX].handle);
  687. /* leave interrupt */
  688. rt_interrupt_leave();
  689. }
  690. #endif
  691. #if defined(BSP_USING_SPI4) && defined(BSP_SPI4_RX_USING_DMA)
  692. /**
  693. * @brief This function handles DMA Rx interrupt request.
  694. * @param None
  695. * @retval None
  696. */
  697. void SPI4_DMA_RX_IRQHandler(void)
  698. {
  699. /* enter interrupt */
  700. rt_interrupt_enter();
  701. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_rx);
  702. /* leave interrupt */
  703. rt_interrupt_leave();
  704. }
  705. #endif
  706. #if defined(BSP_USING_SPI4) && defined(BSP_SPI4_TX_USING_DMA)
  707. /**
  708. * @brief This function handles DMA Tx interrupt request.
  709. * @param None
  710. * @retval None
  711. */
  712. void SPI4_DMA_TX_IRQHandler(void)
  713. {
  714. /* enter interrupt */
  715. rt_interrupt_enter();
  716. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_tx);
  717. /* leave interrupt */
  718. rt_interrupt_leave();
  719. }
  720. #endif /* defined(BSP_USING_SPI4) && defined(BSP_SPI_USING_DMA) */
  721. #if defined(BSP_SPI5_TX_USING_DMA) || defined(BSP_SPI5_RX_USING_DMA)
  722. void SPI5_IRQHandler(void)
  723. {
  724. /* enter interrupt */
  725. rt_interrupt_enter();
  726. HAL_SPI_IRQHandler(&spi_bus_obj[SPI5_INDEX].handle);
  727. /* leave interrupt */
  728. rt_interrupt_leave();
  729. }
  730. #endif
  731. #if defined(BSP_USING_SPI5) && defined(BSP_SPI5_RX_USING_DMA)
  732. /**
  733. * @brief This function handles DMA Rx interrupt request.
  734. * @param None
  735. * @retval None
  736. */
  737. void SPI5_DMA_RX_IRQHandler(void)
  738. {
  739. /* enter interrupt */
  740. rt_interrupt_enter();
  741. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_rx);
  742. /* leave interrupt */
  743. rt_interrupt_leave();
  744. }
  745. #endif
  746. #if defined(BSP_USING_SPI5) && defined(BSP_SPI5_TX_USING_DMA)
  747. /**
  748. * @brief This function handles DMA Tx interrupt request.
  749. * @param None
  750. * @retval None
  751. */
  752. void SPI5_DMA_TX_IRQHandler(void)
  753. {
  754. /* enter interrupt */
  755. rt_interrupt_enter();
  756. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_tx);
  757. /* leave interrupt */
  758. rt_interrupt_leave();
  759. }
  760. #endif /* defined(BSP_USING_SPI5) && defined(BSP_SPI_USING_DMA) */
  761. #if defined(BSP_USING_SPI6) && defined(BSP_SPI6_RX_USING_DMA)
  762. /**
  763. * @brief This function handles DMA Rx interrupt request.
  764. * @param None
  765. * @retval None
  766. */
  767. void SPI6_DMA_RX_IRQHandler(void)
  768. {
  769. /* enter interrupt */
  770. rt_interrupt_enter();
  771. HAL_DMA_IRQHandler(&spi_bus_obj[SPI6_INDEX].dma.handle_rx);
  772. /* leave interrupt */
  773. rt_interrupt_leave();
  774. }
  775. #endif
  776. #if defined(BSP_USING_SPI6) && defined(BSP_SPI6_TX_USING_DMA)
  777. /**
  778. * @brief This function handles DMA Tx interrupt request.
  779. * @param None
  780. * @retval None
  781. */
  782. void SPI6_DMA_TX_IRQHandler(void)
  783. {
  784. /* enter interrupt */
  785. rt_interrupt_enter();
  786. HAL_DMA_IRQHandler(&spi_bus_obj[SPI6_INDEX].dma.handle_tx);
  787. /* leave interrupt */
  788. rt_interrupt_leave();
  789. }
  790. #endif /* defined(BSP_USING_SPI6) && defined(BSP_SPI_USING_DMA) */
  791. static void stm32_get_dma_info(void)
  792. {
  793. #ifdef BSP_SPI1_RX_USING_DMA
  794. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  795. static struct dma_config spi1_dma_rx = SPI1_RX_DMA_CONFIG;
  796. spi_config[SPI1_INDEX].dma_rx = &spi1_dma_rx;
  797. #endif
  798. #ifdef BSP_SPI1_TX_USING_DMA
  799. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  800. static struct dma_config spi1_dma_tx = SPI1_TX_DMA_CONFIG;
  801. spi_config[SPI1_INDEX].dma_tx = &spi1_dma_tx;
  802. #endif
  803. #ifdef BSP_SPI2_RX_USING_DMA
  804. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  805. static struct dma_config spi2_dma_rx = SPI2_RX_DMA_CONFIG;
  806. spi_config[SPI2_INDEX].dma_rx = &spi2_dma_rx;
  807. #endif
  808. #ifdef BSP_SPI2_TX_USING_DMA
  809. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  810. static struct dma_config spi2_dma_tx = SPI2_TX_DMA_CONFIG;
  811. spi_config[SPI2_INDEX].dma_tx = &spi2_dma_tx;
  812. #endif
  813. #ifdef BSP_SPI3_RX_USING_DMA
  814. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  815. static struct dma_config spi3_dma_rx = SPI3_RX_DMA_CONFIG;
  816. spi_config[SPI3_INDEX].dma_rx = &spi3_dma_rx;
  817. #endif
  818. #ifdef BSP_SPI3_TX_USING_DMA
  819. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  820. static struct dma_config spi3_dma_tx = SPI3_TX_DMA_CONFIG;
  821. spi_config[SPI3_INDEX].dma_tx = &spi3_dma_tx;
  822. #endif
  823. #ifdef BSP_SPI4_RX_USING_DMA
  824. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  825. static struct dma_config spi4_dma_rx = SPI4_RX_DMA_CONFIG;
  826. spi_config[SPI4_INDEX].dma_rx = &spi4_dma_rx;
  827. #endif
  828. #ifdef BSP_SPI4_TX_USING_DMA
  829. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  830. static struct dma_config spi4_dma_tx = SPI4_TX_DMA_CONFIG;
  831. spi_config[SPI4_INDEX].dma_tx = &spi4_dma_tx;
  832. #endif
  833. #ifdef BSP_SPI5_RX_USING_DMA
  834. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  835. static struct dma_config spi5_dma_rx = SPI5_RX_DMA_CONFIG;
  836. spi_config[SPI5_INDEX].dma_rx = &spi5_dma_rx;
  837. #endif
  838. #ifdef BSP_SPI5_TX_USING_DMA
  839. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  840. static struct dma_config spi5_dma_tx = SPI5_TX_DMA_CONFIG;
  841. spi_config[SPI5_INDEX].dma_tx = &spi5_dma_tx;
  842. #endif
  843. #ifdef BSP_SPI6_RX_USING_DMA
  844. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  845. static struct dma_config spi6_dma_rx = SPI6_RX_DMA_CONFIG;
  846. spi_config[SPI6_INDEX].dma_rx = &spi6_dma_rx;
  847. #endif
  848. #ifdef BSP_SPI6_TX_USING_DMA
  849. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  850. static struct dma_config spi6_dma_tx = SPI6_TX_DMA_CONFIG;
  851. spi_config[SPI6_INDEX].dma_tx = &spi6_dma_tx;
  852. #endif
  853. }
  854. void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
  855. {
  856. struct stm32_spi *spi_drv = rt_container_of(hspi, struct stm32_spi, handle);
  857. rt_completion_done(&spi_drv->cpt);
  858. }
  859. void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
  860. {
  861. struct stm32_spi *spi_drv = rt_container_of(hspi, struct stm32_spi, handle);
  862. rt_completion_done(&spi_drv->cpt);
  863. }
  864. void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
  865. {
  866. struct stm32_spi *spi_drv = rt_container_of(hspi, struct stm32_spi, handle);
  867. rt_completion_done(&spi_drv->cpt);
  868. }
  869. #if defined(SOC_SERIES_STM32F0)
  870. void SPI1_DMA_RX_TX_IRQHandler(void)
  871. {
  872. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_TX_USING_DMA)
  873. SPI1_DMA_TX_IRQHandler();
  874. #endif
  875. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_RX_USING_DMA)
  876. SPI1_DMA_RX_IRQHandler();
  877. #endif
  878. }
  879. void SPI2_DMA_RX_TX_IRQHandler(void)
  880. {
  881. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_TX_USING_DMA)
  882. SPI2_DMA_TX_IRQHandler();
  883. #endif
  884. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_RX_USING_DMA)
  885. SPI2_DMA_RX_IRQHandler();
  886. #endif
  887. }
  888. #endif /* SOC_SERIES_STM32F0 */
  889. int rt_hw_spi_init(void)
  890. {
  891. stm32_get_dma_info();
  892. return rt_hw_spi_bus_init();
  893. }
  894. INIT_BOARD_EXPORT(rt_hw_spi_init);
  895. #endif /* BSP_USING_SPI1 || BSP_USING_SPI2 || BSP_USING_SPI3 || BSP_USING_SPI4 || BSP_USING_SPI5 */
  896. #endif /* BSP_USING_SPI */