drv_spi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 change to new framework
  9. */
  10. #include "board.h"
  11. #ifdef RT_USING_SPI
  12. #if defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) || defined(BSP_USING_SPI4) || defined(BSP_USING_SPI5)
  13. /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
  14. #include "drv_spi.h"
  15. #include "drv_config.h"
  16. //#define DRV_DEBUG
  17. #define LOG_TAG "drv.spi"
  18. #include <drv_log.h>
  19. enum
  20. {
  21. #ifdef BSP_USING_SPI1
  22. SPI1_INDEX,
  23. #endif
  24. #ifdef BSP_USING_SPI2
  25. SPI2_INDEX,
  26. #endif
  27. #ifdef BSP_USING_SPI3
  28. SPI3_INDEX,
  29. #endif
  30. #ifdef BSP_USING_SPI4
  31. SPI4_INDEX,
  32. #endif
  33. #ifdef BSP_USING_SPI5
  34. SPI5_INDEX,
  35. #endif
  36. };
  37. static struct stm32_spi_config spi_config[] =
  38. {
  39. #ifdef BSP_USING_SPI1
  40. SPI1_BUS_CONFIG,
  41. #endif
  42. #ifdef BSP_USING_SPI2
  43. SPI2_BUS_CONFIG,
  44. #endif
  45. #ifdef BSP_USING_SPI3
  46. SPI3_BUS_CONFIG,
  47. #endif
  48. #ifdef BSP_USING_SPI4
  49. SPI4_BUS_CONFIG,
  50. #endif
  51. #ifdef BSP_USING_SPI5
  52. SPI5_BUS_CONFIG,
  53. #endif
  54. };
  55. static struct stm32_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])];
  56. static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configuration *cfg)
  57. {
  58. RT_ASSERT(spi_drv != RT_NULL);
  59. RT_ASSERT(cfg != RT_NULL);
  60. SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
  61. if (cfg->mode & RT_SPI_SLAVE)
  62. {
  63. spi_handle->Init.Mode = SPI_MODE_SLAVE;
  64. }
  65. else
  66. {
  67. spi_handle->Init.Mode = SPI_MODE_MASTER;
  68. }
  69. if (cfg->mode & RT_SPI_3WIRE)
  70. {
  71. spi_handle->Init.Direction = SPI_DIRECTION_1LINE;
  72. }
  73. else
  74. {
  75. spi_handle->Init.Direction = SPI_DIRECTION_2LINES;
  76. }
  77. if (cfg->data_width == 8)
  78. {
  79. spi_handle->Init.DataSize = SPI_DATASIZE_8BIT;
  80. spi_handle->TxXferSize = 8;
  81. spi_handle->RxXferSize = 8;
  82. }
  83. else if (cfg->data_width == 16)
  84. {
  85. spi_handle->Init.DataSize = SPI_DATASIZE_16BIT;
  86. }
  87. else
  88. {
  89. return RT_EIO;
  90. }
  91. if (cfg->mode & RT_SPI_CPHA)
  92. {
  93. spi_handle->Init.CLKPhase = SPI_PHASE_2EDGE;
  94. }
  95. else
  96. {
  97. spi_handle->Init.CLKPhase = SPI_PHASE_1EDGE;
  98. }
  99. if (cfg->mode & RT_SPI_CPOL)
  100. {
  101. spi_handle->Init.CLKPolarity = SPI_POLARITY_HIGH;
  102. }
  103. else
  104. {
  105. spi_handle->Init.CLKPolarity = SPI_POLARITY_LOW;
  106. }
  107. if (cfg->mode & RT_SPI_NO_CS)
  108. {
  109. spi_handle->Init.NSS = SPI_NSS_SOFT;
  110. }
  111. else
  112. {
  113. spi_handle->Init.NSS = SPI_NSS_SOFT;
  114. }
  115. uint32_t SPI_APB_CLOCK;
  116. SPI_APB_CLOCK = HAL_RCC_GetPCLK2Freq();
  117. if (cfg->max_hz >= SPI_APB_CLOCK / 2)
  118. {
  119. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  120. }
  121. else if (cfg->max_hz >= SPI_APB_CLOCK / 4)
  122. {
  123. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  124. }
  125. else if (cfg->max_hz >= SPI_APB_CLOCK / 8)
  126. {
  127. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  128. }
  129. else if (cfg->max_hz >= SPI_APB_CLOCK / 16)
  130. {
  131. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  132. }
  133. else if (cfg->max_hz >= SPI_APB_CLOCK / 32)
  134. {
  135. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  136. }
  137. else if (cfg->max_hz >= SPI_APB_CLOCK / 64)
  138. {
  139. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
  140. }
  141. else if (cfg->max_hz >= SPI_APB_CLOCK / 128)
  142. {
  143. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
  144. }
  145. else
  146. {
  147. /* min prescaler 256 */
  148. spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  149. }
  150. LOG_D("sys freq: %d, pclk2 freq: %d, SPI limiting freq: %d, BaudRatePrescaler: %d",
  151. HAL_RCC_GetSysClockFreq(),
  152. SPI_APB_CLOCK,
  153. cfg->max_hz,
  154. spi_handle->Init.BaudRatePrescaler);
  155. if (cfg->mode & RT_SPI_MSB)
  156. {
  157. spi_handle->Init.FirstBit = SPI_FIRSTBIT_MSB;
  158. }
  159. else
  160. {
  161. spi_handle->Init.FirstBit = SPI_FIRSTBIT_LSB;
  162. }
  163. spi_handle->Init.TIMode = SPI_TIMODE_DISABLE;
  164. spi_handle->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  165. spi_handle->State = HAL_SPI_STATE_RESET;
  166. if (HAL_SPI_Init(spi_handle) != HAL_OK)
  167. {
  168. return RT_EIO;
  169. }
  170. #if defined(SOC_SERIES_STM32L4)
  171. SET_BIT(spi_handle->Instance->CR2, SPI_RXFIFO_THRESHOLD_HF);
  172. #endif
  173. __HAL_SPI_ENABLE(spi_handle);
  174. LOG_D("%s init done", spi_drv->config->bus_name);
  175. return RT_EOK;
  176. }
  177. #ifdef BSP_SPI_USING_DMA
  178. static uint8_t dummy = 0xFF;
  179. static void spi_dma_transfer_prepare(struct rt_spi_bus * spi_bus, struct rt_spi_message* message)
  180. {
  181. struct stm32_spi *spi_drv = rt_container_of(spi_bus, struct stm32_spi, spi_bus);
  182. DMA_HandleTypeDef * hdma_tx = (DMA_HandleTypeDef *)&spi_drv->dma.handle_tx;
  183. DMA_HandleTypeDef * hdma_rx = (DMA_HandleTypeDef *)&spi_drv->dma.handle_rx;
  184. HAL_DMA_DeInit(hdma_tx);
  185. HAL_DMA_DeInit(hdma_rx);
  186. /*
  187. * Check if the DMA Stream is disabled before enabling it.
  188. * Note that this step is useful when the same Stream is used multiple times.
  189. */
  190. #if defined(SOC_SERIES_STM32F4)
  191. while (hdma_tx->Instance->CR & DMA_SxCR_EN);
  192. while (hdma_rx->Instance->CR & DMA_SxCR_EN);
  193. #endif
  194. if(message->recv_buf != RT_NULL)
  195. {
  196. hdma_rx->Init.MemInc = DMA_MINC_ENABLE;
  197. }
  198. else
  199. {
  200. message->recv_buf = &dummy;
  201. hdma_rx->Init.MemInc = DMA_MINC_DISABLE;
  202. }
  203. HAL_DMA_Init(hdma_rx);
  204. __HAL_LINKDMA(&spi_drv->handle, hdmarx, spi_drv->dma.handle_rx);
  205. if(message->send_buf != RT_NULL)
  206. {
  207. hdma_tx->Init.MemInc = DMA_MINC_ENABLE;
  208. }
  209. else
  210. {
  211. dummy = 0xFF;
  212. message->send_buf = &dummy;
  213. hdma_tx->Init.MemInc = DMA_MINC_DISABLE;
  214. }
  215. HAL_DMA_Init(hdma_tx);
  216. /* link DMA with SPI */
  217. __HAL_LINKDMA(&spi_drv->handle, hdmatx, spi_drv->dma.handle_tx);
  218. LOG_D("%s RX Instance: %x, TX Instance: %x", spi_drv->config->bus_name, hdma_rx->Instance, hdma_tx->Instance);
  219. LOG_D("%s dma config done, TX dma_irq number: %d, RX dma_irq number: %d",
  220. spi_drv->config->bus_name,
  221. spi_drv->config->dma_tx.dma_irq,
  222. spi_drv->config->dma_rx.dma_irq);
  223. /* NVIC configuration for DMA transfer complete interrupt*/
  224. HAL_NVIC_SetPriority(spi_drv->config->dma_tx.dma_irq, 0, 1);
  225. HAL_NVIC_EnableIRQ(spi_drv->config->dma_tx.dma_irq);
  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. #endif
  231. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  232. {
  233. RT_ASSERT(device != RT_NULL);
  234. RT_ASSERT(device->bus != RT_NULL);
  235. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  236. RT_ASSERT(message != RT_NULL);
  237. struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
  238. SPI_HandleTypeDef * spi_handle = &spi_drv->handle;
  239. struct stm32_hw_spi_cs *cs = device->parent.user_data;
  240. rt_int32_t length = message->length;
  241. rt_int32_t data_width = spi_drv->cfg->data_width;
  242. if (message->cs_take)
  243. {
  244. HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_RESET);
  245. }
  246. #ifdef BSP_SPI_USING_DMA
  247. if(message->length > 32)
  248. {
  249. if(data_width <= 8)
  250. {
  251. HAL_StatusTypeDef state;
  252. LOG_D("%s dma transfer prepare and start", spi_drv->config->bus_name);
  253. LOG_D("%s sendbuf: %X, recvbuf: %X, length: %d",
  254. spi_drv->config->bus_name,
  255. (uint32_t)message->send_buf,
  256. (uint32_t)message->recv_buf, message->length);
  257. spi_dma_transfer_prepare(device->bus, message);
  258. /* start once data exchange in DMA mode */
  259. state = HAL_SPI_TransmitReceive_DMA(spi_handle,
  260. (uint8_t*)message->send_buf,
  261. (uint8_t*)message->recv_buf,
  262. message->length);
  263. if (state != HAL_OK)
  264. {
  265. LOG_D("spi flash configuration error : %d", state);
  266. message->length = 0;
  267. //while(1);
  268. }
  269. else
  270. {
  271. LOG_D("%s dma transfer done", spi_drv->config->bus_name);
  272. }
  273. /* For simplicity reasons, this example is just waiting till the end of the
  274. transfer, but application may perform other tasks while transfer operation
  275. is ongoing. */
  276. while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY);
  277. LOG_D("%s get state done", spi_drv->config->bus_name);
  278. }
  279. else
  280. {
  281. // TODO
  282. }
  283. } else
  284. #endif
  285. {
  286. if (data_width == 8)
  287. {
  288. const rt_uint8_t * send_ptr = message->send_buf;
  289. rt_uint8_t * recv_ptr = message->recv_buf;
  290. while (length--)
  291. {
  292. rt_uint8_t data = ~0;
  293. if(send_ptr != RT_NULL)
  294. {
  295. data = *send_ptr++;
  296. }
  297. /* send data once */
  298. while (__HAL_SPI_GET_FLAG(spi_handle, SPI_FLAG_TXE) == RESET);
  299. *(volatile rt_uint8_t *)(&spi_handle->Instance->DR) = data;
  300. /* receive data once */
  301. #if defined(SOC_SERIES_STM32L4)
  302. SET_BIT(spi_handle->Instance->CR2, SPI_RXFIFO_THRESHOLD_HF);
  303. #endif
  304. while (__HAL_SPI_GET_FLAG(spi_handle, SPI_FLAG_RXNE) == RESET);
  305. data = *(volatile rt_uint8_t *)(&spi_handle->Instance->DR);
  306. if(recv_ptr != RT_NULL)
  307. {
  308. *recv_ptr++ = data;
  309. }
  310. }
  311. } else
  312. {
  313. const rt_uint16_t * send_ptr = message->send_buf;
  314. rt_uint16_t * recv_ptr = message->recv_buf;
  315. while (length--)
  316. {
  317. rt_uint16_t data = ~0;
  318. if(send_ptr != RT_NULL)
  319. {
  320. data = *send_ptr++;
  321. }
  322. /* send data once */
  323. while (__HAL_SPI_GET_FLAG(spi_handle, SPI_FLAG_TXE) == RESET);
  324. *(volatile rt_uint16_t *)(&spi_handle->Instance->DR) = data;
  325. /* receive data once */
  326. #if defined(SOC_SERIES_STM32L4)
  327. SET_BIT(spi_handle->Instance->CR2, SPI_RXFIFO_THRESHOLD_HF);
  328. #endif
  329. while (__HAL_SPI_GET_FLAG(spi_handle, SPI_FLAG_RXNE) == RESET);
  330. data = *(volatile rt_uint16_t *)(&spi_handle->Instance->DR);
  331. if(recv_ptr != RT_NULL)
  332. {
  333. *recv_ptr++ = data;
  334. }
  335. }
  336. }
  337. }
  338. /* Wait until Busy flag is reset before disabling SPI */
  339. while (__HAL_SPI_GET_FLAG(spi_handle, SPI_FLAG_BSY) == SET);
  340. if (message->cs_release)
  341. {
  342. HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_SET);
  343. }
  344. return message->length;
  345. }
  346. static rt_err_t spi_configure(struct rt_spi_device *device,
  347. struct rt_spi_configuration *configuration)
  348. {
  349. RT_ASSERT(device != RT_NULL);
  350. RT_ASSERT(configuration != RT_NULL);
  351. struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
  352. spi_drv->cfg = configuration;
  353. return stm32_spi_init(spi_drv, configuration);
  354. }
  355. static const struct rt_spi_ops stm_spi_ops =
  356. {
  357. .configure = spi_configure,
  358. .xfer = spixfer,
  359. };
  360. static int rt_hw_spi_bus_init(void)
  361. {
  362. rt_err_t result;
  363. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  364. {
  365. spi_bus_obj[i].config = &spi_config[i];
  366. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i];
  367. spi_bus_obj[i].handle.Instance = spi_config[i].Instance;
  368. #ifdef BSP_SPI_USING_DMA
  369. /* Configure the DMA handler for Transmission process */
  370. spi_bus_obj[i].dma.handle_tx.Instance = spi_config[i].dma_tx.Instance;
  371. #if defined(SOC_SERIES_STM32F4)
  372. spi_bus_obj[i].dma.handle_tx.Init.Channel = spi_config[i].dma_tx.channel;
  373. #elif defined(SOC_SERIES_STM32L4)
  374. spi_bus_obj[i].dma.handle_tx.Init.Request = spi_config[i].dma_tx.request;
  375. #endif
  376. spi_bus_obj[i].dma.handle_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  377. spi_bus_obj[i].dma.handle_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  378. spi_bus_obj[i].dma.handle_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  379. spi_bus_obj[i].dma.handle_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  380. spi_bus_obj[i].dma.handle_tx.Init.Mode = DMA_NORMAL;
  381. spi_bus_obj[i].dma.handle_tx.Init.Priority = DMA_PRIORITY_LOW;
  382. #if defined(SOC_SERIES_STM32F4)
  383. spi_bus_obj[i].dma.handle_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  384. spi_bus_obj[i].dma.handle_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  385. spi_bus_obj[i].dma.handle_tx.Init.MemBurst = DMA_MBURST_INC4;
  386. spi_bus_obj[i].dma.handle_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  387. #endif
  388. spi_bus_obj[i].dma.handle_rx.Instance = spi_config[i].dma_rx.Instance;
  389. #if defined(SOC_SERIES_STM32F4)
  390. spi_bus_obj[i].dma.handle_rx.Init.Channel = spi_config[i].dma_rx.channel;
  391. #elif defined(SOC_SERIES_STM32L4)
  392. spi_bus_obj[i].dma.handle_rx.Init.Request = spi_config[i].dma_rx.request;
  393. #endif
  394. spi_bus_obj[i].dma.handle_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  395. spi_bus_obj[i].dma.handle_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  396. spi_bus_obj[i].dma.handle_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  397. spi_bus_obj[i].dma.handle_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  398. spi_bus_obj[i].dma.handle_rx.Init.Mode = DMA_NORMAL;
  399. spi_bus_obj[i].dma.handle_rx.Init.Priority = DMA_PRIORITY_HIGH;
  400. #if defined(SOC_SERIES_STM32F4)
  401. spi_bus_obj[i].dma.handle_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  402. spi_bus_obj[i].dma.handle_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  403. spi_bus_obj[i].dma.handle_rx.Init.MemBurst = DMA_MBURST_INC4;
  404. spi_bus_obj[i].dma.handle_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  405. #endif
  406. {
  407. rt_uint32_t tmpreg = 0x00U;
  408. #if defined(SOC_SERIES_STM32F1)
  409. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  410. SET_BIT(RCC->AHBENR, spi_config[i].dma_rx.dma_rcc);
  411. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_rx.dma_rcc);
  412. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32L4)
  413. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_rx.dma_rcc);
  414. /* Delay after an RCC peripheral clock enabling */
  415. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_rx.dma_rcc);
  416. #endif
  417. UNUSED(tmpreg); /* To avoid compiler warnings */
  418. }
  419. LOG_D("%s DMA clock init done", spi_config[i].bus_name);
  420. #endif /* BSP_SPI_USING_DMA */
  421. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &stm_spi_ops);
  422. RT_ASSERT(result == RT_EOK);
  423. LOG_D("%s bus init done", spi_config[i].bus_name);
  424. }
  425. return result;
  426. }
  427. /**
  428. * Attach the spi device to SPI bus, this function must be used after initialization.
  429. */
  430. 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)
  431. {
  432. RT_ASSERT(bus_name != RT_NULL);
  433. RT_ASSERT(device_name != RT_NULL);
  434. rt_err_t result;
  435. struct rt_spi_device *spi_device;
  436. struct stm32_hw_spi_cs *cs_pin;
  437. /* initialize the cs pin && select the slave*/
  438. GPIO_InitTypeDef GPIO_Initure;
  439. GPIO_Initure.Pin = cs_gpio_pin;
  440. GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
  441. GPIO_Initure.Pull = GPIO_PULLUP;
  442. GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
  443. HAL_GPIO_Init(cs_gpiox, &GPIO_Initure);
  444. HAL_GPIO_WritePin(cs_gpiox, cs_gpio_pin, GPIO_PIN_SET);
  445. /* attach the device to spi bus*/
  446. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  447. RT_ASSERT(spi_device != RT_NULL);
  448. cs_pin = (struct stm32_hw_spi_cs *)rt_malloc(sizeof(struct stm32_hw_spi_cs));
  449. RT_ASSERT(cs_pin != RT_NULL);
  450. cs_pin->GPIOx = cs_gpiox;
  451. cs_pin->GPIO_Pin = cs_gpio_pin;
  452. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  453. if (result != RT_EOK)
  454. {
  455. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  456. }
  457. RT_ASSERT(result == RT_EOK);
  458. LOG_D("%s attach to %s done", device_name, bus_name);
  459. return result;
  460. }
  461. #if defined(BSP_USING_SPI1) && defined(BSP_SPI_USING_DMA)
  462. /**
  463. * @brief This function handles DMA Rx interrupt request.
  464. * @param None
  465. * @retval None
  466. */
  467. void SPI1_DMA_RX_IRQHandler(void)
  468. {
  469. /* enter interrupt */
  470. rt_interrupt_enter();
  471. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_rx);
  472. /* leave interrupt */
  473. rt_interrupt_leave();
  474. }
  475. /**
  476. * @brief This function handles DMA Tx interrupt request.
  477. * @param None
  478. * @retval None
  479. */
  480. void SPI1_DMA_TX_IRQHandler(void)
  481. {
  482. /* enter interrupt */
  483. rt_interrupt_enter();
  484. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_tx);
  485. /* leave interrupt */
  486. rt_interrupt_leave();
  487. }
  488. #endif /* defined(BSP_USING_SPI1) && defined(BSP_SPI_USING_DMA) */
  489. #if defined(BSP_USING_SPI2) && defined(BSP_SPI_USING_DMA)
  490. /**
  491. * @brief This function handles DMA Rx interrupt request.
  492. * @param None
  493. * @retval None
  494. */
  495. void SPI2_DMA_RX_IRQHandler(void)
  496. {
  497. /* enter interrupt */
  498. rt_interrupt_enter();
  499. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_rx);
  500. /* leave interrupt */
  501. rt_interrupt_leave();
  502. }
  503. /**
  504. * @brief This function handles DMA Tx interrupt request.
  505. * @param None
  506. * @retval None
  507. */
  508. void SPI2_DMA_TX_IRQHandler(void)
  509. {
  510. /* enter interrupt */
  511. rt_interrupt_enter();
  512. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_tx);
  513. /* leave interrupt */
  514. rt_interrupt_leave();
  515. }
  516. #endif /* defined(BSP_USING_SPI2) && defined(BSP_SPI_USING_DMA) */
  517. #if defined(BSP_USING_SPI3) && defined(BSP_SPI_USING_DMA)
  518. /**
  519. * @brief This function handles DMA Rx interrupt request.
  520. * @param None
  521. * @retval None
  522. */
  523. void SPI3_DMA_RX_IRQHandler(void)
  524. {
  525. /* enter interrupt */
  526. rt_interrupt_enter();
  527. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_rx);
  528. /* leave interrupt */
  529. rt_interrupt_leave();
  530. }
  531. /**
  532. * @brief This function handles DMA Tx interrupt request.
  533. * @param None
  534. * @retval None
  535. */
  536. void SPI3_DMA_TX_IRQHandler(void)
  537. {
  538. /* enter interrupt */
  539. rt_interrupt_enter();
  540. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_tx);
  541. /* leave interrupt */
  542. rt_interrupt_leave();
  543. }
  544. #endif /* defined(BSP_USING_SPI3) && defined(BSP_SPI_USING_DMA) */
  545. #if defined(BSP_USING_SPI4) && defined(BSP_SPI_USING_DMA)
  546. /**
  547. * @brief This function handles DMA Rx interrupt request.
  548. * @param None
  549. * @retval None
  550. */
  551. void SPI4_DMA_RX_IRQHandler(void)
  552. {
  553. /* enter interrupt */
  554. rt_interrupt_enter();
  555. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_rx);
  556. /* leave interrupt */
  557. rt_interrupt_leave();
  558. }
  559. /**
  560. * @brief This function handles DMA Tx interrupt request.
  561. * @param None
  562. * @retval None
  563. */
  564. void SPI4_DMA_TX_IRQHandler(void)
  565. {
  566. /* enter interrupt */
  567. rt_interrupt_enter();
  568. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_tx);
  569. /* leave interrupt */
  570. rt_interrupt_leave();
  571. }
  572. #endif /* defined(BSP_USING_SPI4) && defined(BSP_SPI_USING_DMA) */
  573. #if defined(BSP_USING_SPI5) && defined(BSP_SPI_USING_DMA)
  574. /**
  575. * @brief This function handles DMA Rx interrupt request.
  576. * @param None
  577. * @retval None
  578. */
  579. void SPI5_DMA_RX_IRQHandler(void)
  580. {
  581. /* enter interrupt */
  582. rt_interrupt_enter();
  583. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_rx);
  584. /* leave interrupt */
  585. rt_interrupt_leave();
  586. }
  587. /**
  588. * @brief This function handles DMA Tx interrupt request.
  589. * @param None
  590. * @retval None
  591. */
  592. void SPI5_DMA_TX_IRQHandler(void)
  593. {
  594. /* enter interrupt */
  595. rt_interrupt_enter();
  596. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_tx);
  597. /* leave interrupt */
  598. rt_interrupt_leave();
  599. }
  600. #endif /* defined(BSP_USING_SPI5) && defined(BSP_SPI_USING_DMA) */
  601. int rt_hw_spi_init(void)
  602. {
  603. return rt_hw_spi_bus_init();
  604. }
  605. INIT_BOARD_EXPORT(rt_hw_spi_init);
  606. #endif /* BSP_USING_SPI1 || BSP_USING_SPI2 || BSP_USING_SPI3 || BSP_USING_SPI4 || BSP_USING_SPI5 */
  607. #endif /* RT_USING_SPI */