drv_spi.c 25 KB

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