drv_spi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  12. #include "board.h"
  13. #ifdef RT_USING_SPI
  14. #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)
  15. /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
  16. #include "drv_spi.h"
  17. #include "drv_config.h"
  18. #include <string.h>
  19. //#define DRV_DEBUG
  20. #define LOG_TAG "drv.spi"
  21. #include <drv_log.h>
  22. enum
  23. {
  24. #ifdef BSP_USING_SPI1
  25. SPI1_INDEX,
  26. #endif
  27. #ifdef BSP_USING_SPI2
  28. SPI2_INDEX,
  29. #endif
  30. #ifdef BSP_USING_SPI3
  31. SPI3_INDEX,
  32. #endif
  33. #ifdef BSP_USING_SPI4
  34. SPI4_INDEX,
  35. #endif
  36. #ifdef BSP_USING_SPI5
  37. SPI5_INDEX,
  38. #endif
  39. #ifdef BSP_USING_SPI6
  40. SPI6_INDEX,
  41. #endif
  42. };
  43. static struct stm32_spi_config spi_config[] =
  44. {
  45. #ifdef BSP_USING_SPI1
  46. SPI1_BUS_CONFIG,
  47. #endif
  48. #ifdef BSP_USING_SPI2
  49. SPI2_BUS_CONFIG,
  50. #endif
  51. #ifdef BSP_USING_SPI3
  52. SPI3_BUS_CONFIG,
  53. #endif
  54. #ifdef BSP_USING_SPI4
  55. SPI4_BUS_CONFIG,
  56. #endif
  57. #ifdef BSP_USING_SPI5
  58. SPI5_BUS_CONFIG,
  59. #endif
  60. #ifdef BSP_USING_SPI6
  61. SPI6_BUS_CONFIG,
  62. #endif
  63. };
  64. static struct stm32_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])] = {0};
  65. static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configuration *cfg)
  66. {
  67. RT_ASSERT(spi_drv != RT_NULL);
  68. RT_ASSERT(cfg != RT_NULL);
  69. SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
  70. if (cfg->mode & RT_SPI_SLAVE)
  71. {
  72. spi_handle->Init.Mode = SPI_MODE_SLAVE;
  73. }
  74. else
  75. {
  76. spi_handle->Init.Mode = SPI_MODE_MASTER;
  77. }
  78. if (cfg->mode & RT_SPI_3WIRE)
  79. {
  80. spi_handle->Init.Direction = SPI_DIRECTION_1LINE;
  81. }
  82. else
  83. {
  84. spi_handle->Init.Direction = SPI_DIRECTION_2LINES;
  85. }
  86. if (cfg->data_width == 8)
  87. {
  88. spi_handle->Init.DataSize = SPI_DATASIZE_8BIT;
  89. spi_handle->TxXferSize = 8;
  90. spi_handle->RxXferSize = 8;
  91. }
  92. else if (cfg->data_width == 16)
  93. {
  94. spi_handle->Init.DataSize = SPI_DATASIZE_16BIT;
  95. }
  96. else
  97. {
  98. return RT_EIO;
  99. }
  100. if (cfg->mode & RT_SPI_CPHA)
  101. {
  102. spi_handle->Init.CLKPhase = SPI_PHASE_2EDGE;
  103. }
  104. else
  105. {
  106. spi_handle->Init.CLKPhase = SPI_PHASE_1EDGE;
  107. }
  108. if (cfg->mode & RT_SPI_CPOL)
  109. {
  110. spi_handle->Init.CLKPolarity = SPI_POLARITY_HIGH;
  111. }
  112. else
  113. {
  114. spi_handle->Init.CLKPolarity = SPI_POLARITY_LOW;
  115. }
  116. if (cfg->mode & RT_SPI_NO_CS)
  117. {
  118. spi_handle->Init.NSS = SPI_NSS_SOFT;
  119. }
  120. else
  121. {
  122. spi_handle->Init.NSS = SPI_NSS_SOFT;
  123. }
  124. uint32_t SPI_APB_CLOCK;
  125. #ifdef SOC_SERIES_STM32F0
  126. SPI_APB_CLOCK = HAL_RCC_GetPCLK1Freq();
  127. #else
  128. SPI_APB_CLOCK = HAL_RCC_GetPCLK2Freq();
  129. #endif
  130. if (cfg->max_hz >= SPI_APB_CLOCK / 2)
  131. {
  132. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  133. }
  134. else if (cfg->max_hz >= SPI_APB_CLOCK / 4)
  135. {
  136. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  137. }
  138. else if (cfg->max_hz >= SPI_APB_CLOCK / 8)
  139. {
  140. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  141. }
  142. else if (cfg->max_hz >= SPI_APB_CLOCK / 16)
  143. {
  144. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  145. }
  146. else if (cfg->max_hz >= SPI_APB_CLOCK / 32)
  147. {
  148. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  149. }
  150. else if (cfg->max_hz >= SPI_APB_CLOCK / 64)
  151. {
  152. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
  153. }
  154. else if (cfg->max_hz >= SPI_APB_CLOCK / 128)
  155. {
  156. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
  157. }
  158. else
  159. {
  160. /* min prescaler 256 */
  161. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  162. }
  163. LOG_D("sys freq: %d, pclk2 freq: %d, SPI limiting freq: %d, BaudRatePrescaler: %d",
  164. HAL_RCC_GetSysClockFreq(),
  165. SPI_APB_CLOCK,
  166. cfg->max_hz,
  167. spi_handle->Init.BaudRatePrescaler);
  168. if (cfg->mode & RT_SPI_MSB)
  169. {
  170. spi_handle->Init.FirstBit = SPI_FIRSTBIT_MSB;
  171. }
  172. else
  173. {
  174. spi_handle->Init.FirstBit = SPI_FIRSTBIT_LSB;
  175. }
  176. spi_handle->Init.TIMode = SPI_TIMODE_DISABLE;
  177. spi_handle->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  178. spi_handle->State = HAL_SPI_STATE_RESET;
  179. #ifdef SOC_SERIES_STM32L4
  180. spi_handle->Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  181. #endif
  182. if (HAL_SPI_Init(spi_handle) != HAL_OK)
  183. {
  184. return RT_EIO;
  185. }
  186. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0)|| defined(SOC_SERIES_STM32F7)
  187. SET_BIT(spi_handle->Instance->CR2, SPI_RXFIFO_THRESHOLD_HF);
  188. #endif
  189. /* DMA configuration */
  190. if (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  191. {
  192. HAL_DMA_Init(&spi_drv->dma.handle_rx);
  193. __HAL_LINKDMA(&spi_drv->handle, hdmarx, spi_drv->dma.handle_rx);
  194. /* NVIC configuration for DMA transfer complete interrupt */
  195. HAL_NVIC_SetPriority(spi_drv->config->dma_rx->dma_irq, 0, 0);
  196. HAL_NVIC_EnableIRQ(spi_drv->config->dma_rx->dma_irq);
  197. }
  198. if (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  199. {
  200. HAL_DMA_Init(&spi_drv->dma.handle_tx);
  201. __HAL_LINKDMA(&spi_drv->handle, hdmatx, spi_drv->dma.handle_tx);
  202. /* NVIC configuration for DMA transfer complete interrupt */
  203. HAL_NVIC_SetPriority(spi_drv->config->dma_tx->dma_irq, 0, 1);
  204. HAL_NVIC_EnableIRQ(spi_drv->config->dma_tx->dma_irq);
  205. }
  206. __HAL_SPI_ENABLE(spi_handle);
  207. LOG_D("%s init done", spi_drv->config->bus_name);
  208. return RT_EOK;
  209. }
  210. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  211. {
  212. HAL_StatusTypeDef state;
  213. RT_ASSERT(device != RT_NULL);
  214. RT_ASSERT(device->bus != RT_NULL);
  215. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  216. RT_ASSERT(message != RT_NULL);
  217. struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
  218. SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
  219. struct stm32_hw_spi_cs *cs = device->parent.user_data;
  220. if (message->cs_take)
  221. {
  222. HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_RESET);
  223. }
  224. LOG_D("%s transfer prepare and start", spi_drv->config->bus_name);
  225. LOG_D("%s sendbuf: %X, recvbuf: %X, length: %d",
  226. spi_drv->config->bus_name,
  227. (uint32_t)message->send_buf,
  228. (uint32_t)message->recv_buf, message->length);
  229. if (message->length)
  230. {
  231. /* start once data exchange in DMA mode */
  232. if (message->send_buf && message->recv_buf)
  233. {
  234. if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG))
  235. {
  236. state = HAL_SPI_TransmitReceive_DMA(spi_handle, (uint8_t *)message->send_buf, (uint8_t *)message->recv_buf, message->length);
  237. }
  238. else
  239. {
  240. state = HAL_SPI_TransmitReceive(spi_handle, (uint8_t *)message->send_buf, (uint8_t *)message->recv_buf, message->length, 1000);
  241. }
  242. }
  243. else if (message->send_buf)
  244. {
  245. if (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  246. {
  247. state = HAL_SPI_Transmit_DMA(spi_handle, (uint8_t *)message->send_buf, message->length);
  248. }
  249. else
  250. {
  251. state = HAL_SPI_Transmit(spi_handle, (uint8_t *)message->send_buf, message->length, 1000);
  252. }
  253. }
  254. else
  255. {
  256. memset(message->recv_buf, 0xff, message->length);
  257. if (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  258. {
  259. state = HAL_SPI_Receive_DMA(spi_handle, (uint8_t *)message->recv_buf, message->length);
  260. }
  261. else
  262. {
  263. state = HAL_SPI_Receive(spi_handle, (uint8_t *)message->recv_buf, message->length, 1000);
  264. }
  265. }
  266. if (state != HAL_OK)
  267. {
  268. LOG_I("spi transfer error : %d", state);
  269. message->length = 0;
  270. spi_handle->State = HAL_SPI_STATE_READY;
  271. }
  272. else
  273. {
  274. LOG_D("%s transfer done", spi_drv->config->bus_name);
  275. }
  276. /* For simplicity reasons, this example is just waiting till the end of the
  277. transfer, but application may perform other tasks while transfer operation
  278. is ongoing. */
  279. while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY);
  280. }
  281. if (message->cs_release)
  282. {
  283. HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_SET);
  284. }
  285. return message->length;
  286. }
  287. static rt_err_t spi_configure(struct rt_spi_device *device,
  288. struct rt_spi_configuration *configuration)
  289. {
  290. RT_ASSERT(device != RT_NULL);
  291. RT_ASSERT(configuration != RT_NULL);
  292. struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
  293. spi_drv->cfg = configuration;
  294. return stm32_spi_init(spi_drv, configuration);
  295. }
  296. static const struct rt_spi_ops stm_spi_ops =
  297. {
  298. .configure = spi_configure,
  299. .xfer = spixfer,
  300. };
  301. static int rt_hw_spi_bus_init(void)
  302. {
  303. rt_err_t result;
  304. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  305. {
  306. spi_bus_obj[i].config = &spi_config[i];
  307. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i];
  308. spi_bus_obj[i].handle.Instance = spi_config[i].Instance;
  309. if (spi_bus_obj[i].spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  310. {
  311. /* Configure the DMA handler for Transmission process */
  312. spi_bus_obj[i].dma.handle_rx.Instance = spi_config[i].dma_rx->Instance;
  313. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  314. spi_bus_obj[i].dma.handle_rx.Init.Channel = spi_config[i].dma_rx->channel;
  315. #elif defined(SOC_SERIES_STM32L4)
  316. spi_bus_obj[i].dma.handle_rx.Init.Request = spi_config[i].dma_rx->request;
  317. #endif
  318. spi_bus_obj[i].dma.handle_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  319. spi_bus_obj[i].dma.handle_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  320. spi_bus_obj[i].dma.handle_rx.Init.MemInc = DMA_MINC_ENABLE;
  321. spi_bus_obj[i].dma.handle_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  322. spi_bus_obj[i].dma.handle_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  323. spi_bus_obj[i].dma.handle_rx.Init.Mode = DMA_NORMAL;
  324. spi_bus_obj[i].dma.handle_rx.Init.Priority = DMA_PRIORITY_HIGH;
  325. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  326. spi_bus_obj[i].dma.handle_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  327. spi_bus_obj[i].dma.handle_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  328. spi_bus_obj[i].dma.handle_rx.Init.MemBurst = DMA_MBURST_INC4;
  329. spi_bus_obj[i].dma.handle_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  330. #endif
  331. {
  332. rt_uint32_t tmpreg = 0x00U;
  333. #if defined(SOC_SERIES_STM32F1)
  334. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  335. SET_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  336. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  337. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4)
  338. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_rx->dma_rcc);
  339. /* Delay after an RCC peripheral clock enabling */
  340. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_rx->dma_rcc);
  341. #endif
  342. UNUSED(tmpreg); /* To avoid compiler warnings */
  343. }
  344. }
  345. if (spi_bus_obj[i].spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  346. {
  347. /* Configure the DMA handler for Transmission process */
  348. spi_bus_obj[i].dma.handle_tx.Instance = spi_config[i].dma_tx->Instance;
  349. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  350. spi_bus_obj[i].dma.handle_tx.Init.Channel = spi_config[i].dma_tx->channel;
  351. #elif defined(SOC_SERIES_STM32L4)
  352. spi_bus_obj[i].dma.handle_tx.Init.Request = spi_config[i].dma_tx->request;
  353. #endif
  354. spi_bus_obj[i].dma.handle_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  355. spi_bus_obj[i].dma.handle_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  356. spi_bus_obj[i].dma.handle_tx.Init.MemInc = DMA_MINC_ENABLE;
  357. spi_bus_obj[i].dma.handle_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  358. spi_bus_obj[i].dma.handle_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  359. spi_bus_obj[i].dma.handle_tx.Init.Mode = DMA_NORMAL;
  360. spi_bus_obj[i].dma.handle_tx.Init.Priority = DMA_PRIORITY_LOW;
  361. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  362. spi_bus_obj[i].dma.handle_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  363. spi_bus_obj[i].dma.handle_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  364. spi_bus_obj[i].dma.handle_tx.Init.MemBurst = DMA_MBURST_INC4;
  365. spi_bus_obj[i].dma.handle_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  366. #endif
  367. {
  368. rt_uint32_t tmpreg = 0x00U;
  369. #if defined(SOC_SERIES_STM32F1)
  370. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  371. SET_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  372. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  373. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4)
  374. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_tx->dma_rcc);
  375. /* Delay after an RCC peripheral clock enabling */
  376. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_tx->dma_rcc);
  377. #endif
  378. UNUSED(tmpreg); /* To avoid compiler warnings */
  379. }
  380. }
  381. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &stm_spi_ops);
  382. RT_ASSERT(result == RT_EOK);
  383. LOG_D("%s bus init done", spi_config[i].bus_name);
  384. }
  385. return result;
  386. }
  387. /**
  388. * Attach the spi device to SPI bus, this function must be used after initialization.
  389. */
  390. 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)
  391. {
  392. RT_ASSERT(bus_name != RT_NULL);
  393. RT_ASSERT(device_name != RT_NULL);
  394. rt_err_t result;
  395. struct rt_spi_device *spi_device;
  396. struct stm32_hw_spi_cs *cs_pin;
  397. /* initialize the cs pin && select the slave*/
  398. GPIO_InitTypeDef GPIO_Initure;
  399. GPIO_Initure.Pin = cs_gpio_pin;
  400. GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
  401. GPIO_Initure.Pull = GPIO_PULLUP;
  402. GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
  403. HAL_GPIO_Init(cs_gpiox, &GPIO_Initure);
  404. HAL_GPIO_WritePin(cs_gpiox, cs_gpio_pin, GPIO_PIN_SET);
  405. /* attach the device to spi bus*/
  406. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  407. RT_ASSERT(spi_device != RT_NULL);
  408. cs_pin = (struct stm32_hw_spi_cs *)rt_malloc(sizeof(struct stm32_hw_spi_cs));
  409. RT_ASSERT(cs_pin != RT_NULL);
  410. cs_pin->GPIOx = cs_gpiox;
  411. cs_pin->GPIO_Pin = cs_gpio_pin;
  412. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  413. if (result != RT_EOK)
  414. {
  415. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  416. }
  417. RT_ASSERT(result == RT_EOK);
  418. LOG_D("%s attach to %s done", device_name, bus_name);
  419. return result;
  420. }
  421. #if defined(BSP_SPI1_TX_USING_DMA) || defined(BSP_SPI1_RX_USING_DMA)
  422. void SPI1_IRQHandler(void)
  423. {
  424. /* enter interrupt */
  425. rt_interrupt_enter();
  426. HAL_SPI_IRQHandler(&spi_bus_obj[SPI1_INDEX].handle);
  427. /* leave interrupt */
  428. rt_interrupt_leave();
  429. }
  430. #endif
  431. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_RX_USING_DMA)
  432. /**
  433. * @brief This function handles DMA Rx interrupt request.
  434. * @param None
  435. * @retval None
  436. */
  437. void SPI1_DMA_RX_IRQHandler(void)
  438. {
  439. /* enter interrupt */
  440. rt_interrupt_enter();
  441. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_rx);
  442. /* leave interrupt */
  443. rt_interrupt_leave();
  444. }
  445. #endif
  446. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_TX_USING_DMA)
  447. /**
  448. * @brief This function handles DMA Tx interrupt request.
  449. * @param None
  450. * @retval None
  451. */
  452. void SPI1_DMA_TX_IRQHandler(void)
  453. {
  454. /* enter interrupt */
  455. rt_interrupt_enter();
  456. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_tx);
  457. /* leave interrupt */
  458. rt_interrupt_leave();
  459. }
  460. #endif /* defined(BSP_USING_SPI1) && defined(BSP_SPI_USING_DMA) */
  461. #if defined(BSP_SPI2_TX_USING_DMA) || defined(BSP_SPI2_RX_USING_DMA)
  462. void SPI2_IRQHandler(void)
  463. {
  464. /* enter interrupt */
  465. rt_interrupt_enter();
  466. HAL_SPI_IRQHandler(&spi_bus_obj[SPI2_INDEX].handle);
  467. /* leave interrupt */
  468. rt_interrupt_leave();
  469. }
  470. #endif
  471. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_RX_USING_DMA)
  472. /**
  473. * @brief This function handles DMA Rx interrupt request.
  474. * @param None
  475. * @retval None
  476. */
  477. void SPI2_DMA_RX_IRQHandler(void)
  478. {
  479. /* enter interrupt */
  480. rt_interrupt_enter();
  481. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_rx);
  482. /* leave interrupt */
  483. rt_interrupt_leave();
  484. }
  485. #endif
  486. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_TX_USING_DMA)
  487. /**
  488. * @brief This function handles DMA Tx interrupt request.
  489. * @param None
  490. * @retval None
  491. */
  492. void SPI2_DMA_TX_IRQHandler(void)
  493. {
  494. /* enter interrupt */
  495. rt_interrupt_enter();
  496. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_tx);
  497. /* leave interrupt */
  498. rt_interrupt_leave();
  499. }
  500. #endif /* defined(BSP_USING_SPI2) && defined(BSP_SPI_USING_DMA) */
  501. #if defined(BSP_SPI3_TX_USING_DMA) || defined(BSP_SPI3_RX_USING_DMA)
  502. void SPI3_IRQHandler(void)
  503. {
  504. /* enter interrupt */
  505. rt_interrupt_enter();
  506. HAL_SPI_IRQHandler(&spi_bus_obj[SPI3_INDEX].handle);
  507. /* leave interrupt */
  508. rt_interrupt_leave();
  509. }
  510. #endif
  511. #if defined(BSP_USING_SPI3) && defined(BSP_SPI3_RX_USING_DMA)
  512. /**
  513. * @brief This function handles DMA Rx interrupt request.
  514. * @param None
  515. * @retval None
  516. */
  517. void SPI3_DMA_RX_IRQHandler(void)
  518. {
  519. /* enter interrupt */
  520. rt_interrupt_enter();
  521. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_rx);
  522. /* leave interrupt */
  523. rt_interrupt_leave();
  524. }
  525. #endif
  526. #if defined(BSP_USING_SPI3) && defined(BSP_SPI3_TX_USING_DMA)
  527. /**
  528. * @brief This function handles DMA Tx interrupt request.
  529. * @param None
  530. * @retval None
  531. */
  532. void SPI3_DMA_TX_IRQHandler(void)
  533. {
  534. /* enter interrupt */
  535. rt_interrupt_enter();
  536. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_tx);
  537. /* leave interrupt */
  538. rt_interrupt_leave();
  539. }
  540. #endif /* defined(BSP_USING_SPI3) && defined(BSP_SPI_USING_DMA) */
  541. #if defined(BSP_SPI4_TX_USING_DMA) || defined(BSP_SPI4_RX_USING_DMA)
  542. void SPI4_IRQHandler(void)
  543. {
  544. /* enter interrupt */
  545. rt_interrupt_enter();
  546. HAL_SPI_IRQHandler(&spi_bus_obj[SPI4_INDEX].handle);
  547. /* leave interrupt */
  548. rt_interrupt_leave();
  549. }
  550. #endif
  551. #if defined(BSP_USING_SPI4) && defined(BSP_SPI4_RX_USING_DMA)
  552. /**
  553. * @brief This function handles DMA Rx interrupt request.
  554. * @param None
  555. * @retval None
  556. */
  557. void SPI4_DMA_RX_IRQHandler(void)
  558. {
  559. /* enter interrupt */
  560. rt_interrupt_enter();
  561. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_rx);
  562. /* leave interrupt */
  563. rt_interrupt_leave();
  564. }
  565. #endif
  566. #if defined(BSP_USING_SPI4) && defined(BSP_SPI4_TX_USING_DMA)
  567. /**
  568. * @brief This function handles DMA Tx interrupt request.
  569. * @param None
  570. * @retval None
  571. */
  572. void SPI4_DMA_TX_IRQHandler(void)
  573. {
  574. /* enter interrupt */
  575. rt_interrupt_enter();
  576. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_tx);
  577. /* leave interrupt */
  578. rt_interrupt_leave();
  579. }
  580. #endif /* defined(BSP_USING_SPI4) && defined(BSP_SPI_USING_DMA) */
  581. #if defined(BSP_SPI5_TX_USING_DMA) || defined(BSP_SPI5_RX_USING_DMA)
  582. void SPI5_IRQHandler(void)
  583. {
  584. /* enter interrupt */
  585. rt_interrupt_enter();
  586. HAL_SPI_IRQHandler(&spi_bus_obj[SPI5_INDEX].handle);
  587. /* leave interrupt */
  588. rt_interrupt_leave();
  589. }
  590. #endif
  591. #if defined(BSP_USING_SPI5) && defined(BSP_SPI5_RX_USING_DMA)
  592. /**
  593. * @brief This function handles DMA Rx interrupt request.
  594. * @param None
  595. * @retval None
  596. */
  597. void SPI5_DMA_RX_IRQHandler(void)
  598. {
  599. /* enter interrupt */
  600. rt_interrupt_enter();
  601. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_rx);
  602. /* leave interrupt */
  603. rt_interrupt_leave();
  604. }
  605. #endif
  606. #if defined(BSP_USING_SPI5) && defined(BSP_SPI5_TX_USING_DMA)
  607. /**
  608. * @brief This function handles DMA Tx interrupt request.
  609. * @param None
  610. * @retval None
  611. */
  612. void SPI5_DMA_TX_IRQHandler(void)
  613. {
  614. /* enter interrupt */
  615. rt_interrupt_enter();
  616. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_tx);
  617. /* leave interrupt */
  618. rt_interrupt_leave();
  619. }
  620. #endif /* defined(BSP_USING_SPI5) && defined(BSP_SPI_USING_DMA) */
  621. #if defined(BSP_USING_SPI6) && defined(BSP_SPI6_RX_USING_DMA)
  622. /**
  623. * @brief This function handles DMA Rx interrupt request.
  624. * @param None
  625. * @retval None
  626. */
  627. void SPI6_DMA_RX_IRQHandler(void)
  628. {
  629. /* enter interrupt */
  630. rt_interrupt_enter();
  631. HAL_DMA_IRQHandler(&spi_bus_obj[SPI6_INDEX].dma.handle_rx);
  632. /* leave interrupt */
  633. rt_interrupt_leave();
  634. }
  635. #endif
  636. #if defined(BSP_USING_SPI6) && defined(BSP_SPI6_TX_USING_DMA)
  637. /**
  638. * @brief This function handles DMA Tx interrupt request.
  639. * @param None
  640. * @retval None
  641. */
  642. void SPI6_DMA_TX_IRQHandler(void)
  643. {
  644. /* enter interrupt */
  645. rt_interrupt_enter();
  646. HAL_DMA_IRQHandler(&spi_bus_obj[SPI6_INDEX].dma.handle_tx);
  647. /* leave interrupt */
  648. rt_interrupt_leave();
  649. }
  650. #endif /* defined(BSP_USING_SPI6) && defined(BSP_SPI_USING_DMA) */
  651. static void stm32_get_dma_info(void)
  652. {
  653. #ifdef BSP_SPI1_RX_USING_DMA
  654. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  655. static struct dma_config spi1_dma_rx = SPI1_RX_DMA_CONFIG;
  656. spi_config[SPI1_INDEX].dma_rx = &spi1_dma_rx;
  657. #endif
  658. #ifdef BSP_SPI1_TX_USING_DMA
  659. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  660. static struct dma_config spi1_dma_tx = SPI1_TX_DMA_CONFIG;
  661. spi_config[SPI1_INDEX].dma_tx = &spi1_dma_tx;
  662. #endif
  663. #ifdef BSP_SPI2_RX_USING_DMA
  664. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  665. static struct dma_config spi2_dma_rx = SPI2_RX_DMA_CONFIG;
  666. spi_config[SPI2_INDEX].dma_rx = &spi2_dma_rx;
  667. #endif
  668. #ifdef BSP_SPI2_TX_USING_DMA
  669. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  670. static struct dma_config spi2_dma_tx = SPI2_TX_DMA_CONFIG;
  671. spi_config[SPI2_INDEX].dma_tx = &spi2_dma_tx;
  672. #endif
  673. #ifdef BSP_SPI3_RX_USING_DMA
  674. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  675. static struct dma_config spi3_dma_rx = SPI3_RX_DMA_CONFIG;
  676. spi_config[SPI3_INDEX].dma_rx = &spi3_dma_rx;
  677. #endif
  678. #ifdef BSP_SPI3_TX_USING_DMA
  679. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  680. static struct dma_config spi3_dma_tx = SPI3_TX_DMA_CONFIG;
  681. spi_config[SPI3_INDEX].dma_tx = &spi3_dma_tx;
  682. #endif
  683. #ifdef BSP_SPI4_RX_USING_DMA
  684. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  685. static struct dma_config spi4_dma_rx = SPI4_RX_DMA_CONFIG;
  686. spi_config[SPI4_INDEX].dma_rx = &spi4_dma_rx;
  687. #endif
  688. #ifdef BSP_SPI4_TX_USING_DMA
  689. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  690. static struct dma_config spi4_dma_tx = SPI4_TX_DMA_CONFIG;
  691. spi_config[SPI4_INDEX].dma_tx = &spi4_dma_tx;
  692. #endif
  693. #ifdef BSP_SPI5_RX_USING_DMA
  694. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  695. static struct dma_config spi5_dma_rx = SPI5_RX_DMA_CONFIG;
  696. spi_config[SPI5_INDEX].dma_rx = &spi5_dma_rx;
  697. #endif
  698. #ifdef BSP_SPI5_TX_USING_DMA
  699. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  700. static struct dma_config spi5_dma_tx = SPI5_TX_DMA_CONFIG;
  701. spi_config[SPI5_INDEX].dma_tx = &spi5_dma_tx;
  702. #endif
  703. #ifdef BSP_SPI6_RX_USING_DMA
  704. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  705. static struct dma_config spi6_dma_rx = SPI6_RX_DMA_CONFIG;
  706. spi_config[SPI6_INDEX].dma_rx = &spi6_dma_rx;
  707. #endif
  708. #ifdef BSP_SPI6_TX_USING_DMA
  709. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  710. static struct dma_config spi6_dma_tx = SPI6_TX_DMA_CONFIG;
  711. spi_config[SPI6_INDEX].dma_tx = &spi6_dma_tx;
  712. #endif
  713. }
  714. int rt_hw_spi_init(void)
  715. {
  716. stm32_get_dma_info();
  717. return rt_hw_spi_bus_init();
  718. }
  719. INIT_BOARD_EXPORT(rt_hw_spi_init);
  720. #endif /* BSP_USING_SPI1 || BSP_USING_SPI2 || BSP_USING_SPI3 || BSP_USING_SPI4 || BSP_USING_SPI5 */
  721. #endif /* RT_USING_SPI */