drv_spi.c 32 KB

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