drv_spi.c 22 KB

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