drv_spi.c 19 KB

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