drv_spi.c 28 KB

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