drv_spi.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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 "board.h"
  14. #ifdef RT_USING_SPI
  15. #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)
  16. /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
  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_SOFT;
  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. __HAL_SPI_ENABLE(spi_handle);
  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)
  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. }
  294. else
  295. {
  296. memset((uint8_t *)recv_buf, 0xff, send_length);
  297. if (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  298. {
  299. state = HAL_SPI_Receive_DMA(spi_handle, (uint8_t *)recv_buf, send_length);
  300. }
  301. else
  302. {
  303. state = HAL_SPI_Receive(spi_handle, (uint8_t *)recv_buf, send_length, 1000);
  304. }
  305. }
  306. if (state != HAL_OK)
  307. {
  308. LOG_I("spi transfer error : %d", state);
  309. message->length = 0;
  310. spi_handle->State = HAL_SPI_STATE_READY;
  311. }
  312. else
  313. {
  314. LOG_D("%s transfer done", spi_drv->config->bus_name);
  315. }
  316. /* For simplicity reasons, this example is just waiting till the end of the
  317. transfer, but application may perform other tasks while transfer operation
  318. is ongoing. */
  319. while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY);
  320. }
  321. if (message->cs_release)
  322. {
  323. HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_SET);
  324. }
  325. return message->length;
  326. }
  327. static rt_err_t spi_configure(struct rt_spi_device *device,
  328. struct rt_spi_configuration *configuration)
  329. {
  330. RT_ASSERT(device != RT_NULL);
  331. RT_ASSERT(configuration != RT_NULL);
  332. struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
  333. spi_drv->cfg = configuration;
  334. return stm32_spi_init(spi_drv, configuration);
  335. }
  336. static const struct rt_spi_ops stm_spi_ops =
  337. {
  338. .configure = spi_configure,
  339. .xfer = spixfer,
  340. };
  341. static int rt_hw_spi_bus_init(void)
  342. {
  343. rt_err_t result;
  344. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  345. {
  346. spi_bus_obj[i].config = &spi_config[i];
  347. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i];
  348. spi_bus_obj[i].handle.Instance = spi_config[i].Instance;
  349. if (spi_bus_obj[i].spi_dma_flag & SPI_USING_RX_DMA_FLAG)
  350. {
  351. /* Configure the DMA handler for Transmission process */
  352. spi_bus_obj[i].dma.handle_rx.Instance = spi_config[i].dma_rx->Instance;
  353. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  354. spi_bus_obj[i].dma.handle_rx.Init.Channel = spi_config[i].dma_rx->channel;
  355. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0)
  356. spi_bus_obj[i].dma.handle_rx.Init.Request = spi_config[i].dma_rx->request;
  357. #endif
  358. spi_bus_obj[i].dma.handle_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  359. spi_bus_obj[i].dma.handle_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  360. spi_bus_obj[i].dma.handle_rx.Init.MemInc = DMA_MINC_ENABLE;
  361. spi_bus_obj[i].dma.handle_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  362. spi_bus_obj[i].dma.handle_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  363. spi_bus_obj[i].dma.handle_rx.Init.Mode = DMA_NORMAL;
  364. spi_bus_obj[i].dma.handle_rx.Init.Priority = DMA_PRIORITY_HIGH;
  365. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  366. spi_bus_obj[i].dma.handle_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  367. spi_bus_obj[i].dma.handle_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  368. spi_bus_obj[i].dma.handle_rx.Init.MemBurst = DMA_MBURST_INC4;
  369. spi_bus_obj[i].dma.handle_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  370. #endif
  371. {
  372. rt_uint32_t tmpreg = 0x00U;
  373. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32F0)
  374. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  375. SET_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  376. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_rx->dma_rcc);
  377. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4)
  378. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_rx->dma_rcc);
  379. /* Delay after an RCC peripheral clock enabling */
  380. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_rx->dma_rcc);
  381. #endif
  382. UNUSED(tmpreg); /* To avoid compiler warnings */
  383. }
  384. }
  385. if (spi_bus_obj[i].spi_dma_flag & SPI_USING_TX_DMA_FLAG)
  386. {
  387. /* Configure the DMA handler for Transmission process */
  388. spi_bus_obj[i].dma.handle_tx.Instance = spi_config[i].dma_tx->Instance;
  389. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  390. spi_bus_obj[i].dma.handle_tx.Init.Channel = spi_config[i].dma_tx->channel;
  391. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0)
  392. spi_bus_obj[i].dma.handle_tx.Init.Request = spi_config[i].dma_tx->request;
  393. #endif
  394. spi_bus_obj[i].dma.handle_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  395. spi_bus_obj[i].dma.handle_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  396. spi_bus_obj[i].dma.handle_tx.Init.MemInc = DMA_MINC_ENABLE;
  397. spi_bus_obj[i].dma.handle_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  398. spi_bus_obj[i].dma.handle_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  399. spi_bus_obj[i].dma.handle_tx.Init.Mode = DMA_NORMAL;
  400. spi_bus_obj[i].dma.handle_tx.Init.Priority = DMA_PRIORITY_LOW;
  401. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  402. spi_bus_obj[i].dma.handle_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  403. spi_bus_obj[i].dma.handle_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  404. spi_bus_obj[i].dma.handle_tx.Init.MemBurst = DMA_MBURST_INC4;
  405. spi_bus_obj[i].dma.handle_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  406. #endif
  407. {
  408. rt_uint32_t tmpreg = 0x00U;
  409. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32F0)
  410. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  411. SET_BIT(RCC->AHBENR, spi_config[i].dma_tx->dma_rcc);
  412. tmpreg = READ_BIT(RCC->AHBENR, spi_config[i].dma_tx->dma_rcc);
  413. #elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4)
  414. SET_BIT(RCC->AHB1ENR, spi_config[i].dma_tx->dma_rcc);
  415. /* Delay after an RCC peripheral clock enabling */
  416. tmpreg = READ_BIT(RCC->AHB1ENR, spi_config[i].dma_tx->dma_rcc);
  417. #endif
  418. UNUSED(tmpreg); /* To avoid compiler warnings */
  419. }
  420. }
  421. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &stm_spi_ops);
  422. RT_ASSERT(result == RT_EOK);
  423. LOG_D("%s bus init done", spi_config[i].bus_name);
  424. }
  425. return result;
  426. }
  427. /**
  428. * Attach the spi device to SPI bus, this function must be used after initialization.
  429. */
  430. 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)
  431. {
  432. RT_ASSERT(bus_name != RT_NULL);
  433. RT_ASSERT(device_name != RT_NULL);
  434. rt_err_t result;
  435. struct rt_spi_device *spi_device;
  436. struct stm32_hw_spi_cs *cs_pin;
  437. /* initialize the cs pin && select the slave*/
  438. GPIO_InitTypeDef GPIO_Initure;
  439. GPIO_Initure.Pin = cs_gpio_pin;
  440. GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
  441. GPIO_Initure.Pull = GPIO_PULLUP;
  442. GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
  443. HAL_GPIO_Init(cs_gpiox, &GPIO_Initure);
  444. HAL_GPIO_WritePin(cs_gpiox, cs_gpio_pin, GPIO_PIN_SET);
  445. /* attach the device to spi bus*/
  446. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  447. RT_ASSERT(spi_device != RT_NULL);
  448. cs_pin = (struct stm32_hw_spi_cs *)rt_malloc(sizeof(struct stm32_hw_spi_cs));
  449. RT_ASSERT(cs_pin != RT_NULL);
  450. cs_pin->GPIOx = cs_gpiox;
  451. cs_pin->GPIO_Pin = cs_gpio_pin;
  452. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  453. if (result != RT_EOK)
  454. {
  455. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  456. }
  457. RT_ASSERT(result == RT_EOK);
  458. LOG_D("%s attach to %s done", device_name, bus_name);
  459. return result;
  460. }
  461. #if defined(BSP_SPI1_TX_USING_DMA) || defined(BSP_SPI1_RX_USING_DMA)
  462. void SPI1_IRQHandler(void)
  463. {
  464. /* enter interrupt */
  465. rt_interrupt_enter();
  466. HAL_SPI_IRQHandler(&spi_bus_obj[SPI1_INDEX].handle);
  467. /* leave interrupt */
  468. rt_interrupt_leave();
  469. }
  470. #endif
  471. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_RX_USING_DMA)
  472. /**
  473. * @brief This function handles DMA Rx interrupt request.
  474. * @param None
  475. * @retval None
  476. */
  477. void SPI1_DMA_RX_IRQHandler(void)
  478. {
  479. /* enter interrupt */
  480. rt_interrupt_enter();
  481. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_rx);
  482. /* leave interrupt */
  483. rt_interrupt_leave();
  484. }
  485. #endif
  486. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_TX_USING_DMA)
  487. /**
  488. * @brief This function handles DMA Tx interrupt request.
  489. * @param None
  490. * @retval None
  491. */
  492. void SPI1_DMA_TX_IRQHandler(void)
  493. {
  494. /* enter interrupt */
  495. rt_interrupt_enter();
  496. HAL_DMA_IRQHandler(&spi_bus_obj[SPI1_INDEX].dma.handle_tx);
  497. /* leave interrupt */
  498. rt_interrupt_leave();
  499. }
  500. #endif /* defined(BSP_USING_SPI1) && defined(BSP_SPI_USING_DMA) */
  501. #if defined(BSP_SPI2_TX_USING_DMA) || defined(BSP_SPI2_RX_USING_DMA)
  502. void SPI2_IRQHandler(void)
  503. {
  504. /* enter interrupt */
  505. rt_interrupt_enter();
  506. HAL_SPI_IRQHandler(&spi_bus_obj[SPI2_INDEX].handle);
  507. /* leave interrupt */
  508. rt_interrupt_leave();
  509. }
  510. #endif
  511. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_RX_USING_DMA)
  512. /**
  513. * @brief This function handles DMA Rx interrupt request.
  514. * @param None
  515. * @retval None
  516. */
  517. void SPI2_DMA_RX_IRQHandler(void)
  518. {
  519. /* enter interrupt */
  520. rt_interrupt_enter();
  521. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_rx);
  522. /* leave interrupt */
  523. rt_interrupt_leave();
  524. }
  525. #endif
  526. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_TX_USING_DMA)
  527. /**
  528. * @brief This function handles DMA Tx interrupt request.
  529. * @param None
  530. * @retval None
  531. */
  532. void SPI2_DMA_TX_IRQHandler(void)
  533. {
  534. /* enter interrupt */
  535. rt_interrupt_enter();
  536. HAL_DMA_IRQHandler(&spi_bus_obj[SPI2_INDEX].dma.handle_tx);
  537. /* leave interrupt */
  538. rt_interrupt_leave();
  539. }
  540. #endif /* defined(BSP_USING_SPI2) && defined(BSP_SPI_USING_DMA) */
  541. #if defined(BSP_SPI3_TX_USING_DMA) || defined(BSP_SPI3_RX_USING_DMA)
  542. void SPI3_IRQHandler(void)
  543. {
  544. /* enter interrupt */
  545. rt_interrupt_enter();
  546. HAL_SPI_IRQHandler(&spi_bus_obj[SPI3_INDEX].handle);
  547. /* leave interrupt */
  548. rt_interrupt_leave();
  549. }
  550. #endif
  551. #if defined(BSP_USING_SPI3) && defined(BSP_SPI3_RX_USING_DMA)
  552. /**
  553. * @brief This function handles DMA Rx interrupt request.
  554. * @param None
  555. * @retval None
  556. */
  557. void SPI3_DMA_RX_IRQHandler(void)
  558. {
  559. /* enter interrupt */
  560. rt_interrupt_enter();
  561. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_rx);
  562. /* leave interrupt */
  563. rt_interrupt_leave();
  564. }
  565. #endif
  566. #if defined(BSP_USING_SPI3) && defined(BSP_SPI3_TX_USING_DMA)
  567. /**
  568. * @brief This function handles DMA Tx interrupt request.
  569. * @param None
  570. * @retval None
  571. */
  572. void SPI3_DMA_TX_IRQHandler(void)
  573. {
  574. /* enter interrupt */
  575. rt_interrupt_enter();
  576. HAL_DMA_IRQHandler(&spi_bus_obj[SPI3_INDEX].dma.handle_tx);
  577. /* leave interrupt */
  578. rt_interrupt_leave();
  579. }
  580. #endif /* defined(BSP_USING_SPI3) && defined(BSP_SPI_USING_DMA) */
  581. #if defined(BSP_SPI4_TX_USING_DMA) || defined(BSP_SPI4_RX_USING_DMA)
  582. void SPI4_IRQHandler(void)
  583. {
  584. /* enter interrupt */
  585. rt_interrupt_enter();
  586. HAL_SPI_IRQHandler(&spi_bus_obj[SPI4_INDEX].handle);
  587. /* leave interrupt */
  588. rt_interrupt_leave();
  589. }
  590. #endif
  591. #if defined(BSP_USING_SPI4) && defined(BSP_SPI4_RX_USING_DMA)
  592. /**
  593. * @brief This function handles DMA Rx interrupt request.
  594. * @param None
  595. * @retval None
  596. */
  597. void SPI4_DMA_RX_IRQHandler(void)
  598. {
  599. /* enter interrupt */
  600. rt_interrupt_enter();
  601. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_rx);
  602. /* leave interrupt */
  603. rt_interrupt_leave();
  604. }
  605. #endif
  606. #if defined(BSP_USING_SPI4) && defined(BSP_SPI4_TX_USING_DMA)
  607. /**
  608. * @brief This function handles DMA Tx interrupt request.
  609. * @param None
  610. * @retval None
  611. */
  612. void SPI4_DMA_TX_IRQHandler(void)
  613. {
  614. /* enter interrupt */
  615. rt_interrupt_enter();
  616. HAL_DMA_IRQHandler(&spi_bus_obj[SPI4_INDEX].dma.handle_tx);
  617. /* leave interrupt */
  618. rt_interrupt_leave();
  619. }
  620. #endif /* defined(BSP_USING_SPI4) && defined(BSP_SPI_USING_DMA) */
  621. #if defined(BSP_SPI5_TX_USING_DMA) || defined(BSP_SPI5_RX_USING_DMA)
  622. void SPI5_IRQHandler(void)
  623. {
  624. /* enter interrupt */
  625. rt_interrupt_enter();
  626. HAL_SPI_IRQHandler(&spi_bus_obj[SPI5_INDEX].handle);
  627. /* leave interrupt */
  628. rt_interrupt_leave();
  629. }
  630. #endif
  631. #if defined(BSP_USING_SPI5) && defined(BSP_SPI5_RX_USING_DMA)
  632. /**
  633. * @brief This function handles DMA Rx interrupt request.
  634. * @param None
  635. * @retval None
  636. */
  637. void SPI5_DMA_RX_IRQHandler(void)
  638. {
  639. /* enter interrupt */
  640. rt_interrupt_enter();
  641. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_rx);
  642. /* leave interrupt */
  643. rt_interrupt_leave();
  644. }
  645. #endif
  646. #if defined(BSP_USING_SPI5) && defined(BSP_SPI5_TX_USING_DMA)
  647. /**
  648. * @brief This function handles DMA Tx interrupt request.
  649. * @param None
  650. * @retval None
  651. */
  652. void SPI5_DMA_TX_IRQHandler(void)
  653. {
  654. /* enter interrupt */
  655. rt_interrupt_enter();
  656. HAL_DMA_IRQHandler(&spi_bus_obj[SPI5_INDEX].dma.handle_tx);
  657. /* leave interrupt */
  658. rt_interrupt_leave();
  659. }
  660. #endif /* defined(BSP_USING_SPI5) && defined(BSP_SPI_USING_DMA) */
  661. #if defined(BSP_USING_SPI6) && defined(BSP_SPI6_RX_USING_DMA)
  662. /**
  663. * @brief This function handles DMA Rx interrupt request.
  664. * @param None
  665. * @retval None
  666. */
  667. void SPI6_DMA_RX_IRQHandler(void)
  668. {
  669. /* enter interrupt */
  670. rt_interrupt_enter();
  671. HAL_DMA_IRQHandler(&spi_bus_obj[SPI6_INDEX].dma.handle_rx);
  672. /* leave interrupt */
  673. rt_interrupt_leave();
  674. }
  675. #endif
  676. #if defined(BSP_USING_SPI6) && defined(BSP_SPI6_TX_USING_DMA)
  677. /**
  678. * @brief This function handles DMA Tx interrupt request.
  679. * @param None
  680. * @retval None
  681. */
  682. void SPI6_DMA_TX_IRQHandler(void)
  683. {
  684. /* enter interrupt */
  685. rt_interrupt_enter();
  686. HAL_DMA_IRQHandler(&spi_bus_obj[SPI6_INDEX].dma.handle_tx);
  687. /* leave interrupt */
  688. rt_interrupt_leave();
  689. }
  690. #endif /* defined(BSP_USING_SPI6) && defined(BSP_SPI_USING_DMA) */
  691. static void stm32_get_dma_info(void)
  692. {
  693. #ifdef BSP_SPI1_RX_USING_DMA
  694. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  695. static struct dma_config spi1_dma_rx = SPI1_RX_DMA_CONFIG;
  696. spi_config[SPI1_INDEX].dma_rx = &spi1_dma_rx;
  697. #endif
  698. #ifdef BSP_SPI1_TX_USING_DMA
  699. spi_bus_obj[SPI1_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  700. static struct dma_config spi1_dma_tx = SPI1_TX_DMA_CONFIG;
  701. spi_config[SPI1_INDEX].dma_tx = &spi1_dma_tx;
  702. #endif
  703. #ifdef BSP_SPI2_RX_USING_DMA
  704. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  705. static struct dma_config spi2_dma_rx = SPI2_RX_DMA_CONFIG;
  706. spi_config[SPI2_INDEX].dma_rx = &spi2_dma_rx;
  707. #endif
  708. #ifdef BSP_SPI2_TX_USING_DMA
  709. spi_bus_obj[SPI2_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  710. static struct dma_config spi2_dma_tx = SPI2_TX_DMA_CONFIG;
  711. spi_config[SPI2_INDEX].dma_tx = &spi2_dma_tx;
  712. #endif
  713. #ifdef BSP_SPI3_RX_USING_DMA
  714. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  715. static struct dma_config spi3_dma_rx = SPI3_RX_DMA_CONFIG;
  716. spi_config[SPI3_INDEX].dma_rx = &spi3_dma_rx;
  717. #endif
  718. #ifdef BSP_SPI3_TX_USING_DMA
  719. spi_bus_obj[SPI3_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  720. static struct dma_config spi3_dma_tx = SPI3_TX_DMA_CONFIG;
  721. spi_config[SPI3_INDEX].dma_tx = &spi3_dma_tx;
  722. #endif
  723. #ifdef BSP_SPI4_RX_USING_DMA
  724. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  725. static struct dma_config spi4_dma_rx = SPI4_RX_DMA_CONFIG;
  726. spi_config[SPI4_INDEX].dma_rx = &spi4_dma_rx;
  727. #endif
  728. #ifdef BSP_SPI4_TX_USING_DMA
  729. spi_bus_obj[SPI4_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  730. static struct dma_config spi4_dma_tx = SPI4_TX_DMA_CONFIG;
  731. spi_config[SPI4_INDEX].dma_tx = &spi4_dma_tx;
  732. #endif
  733. #ifdef BSP_SPI5_RX_USING_DMA
  734. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  735. static struct dma_config spi5_dma_rx = SPI5_RX_DMA_CONFIG;
  736. spi_config[SPI5_INDEX].dma_rx = &spi5_dma_rx;
  737. #endif
  738. #ifdef BSP_SPI5_TX_USING_DMA
  739. spi_bus_obj[SPI5_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  740. static struct dma_config spi5_dma_tx = SPI5_TX_DMA_CONFIG;
  741. spi_config[SPI5_INDEX].dma_tx = &spi5_dma_tx;
  742. #endif
  743. #ifdef BSP_SPI6_RX_USING_DMA
  744. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= SPI_USING_RX_DMA_FLAG;
  745. static struct dma_config spi6_dma_rx = SPI6_RX_DMA_CONFIG;
  746. spi_config[SPI6_INDEX].dma_rx = &spi6_dma_rx;
  747. #endif
  748. #ifdef BSP_SPI6_TX_USING_DMA
  749. spi_bus_obj[SPI6_INDEX].spi_dma_flag |= SPI_USING_TX_DMA_FLAG;
  750. static struct dma_config spi6_dma_tx = SPI6_TX_DMA_CONFIG;
  751. spi_config[SPI6_INDEX].dma_tx = &spi6_dma_tx;
  752. #endif
  753. }
  754. #if defined(SOC_SERIES_STM32F0)
  755. void SPI1_DMA_RX_TX_IRQHandler(void)
  756. {
  757. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_TX_USING_DMA)
  758. SPI1_DMA_TX_IRQHandler();
  759. #endif
  760. #if defined(BSP_USING_SPI1) && defined(BSP_SPI1_RX_USING_DMA)
  761. SPI1_DMA_RX_IRQHandler();
  762. #endif
  763. }
  764. void SPI2_DMA_RX_TX_IRQHandler(void)
  765. {
  766. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_TX_USING_DMA)
  767. SPI2_DMA_TX_IRQHandler();
  768. #endif
  769. #if defined(BSP_USING_SPI2) && defined(BSP_SPI2_RX_USING_DMA)
  770. SPI2_DMA_RX_IRQHandler();
  771. #endif
  772. }
  773. #endif /* SOC_SERIES_STM32F0 */
  774. int rt_hw_spi_init(void)
  775. {
  776. stm32_get_dma_info();
  777. return rt_hw_spi_bus_init();
  778. }
  779. INIT_BOARD_EXPORT(rt_hw_spi_init);
  780. #endif /* BSP_USING_SPI1 || BSP_USING_SPI2 || BSP_USING_SPI3 || BSP_USING_SPI4 || BSP_USING_SPI5 */
  781. #endif /* RT_USING_SPI */