drv_spi.c 22 KB

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