drv_spi.c 28 KB

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