drv_spi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. * 2017-06-05 tanek first implementation.
  9. */
  10. #include "drv_spi.h"
  11. #include <board.h>
  12. #include <finsh.h>
  13. //#define DEBUG
  14. #ifdef DEBUG
  15. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  16. #else
  17. #define DEBUG_PRINTF(...)
  18. #endif
  19. /* private rt-thread spi ops function */
  20. static rt_err_t configure(struct rt_spi_device* device, struct rt_spi_configuration* configuration);
  21. static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message);
  22. static struct rt_spi_ops stm32_spi_ops =
  23. {
  24. configure,
  25. xfer
  26. };
  27. #ifdef SPI_USE_DMA
  28. static uint8_t dummy = 0xFF;
  29. static void DMA_RxConfiguration(struct rt_spi_bus * spi_bus,
  30. struct rt_spi_message* message)
  31. {
  32. struct stm32f4_spi *f4_spi = (struct stm32f4_spi *)spi_bus->parent.user_data;
  33. DMA_HandleTypeDef * hdma_tx = &f4_spi->hdma_tx;
  34. DMA_HandleTypeDef * hdma_rx = &f4_spi->hdma_rx;
  35. HAL_DMA_DeInit(hdma_tx);
  36. HAL_DMA_DeInit(hdma_rx);
  37. /* Check if the DMA Stream is disabled before enabling it.
  38. Note that this step is useful when the same Stream is used multiple times:
  39. enabled, then disabled then re-enabled... In this case, the DMA Stream disable
  40. will be effective only at the end of the ongoing data transfer and it will
  41. not be possible to re-configure it before making sure that the Enable bit
  42. has been cleared by hardware. If the Stream is used only once, this step might
  43. be bypassed. */
  44. while (hdma_tx->Instance->CR & DMA_SxCR_EN);
  45. while (hdma_rx->Instance->CR & DMA_SxCR_EN);
  46. if(message->recv_buf != RT_NULL)
  47. {
  48. hdma_rx->Init.MemInc = DMA_MINC_ENABLE;
  49. }
  50. else
  51. {
  52. message->recv_buf = &dummy;
  53. hdma_rx->Init.MemInc = DMA_MINC_DISABLE;
  54. }
  55. HAL_DMA_Init(hdma_rx);
  56. __HAL_LINKDMA(&f4_spi->spi_handle, hdmarx, f4_spi->hdma_rx);
  57. if(message->send_buf != RT_NULL)
  58. {
  59. hdma_tx->Init.MemInc = DMA_MINC_ENABLE;
  60. }
  61. else
  62. {
  63. dummy = 0xFF;
  64. message->send_buf = &dummy;
  65. hdma_tx->Init.MemInc = DMA_MINC_DISABLE;
  66. }
  67. HAL_DMA_Init(hdma_tx);
  68. __HAL_LINKDMA(&f4_spi->spi_handle, hdmatx, f4_spi->hdma_tx);
  69. /* NVIC configuration for DMA transfer complete interrupt*/
  70. HAL_NVIC_SetPriority(f4_spi->hdma_tx_irq, 0, 1);
  71. HAL_NVIC_EnableIRQ(f4_spi->hdma_tx_irq);
  72. /* NVIC configuration for DMA transfer complete interrupt*/
  73. HAL_NVIC_SetPriority(f4_spi->hdma_rx_irq, 0, 0);
  74. HAL_NVIC_EnableIRQ(f4_spi->hdma_rx_irq);
  75. }
  76. #endif
  77. static rt_err_t configure(struct rt_spi_device* device,
  78. struct rt_spi_configuration* configuration)
  79. {
  80. struct rt_spi_bus * spi_bus = (struct rt_spi_bus *)device->bus;
  81. struct stm32f4_spi *f4_spi = (struct stm32f4_spi *)spi_bus->parent.user_data;
  82. SPI_HandleTypeDef * SpiHandle = &f4_spi->spi_handle;
  83. RT_ASSERT(device != RT_NULL);
  84. RT_ASSERT(configuration != RT_NULL);
  85. /* data_width */
  86. if(configuration->data_width <= 8)
  87. {
  88. SpiHandle->Init.DataSize = SPI_DATASIZE_8BIT;
  89. }
  90. else if(configuration->data_width <= 16)
  91. {
  92. SpiHandle->Init.DataSize = SPI_DATASIZE_16BIT;
  93. }
  94. else
  95. {
  96. return RT_EIO;
  97. }
  98. /* baudrate */
  99. {
  100. uint32_t SPI_APB_CLOCK;
  101. uint32_t max_hz;
  102. max_hz = configuration->max_hz;
  103. DEBUG_PRINTF("sys freq: %d\n", HAL_RCC_GetSysClockFreq());
  104. DEBUG_PRINTF("pclk2 freq: %d\n", HAL_RCC_GetPCLK2Freq());
  105. SPI_APB_CLOCK = HAL_RCC_GetPCLK2Freq();
  106. if(max_hz >= SPI_APB_CLOCK/2)
  107. {
  108. SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  109. }
  110. else if(max_hz >= SPI_APB_CLOCK/4)
  111. {
  112. SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  113. }
  114. else if(max_hz >= SPI_APB_CLOCK/8)
  115. {
  116. SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  117. }
  118. else if(max_hz >= SPI_APB_CLOCK/16)
  119. {
  120. SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  121. }
  122. else if(max_hz >= SPI_APB_CLOCK/32)
  123. {
  124. SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  125. }
  126. else if(max_hz >= SPI_APB_CLOCK/64)
  127. {
  128. SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
  129. }
  130. else if(max_hz >= SPI_APB_CLOCK/128)
  131. {
  132. SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
  133. }
  134. else
  135. {
  136. /* min prescaler 256 */
  137. SpiHandle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  138. }
  139. } /* baudrate */
  140. /* CPOL */
  141. if(configuration->mode & RT_SPI_CPOL)
  142. {
  143. SpiHandle->Init.CLKPolarity = SPI_POLARITY_HIGH;
  144. }
  145. else
  146. {
  147. SpiHandle->Init.CLKPolarity = SPI_POLARITY_LOW;
  148. }
  149. /* CPHA */
  150. if(configuration->mode & RT_SPI_CPHA)
  151. {
  152. SpiHandle->Init.CLKPhase = SPI_PHASE_2EDGE;
  153. }
  154. else
  155. {
  156. SpiHandle->Init.CLKPhase = SPI_PHASE_1EDGE;
  157. }
  158. /* MSB or LSB */
  159. if(configuration->mode & RT_SPI_MSB)
  160. {
  161. SpiHandle->Init.FirstBit = SPI_FIRSTBIT_MSB;
  162. }
  163. else
  164. {
  165. SpiHandle->Init.FirstBit = SPI_FIRSTBIT_LSB;
  166. }
  167. SpiHandle->Init.Direction = SPI_DIRECTION_2LINES;
  168. SpiHandle->Init.Mode = SPI_MODE_MASTER;
  169. SpiHandle->Init.NSS = SPI_NSS_SOFT;
  170. SpiHandle->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  171. SpiHandle->Init.TIMode = SPI_TIMODE_DISABLE;
  172. /* init SPI */
  173. if (HAL_SPI_Init(SpiHandle) != HAL_OK)
  174. {
  175. return RT_ERROR;
  176. }
  177. /* Enable SPI_MASTER */
  178. __HAL_SPI_ENABLE(SpiHandle);
  179. DEBUG_PRINTF("spi configuration\n");
  180. return RT_EOK;
  181. };
  182. static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message)
  183. {
  184. struct rt_spi_bus * stm32_spi_bus = (struct rt_spi_bus *)device->bus;
  185. struct stm32f4_spi *f4_spi = (struct stm32f4_spi *)stm32_spi_bus->parent.user_data;
  186. struct rt_spi_configuration * config = &device->config;
  187. SPI_TypeDef * SPI = f4_spi->spi_handle.Instance;
  188. struct stm32_spi_cs * stm32_spi_cs = device->parent.user_data;
  189. rt_uint32_t size = message->length;
  190. RT_ASSERT(device != NULL);
  191. RT_ASSERT(message != NULL);
  192. /* take CS */
  193. if(message->cs_take)
  194. {
  195. HAL_GPIO_WritePin(stm32_spi_cs->GPIOx, stm32_spi_cs->GPIO_Pin, GPIO_PIN_RESET);
  196. }
  197. #ifdef SPI_USE_DMA
  198. if(message->length > 32)
  199. {
  200. if(config->data_width <= 8)
  201. {
  202. HAL_StatusTypeDef state;
  203. DEBUG_PRINTF("spi dma transfer start\n");
  204. DMA_RxConfiguration(stm32_spi_bus, message);
  205. DEBUG_PRINTF("dma configuration finish , send buf %X, rec buf %X, length: %d\n",
  206. (uint32_t)message->send_buf, (uint32_t)message->recv_buf, message->length);
  207. state = HAL_SPI_TransmitReceive_DMA(&f4_spi->spi_handle,
  208. (uint8_t*)message->send_buf,
  209. (uint8_t*)message->recv_buf,
  210. message->length);
  211. if (state != HAL_OK)
  212. {
  213. DEBUG_PRINTF("spi flash configuration error : %d\n", state);
  214. message->length = 0;
  215. //while(1);
  216. }
  217. else
  218. {
  219. DEBUG_PRINTF("spi dma transfer finish\n");
  220. }
  221. while (HAL_SPI_GetState(&f4_spi->spi_handle) != HAL_SPI_STATE_READY);
  222. DEBUG_PRINTF("spi get state finish\n");
  223. }
  224. else
  225. {
  226. // Todo
  227. }
  228. }
  229. else
  230. #endif
  231. {
  232. if(config->data_width <= 8)
  233. {
  234. const rt_uint8_t * send_ptr = message->send_buf;
  235. rt_uint8_t * recv_ptr = message->recv_buf;
  236. while(size--)
  237. {
  238. rt_uint8_t data = 0xFF;
  239. if(send_ptr != RT_NULL)
  240. {
  241. data = *send_ptr++;
  242. }
  243. // Todo: replace register read/write by stm32f4 lib
  244. //Wait until the transmit buffer is empty
  245. while ((SPI->SR & SPI_FLAG_TXE) == RESET);
  246. // Send the byte
  247. SPI->DR = data;
  248. //Wait until a data is received
  249. while ((SPI->SR & SPI_FLAG_RXNE) == RESET);
  250. // Get the received data
  251. data = SPI->DR;
  252. if(recv_ptr != RT_NULL)
  253. {
  254. *recv_ptr++ = data;
  255. }
  256. }
  257. }
  258. else if(config->data_width <= 16)
  259. {
  260. const rt_uint16_t * send_ptr = message->send_buf;
  261. rt_uint16_t * recv_ptr = message->recv_buf;
  262. while(size--)
  263. {
  264. rt_uint16_t data = 0xFF;
  265. if(send_ptr != RT_NULL)
  266. {
  267. data = *send_ptr++;
  268. }
  269. //Wait until the transmit buffer is empty
  270. while ((SPI->SR & SPI_FLAG_TXE) == RESET);
  271. // Send the byte
  272. SPI->DR = data;
  273. //Wait until a data is received
  274. while ((SPI->SR & SPI_FLAG_RXNE) == RESET);
  275. // Get the received data
  276. data = SPI->DR;
  277. if(recv_ptr != RT_NULL)
  278. {
  279. *recv_ptr++ = data;
  280. }
  281. }
  282. }
  283. }
  284. /* release CS */
  285. if(message->cs_release)
  286. {
  287. //GPIO_SetBits(stm32_spi_cs->GPIOx, stm32_spi_cs->GPIO_Pin);
  288. HAL_GPIO_WritePin(stm32_spi_cs->GPIOx, stm32_spi_cs->GPIO_Pin, GPIO_PIN_SET);
  289. }
  290. return message->length;
  291. };
  292. #ifdef RT_USING_SPI1
  293. static struct stm32f4_spi stm32f4_spi1 =
  294. {
  295. /* .spi_handle = */{
  296. /* .Instance = */ SPI1,
  297. },
  298. /* .hdma_rx = */ {
  299. DMA2_Stream2,
  300. DMA_CHANNEL_3,
  301. },
  302. /* .hdma_rx_irq = */ DMA2_Stream2_IRQn,
  303. /* .hdma_tx = */{
  304. DMA2_Stream3,
  305. DMA_CHANNEL_3,
  306. },
  307. /* .hdma_tx_irq = */ DMA2_Stream3_IRQn,
  308. };
  309. static struct rt_spi_bus spi1_bus;
  310. /**
  311. * @brief This function handles DMA Rx interrupt request.
  312. * @param None
  313. * @retval None
  314. */
  315. void DMA2_Stream2_IRQHandler(void)
  316. {
  317. HAL_DMA_IRQHandler(stm32f4_spi1.spi_handle.hdmarx);
  318. }
  319. /**
  320. * @brief This function handles DMA Tx interrupt request.
  321. * @param None
  322. * @retval None
  323. */
  324. void DMA2_Stream3_IRQHandler(void)
  325. {
  326. HAL_DMA_IRQHandler(stm32f4_spi1.spi_handle.hdmatx);
  327. }
  328. #endif
  329. #ifdef RT_USING_SPI2
  330. struct stm32f4_spi stm32f4_spi2 =
  331. {
  332. /* .spi_handle = */{
  333. /* .Instance = */ SPI2,
  334. },
  335. /* .hdma_rx = */ {
  336. DMA1_Stream3,
  337. DMA_CHANNEL_0,
  338. },
  339. /* .hdma_rx_irq = */ DMA1_Stream3_IRQn,
  340. /* .hdma_tx = */{
  341. DMA1_Stream4,
  342. DMA_CHANNEL_0,
  343. },
  344. /* .hdma_tx_irq = */ DMA1_Stream4_IRQn,
  345. };
  346. static struct rt_spi_bus spi2_bus;
  347. /**
  348. * @brief This function handles DMA Rx interrupt request.
  349. * @param None
  350. * @retval None
  351. */
  352. void DMA1_Stream3_IRQHandler(void)
  353. {
  354. HAL_DMA_IRQHandler(stm32f4_spi2.spi_handle.hdmarx);
  355. }
  356. /**
  357. * @brief This function handles DMA Tx interrupt request.
  358. * @param None
  359. * @retval None
  360. */
  361. void DMA1_Stream4_IRQHandler(void)
  362. {
  363. HAL_DMA_IRQHandler(stm32f4_spi2.spi_handle.hdmatx);
  364. }
  365. #endif
  366. #ifdef RT_USING_SPI3
  367. struct stm32f4_spi stm32f4_spi3 =
  368. {
  369. /* .spi_handle = */{
  370. /* .Instance = */ SPI3,
  371. },
  372. /* .hdma_rx = */ {
  373. DMA1_Stream0,
  374. DMA_CHANNEL_0,
  375. },
  376. /* .hdma_rx_irq = */ DMA1_Stream0_IRQn,
  377. /* .hdma_tx = */{
  378. DMA1_Stream2,
  379. DMA_CHANNEL_0,
  380. },
  381. /* .hdma_tx_irq = */ DMA1_Stream2_IRQn,
  382. };
  383. static struct rt_spi_bus spi3_bus;
  384. /**
  385. * @brief This function handles DMA Rx interrupt request.
  386. * @param None
  387. * @retval None
  388. */
  389. void DMA1_Stream0_IRQHandler(void)
  390. {
  391. HAL_DMA_IRQHandler(stm32f4_spi3.spi_handle.hdmarx);
  392. }
  393. /**
  394. * @brief This function handles DMA Tx interrupt request.
  395. * @param None
  396. * @retval None
  397. */
  398. void DMA1_Stream2_IRQHandler(void)
  399. {
  400. HAL_DMA_IRQHandler(stm32f4_spi3.spi_handle.hdmatx);
  401. }
  402. #endif
  403. #ifdef RT_USING_SPI4
  404. struct stm32f4_spi stm32f4_spi4 =
  405. {
  406. /* .spi_handle = */{
  407. /* .Instance = */ SPI4,
  408. },
  409. /* .hdma_rx = */ {
  410. DMA2_Stream0,
  411. DMA_CHANNEL_4,
  412. },
  413. /* .hdma_rx_irq = */ DMA2_Stream0_IRQn,
  414. /* .hdma_tx = */{
  415. DMA2_Stream1,
  416. DMA_CHANNEL_4,
  417. },
  418. /* .hdma_tx_irq = */ DMA2_Stream1_IRQn,
  419. };
  420. static struct rt_spi_bus spi4_bus;
  421. /**
  422. * @brief This function handles DMA Rx interrupt request.
  423. * @param None
  424. * @retval None
  425. */
  426. void DMA2_Stream0_IRQHandler(void)
  427. {
  428. HAL_DMA_IRQHandler(stm32f4_spi4.spi_handle.hdmarx);
  429. }
  430. /**
  431. * @brief This function handles DMA Tx interrupt request.
  432. * @param None
  433. * @retval None
  434. */
  435. void DMA2_Stream1_IRQHandler(void)
  436. {
  437. HAL_DMA_IRQHandler(stm32f4_spi4.spi_handle.hdmatx);
  438. }
  439. #endif
  440. #ifdef RT_USING_SPI5
  441. struct stm32f4_spi stm32f4_spi5 =
  442. {
  443. /* .spi_handle = */{
  444. /* .Instance = */ SPI5,
  445. },
  446. #ifdef SPI_USE_DMA
  447. /* .hdma_rx = */ {
  448. DMA2_Stream3,
  449. DMA_CHANNEL_2,
  450. },
  451. /* .hdma_rx_irq = */ DMA2_Stream3_IRQn,
  452. /* .hdma_tx = */{
  453. DMA2_Stream4,
  454. DMA_CHANNEL_2,
  455. },
  456. /* .hdma_tx_irq = */ DMA2_Stream4_IRQn,
  457. #endif /* SPI_USE_DMA */
  458. };
  459. static struct rt_spi_bus spi5_bus;
  460. /**
  461. * @brief This function handles DMA Rx interrupt request.
  462. * @param None
  463. * @retval None
  464. */
  465. void DMA2_Stream3_IRQHandler(void)
  466. {
  467. HAL_DMA_IRQHandler(stm32f4_spi5.spi_handle.hdmarx);
  468. }
  469. /**
  470. * @brief This function handles DMA Tx interrupt request.
  471. * @param None
  472. * @retval None
  473. */
  474. void DMA2_Stream4_IRQHandler(void)
  475. {
  476. HAL_DMA_IRQHandler(stm32f4_spi5.spi_handle.hdmatx);
  477. }
  478. #endif
  479. #ifdef RT_USING_SPI6
  480. struct stm32f4_spi stm32f4_spi6 =
  481. {
  482. /* .spi_handle = */{
  483. /* .Instance = */ SPI5,
  484. },
  485. /* .hdma_rx = */ {
  486. DMA2_Stream6,
  487. DMA_CHANNEL_2,
  488. },
  489. /* .hdma_rx_irq = */ DMA2_Stream6_IRQn,
  490. /* .hdma_tx = */{
  491. DMA2_Stream5,
  492. DMA_CHANNEL_2,
  493. },
  494. /* .hdma_tx_irq = */ DMA2_Stream5_IRQn,
  495. };
  496. static struct rt_spi_bus spi6_bus;
  497. /**
  498. * @brief This function handles DMA Rx interrupt request.
  499. * @param None
  500. * @retval None
  501. */
  502. void DMA2_Stream6_IRQHandler(void)
  503. {
  504. HAL_DMA_IRQHandler(stm32f4_spi6.spi_handle.hdmarx);
  505. }
  506. /**
  507. * @brief This function handles DMA Tx interrupt request.
  508. * @param None
  509. * @retval None
  510. */
  511. void DMA2_Stream5_IRQHandler(void)
  512. {
  513. HAL_DMA_IRQHandler(stm32f4_spi6.spi_handle.hdmatx);
  514. }
  515. #endif
  516. /** \brief init and register stm32 spi bus.
  517. *
  518. * \param SPI: STM32 SPI, e.g: SPI1,SPI2,SPI3.
  519. * \param spi_bus_name: spi bus name, e.g: "spi1"
  520. * \return
  521. *
  522. */
  523. rt_err_t stm32_spi_bus_register(SPI_TypeDef * SPI,
  524. //struct stm32_spi_bus * stm32_spi,
  525. const char * spi_bus_name)
  526. {
  527. struct stm32f4_spi * p_spi_bus;
  528. struct rt_spi_bus * spi_bus;
  529. RT_ASSERT(SPI != RT_NULL);
  530. //RT_ASSERT(stm32_spi != RT_NULL);
  531. RT_ASSERT(spi_bus_name != RT_NULL);
  532. #ifdef RT_USING_SPI1
  533. if(SPI == SPI1)
  534. {
  535. #ifdef SPI_USE_DMA
  536. __HAL_RCC_DMA2_CLK_ENABLE();
  537. p_spi_bus = &stm32f4_spi1;
  538. #endif
  539. __HAL_RCC_SPI1_CLK_ENABLE();
  540. spi_bus = &spi1_bus;
  541. }
  542. #endif
  543. #ifdef RT_USING_SPI2
  544. if(SPI == SPI2)
  545. {
  546. #ifdef SPI_USE_DMA
  547. __HAL_RCC_DMA1_CLK_ENABLE();
  548. p_spi_bus = &stm32f4_spi2;
  549. #endif
  550. __HAL_RCC_SPI2_CLK_ENABLE();
  551. spi_bus = &spi2_bus;
  552. }
  553. #endif
  554. #ifdef RT_USING_SPI3
  555. if(SPI == SPI3)
  556. {
  557. //stm32_spi->spi_handle.Instance = SPI3;
  558. #ifdef SPI_USE_DMA
  559. __HAL_RCC_DMA1_CLK_ENABLE();
  560. p_spi_bus = &stm32f4_spi3;
  561. #endif
  562. __HAL_RCC_SPI3_CLK_ENABLE();
  563. spi_bus = &spi3_bus;
  564. }
  565. #endif
  566. #ifdef RT_USING_SPI4
  567. if(SPI == SPI4)
  568. {
  569. #ifdef SPI_USE_DMA
  570. __HAL_RCC_DMA2_CLK_ENABLE();
  571. #endif
  572. __HAL_RCC_SPI4_CLK_ENABLE();
  573. spi_bus = &spi4_bus;
  574. }
  575. #endif
  576. #ifdef RT_USING_SPI5
  577. if(SPI == SPI5)
  578. {
  579. #ifdef SPI_USE_DMA
  580. __HAL_RCC_DMA2_CLK_ENABLE();
  581. p_spi_bus = &stm32f4_spi5;
  582. #endif
  583. __HAL_RCC_SPI5_CLK_ENABLE();
  584. spi_bus = &spi5_bus;
  585. }
  586. #endif
  587. #ifdef RT_USING_SPI6
  588. if(SPI == SPI6)
  589. {
  590. #ifdef SPI_USE_DMA
  591. __HAL_RCC_DMA2_CLK_ENABLE();
  592. p_spi_bus = &stm32f4_spi5;
  593. #endif
  594. __HAL_RCC_SPI6_CLK_ENABLE();
  595. spi_bus = &spi6_bus;
  596. }
  597. #endif
  598. if ( (SPI != SPI1) && (SPI != SPI2) && (SPI != SPI3)
  599. && (SPI != SPI4) && (SPI != SPI5) && (SPI != SPI6))
  600. {
  601. return RT_ENOSYS;
  602. }
  603. #ifdef SPI_USE_DMA
  604. /* Configure the DMA handler for Transmission process */
  605. p_spi_bus->hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  606. p_spi_bus->hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  607. //p_spi_bus->hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
  608. p_spi_bus->hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  609. p_spi_bus->hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  610. p_spi_bus->hdma_tx.Init.Mode = DMA_NORMAL;
  611. p_spi_bus->hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
  612. p_spi_bus->hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  613. p_spi_bus->hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  614. p_spi_bus->hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
  615. p_spi_bus->hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
  616. p_spi_bus->hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  617. p_spi_bus->hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  618. //p_spi_bus->hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
  619. p_spi_bus->hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  620. p_spi_bus->hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  621. p_spi_bus->hdma_rx.Init.Mode = DMA_NORMAL;
  622. p_spi_bus->hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
  623. p_spi_bus->hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  624. p_spi_bus->hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  625. p_spi_bus->hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
  626. p_spi_bus->hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
  627. #endif /* SPI_USE_DMA */
  628. spi_bus->parent.user_data = &stm32f4_spi5;
  629. return rt_spi_bus_register(spi_bus, spi_bus_name, &stm32_spi_ops);
  630. }