drv_spi.c 19 KB

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