drv_spi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. #if defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0)
  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. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32F0)
  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) \
  187. || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32G0)
  188. SET_BIT(spi_handle->Instance->CR2, SPI_RXFIFO_THRESHOLD_HF);
  189. #endif
  190. /* DMA configuration */
  191. if (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  192. {
  193. HAL_DMA_Init(&spi_drv->dma.handle_rx);
  194. __HAL_LINKDMA(&spi_drv->handle, hdmarx, spi_drv->dma.handle_rx);
  195. /* NVIC configuration for DMA transfer complete interrupt */
  196. HAL_NVIC_SetPriority(spi_drv->config->dma_rx->dma_irq, 0, 0);
  197. HAL_NVIC_EnableIRQ(spi_drv->config->dma_rx->dma_irq);
  198. }
  199. if (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  200. {
  201. HAL_DMA_Init(&spi_drv->dma.handle_tx);
  202. __HAL_LINKDMA(&spi_drv->handle, hdmatx, spi_drv->dma.handle_tx);
  203. /* NVIC configuration for DMA transfer complete interrupt */
  204. HAL_NVIC_SetPriority(spi_drv->config->dma_tx->dma_irq, 0, 1);
  205. HAL_NVIC_EnableIRQ(spi_drv->config->dma_tx->dma_irq);
  206. }
  207. __HAL_SPI_ENABLE(spi_handle);
  208. LOG_D("%s init done", spi_drv->config->bus_name);
  209. return RT_EOK;
  210. }
  211. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  212. {
  213. HAL_StatusTypeDef state;
  214. rt_size_t message_length, already_send_length;
  215. rt_uint16_t send_length;
  216. rt_uint8_t *recv_buf;
  217. const rt_uint8_t *send_buf;
  218. RT_ASSERT(device != RT_NULL);
  219. RT_ASSERT(device->bus != RT_NULL);
  220. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  221. RT_ASSERT(message != RT_NULL);
  222. struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
  223. SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
  224. struct stm32_hw_spi_cs *cs = device->parent.user_data;
  225. if (message->cs_take)
  226. {
  227. HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_RESET);
  228. }
  229. LOG_D("%s transfer prepare and start", spi_drv->config->bus_name);
  230. LOG_D("%s sendbuf: %X, recvbuf: %X, length: %d",
  231. spi_drv->config->bus_name,
  232. (uint32_t)message->send_buf,
  233. (uint32_t)message->recv_buf, message->length);
  234. message_length = message->length;
  235. recv_buf = message->recv_buf;
  236. send_buf = message->send_buf;
  237. while (message_length)
  238. {
  239. /* the HAL library use uint16 to save the data length */
  240. if (message_length > 65535)
  241. {
  242. send_length = 65535;
  243. message_length = message_length - 65535;
  244. }
  245. else
  246. {
  247. send_length = message_length;
  248. message_length = 0;
  249. }
  250. /* calculate the start address */
  251. already_send_length = message->length - send_length - message_length;
  252. send_buf = (rt_uint8_t *)message->send_buf + already_send_length;
  253. recv_buf = (rt_uint8_t *)message->recv_buf + already_send_length;
  254. /* start once data exchange in DMA mode */
  255. if (message->send_buf && message->recv_buf)
  256. {
  257. if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG))
  258. {
  259. state = HAL_SPI_TransmitReceive_DMA(spi_handle, (uint8_t *)send_buf, (uint8_t *)recv_buf, send_length);
  260. }
  261. else
  262. {
  263. state = HAL_SPI_TransmitReceive(spi_handle, (uint8_t *)send_buf, (uint8_t *)recv_buf, send_length, 1000);
  264. }
  265. }
  266. else if (message->send_buf)
  267. {
  268. if (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  269. {
  270. state = HAL_SPI_Transmit_DMA(spi_handle, (uint8_t *)send_buf, send_length);
  271. }
  272. else
  273. {
  274. state = HAL_SPI_Transmit(spi_handle, (uint8_t *)send_buf, send_length, 1000);
  275. }
  276. }
  277. else
  278. {
  279. memset((uint8_t *)recv_buf, 0xff, send_length);
  280. if (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  281. {
  282. state = HAL_SPI_Receive_DMA(spi_handle, (uint8_t *)recv_buf, send_length);
  283. }
  284. else
  285. {
  286. state = HAL_SPI_Receive(spi_handle, (uint8_t *)recv_buf, send_length, 1000);
  287. }
  288. }
  289. if (state != HAL_OK)
  290. {
  291. LOG_I("spi transfer error : %d", state);
  292. message->length = 0;
  293. spi_handle->State = HAL_SPI_STATE_READY;
  294. }
  295. else
  296. {
  297. LOG_D("%s transfer done", spi_drv->config->bus_name);
  298. }
  299. /* For simplicity reasons, this example is just waiting till the end of the
  300. transfer, but application may perform other tasks while transfer operation
  301. is ongoing. */
  302. while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY);
  303. }
  304. if (message->cs_release)
  305. {
  306. HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_SET);
  307. }
  308. return message->length;
  309. }
  310. static rt_err_t spi_configure(struct rt_spi_device *device,
  311. struct rt_spi_configuration *configuration)
  312. {
  313. RT_ASSERT(device != RT_NULL);
  314. RT_ASSERT(configuration != RT_NULL);
  315. struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
  316. spi_drv->cfg = configuration;
  317. return stm32_spi_init(spi_drv, configuration);
  318. }
  319. static const struct rt_spi_ops stm_spi_ops =
  320. {
  321. .configure = spi_configure,
  322. .xfer = spixfer,
  323. };
  324. static int rt_hw_spi_bus_init(void)
  325. {
  326. rt_err_t result;
  327. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  328. {
  329. spi_bus_obj[i].config = &spi_config[i];
  330. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i];
  331. spi_bus_obj[i].handle.Instance = spi_config[i].Instance;
  332. if (spi_bus_obj[i].spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  333. {
  334. /* Configure the DMA handler for Transmission process */
  335. spi_bus_obj[i].dma.handle_rx.Instance = spi_config[i].dma_rx->Instance;
  336. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  337. spi_bus_obj[i].dma.handle_rx.Init.Channel = spi_config[i].dma_rx->channel;
  338. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0)
  339. spi_bus_obj[i].dma.handle_rx.Init.Request = spi_config[i].dma_rx->request;
  340. #endif
  341. spi_bus_obj[i].dma.handle_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  342. spi_bus_obj[i].dma.handle_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  343. spi_bus_obj[i].dma.handle_rx.Init.MemInc = DMA_MINC_ENABLE;
  344. spi_bus_obj[i].dma.handle_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  345. spi_bus_obj[i].dma.handle_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  346. spi_bus_obj[i].dma.handle_rx.Init.Mode = DMA_NORMAL;
  347. spi_bus_obj[i].dma.handle_rx.Init.Priority = DMA_PRIORITY_HIGH;
  348. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  349. spi_bus_obj[i].dma.handle_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  350. spi_bus_obj[i].dma.handle_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  351. spi_bus_obj[i].dma.handle_rx.Init.MemBurst = DMA_MBURST_INC4;
  352. spi_bus_obj[i].dma.handle_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  353. #endif
  354. {
  355. rt_uint32_t tmpreg = 0x00U;
  356. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32F0)
  357. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  358. SET_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  359. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  360. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4)
  361. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_rx->dma_rcc);
  362. /* Delay after an RCC peripheral clock enabling */
  363. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_rx->dma_rcc);
  364. #endif
  365. UNUSED(tmpreg); /* To avoid compiler warnings */
  366. }
  367. }
  368. if (spi_bus_obj[i].spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  369. {
  370. /* Configure the DMA handler for Transmission process */
  371. spi_bus_obj[i].dma.handle_tx.Instance = spi_config[i].dma_tx->Instance;
  372. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  373. spi_bus_obj[i].dma.handle_tx.Init.Channel = spi_config[i].dma_tx->channel;
  374. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0)
  375. spi_bus_obj[i].dma.handle_tx.Init.Request = spi_config[i].dma_tx->request;
  376. #endif
  377. spi_bus_obj[i].dma.handle_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  378. spi_bus_obj[i].dma.handle_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  379. spi_bus_obj[i].dma.handle_tx.Init.MemInc = DMA_MINC_ENABLE;
  380. spi_bus_obj[i].dma.handle_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  381. spi_bus_obj[i].dma.handle_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  382. spi_bus_obj[i].dma.handle_tx.Init.Mode = DMA_NORMAL;
  383. spi_bus_obj[i].dma.handle_tx.Init.Priority = DMA_PRIORITY_LOW;
  384. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  385. spi_bus_obj[i].dma.handle_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  386. spi_bus_obj[i].dma.handle_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  387. spi_bus_obj[i].dma.handle_tx.Init.MemBurst = DMA_MBURST_INC4;
  388. spi_bus_obj[i].dma.handle_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  389. #endif
  390. {
  391. rt_uint32_t tmpreg = 0x00U;
  392. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32F0)
  393. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  394. SET_BIT(RCC->AHBENR, spi_config[i].dma_tx->dma_rcc);
  395. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_tx->dma_rcc);
  396. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4)
  397. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_tx->dma_rcc);
  398. /* Delay after an RCC peripheral clock enabling */
  399. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_tx->dma_rcc);
  400. #endif
  401. UNUSED(tmpreg); /* To avoid compiler warnings */
  402. }
  403. }
  404. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &stm_spi_ops);
  405. RT_ASSERT(result == RT_EOK);
  406. LOG_D("%s bus init done", spi_config[i].bus_name);
  407. }
  408. return result;
  409. }
  410. /**
  411. * Attach the spi device to SPI bus, this function must be used after initialization.
  412. */
  413. 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)
  414. {
  415. RT_ASSERT(bus_name != RT_NULL);
  416. RT_ASSERT(device_name != RT_NULL);
  417. rt_err_t result;
  418. struct rt_spi_device *spi_device;
  419. struct stm32_hw_spi_cs *cs_pin;
  420. /* initialize the cs pin && select the slave*/
  421. GPIO_InitTypeDef GPIO_Initure;
  422. GPIO_Initure.Pin = cs_gpio_pin;
  423. GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
  424. GPIO_Initure.Pull = GPIO_PULLUP;
  425. GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
  426. HAL_GPIO_Init(cs_gpiox, &GPIO_Initure);
  427. HAL_GPIO_WritePin(cs_gpiox, cs_gpio_pin, GPIO_PIN_SET);
  428. /* attach the device to spi bus*/
  429. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  430. RT_ASSERT(spi_device != RT_NULL);
  431. cs_pin = (struct stm32_hw_spi_cs *)rt_malloc(sizeof(struct stm32_hw_spi_cs));
  432. RT_ASSERT(cs_pin != RT_NULL);
  433. cs_pin->GPIOx = cs_gpiox;
  434. cs_pin->GPIO_Pin = cs_gpio_pin;
  435. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  436. if (result != RT_EOK)
  437. {
  438. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  439. }
  440. RT_ASSERT(result == RT_EOK);
  441. LOG_D("%s attach to %s done", device_name, bus_name);
  442. return result;
  443. }
  444. #if defined(BSP_SPI1_TX_USING_DMA) || defined(BSP_SPI1_RX_USING_DMA)
  445. void SPI1_IRQHandler(void)
  446. {
  447. /* enter interrupt */
  448. rt_interrupt_enter();
  449. HAL_SPI_IRQHandler(&spi_bus_obj[SPI1_INDEX].handle);
  450. /* leave interrupt */
  451. rt_interrupt_leave();
  452. }
  453. #endif
  454. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_RX_USING_DMA)
  455. /**
  456. * @brief This function handles DMA Rx interrupt request.
  457. * @param None
  458. * @retval None
  459. */
  460. void SPI1_DMA_RX_IRQHandler(void)
  461. {
  462. /* enter interrupt */
  463. rt_interrupt_enter();
  464. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_rx);
  465. /* leave interrupt */
  466. rt_interrupt_leave();
  467. }
  468. #endif
  469. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_TX_USING_DMA)
  470. /**
  471. * @brief This function handles DMA Tx interrupt request.
  472. * @param None
  473. * @retval None
  474. */
  475. void SPI1_DMA_TX_IRQHandler(void)
  476. {
  477. /* enter interrupt */
  478. rt_interrupt_enter();
  479. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_tx);
  480. /* leave interrupt */
  481. rt_interrupt_leave();
  482. }
  483. #endif /* defined(BSP_USING_SPI1) && defined(BSP_SPI_USING_DMA) */
  484. #if defined(BSP_SPI2_TX_USING_DMA) || defined(BSP_SPI2_RX_USING_DMA)
  485. void SPI2_IRQHandler(void)
  486. {
  487. /* enter interrupt */
  488. rt_interrupt_enter();
  489. HAL_SPI_IRQHandler(&spi_bus_obj[SPI2_INDEX].handle);
  490. /* leave interrupt */
  491. rt_interrupt_leave();
  492. }
  493. #endif
  494. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_RX_USING_DMA)
  495. /**
  496. * @brief This function handles DMA Rx interrupt request.
  497. * @param None
  498. * @retval None
  499. */
  500. void SPI2_DMA_RX_IRQHandler(void)
  501. {
  502. /* enter interrupt */
  503. rt_interrupt_enter();
  504. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_rx);
  505. /* leave interrupt */
  506. rt_interrupt_leave();
  507. }
  508. #endif
  509. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_TX_USING_DMA)
  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_SPI3_TX_USING_DMA) || defined(BSP_SPI3_RX_USING_DMA)
  525. void SPI3_IRQHandler(void)
  526. {
  527. /* enter interrupt */
  528. rt_interrupt_enter();
  529. HAL_SPI_IRQHandler(&spi_bus_obj[SPI3_INDEX].handle);
  530. /* leave interrupt */
  531. rt_interrupt_leave();
  532. }
  533. #endif
  534. #if defined(BSP_USING_SPI3) && defined(BSP_SPI3_RX_USING_DMA)
  535. /**
  536. * @brief This function handles DMA Rx interrupt request.
  537. * @param None
  538. * @retval None
  539. */
  540. void SPI3_DMA_RX_IRQHandler(void)
  541. {
  542. /* enter interrupt */
  543. rt_interrupt_enter();
  544. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_rx);
  545. /* leave interrupt */
  546. rt_interrupt_leave();
  547. }
  548. #endif
  549. #if defined(BSP_USING_SPI3) && defined(BSP_SPI3_TX_USING_DMA)
  550. /**
  551. * @brief This function handles DMA Tx interrupt request.
  552. * @param None
  553. * @retval None
  554. */
  555. void SPI3_DMA_TX_IRQHandler(void)
  556. {
  557. /* enter interrupt */
  558. rt_interrupt_enter();
  559. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_tx);
  560. /* leave interrupt */
  561. rt_interrupt_leave();
  562. }
  563. #endif /* defined(BSP_USING_SPI3) && defined(BSP_SPI_USING_DMA) */
  564. #if defined(BSP_SPI4_TX_USING_DMA) || defined(BSP_SPI4_RX_USING_DMA)
  565. void SPI4_IRQHandler(void)
  566. {
  567. /* enter interrupt */
  568. rt_interrupt_enter();
  569. HAL_SPI_IRQHandler(&spi_bus_obj[SPI4_INDEX].handle);
  570. /* leave interrupt */
  571. rt_interrupt_leave();
  572. }
  573. #endif
  574. #if defined(BSP_USING_SPI4) && defined(BSP_SPI4_RX_USING_DMA)
  575. /**
  576. * @brief This function handles DMA Rx interrupt request.
  577. * @param None
  578. * @retval None
  579. */
  580. void SPI4_DMA_RX_IRQHandler(void)
  581. {
  582. /* enter interrupt */
  583. rt_interrupt_enter();
  584. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_rx);
  585. /* leave interrupt */
  586. rt_interrupt_leave();
  587. }
  588. #endif
  589. #if defined(BSP_USING_SPI4) && defined(BSP_SPI4_TX_USING_DMA)
  590. /**
  591. * @brief This function handles DMA Tx interrupt request.
  592. * @param None
  593. * @retval None
  594. */
  595. void SPI4_DMA_TX_IRQHandler(void)
  596. {
  597. /* enter interrupt */
  598. rt_interrupt_enter();
  599. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_tx);
  600. /* leave interrupt */
  601. rt_interrupt_leave();
  602. }
  603. #endif /* defined(BSP_USING_SPI4) && defined(BSP_SPI_USING_DMA) */
  604. #if defined(BSP_SPI5_TX_USING_DMA) || defined(BSP_SPI5_RX_USING_DMA)
  605. void SPI5_IRQHandler(void)
  606. {
  607. /* enter interrupt */
  608. rt_interrupt_enter();
  609. HAL_SPI_IRQHandler(&spi_bus_obj[SPI5_INDEX].handle);
  610. /* leave interrupt */
  611. rt_interrupt_leave();
  612. }
  613. #endif
  614. #if defined(BSP_USING_SPI5) && defined(BSP_SPI5_RX_USING_DMA)
  615. /**
  616. * @brief This function handles DMA Rx interrupt request.
  617. * @param None
  618. * @retval None
  619. */
  620. void SPI5_DMA_RX_IRQHandler(void)
  621. {
  622. /* enter interrupt */
  623. rt_interrupt_enter();
  624. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_rx);
  625. /* leave interrupt */
  626. rt_interrupt_leave();
  627. }
  628. #endif
  629. #if defined(BSP_USING_SPI5) && defined(BSP_SPI5_TX_USING_DMA)
  630. /**
  631. * @brief This function handles DMA Tx interrupt request.
  632. * @param None
  633. * @retval None
  634. */
  635. void SPI5_DMA_TX_IRQHandler(void)
  636. {
  637. /* enter interrupt */
  638. rt_interrupt_enter();
  639. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_tx);
  640. /* leave interrupt */
  641. rt_interrupt_leave();
  642. }
  643. #endif /* defined(BSP_USING_SPI5) && defined(BSP_SPI_USING_DMA) */
  644. #if defined(BSP_USING_SPI6) && defined(BSP_SPI6_RX_USING_DMA)
  645. /**
  646. * @brief This function handles DMA Rx interrupt request.
  647. * @param None
  648. * @retval None
  649. */
  650. void SPI6_DMA_RX_IRQHandler(void)
  651. {
  652. /* enter interrupt */
  653. rt_interrupt_enter();
  654. HAL_DMA_IRQHandler(&spi_bus_obj[SPI6_INDEX].dma.handle_rx);
  655. /* leave interrupt */
  656. rt_interrupt_leave();
  657. }
  658. #endif
  659. #if defined(BSP_USING_SPI6) && defined(BSP_SPI6_TX_USING_DMA)
  660. /**
  661. * @brief This function handles DMA Tx interrupt request.
  662. * @param None
  663. * @retval None
  664. */
  665. void SPI6_DMA_TX_IRQHandler(void)
  666. {
  667. /* enter interrupt */
  668. rt_interrupt_enter();
  669. HAL_DMA_IRQHandler(&spi_bus_obj[SPI6_INDEX].dma.handle_tx);
  670. /* leave interrupt */
  671. rt_interrupt_leave();
  672. }
  673. #endif /* defined(BSP_USING_SPI6) && defined(BSP_SPI_USING_DMA) */
  674. static void stm32_get_dma_info(void)
  675. {
  676. #ifdef BSP_SPI1_RX_USING_DMA
  677. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  678. static struct dma_config spi1_dma_rx = SPI1_RX_DMA_CONFIG;
  679. spi_config[SPI1_INDEX].dma_rx = &spi1_dma_rx;
  680. #endif
  681. #ifdef BSP_SPI1_TX_USING_DMA
  682. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  683. static struct dma_config spi1_dma_tx = SPI1_TX_DMA_CONFIG;
  684. spi_config[SPI1_INDEX].dma_tx = &spi1_dma_tx;
  685. #endif
  686. #ifdef BSP_SPI2_RX_USING_DMA
  687. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  688. static struct dma_config spi2_dma_rx = SPI2_RX_DMA_CONFIG;
  689. spi_config[SPI2_INDEX].dma_rx = &spi2_dma_rx;
  690. #endif
  691. #ifdef BSP_SPI2_TX_USING_DMA
  692. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  693. static struct dma_config spi2_dma_tx = SPI2_TX_DMA_CONFIG;
  694. spi_config[SPI2_INDEX].dma_tx = &spi2_dma_tx;
  695. #endif
  696. #ifdef BSP_SPI3_RX_USING_DMA
  697. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  698. static struct dma_config spi3_dma_rx = SPI3_RX_DMA_CONFIG;
  699. spi_config[SPI3_INDEX].dma_rx = &spi3_dma_rx;
  700. #endif
  701. #ifdef BSP_SPI3_TX_USING_DMA
  702. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  703. static struct dma_config spi3_dma_tx = SPI3_TX_DMA_CONFIG;
  704. spi_config[SPI3_INDEX].dma_tx = &spi3_dma_tx;
  705. #endif
  706. #ifdef BSP_SPI4_RX_USING_DMA
  707. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  708. static struct dma_config spi4_dma_rx = SPI4_RX_DMA_CONFIG;
  709. spi_config[SPI4_INDEX].dma_rx = &spi4_dma_rx;
  710. #endif
  711. #ifdef BSP_SPI4_TX_USING_DMA
  712. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  713. static struct dma_config spi4_dma_tx = SPI4_TX_DMA_CONFIG;
  714. spi_config[SPI4_INDEX].dma_tx = &spi4_dma_tx;
  715. #endif
  716. #ifdef BSP_SPI5_RX_USING_DMA
  717. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  718. static struct dma_config spi5_dma_rx = SPI5_RX_DMA_CONFIG;
  719. spi_config[SPI5_INDEX].dma_rx = &spi5_dma_rx;
  720. #endif
  721. #ifdef BSP_SPI5_TX_USING_DMA
  722. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  723. static struct dma_config spi5_dma_tx = SPI5_TX_DMA_CONFIG;
  724. spi_config[SPI5_INDEX].dma_tx = &spi5_dma_tx;
  725. #endif
  726. #ifdef BSP_SPI6_RX_USING_DMA
  727. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  728. static struct dma_config spi6_dma_rx = SPI6_RX_DMA_CONFIG;
  729. spi_config[SPI6_INDEX].dma_rx = &spi6_dma_rx;
  730. #endif
  731. #ifdef BSP_SPI6_TX_USING_DMA
  732. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  733. static struct dma_config spi6_dma_tx = SPI6_TX_DMA_CONFIG;
  734. spi_config[SPI6_INDEX].dma_tx = &spi6_dma_tx;
  735. #endif
  736. }
  737. #if defined(SOC_SERIES_STM32F0)
  738. void SPI1_DMA_RX_TX_IRQHandler(void)
  739. {
  740. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_TX_USING_DMA)
  741. SPI1_DMA_TX_IRQHandler();
  742. #endif
  743. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_RX_USING_DMA)
  744. SPI1_DMA_RX_IRQHandler();
  745. #endif
  746. }
  747. void SPI2_DMA_RX_TX_IRQHandler(void)
  748. {
  749. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_TX_USING_DMA)
  750. SPI2_DMA_TX_IRQHandler();
  751. #endif
  752. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_RX_USING_DMA)
  753. SPI2_DMA_RX_IRQHandler();
  754. #endif
  755. }
  756. #endif /* SOC_SERIES_STM32F0 */
  757. int rt_hw_spi_init(void)
  758. {
  759. stm32_get_dma_info();
  760. return rt_hw_spi_bus_init();
  761. }
  762. INIT_BOARD_EXPORT(rt_hw_spi_init);
  763. #endif /* BSP_USING_SPI1 || BSP_USING_SPI2 || BSP_USING_SPI3 || BSP_USING_SPI4 || BSP_USING_SPI5 */
  764. #endif /* RT_USING_SPI */