drv_usart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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-10-30 SummerGift change to new framework
  9. */
  10. #include "board.h"
  11. #include "drv_usart.h"
  12. #include "drv_config.h"
  13. #ifdef RT_USING_SERIAL
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.usart"
  16. #include <drv_log.h>
  17. #if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) && !defined(BSP_USING_UART4) && !defined(BSP_USING_UART5)
  18. #error "Please define at least one BSP_USING_UARTx"
  19. /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
  20. #endif
  21. #ifdef RT_SERIAL_USING_DMA
  22. static void stm32_dma_config(struct rt_serial_device *serial);
  23. #endif
  24. enum
  25. {
  26. #ifdef BSP_USING_UART1
  27. UART1_INDEX,
  28. #endif
  29. #ifdef BSP_USING_UART2
  30. UART2_INDEX,
  31. #endif
  32. #ifdef BSP_USING_UART3
  33. UART3_INDEX,
  34. #endif
  35. #ifdef BSP_USING_UART4
  36. UART4_INDEX,
  37. #endif
  38. #ifdef BSP_USING_UART5
  39. UART5_INDEX,
  40. #endif
  41. };
  42. static struct stm32_uart_config uart_config[] =
  43. {
  44. #ifdef BSP_USING_UART1
  45. UART1_CONFIG,
  46. #endif
  47. #ifdef BSP_USING_UART2
  48. UART2_CONFIG,
  49. #endif
  50. #ifdef BSP_USING_UART3
  51. UART3_CONFIG,
  52. #endif
  53. #ifdef BSP_USING_UART4
  54. UART4_CONFIG,
  55. #endif
  56. #ifdef BSP_USING_UART5
  57. UART5_CONFIG,
  58. #endif
  59. };
  60. static struct stm32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
  61. static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  62. {
  63. struct stm32_uart *uart;
  64. RT_ASSERT(serial != RT_NULL);
  65. RT_ASSERT(cfg != RT_NULL);
  66. uart = (struct stm32_uart *)serial->parent.user_data;
  67. RT_ASSERT(uart != RT_NULL);
  68. uart->handle.Instance = uart->config->Instance;
  69. uart->handle.Init.BaudRate = cfg->baud_rate;
  70. uart->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  71. uart->handle.Init.Mode = UART_MODE_TX_RX;
  72. uart->handle.Init.OverSampling = UART_OVERSAMPLING_16;
  73. switch (cfg->data_bits)
  74. {
  75. case DATA_BITS_8:
  76. uart->handle.Init.WordLength = UART_WORDLENGTH_8B;
  77. break;
  78. case DATA_BITS_9:
  79. uart->handle.Init.WordLength = UART_WORDLENGTH_9B;
  80. break;
  81. default:
  82. uart->handle.Init.WordLength = UART_WORDLENGTH_8B;
  83. break;
  84. }
  85. switch (cfg->stop_bits)
  86. {
  87. case STOP_BITS_1:
  88. uart->handle.Init.StopBits = UART_STOPBITS_1;
  89. break;
  90. case STOP_BITS_2:
  91. uart->handle.Init.StopBits = UART_STOPBITS_2;
  92. break;
  93. default:
  94. uart->handle.Init.StopBits = UART_STOPBITS_1;
  95. break;
  96. }
  97. switch (cfg->parity)
  98. {
  99. case PARITY_NONE:
  100. uart->handle.Init.Parity = UART_PARITY_NONE;
  101. break;
  102. case PARITY_ODD:
  103. uart->handle.Init.Parity = UART_PARITY_ODD;
  104. break;
  105. case PARITY_EVEN:
  106. uart->handle.Init.Parity = UART_PARITY_EVEN;
  107. break;
  108. default:
  109. uart->handle.Init.Parity = UART_PARITY_NONE;
  110. break;
  111. }
  112. if (HAL_UART_Init(&uart->handle) != HAL_OK)
  113. {
  114. return -RT_ERROR;
  115. }
  116. return RT_EOK;
  117. }
  118. static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
  119. {
  120. struct stm32_uart *uart;
  121. #ifdef RT_SERIAL_USING_DMA
  122. rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
  123. #endif
  124. RT_ASSERT(serial != RT_NULL);
  125. uart = (struct stm32_uart *)serial->parent.user_data;
  126. RT_ASSERT(uart != RT_NULL);
  127. switch (cmd)
  128. {
  129. /* disable interrupt */
  130. case RT_DEVICE_CTRL_CLR_INT:
  131. /* disable rx irq */
  132. NVIC_DisableIRQ(uart->config->irq_type);
  133. /* disable interrupt */
  134. __HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_RXNE);
  135. break;
  136. /* enable interrupt */
  137. case RT_DEVICE_CTRL_SET_INT:
  138. /* enable rx irq */
  139. NVIC_EnableIRQ(uart->config->irq_type);
  140. /* enable interrupt */
  141. __HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_RXNE);
  142. break;
  143. #ifdef RT_SERIAL_USING_DMA
  144. case RT_DEVICE_CTRL_CONFIG:
  145. if (ctrl_arg == RT_DEVICE_FLAG_DMA_RX)
  146. {
  147. stm32_dma_config(serial);
  148. }
  149. break;
  150. #endif
  151. }
  152. return RT_EOK;
  153. }
  154. static int stm32_putc(struct rt_serial_device *serial, char c)
  155. {
  156. struct stm32_uart *uart;
  157. RT_ASSERT(serial != RT_NULL);
  158. uart = (struct stm32_uart *)serial->parent.user_data;
  159. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
  160. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0)
  161. uart->handle.Instance->TDR = c;
  162. #else
  163. uart->handle.Instance->DR = c;
  164. #endif
  165. while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET);
  166. return 1;
  167. }
  168. static int stm32_getc(struct rt_serial_device *serial)
  169. {
  170. int ch;
  171. struct stm32_uart *uart;
  172. RT_ASSERT(serial != RT_NULL);
  173. uart = (struct stm32_uart *)serial->parent.user_data;
  174. RT_ASSERT(uart != RT_NULL);
  175. ch = -1;
  176. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)
  177. {
  178. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0)
  179. ch = uart->handle.Instance->RDR & 0xff;
  180. #else
  181. ch = uart->handle.Instance->DR & 0xff;
  182. #endif
  183. }
  184. return ch;
  185. }
  186. static const struct rt_uart_ops stm32_uart_ops =
  187. {
  188. .configure = stm32_configure,
  189. .control = stm32_control,
  190. .putc = stm32_putc,
  191. .getc = stm32_getc,
  192. };
  193. /**
  194. * Uart common interrupt process. This need add to uart ISR.
  195. *
  196. * @param serial serial device
  197. */
  198. static void uart_isr(struct rt_serial_device *serial)
  199. {
  200. struct stm32_uart *uart;
  201. #ifdef RT_SERIAL_USING_DMA
  202. rt_size_t recv_total_index, recv_len;
  203. rt_base_t level;
  204. #endif
  205. RT_ASSERT(serial != RT_NULL);
  206. uart = (struct stm32_uart *) serial->parent.user_data;
  207. RT_ASSERT(uart != RT_NULL);
  208. /* UART in mode Receiver -------------------------------------------------*/
  209. if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) &&
  210. (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE) != RESET))
  211. {
  212. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  213. }
  214. #ifdef RT_SERIAL_USING_DMA
  215. else if ((uart->uart_dma_flag) && (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_IDLE) != RESET) &&
  216. (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_IDLE) != RESET))
  217. {
  218. level = rt_hw_interrupt_disable();
  219. recv_total_index = serial->config.bufsz - __HAL_DMA_GET_COUNTER(&(uart->dma.handle));
  220. recv_len = recv_total_index - uart->dma.last_index;
  221. uart->dma.last_index = recv_total_index;
  222. rt_hw_interrupt_enable(level);
  223. if (recv_len)
  224. {
  225. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
  226. }
  227. __HAL_UART_CLEAR_IDLEFLAG(&uart->handle);
  228. }
  229. #endif
  230. else
  231. {
  232. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_ORE) != RESET)
  233. {
  234. __HAL_UART_CLEAR_OREFLAG(&uart->handle);
  235. }
  236. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_NE) != RESET)
  237. {
  238. __HAL_UART_CLEAR_NEFLAG(&uart->handle);
  239. }
  240. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_FE) != RESET)
  241. {
  242. __HAL_UART_CLEAR_FEFLAG(&uart->handle);
  243. }
  244. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_PE) != RESET)
  245. {
  246. __HAL_UART_CLEAR_PEFLAG(&uart->handle);
  247. }
  248. #if !defined(SOC_SERIES_STM32L4) && !defined(SOC_SERIES_STM32F7) && !defined(SOC_SERIES_STM32F0)
  249. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_LBD) != RESET)
  250. {
  251. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_LBD);
  252. }
  253. #endif
  254. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_CTS) != RESET)
  255. {
  256. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_CTS);
  257. }
  258. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) != RESET)
  259. {
  260. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TXE);
  261. }
  262. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) != RESET)
  263. {
  264. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
  265. }
  266. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)
  267. {
  268. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_RXNE);
  269. }
  270. }
  271. }
  272. #if defined(BSP_USING_UART1)
  273. void USART1_IRQHandler(void)
  274. {
  275. /* enter interrupt */
  276. rt_interrupt_enter();
  277. uart_isr(&(uart_obj[UART1_INDEX].serial));
  278. /* leave interrupt */
  279. rt_interrupt_leave();
  280. }
  281. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA)
  282. void USART1_DMA_RX_IRQHandler(void)
  283. {
  284. /* enter interrupt */
  285. rt_interrupt_enter();
  286. HAL_DMA_IRQHandler(&uart_obj[UART1_INDEX].dma.handle);
  287. /* leave interrupt */
  288. rt_interrupt_leave();
  289. }
  290. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA) */
  291. #endif /* BSP_USING_UART1 */
  292. #if defined(BSP_USING_UART2)
  293. void USART2_IRQHandler(void)
  294. {
  295. /* enter interrupt */
  296. rt_interrupt_enter();
  297. uart_isr(&(uart_obj[UART2_INDEX].serial));
  298. /* leave interrupt */
  299. rt_interrupt_leave();
  300. }
  301. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_RX_USING_DMA)
  302. void USART2_DMA_RX_IRQHandler(void)
  303. {
  304. /* enter interrupt */
  305. rt_interrupt_enter();
  306. HAL_DMA_IRQHandler(&uart_obj[UART2_INDEX].dma.handle);
  307. /* leave interrupt */
  308. rt_interrupt_leave();
  309. }
  310. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_RX_USING_DMA) */
  311. #endif /* BSP_USING_UART2 */
  312. #if defined(BSP_USING_UART3)
  313. void USART3_IRQHandler(void)
  314. {
  315. /* enter interrupt */
  316. rt_interrupt_enter();
  317. uart_isr(&(uart_obj[UART3_INDEX].serial));
  318. /* leave interrupt */
  319. rt_interrupt_leave();
  320. }
  321. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART3_RX_USING_DMA)
  322. void USART3_DMA_RX_IRQHandler(void)
  323. {
  324. /* enter interrupt */
  325. rt_interrupt_enter();
  326. HAL_DMA_IRQHandler(&uart_obj[UART3_INDEX].dma.handle);
  327. /* leave interrupt */
  328. rt_interrupt_leave();
  329. }
  330. #endif /* defined(BSP_UART_USING_DMA_RX) && defined(BSP_UART3_RX_USING_DMA) */
  331. #endif /* BSP_USING_UART3*/
  332. #if defined(BSP_USING_UART4)
  333. void UART4_IRQHandler(void)
  334. {
  335. /* enter interrupt */
  336. rt_interrupt_enter();
  337. uart_isr(&(uart_obj[UART4_INDEX].serial));
  338. /* leave interrupt */
  339. rt_interrupt_leave();
  340. }
  341. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART4_RX_USING_DMA)
  342. void USART4_DMA_RX_IRQHandler(void)
  343. {
  344. /* enter interrupt */
  345. rt_interrupt_enter();
  346. HAL_DMA_IRQHandler(&uart_obj[UART4_INDEX].dma.handle);
  347. /* leave interrupt */
  348. rt_interrupt_leave();
  349. }
  350. #endif /* defined(BSP_UART_USING_DMA_RX) && defined(BSP_UART4_RX_USING_DMA) */
  351. #endif /* BSP_USING_UART4*/
  352. #if defined(BSP_USING_UART5)
  353. void UART5_IRQHandler(void)
  354. {
  355. /* enter interrupt */
  356. rt_interrupt_enter();
  357. uart_isr(&(uart_obj[UART5_INDEX].serial));
  358. /* leave interrupt */
  359. rt_interrupt_leave();
  360. }
  361. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_RX_USING_DMA)
  362. void USART5_DMA_RX_IRQHandler(void)
  363. {
  364. /* enter interrupt */
  365. rt_interrupt_enter();
  366. HAL_DMA_IRQHandler(&uart_obj[UART5_INDEX].dma.handle);
  367. /* leave interrupt */
  368. rt_interrupt_leave();
  369. }
  370. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_RX_USING_DMA) */
  371. #endif /* BSP_USING_UART5*/
  372. #ifdef RT_SERIAL_USING_DMA
  373. static void stm32_dma_config(struct rt_serial_device *serial)
  374. {
  375. RT_ASSERT(serial != RT_NULL);
  376. struct stm32_uart *uart = (struct stm32_uart *)serial->parent.user_data;
  377. RT_ASSERT(uart != RT_NULL);
  378. struct rt_serial_rx_fifo *rx_fifo;
  379. LOG_D("%s dma config start", uart->config->name);
  380. {
  381. rt_uint32_t tmpreg= 0x00U;
  382. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0)
  383. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  384. SET_BIT(RCC->AHBENR, uart->config->dma_rx->dma_rcc);
  385. tmpreg = READ_BIT(RCC->AHBENR, uart->config->dma_rx->dma_rcc);
  386. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4)
  387. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  388. SET_BIT(RCC->AHB1ENR, uart->config->dma_rx->dma_rcc);
  389. tmpreg = READ_BIT(RCC->AHB1ENR, uart->config->dma_rx->dma_rcc);
  390. #endif
  391. UNUSED(tmpreg); /* To avoid compiler warnings */
  392. }
  393. __HAL_LINKDMA(&(uart->handle), hdmarx, uart->dma.handle);
  394. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0)
  395. uart->dma.handle.Instance = uart->config->dma_rx->Instance;
  396. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  397. uart->dma.handle.Instance = uart->config->dma_rx->Instance;
  398. uart->dma.handle.Init.Channel = uart->config->dma_rx->channel;
  399. #elif defined(SOC_SERIES_STM32L4)
  400. uart->dma.handle.Instance = uart->config->dma_rx->Instance;
  401. uart->dma.handle.Init.Request = uart->config->dma_rx->request;
  402. #endif
  403. uart->dma.handle.Init.Direction = DMA_PERIPH_TO_MEMORY;
  404. uart->dma.handle.Init.PeriphInc = DMA_PINC_DISABLE;
  405. uart->dma.handle.Init.MemInc = DMA_MINC_ENABLE;
  406. uart->dma.handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  407. uart->dma.handle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  408. uart->dma.handle.Init.Mode = DMA_CIRCULAR;
  409. uart->dma.handle.Init.Priority = DMA_PRIORITY_MEDIUM;
  410. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  411. uart->dma.handle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  412. #endif
  413. if (HAL_DMA_DeInit(&(uart->dma.handle)) != HAL_OK)
  414. {
  415. RT_ASSERT(0);
  416. }
  417. if (HAL_DMA_Init(&(uart->dma.handle)) != HAL_OK)
  418. {
  419. RT_ASSERT(0);
  420. }
  421. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  422. /* Start DMA transfer */
  423. if (HAL_UART_Receive_DMA(&(uart->handle), rx_fifo->buffer, serial->config.bufsz) != HAL_OK)
  424. {
  425. /* Transfer error in reception process */
  426. RT_ASSERT(0);
  427. }
  428. /* enable interrupt */
  429. __HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_IDLE);
  430. /* enable rx irq */
  431. HAL_NVIC_SetPriority(uart->config->dma_rx->dma_irq, 0, 0);
  432. HAL_NVIC_EnableIRQ(uart->config->dma_rx->dma_irq);
  433. HAL_NVIC_SetPriority(uart->config->irq_type, 1, 0);
  434. HAL_NVIC_EnableIRQ(uart->config->irq_type);
  435. LOG_D("%s dma RX instance: %x", uart->config->name, uart->dma.handle.Instance);
  436. LOG_D("%s dma config done", uart->config->name);
  437. }
  438. /**
  439. * @brief UART error callbacks
  440. * @param huart: UART handle
  441. * @note This example shows a simple way to report transfer error, and you can
  442. * add your own implementation.
  443. * @retval None
  444. */
  445. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  446. {
  447. RT_ASSERT(huart != NULL);
  448. struct stm32_uart *uart = (struct stm32_uart *)huart;
  449. LOG_D("%s: %s %d\n", __FUNCTION__, uart->config->name, huart->ErrorCode);
  450. UNUSED(uart);
  451. }
  452. /**
  453. * @brief Rx Transfer completed callback
  454. * @param huart: UART handle
  455. * @note This example shows a simple way to report end of DMA Rx transfer, and
  456. * you can add your own implementation.
  457. * @retval None
  458. */
  459. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  460. {
  461. struct rt_serial_device *serial;
  462. struct stm32_uart *uart;
  463. rt_size_t recv_len;
  464. rt_base_t level;
  465. RT_ASSERT(huart != NULL);
  466. uart = (struct stm32_uart *)huart;
  467. serial = &uart->serial;
  468. level = rt_hw_interrupt_disable();
  469. recv_len = serial->config.bufsz - uart->dma.last_index;
  470. uart->dma.last_index = 0;
  471. rt_hw_interrupt_enable(level);
  472. if (recv_len)
  473. {
  474. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
  475. }
  476. }
  477. #endif /* RT_SERIAL_USING_DMA */
  478. static void stm32_uart_get_dma_config(void)
  479. {
  480. #ifdef BSP_UART1_RX_USING_DMA
  481. uart_obj[UART1_INDEX].uart_dma_flag = 1;
  482. static struct dma_config uart1_dma_rx = UART1_DMA_CONFIG;
  483. uart_config[UART1_INDEX].dma_rx = &uart1_dma_rx;
  484. #endif
  485. #ifdef BSP_UART2_RX_USING_DMA
  486. uart_obj[UART2_INDEX].uart_dma_flag = 1;
  487. static struct dma_config uart2_dma_rx = UART2_DMA_CONFIG;
  488. uart_config[UART2_INDEX].dma_rx = &uart2_dma_rx;
  489. #endif
  490. #ifdef BSP_UART3_RX_USING_DMA
  491. uart_obj[UART3_INDEX].uart_dma_flag = 1;
  492. static struct dma_config uart3_dma_rx = UART3_DMA_CONFIG;
  493. uart_config[UART3_INDEX].dma_rx = &uart3_dma_rx;
  494. #endif
  495. #ifdef BSP_UART4_RX_USING_DMA
  496. uart_obj[UART4_INDEX].uart_dma_flag = 1;
  497. static struct dma_config uart4_dma_rx = UART4_DMA_CONFIG;
  498. uart_config[UART4_INDEX].dma_rx = &uart4_dma_rx;
  499. #endif
  500. #ifdef BSP_UART5_RX_USING_DMA
  501. uart_obj[UART5_INDEX].uart_dma_flag = 1;
  502. static struct dma_config uart5_dma_rx = UART5_DMA_CONFIG;
  503. uart_config[UART5_INDEX].dma_rx = &uart5_dma_rx;
  504. #endif
  505. }
  506. int rt_hw_usart_init(void)
  507. {
  508. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct stm32_uart);
  509. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  510. rt_err_t result = 0;
  511. stm32_uart_get_dma_config();
  512. for (int i = 0; i < obj_num; i++)
  513. {
  514. uart_obj[i].config = &uart_config[i];
  515. uart_obj[i].serial.ops = &stm32_uart_ops;
  516. uart_obj[i].serial.config = config;
  517. #if defined(RT_SERIAL_USING_DMA)
  518. if(uart_obj[i].uart_dma_flag)
  519. {
  520. /* register UART device */
  521. result = rt_hw_serial_register(&uart_obj[i].serial,uart_obj[i].config->name,
  522. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX| RT_DEVICE_FLAG_DMA_RX
  523. ,&uart_obj[i]);
  524. }
  525. else
  526. #endif
  527. {
  528. /* register UART device */
  529. result = rt_hw_serial_register(&uart_obj[i].serial,uart_obj[i].config->name,
  530. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX
  531. ,&uart_obj[i]);
  532. }
  533. RT_ASSERT(result == RT_EOK);
  534. }
  535. return result;
  536. }
  537. #endif /* RT_USING_SERIAL */