drv_usartX.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-04-10 THEWON first version for serialX
  9. */
  10. #include "board.h"
  11. #include "drv_usartX.h"
  12. #include "drv_config.h"
  13. #ifdef RT_USING_SERIAL_X
  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) && \
  18. !defined(BSP_USING_UART4) && !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && \
  19. !defined(BSP_USING_UART7) && !defined(BSP_USING_UART8) && !defined(BSP_USING_LPUART1)
  20. #error "Please define at least one BSP_USING_UARTx"
  21. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  22. #endif
  23. #ifdef RT_SERIAL_USING_DMA
  24. static void stm32_dma_rx_config(struct rt_serial_device *serial);
  25. static void stm32_dma_tx_config(struct rt_serial_device *serial);
  26. #endif
  27. enum
  28. {
  29. #ifdef BSP_USING_UART1
  30. UART1_INDEX,
  31. #endif
  32. #ifdef BSP_USING_UART2
  33. UART2_INDEX,
  34. #endif
  35. #ifdef BSP_USING_UART3
  36. UART3_INDEX,
  37. #endif
  38. #ifdef BSP_USING_UART4
  39. UART4_INDEX,
  40. #endif
  41. #ifdef BSP_USING_UART5
  42. UART5_INDEX,
  43. #endif
  44. #ifdef BSP_USING_UART6
  45. UART6_INDEX,
  46. #endif
  47. #ifdef BSP_USING_UART7
  48. UART7_INDEX,
  49. #endif
  50. #ifdef BSP_USING_UART8
  51. UART8_INDEX,
  52. #endif
  53. #ifdef BSP_USING_LPUART1
  54. LPUART1_INDEX,
  55. #endif
  56. };
  57. static struct stm32_uart_config uart_config[] =
  58. {
  59. #ifdef BSP_USING_UART1
  60. UART1_CONFIG,
  61. #endif
  62. #ifdef BSP_USING_UART2
  63. UART2_CONFIG,
  64. #endif
  65. #ifdef BSP_USING_UART3
  66. UART3_CONFIG,
  67. #endif
  68. #ifdef BSP_USING_UART4
  69. UART4_CONFIG,
  70. #endif
  71. #ifdef BSP_USING_UART5
  72. UART5_CONFIG,
  73. #endif
  74. #ifdef BSP_USING_UART6
  75. UART6_CONFIG,
  76. #endif
  77. #ifdef BSP_USING_UART7
  78. UART7_CONFIG,
  79. #endif
  80. #ifdef BSP_USING_UART8
  81. UART8_CONFIG,
  82. #endif
  83. #ifdef BSP_USING_LPUART1
  84. LPUART1_CONFIG,
  85. #endif
  86. };
  87. static struct stm32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
  88. static rt_uint32_t stm32_uart_get_mask(rt_uint32_t word_length, rt_uint32_t parity)
  89. {
  90. rt_uint32_t mask;
  91. if (word_length == UART_WORDLENGTH_8B)
  92. {
  93. if (parity == UART_PARITY_NONE)
  94. {
  95. mask = 0x00FFU ;
  96. }
  97. else
  98. {
  99. mask = 0x007FU ;
  100. }
  101. }
  102. #ifdef UART_WORDLENGTH_9B
  103. else if (word_length == UART_WORDLENGTH_9B)
  104. {
  105. if (parity == UART_PARITY_NONE)
  106. {
  107. mask = 0x01FFU ;
  108. }
  109. else
  110. {
  111. mask = 0x00FFU ;
  112. }
  113. }
  114. #endif
  115. #ifdef UART_WORDLENGTH_7B
  116. else if (word_length == UART_WORDLENGTH_7B)
  117. {
  118. if (parity == UART_PARITY_NONE)
  119. {
  120. mask = 0x007FU ;
  121. }
  122. else
  123. {
  124. mask = 0x003FU ;
  125. }
  126. }
  127. else
  128. {
  129. mask = 0x0000U;
  130. }
  131. #endif
  132. return mask;
  133. }
  134. static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  135. {
  136. struct stm32_uart *uart;
  137. RT_ASSERT(serial != RT_NULL);
  138. RT_ASSERT(cfg != RT_NULL);
  139. uart = rt_container_of(serial, struct stm32_uart, serial);
  140. uart->handle.Instance = uart->uart_config->Instance;
  141. uart->handle.Init.BaudRate = cfg->baud_rate;
  142. uart->handle.Init.Mode = UART_MODE_TX_RX;
  143. uart->handle.Init.OverSampling = UART_OVERSAMPLING_16;
  144. switch (cfg->flowcontrol)
  145. {
  146. case RT_SERIAL_FLOWCONTROL_NONE:
  147. uart->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  148. break;
  149. case RT_SERIAL_FLOWCONTROL_CTSRTS:
  150. uart->handle.Init.HwFlowCtl = UART_HWCONTROL_RTS_CTS;
  151. break;
  152. default:
  153. uart->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  154. break;
  155. }
  156. switch (cfg->data_bits)
  157. {
  158. case DATA_BITS_8:
  159. if (cfg->parity == PARITY_ODD || cfg->parity == PARITY_EVEN)
  160. uart->handle.Init.WordLength = UART_WORDLENGTH_9B;
  161. else
  162. uart->handle.Init.WordLength = UART_WORDLENGTH_8B;
  163. break;
  164. case DATA_BITS_9:
  165. uart->handle.Init.WordLength = UART_WORDLENGTH_9B;
  166. break;
  167. default:
  168. uart->handle.Init.WordLength = UART_WORDLENGTH_8B;
  169. break;
  170. }
  171. switch (cfg->stop_bits)
  172. {
  173. case STOP_BITS_1:
  174. uart->handle.Init.StopBits = UART_STOPBITS_1;
  175. break;
  176. case STOP_BITS_2:
  177. uart->handle.Init.StopBits = UART_STOPBITS_2;
  178. break;
  179. default:
  180. uart->handle.Init.StopBits = UART_STOPBITS_1;
  181. break;
  182. }
  183. switch (cfg->parity)
  184. {
  185. case PARITY_NONE:
  186. uart->handle.Init.Parity = UART_PARITY_NONE;
  187. break;
  188. case PARITY_ODD:
  189. uart->handle.Init.Parity = UART_PARITY_ODD;
  190. break;
  191. case PARITY_EVEN:
  192. uart->handle.Init.Parity = UART_PARITY_EVEN;
  193. break;
  194. default:
  195. uart->handle.Init.Parity = UART_PARITY_NONE;
  196. break;
  197. }
  198. uart->uart_config->mask = stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity);
  199. if (HAL_UART_Init(&uart->handle) != HAL_OK)
  200. {
  201. return -RT_ERROR;
  202. }
  203. return RT_EOK;
  204. }
  205. static rt_err_t stm32_init(struct rt_serial_device *serial)
  206. {
  207. if (stm32_configure(serial, &serial->config) != RT_EOK)
  208. {
  209. return -RT_ERROR;
  210. }
  211. return RT_EOK;
  212. }
  213. static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
  214. {
  215. struct stm32_uart *uart;
  216. rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
  217. RT_ASSERT(serial != RT_NULL);
  218. uart = rt_container_of(serial, struct stm32_uart, serial);
  219. switch (cmd) {
  220. case RT_DEVICE_CTRL_OPEN:
  221. __HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_TXE);
  222. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_RXNE);
  223. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TXE);
  224. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
  225. /* enable interrupt */
  226. HAL_NVIC_SetPriority(uart->uart_config->irq_type, 1, 0);
  227. HAL_NVIC_EnableIRQ(uart->uart_config->irq_type);
  228. #ifdef RT_SERIAL_USING_DMA
  229. uart->dmaTxing = RT_FALSE;
  230. #endif
  231. break;
  232. case RT_DEVICE_CTRL_CLOSE:
  233. HAL_NVIC_DisableIRQ(uart->uart_config->irq_type);
  234. #ifdef RT_SERIAL_USING_DMA
  235. HAL_NVIC_DisableIRQ(uart->uart_config->dma_conf_rx->dma_irq);
  236. if (HAL_DMA_Abort(&(uart->dma_rx.handle)) != HAL_OK) {
  237. RT_ASSERT(0);
  238. }
  239. if (HAL_DMA_DeInit(&(uart->dma_rx.handle)) != HAL_OK) {
  240. RT_ASSERT(0);
  241. }
  242. HAL_NVIC_DisableIRQ(uart->uart_config->dma_conf_tx->dma_irq);
  243. if (HAL_DMA_Abort(&(uart->dma_tx.handle)) != HAL_OK) {
  244. RT_ASSERT(0);
  245. }
  246. if (HAL_DMA_DeInit(&(uart->dma_tx.handle)) != HAL_OK) {
  247. RT_ASSERT(0);
  248. }
  249. #endif
  250. __HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_RXNE);
  251. __HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_TXE);
  252. if (HAL_UART_DeInit(&(uart->handle)) != HAL_OK ) {
  253. }
  254. break;
  255. /* disable interrupt */
  256. case RT_DEVICE_CTRL_CLR_INT:
  257. /* disable interrupt */
  258. if (ctrl_arg & RT_DEVICE_FLAG_INT_RX) {
  259. __HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_RXNE);
  260. }
  261. #ifdef RT_SERIAL_USING_DMA
  262. /* disable DMA */
  263. if (ctrl_arg & RT_DEVICE_FLAG_DMA_RX) {
  264. HAL_NVIC_DisableIRQ(uart->uart_config->dma_conf_rx->dma_irq);
  265. }
  266. if(ctrl_arg & RT_DEVICE_FLAG_DMA_TX) {
  267. HAL_NVIC_DisableIRQ(uart->uart_config->dma_conf_tx->dma_irq);
  268. }
  269. #endif
  270. break;
  271. /* enable interrupt */
  272. case RT_DEVICE_CTRL_SET_INT:
  273. /* enable rx irq */
  274. if (ctrl_arg & RT_DEVICE_FLAG_INT_RX) {
  275. __HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_RXNE);
  276. }
  277. break;
  278. #ifdef RT_SERIAL_USING_DMA
  279. case RT_DEVICE_CTRL_CONFIG:
  280. if (ctrl_arg & RT_DEVICE_FLAG_DMA_RX) {
  281. stm32_dma_rx_config(serial);
  282. } else if (ctrl_arg & RT_DEVICE_FLAG_DMA_TX) {
  283. stm32_dma_tx_config(serial);
  284. }
  285. break;
  286. #endif
  287. default :
  288. break;
  289. }
  290. return RT_EOK;
  291. }
  292. static int stm32_putc(struct rt_serial_device *serial, char c)
  293. {
  294. struct stm32_uart *uart;
  295. RT_ASSERT(serial != RT_NULL);
  296. uart = rt_container_of(serial, struct stm32_uart, serial);
  297. while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) == RESET);
  298. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
  299. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \
  300. || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) \
  301. || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32WB) || defined(SOC_SERIES_STM32F3)
  302. uart->handle.Instance->TDR = c;
  303. #else
  304. uart->handle.Instance->DR = c;
  305. #endif
  306. return 1;
  307. }
  308. static int stm32_getc(struct rt_serial_device *serial)
  309. {
  310. int ch = -1;
  311. struct stm32_uart *uart;
  312. RT_ASSERT(serial != RT_NULL);
  313. uart = rt_container_of(serial, struct stm32_uart, serial);
  314. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) == RESET)
  315. {
  316. return -1;
  317. }
  318. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \
  319. || defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) \
  320. || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32WB)|| defined(SOC_SERIES_STM32F3)
  321. ch = uart->handle.Instance->RDR & uart->uart_config->mask;
  322. #else
  323. ch = uart->handle.Instance->DR & uart->uart_config->mask;
  324. #endif
  325. return ch;
  326. }
  327. static int stm32_flush(struct rt_serial_device *serial)
  328. {
  329. struct stm32_uart *uart;
  330. RT_ASSERT(serial != RT_NULL);
  331. uart = rt_container_of(serial, struct stm32_uart, serial);
  332. while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET);
  333. return 1;
  334. }
  335. static void stm32_start_tx(struct rt_serial_device *serial)
  336. {
  337. struct stm32_uart *uart;
  338. RT_ASSERT(serial != RT_NULL);
  339. uart = rt_container_of(serial, struct stm32_uart, serial);
  340. __HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_TXE);
  341. }
  342. static void stm32_stop_tx(struct rt_serial_device *serial)
  343. {
  344. struct stm32_uart *uart;
  345. RT_ASSERT(serial != RT_NULL);
  346. uart = rt_container_of(serial, struct stm32_uart, serial);
  347. __HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_TXE);
  348. }
  349. #ifdef RT_SERIAL_USING_DMA
  350. static rt_bool_t stm32_is_dma_txing(struct rt_serial_device *serial)
  351. {
  352. struct stm32_uart *uart;
  353. RT_ASSERT(serial != RT_NULL);
  354. uart = rt_container_of(serial, struct stm32_uart, serial);
  355. return uart->dmaTxing; //RT_FALSE;
  356. }
  357. static void stm32_start_dma_tx(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size)
  358. {
  359. struct stm32_uart *uart;
  360. HAL_StatusTypeDef status;
  361. //DMA_HandleTypeDef *hdma;
  362. RT_ASSERT(serial != RT_NULL);
  363. uart = rt_container_of(serial, struct stm32_uart, serial);
  364. do { // may fallin dead loop
  365. status = HAL_UART_Transmit_DMA(&uart->handle, buf, size);
  366. } while (status != HAL_OK);
  367. uart->dmaTxing = RT_TRUE;
  368. }
  369. #define DMA_SxCR_EN ((uint32_t)0x00000001U) /*!< Channel enable */
  370. static void stm32_stop_dma_tx(struct rt_serial_device *serial)
  371. {
  372. struct stm32_uart *uart;
  373. RT_ASSERT(serial != RT_NULL);
  374. uart = rt_container_of(serial, struct stm32_uart, serial);
  375. if ((uart->dma_tx.handle.Instance->CCR & DMA_SxCR_EN) == DMA_SxCR_EN) {
  376. return;
  377. }
  378. __HAL_DMA_DISABLE(&uart->dma_tx.handle);
  379. uart->dmaTxing = RT_FALSE;
  380. }
  381. #endif
  382. static void stm32_enable_interrupt(struct rt_serial_device *serial)
  383. {
  384. struct stm32_uart *uart;
  385. RT_ASSERT(serial != RT_NULL);
  386. uart = rt_container_of(serial, struct stm32_uart, serial);
  387. HAL_NVIC_EnableIRQ(uart->uart_config->irq_type);
  388. #ifdef RT_SERIAL_USING_DMA
  389. if (uart->uart_dma_flag) {
  390. HAL_NVIC_EnableIRQ(uart->uart_config->dma_conf_rx->dma_irq);
  391. }
  392. #endif
  393. }
  394. static void stm32_disable_interrupt(struct rt_serial_device *serial)
  395. {
  396. struct stm32_uart *uart;
  397. RT_ASSERT(serial != RT_NULL);
  398. uart = rt_container_of(serial, struct stm32_uart, serial);
  399. HAL_NVIC_DisableIRQ(uart->uart_config->irq_type);
  400. #ifdef RT_SERIAL_USING_DMA
  401. if (uart->uart_dma_flag) {
  402. HAL_NVIC_DisableIRQ(uart->uart_config->dma_conf_rx->dma_irq);
  403. }
  404. #endif
  405. }
  406. /**
  407. * Uart common interrupt process. This need add to uart ISR.
  408. *
  409. * @param serial serial device
  410. */
  411. static void uart_isr(struct rt_serial_device *serial)
  412. {
  413. struct stm32_uart *uart;
  414. #ifdef RT_SERIAL_USING_DMA
  415. rt_size_t dma_cnt;
  416. #endif
  417. RT_ASSERT(serial != RT_NULL);
  418. uart = rt_container_of(serial, struct stm32_uart, serial);
  419. /* UART in mode Receiver -------------------------------------------------*/
  420. if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) &&
  421. (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE) != RESET))
  422. {
  423. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  424. }
  425. if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) != RESET) &&
  426. (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_TXE) != RESET))
  427. {
  428. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
  429. }
  430. #ifdef RT_SERIAL_USING_DMA
  431. else if ((uart->uart_dma_flag) && (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_IDLE) != RESET)
  432. && (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_IDLE) != RESET))
  433. {
  434. __HAL_UART_CLEAR_IDLEFLAG(&uart->handle);
  435. dma_cnt = RT_SERIAL_DMA_BUFSZ - __HAL_DMA_GET_COUNTER(&(uart->dma_rx.handle));
  436. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (dma_cnt << 8));
  437. }
  438. else if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) &&
  439. (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_TC) != RESET))
  440. {
  441. if ((serial->parent.open_flag & RT_DEVICE_FLAG_DMA_TX) != 0)
  442. {
  443. HAL_UART_IRQHandler(&(uart->handle));
  444. }
  445. }
  446. #endif
  447. else
  448. {
  449. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_ORE) != RESET)
  450. {
  451. __HAL_UART_CLEAR_OREFLAG(&uart->handle);
  452. }
  453. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_NE) != RESET)
  454. {
  455. __HAL_UART_CLEAR_NEFLAG(&uart->handle);
  456. }
  457. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_FE) != RESET)
  458. {
  459. __HAL_UART_CLEAR_FEFLAG(&uart->handle);
  460. }
  461. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_PE) != RESET)
  462. {
  463. __HAL_UART_CLEAR_PEFLAG(&uart->handle);
  464. }
  465. #if !defined(SOC_SERIES_STM32L4) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32F7) && !defined(SOC_SERIES_STM32F0) \
  466. && !defined(SOC_SERIES_STM32L0) && !defined(SOC_SERIES_STM32G0) && !defined(SOC_SERIES_STM32H7) \
  467. && !defined(SOC_SERIES_STM32G4) && !defined(SOC_SERIES_STM32MP1) && !defined(SOC_SERIES_STM32WB)
  468. #ifdef SOC_SERIES_STM32F3
  469. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_LBDF) != RESET)
  470. {
  471. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_LBDF);
  472. }
  473. #else
  474. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_LBD) != RESET)
  475. {
  476. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_LBD);
  477. }
  478. #endif
  479. #endif
  480. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_CTS) != RESET)
  481. {
  482. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_CTS);
  483. }
  484. // if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) != RESET)
  485. // {
  486. // UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TXE);
  487. // }
  488. // if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) != RESET)
  489. // {
  490. // UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
  491. // }
  492. // if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)
  493. // {
  494. // UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_RXNE);
  495. // }
  496. }
  497. }
  498. #ifdef RT_SERIAL_USING_DMA
  499. static void dma_isr(struct rt_serial_device *serial)
  500. {
  501. struct stm32_uart *uart;
  502. rt_size_t dma_cnt;
  503. RT_ASSERT(serial != RT_NULL);
  504. uart = rt_container_of(serial, struct stm32_uart, serial);
  505. if ((__HAL_DMA_GET_IT_SOURCE(&(uart->dma_rx.handle), DMA_IT_TC) != RESET) ||
  506. (__HAL_DMA_GET_IT_SOURCE(&(uart->dma_rx.handle), DMA_IT_HT) != RESET))
  507. {
  508. dma_cnt = RT_SERIAL_DMA_BUFSZ - __HAL_DMA_GET_COUNTER(&(uart->dma_rx.handle));
  509. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (dma_cnt << 8));
  510. }
  511. }
  512. #endif
  513. #if defined(BSP_USING_UART1)
  514. void USART1_IRQHandler(void)
  515. {
  516. /* enter interrupt */
  517. rt_interrupt_enter();
  518. uart_isr(&(uart_obj[UART1_INDEX].serial));
  519. /* leave interrupt */
  520. rt_interrupt_leave();
  521. }
  522. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA)
  523. void UART1_DMA_RX_IRQHandler(void)
  524. {
  525. /* enter interrupt */
  526. rt_interrupt_enter();
  527. HAL_DMA_IRQHandler(&uart_obj[UART1_INDEX].dma_rx.handle);
  528. /* leave interrupt */
  529. rt_interrupt_leave();
  530. }
  531. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA) */
  532. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA)
  533. void UART1_DMA_TX_IRQHandler(void)
  534. {
  535. /* enter interrupt */
  536. rt_interrupt_enter();
  537. HAL_DMA_IRQHandler(&uart_obj[UART1_INDEX].dma_tx.handle);
  538. /* leave interrupt */
  539. rt_interrupt_leave();
  540. }
  541. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA) */
  542. #endif /* BSP_USING_UART1 */
  543. #ifdef RT_SERIAL_USING_DMA
  544. static void stm32_uart_get_dma_config(void)
  545. {
  546. #ifdef BSP_USING_UART1
  547. uart_obj[UART1_INDEX].uart_dma_flag = 0;
  548. #ifdef BSP_UART1_RX_USING_DMA
  549. uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  550. static struct dma_config uart1_dma_rx = UART1_DMA_RX_CONFIG;
  551. uart_config[UART1_INDEX].dma_conf_rx = &uart1_dma_rx;
  552. #endif
  553. #ifdef BSP_UART1_TX_USING_DMA
  554. uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  555. static struct dma_config uart1_dma_tx = UART1_DMA_TX_CONFIG;
  556. uart_config[UART1_INDEX].dma_conf_tx = &uart1_dma_tx;
  557. #endif
  558. #endif
  559. }
  560. static void stm32_dma_rx_config(struct rt_serial_device *serial)
  561. {
  562. DMA_HandleTypeDef *DMA_Handle;
  563. struct dma_config *dma_config;
  564. struct stm32_uart *uart;
  565. RT_ASSERT(serial != RT_NULL);
  566. uart = rt_container_of(serial, struct stm32_uart, serial);
  567. DMA_Handle = &uart->dma_rx.handle;
  568. dma_config = uart->uart_config->dma_conf_rx;
  569. LOG_D("%s dma config start", uart->uart_config->name);
  570. {
  571. rt_uint32_t tmpreg = 0x00U;
  572. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) \
  573. || defined(SOC_SERIES_STM32L0)|| defined(SOC_SERIES_STM32F3) || defined(SOC_SERIES_STM32L1)
  574. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  575. SET_BIT(RCC->AHBENR, dma_config->dma_rcc);
  576. tmpreg = READ_BIT(RCC->AHBENR, dma_config->dma_rcc);
  577. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) \
  578. || defined(SOC_SERIES_STM32G4)|| defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32WB)
  579. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  580. SET_BIT(RCC->AHB1ENR, dma_config->dma_rcc);
  581. tmpreg = READ_BIT(RCC->AHB1ENR, dma_config->dma_rcc);
  582. #elif defined(SOC_SERIES_STM32MP1)
  583. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  584. SET_BIT(RCC->MP_AHB2ENSETR, dma_config->dma_rcc);
  585. tmpreg = READ_BIT(RCC->MP_AHB2ENSETR, dma_config->dma_rcc);
  586. #endif
  587. #if (defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32WB)) && defined(DMAMUX1)
  588. /* enable DMAMUX clock for L4+ and G4 */
  589. __HAL_RCC_DMAMUX1_CLK_ENABLE();
  590. #elif defined(SOC_SERIES_STM32MP1)
  591. __HAL_RCC_DMAMUX_CLK_ENABLE();
  592. #endif
  593. UNUSED(tmpreg); /* To avoid compiler warnings */
  594. }
  595. __HAL_LINKDMA(&(uart->handle), hdmarx, uart->dma_rx.handle);
  596. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L0)|| defined(SOC_SERIES_STM32F3) || defined(SOC_SERIES_STM32L1)
  597. DMA_Handle->Instance = dma_config->Instance;
  598. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  599. DMA_Handle->Instance = dma_config->Instance;
  600. DMA_Handle->Init.Channel = dma_config->channel;
  601. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32WB)\
  602. || defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32MP1)
  603. DMA_Handle->Instance = dma_config->Instance;
  604. DMA_Handle->Init.Request = dma_config->request;
  605. #endif
  606. DMA_Handle->Init.PeriphInc = DMA_PINC_DISABLE;
  607. DMA_Handle->Init.MemInc = DMA_MINC_ENABLE;
  608. DMA_Handle->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  609. DMA_Handle->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  610. DMA_Handle->Init.Direction = DMA_PERIPH_TO_MEMORY;
  611. DMA_Handle->Init.Mode = DMA_CIRCULAR;
  612. DMA_Handle->Init.Priority = DMA_PRIORITY_MEDIUM;
  613. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32MP1)
  614. DMA_Handle->Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  615. #endif
  616. if (HAL_DMA_DeInit(DMA_Handle) != HAL_OK)
  617. {
  618. RT_ASSERT(0);
  619. }
  620. if (HAL_DMA_Init(DMA_Handle) != HAL_OK)
  621. {
  622. RT_ASSERT(0);
  623. }
  624. /* enable interrupt */
  625. /* Start DMA transfer */
  626. if (HAL_UART_Receive_DMA(&(uart->handle), serial->serial_dma_rx, RT_SERIAL_DMA_BUFSZ) != HAL_OK)
  627. {
  628. /* Transfer error in reception process */
  629. RT_ASSERT(0);
  630. }
  631. // CLEAR_BIT(uart->handle.Instance->CR3, USART_CR3_EIE);
  632. __HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_IDLE);
  633. /* DMA irq should set in DMA TX mode, or HAL_UART_TxCpltCallback function will not be called */
  634. HAL_NVIC_SetPriority(dma_config->dma_irq, 0, 0);
  635. HAL_NVIC_EnableIRQ(dma_config->dma_irq);
  636. }
  637. static void stm32_dma_tx_config(struct rt_serial_device *serial)
  638. {
  639. DMA_HandleTypeDef *DMA_Handle;
  640. struct dma_config *dma_config;
  641. struct stm32_uart *uart;
  642. RT_ASSERT(serial != RT_NULL);
  643. uart = rt_container_of(serial, struct stm32_uart, serial);
  644. DMA_Handle = &uart->dma_tx.handle;
  645. dma_config = uart->uart_config->dma_conf_tx;
  646. LOG_D("%s dma config start", uart->uart_config->name);
  647. {
  648. rt_uint32_t tmpreg = 0x00U;
  649. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) \
  650. || defined(SOC_SERIES_STM32L0)|| defined(SOC_SERIES_STM32F3) || defined(SOC_SERIES_STM32L1)
  651. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  652. SET_BIT(RCC->AHBENR, dma_config->dma_rcc);
  653. tmpreg = READ_BIT(RCC->AHBENR, dma_config->dma_rcc);
  654. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) \
  655. || defined(SOC_SERIES_STM32G4)|| defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32WB)
  656. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  657. SET_BIT(RCC->AHB1ENR, dma_config->dma_rcc);
  658. tmpreg = READ_BIT(RCC->AHB1ENR, dma_config->dma_rcc);
  659. #elif defined(SOC_SERIES_STM32MP1)
  660. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  661. SET_BIT(RCC->MP_AHB2ENSETR, dma_config->dma_rcc);
  662. tmpreg = READ_BIT(RCC->MP_AHB2ENSETR, dma_config->dma_rcc);
  663. #endif
  664. #if (defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32WB)) && defined(DMAMUX1)
  665. /* enable DMAMUX clock for L4+ and G4 */
  666. __HAL_RCC_DMAMUX1_CLK_ENABLE();
  667. #elif defined(SOC_SERIES_STM32MP1)
  668. __HAL_RCC_DMAMUX_CLK_ENABLE();
  669. #endif
  670. UNUSED(tmpreg); /* To avoid compiler warnings */
  671. }
  672. __HAL_LINKDMA(&(uart->handle), hdmatx, uart->dma_tx.handle);
  673. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L0)|| defined(SOC_SERIES_STM32F3) || defined(SOC_SERIES_STM32L1)
  674. DMA_Handle->Instance = dma_config->Instance;
  675. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  676. DMA_Handle->Instance = dma_config->Instance;
  677. DMA_Handle->Init.Channel = dma_config->channel;
  678. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32WB)\
  679. || defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32MP1)
  680. DMA_Handle->Instance = dma_config->Instance;
  681. DMA_Handle->Init.Request = dma_config->request;
  682. #endif
  683. DMA_Handle->Init.PeriphInc = DMA_PINC_DISABLE;
  684. DMA_Handle->Init.MemInc = DMA_MINC_ENABLE;
  685. DMA_Handle->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  686. DMA_Handle->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  687. DMA_Handle->Init.Direction = DMA_MEMORY_TO_PERIPH;
  688. DMA_Handle->Init.Mode = DMA_NORMAL;
  689. DMA_Handle->Init.Priority = DMA_PRIORITY_MEDIUM;
  690. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32MP1)
  691. DMA_Handle->Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  692. #endif
  693. if (HAL_DMA_DeInit(DMA_Handle) != HAL_OK)
  694. {
  695. RT_ASSERT(0);
  696. }
  697. if (HAL_DMA_Init(DMA_Handle) != HAL_OK)
  698. {
  699. RT_ASSERT(0);
  700. }
  701. /* DMA irq should set in DMA TX mode, or HAL_UART_TxCpltCallback function will not be called */
  702. HAL_NVIC_SetPriority(dma_config->dma_irq, 0, 0);
  703. HAL_NVIC_EnableIRQ(dma_config->dma_irq);
  704. }
  705. /**
  706. * @brief UART error callbacks
  707. * @param huart: UART handle
  708. * @note This example shows a simple way to report transfer error, and you can
  709. * add your own implementation.
  710. * @retval None
  711. */
  712. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  713. {
  714. struct stm32_uart *uart;
  715. RT_ASSERT(huart != NULL);
  716. uart = (struct stm32_uart *)huart;
  717. LOG_D("%s: %s %d\n", __FUNCTION__, uart->uart_config->name, huart->ErrorCode);
  718. UNUSED(uart);
  719. }
  720. /**
  721. * @brief Rx Transfer completed callback
  722. * @param huart: UART handle
  723. * @note This example shows a simple way to report end of DMA Rx transfer, and
  724. * you can add your own implementation.
  725. * @retval None
  726. */
  727. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  728. {
  729. struct stm32_uart *uart;
  730. RT_ASSERT(huart != NULL);
  731. uart = (struct stm32_uart *)huart;
  732. dma_isr(&uart->serial);
  733. }
  734. /**
  735. * @brief Rx Half transfer completed callback
  736. * @param huart: UART handle
  737. * @note This example shows a simple way to report end of DMA Rx Half transfer,
  738. * and you can add your own implementation.
  739. * @retval None
  740. */
  741. void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  742. {
  743. struct stm32_uart *uart;
  744. RT_ASSERT(huart != NULL);
  745. uart = (struct stm32_uart *)huart;
  746. dma_isr(&uart->serial);
  747. }
  748. /**
  749. * @brief HAL_UART_TxCpltCallback
  750. * @param huart: UART handle
  751. * @note This callback can be called by two functions, first in UART_EndTransmit_IT when
  752. * UART Tx complete and second in UART_DMATransmitCplt function in DMA Circular mode.
  753. * @retval None
  754. */
  755. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  756. {
  757. struct stm32_uart *uart;
  758. rt_size_t dma_cnt;
  759. RT_ASSERT(huart != NULL);
  760. uart = (struct stm32_uart *)huart;
  761. dma_cnt = __HAL_DMA_GET_COUNTER(&(uart->dma_tx.handle));
  762. if (dma_cnt == 0)
  763. {
  764. rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_TX_DMADONE);
  765. }
  766. }
  767. #endif /* RT_SERIAL_USING_DMA */
  768. static const struct rt_uart_ops stm32_uart_ops =
  769. {
  770. .init = stm32_init,
  771. .configure = stm32_configure,
  772. .control = stm32_control,
  773. .putc = stm32_putc,
  774. .getc = stm32_getc,
  775. .flush = stm32_flush,
  776. .start_tx = stm32_start_tx,
  777. .stop_tx = stm32_stop_tx,
  778. #ifdef RT_SERIAL_USING_DMA
  779. .is_dma_txing = stm32_is_dma_txing,
  780. .start_dma_tx = stm32_start_dma_tx,
  781. .stop_dma_tx = stm32_stop_dma_tx,
  782. #endif
  783. .enable_interrupt = stm32_enable_interrupt,
  784. .disable_interrupt = stm32_disable_interrupt,
  785. };
  786. int rt_hw_usart_init(void)
  787. {
  788. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct stm32_uart);
  789. rt_err_t result = 0;
  790. #ifdef RT_SERIAL_USING_DMA
  791. stm32_uart_get_dma_config();
  792. #endif
  793. for (int i = 0; i < obj_num; i++)
  794. {
  795. /* init UART object */
  796. uart_obj[i].uart_config = &uart_config[i];
  797. uart_obj[i].serial.ops = &stm32_uart_ops;
  798. /* register UART device */
  799. result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].uart_config->name,
  800. RT_DEVICE_FLAG_RDWR
  801. | RT_DEVICE_FLAG_INT_RX
  802. | RT_DEVICE_FLAG_INT_TX
  803. #ifdef RT_SERIAL_USING_DMA
  804. | uart_obj[i].uart_dma_flag
  805. #endif
  806. , NULL);
  807. RT_ASSERT(result == RT_EOK);
  808. }
  809. return result;
  810. }
  811. #endif /* RT_USING_SERIAL */