drv_spi.c 29 KB

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