drv_spi.c 22 KB

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