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
  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. static void stm32_stop_dma_tx(struct rt_serial_device *serial)
  370. {
  371. struct stm32_uart *uart;
  372. RT_ASSERT(serial != RT_NULL);
  373. uart = rt_container_of(serial, struct stm32_uart, serial);
  374. if ((uart->dma_tx.handle.Instance->CR & DMA_SxCR_EN) == DMA_SxCR_EN) {
  375. return;
  376. }
  377. __HAL_DMA_DISABLE(&uart->dma_tx.handle);
  378. uart->dmaTxing = RT_FALSE;
  379. }
  380. #endif
  381. static void stm32_enable_interrupt(struct rt_serial_device *serial)
  382. {
  383. struct stm32_uart *uart;
  384. RT_ASSERT(serial != RT_NULL);
  385. uart = rt_container_of(serial, struct stm32_uart, serial);
  386. HAL_NVIC_EnableIRQ(uart->uart_config->irq_type);
  387. #ifdef RT_SERIAL_USING_DMA
  388. if (uart->uart_dma_flag) {
  389. HAL_NVIC_EnableIRQ(uart->uart_config->dma_conf_rx->dma_irq);
  390. }
  391. #endif
  392. }
  393. static void stm32_disable_interrupt(struct rt_serial_device *serial)
  394. {
  395. struct stm32_uart *uart;
  396. RT_ASSERT(serial != RT_NULL);
  397. uart = rt_container_of(serial, struct stm32_uart, serial);
  398. HAL_NVIC_DisableIRQ(uart->uart_config->irq_type);
  399. #ifdef RT_SERIAL_USING_DMA
  400. if (uart->uart_dma_flag) {
  401. HAL_NVIC_DisableIRQ(uart->uart_config->dma_conf_rx->dma_irq);
  402. }
  403. #endif
  404. }
  405. /**
  406. * Uart common interrupt process. This need add to uart ISR.
  407. *
  408. * @param serial serial device
  409. */
  410. static void uart_isr(struct rt_serial_device *serial)
  411. {
  412. struct stm32_uart *uart;
  413. #ifdef RT_SERIAL_USING_DMA
  414. rt_size_t dma_cnt;
  415. #endif
  416. RT_ASSERT(serial != RT_NULL);
  417. uart = rt_container_of(serial, struct stm32_uart, serial);
  418. /* UART in mode Receiver -------------------------------------------------*/
  419. if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) &&
  420. (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE) != RESET))
  421. {
  422. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  423. }
  424. if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) != RESET) &&
  425. (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_TXE) != RESET))
  426. {
  427. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
  428. }
  429. #ifdef RT_SERIAL_USING_DMA
  430. else if ((uart->uart_dma_flag) && (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_IDLE) != RESET)
  431. && (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_IDLE) != RESET))
  432. {
  433. __HAL_UART_CLEAR_IDLEFLAG(&uart->handle);
  434. dma_cnt = RT_SERIAL_DMA_BUFSZ - __HAL_DMA_GET_COUNTER(&(uart->dma_rx.handle));
  435. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (dma_cnt << 8));
  436. }
  437. else if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) &&
  438. (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_TC) != RESET))
  439. {
  440. if ((serial->parent.open_flag & RT_DEVICE_FLAG_DMA_TX) != 0)
  441. {
  442. HAL_UART_IRQHandler(&(uart->handle));
  443. }
  444. }
  445. #endif
  446. else
  447. {
  448. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_ORE) != RESET)
  449. {
  450. __HAL_UART_CLEAR_OREFLAG(&uart->handle);
  451. }
  452. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_NE) != RESET)
  453. {
  454. __HAL_UART_CLEAR_NEFLAG(&uart->handle);
  455. }
  456. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_FE) != RESET)
  457. {
  458. __HAL_UART_CLEAR_FEFLAG(&uart->handle);
  459. }
  460. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_PE) != RESET)
  461. {
  462. __HAL_UART_CLEAR_PEFLAG(&uart->handle);
  463. }
  464. #if !defined(SOC_SERIES_STM32L4) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32F7) && !defined(SOC_SERIES_STM32F0) \
  465. && !defined(SOC_SERIES_STM32L0) && !defined(SOC_SERIES_STM32G0) && !defined(SOC_SERIES_STM32H7) \
  466. && !defined(SOC_SERIES_STM32G4) && !defined(SOC_SERIES_STM32MP1) && !defined(SOC_SERIES_STM32WB)
  467. #ifdef SOC_SERIES_STM32F3
  468. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_LBDF) != RESET)
  469. {
  470. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_LBDF);
  471. }
  472. #else
  473. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_LBD) != RESET)
  474. {
  475. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_LBD);
  476. }
  477. #endif
  478. #endif
  479. if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_CTS) != RESET)
  480. {
  481. UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_CTS);
  482. }
  483. // if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) != RESET)
  484. // {
  485. // UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TXE);
  486. // }
  487. // if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) != RESET)
  488. // {
  489. // UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
  490. // }
  491. // if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)
  492. // {
  493. // UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_RXNE);
  494. // }
  495. }
  496. }
  497. #ifdef RT_SERIAL_USING_DMA
  498. static void dma_isr(struct rt_serial_device *serial)
  499. {
  500. struct stm32_uart *uart;
  501. rt_size_t dma_cnt;
  502. RT_ASSERT(serial != RT_NULL);
  503. uart = rt_container_of(serial, struct stm32_uart, serial);
  504. if ((__HAL_DMA_GET_IT_SOURCE(&(uart->dma_rx.handle), DMA_IT_TC) != RESET) ||
  505. (__HAL_DMA_GET_IT_SOURCE(&(uart->dma_rx.handle), DMA_IT_HT) != RESET))
  506. {
  507. dma_cnt = RT_SERIAL_DMA_BUFSZ - __HAL_DMA_GET_COUNTER(&(uart->dma_rx.handle));
  508. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (dma_cnt << 8));
  509. }
  510. }
  511. #endif
  512. #if defined(BSP_USING_UART1)
  513. void USART1_IRQHandler(void)
  514. {
  515. /* enter interrupt */
  516. rt_interrupt_enter();
  517. uart_isr(&(uart_obj[UART1_INDEX].serial));
  518. /* leave interrupt */
  519. rt_interrupt_leave();
  520. }
  521. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA)
  522. void UART1_DMA_RX_IRQHandler(void)
  523. {
  524. /* enter interrupt */
  525. rt_interrupt_enter();
  526. HAL_DMA_IRQHandler(&uart_obj[UART1_INDEX].dma_rx.handle);
  527. /* leave interrupt */
  528. rt_interrupt_leave();
  529. }
  530. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA) */
  531. #if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA)
  532. void UART1_DMA_TX_IRQHandler(void)
  533. {
  534. /* enter interrupt */
  535. rt_interrupt_enter();
  536. HAL_DMA_IRQHandler(&uart_obj[UART1_INDEX].dma_tx.handle);
  537. /* leave interrupt */
  538. rt_interrupt_leave();
  539. }
  540. #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA) */
  541. #endif /* BSP_USING_UART1 */
  542. #ifdef RT_SERIAL_USING_DMA
  543. static void stm32_uart_get_dma_config(void)
  544. {
  545. #ifdef BSP_USING_UART1
  546. uart_obj[UART1_INDEX].uart_dma_flag = 0;
  547. #ifdef BSP_UART1_RX_USING_DMA
  548. uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  549. static struct dma_config uart1_dma_rx = UART1_DMA_RX_CONFIG;
  550. uart_config[UART1_INDEX].dma_conf_rx = &uart1_dma_rx;
  551. #endif
  552. #ifdef BSP_UART1_TX_USING_DMA
  553. uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  554. static struct dma_config uart1_dma_tx = UART1_DMA_TX_CONFIG;
  555. uart_config[UART1_INDEX].dma_conf_tx = &uart1_dma_tx;
  556. #endif
  557. #endif
  558. }
  559. static void stm32_dma_rx_config(struct rt_serial_device *serial)
  560. {
  561. DMA_HandleTypeDef *DMA_Handle;
  562. struct dma_config *dma_config;
  563. struct stm32_uart *uart;
  564. RT_ASSERT(serial != RT_NULL);
  565. uart = rt_container_of(serial, struct stm32_uart, serial);
  566. DMA_Handle = &uart->dma_rx.handle;
  567. dma_config = uart->uart_config->dma_conf_rx;
  568. LOG_D("%s dma config start", uart->uart_config->name);
  569. {
  570. rt_uint32_t tmpreg = 0x00U;
  571. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) \
  572. || defined(SOC_SERIES_STM32L0)|| defined(SOC_SERIES_STM32F3) || defined(SOC_SERIES_STM32L1)
  573. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  574. SET_BIT(RCC->AHBENR, dma_config->dma_rcc);
  575. tmpreg = READ_BIT(RCC->AHBENR, dma_config->dma_rcc);
  576. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) \
  577. || defined(SOC_SERIES_STM32G4)|| defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32WB)
  578. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  579. SET_BIT(RCC->AHB1ENR, dma_config->dma_rcc);
  580. tmpreg = READ_BIT(RCC->AHB1ENR, dma_config->dma_rcc);
  581. #elif defined(SOC_SERIES_STM32MP1)
  582. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  583. SET_BIT(RCC->MP_AHB2ENSETR, dma_config->dma_rcc);
  584. tmpreg = READ_BIT(RCC->MP_AHB2ENSETR, dma_config->dma_rcc);
  585. #endif
  586. #if (defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32WB)) && defined(DMAMUX1)
  587. /* enable DMAMUX clock for L4+ and G4 */
  588. __HAL_RCC_DMAMUX1_CLK_ENABLE();
  589. #elif defined(SOC_SERIES_STM32MP1)
  590. __HAL_RCC_DMAMUX_CLK_ENABLE();
  591. #endif
  592. UNUSED(tmpreg); /* To avoid compiler warnings */
  593. }
  594. __HAL_LINKDMA(&(uart->handle), hdmarx, uart->dma_rx.handle);
  595. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L0)|| defined(SOC_SERIES_STM32F3) || defined(SOC_SERIES_STM32L1)
  596. DMA_Handle->Instance = dma_config->Instance;
  597. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  598. DMA_Handle->Instance = dma_config->Instance;
  599. DMA_Handle->Init.Channel = dma_config->channel;
  600. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32WB)\
  601. || defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32MP1)
  602. DMA_Handle->Instance = dma_config->Instance;
  603. DMA_Handle->Init.Request = dma_config->request;
  604. #endif
  605. DMA_Handle->Init.PeriphInc = DMA_PINC_DISABLE;
  606. DMA_Handle->Init.MemInc = DMA_MINC_ENABLE;
  607. DMA_Handle->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  608. DMA_Handle->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  609. DMA_Handle->Init.Direction = DMA_PERIPH_TO_MEMORY;
  610. DMA_Handle->Init.Mode = DMA_CIRCULAR;
  611. DMA_Handle->Init.Priority = DMA_PRIORITY_MEDIUM;
  612. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32MP1)
  613. DMA_Handle->Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  614. #endif
  615. if (HAL_DMA_DeInit(DMA_Handle) != HAL_OK)
  616. {
  617. RT_ASSERT(0);
  618. }
  619. if (HAL_DMA_Init(DMA_Handle) != HAL_OK)
  620. {
  621. RT_ASSERT(0);
  622. }
  623. /* enable interrupt */
  624. /* Start DMA transfer */
  625. if (HAL_UART_Receive_DMA(&(uart->handle), serial->serial_dma_rx, RT_SERIAL_DMA_BUFSZ) != HAL_OK)
  626. {
  627. /* Transfer error in reception process */
  628. RT_ASSERT(0);
  629. }
  630. CLEAR_BIT(uart->handle.Instance->CR3, USART_CR3_EIE);
  631. __HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_IDLE);
  632. /* DMA irq should set in DMA TX mode, or HAL_UART_TxCpltCallback function will not be called */
  633. HAL_NVIC_SetPriority(dma_config->dma_irq, 0, 0);
  634. HAL_NVIC_EnableIRQ(dma_config->dma_irq);
  635. }
  636. static void stm32_dma_tx_config(struct rt_serial_device *serial)
  637. {
  638. DMA_HandleTypeDef *DMA_Handle;
  639. struct dma_config *dma_config;
  640. struct stm32_uart *uart;
  641. RT_ASSERT(serial != RT_NULL);
  642. uart = rt_container_of(serial, struct stm32_uart, serial);
  643. DMA_Handle = &uart->dma_tx.handle;
  644. dma_config = uart->uart_config->dma_conf_tx;
  645. LOG_D("%s dma config start", uart->uart_config->name);
  646. {
  647. rt_uint32_t tmpreg = 0x00U;
  648. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) \
  649. || defined(SOC_SERIES_STM32L0)|| defined(SOC_SERIES_STM32F3) || defined(SOC_SERIES_STM32L1)
  650. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  651. SET_BIT(RCC->AHBENR, dma_config->dma_rcc);
  652. tmpreg = READ_BIT(RCC->AHBENR, dma_config->dma_rcc);
  653. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) \
  654. || defined(SOC_SERIES_STM32G4)|| defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32WB)
  655. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  656. SET_BIT(RCC->AHB1ENR, dma_config->dma_rcc);
  657. tmpreg = READ_BIT(RCC->AHB1ENR, dma_config->dma_rcc);
  658. #elif defined(SOC_SERIES_STM32MP1)
  659. /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
  660. SET_BIT(RCC->MP_AHB2ENSETR, dma_config->dma_rcc);
  661. tmpreg = READ_BIT(RCC->MP_AHB2ENSETR, dma_config->dma_rcc);
  662. #endif
  663. #if (defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32WB)) && defined(DMAMUX1)
  664. /* enable DMAMUX clock for L4+ and G4 */
  665. __HAL_RCC_DMAMUX1_CLK_ENABLE();
  666. #elif defined(SOC_SERIES_STM32MP1)
  667. __HAL_RCC_DMAMUX_CLK_ENABLE();
  668. #endif
  669. UNUSED(tmpreg); /* To avoid compiler warnings */
  670. }
  671. __HAL_LINKDMA(&(uart->handle), hdmatx, uart->dma_tx.handle);
  672. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L0)|| defined(SOC_SERIES_STM32F3) || defined(SOC_SERIES_STM32L1)
  673. DMA_Handle->Instance = dma_config->Instance;
  674. #elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  675. DMA_Handle->Instance = dma_config->Instance;
  676. DMA_Handle->Init.Channel = dma_config->channel;
  677. #elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32WB)\
  678. || defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32MP1)
  679. DMA_Handle->Instance = dma_config->Instance;
  680. DMA_Handle->Init.Request = dma_config->request;
  681. #endif
  682. DMA_Handle->Init.PeriphInc = DMA_PINC_DISABLE;
  683. DMA_Handle->Init.MemInc = DMA_MINC_ENABLE;
  684. DMA_Handle->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  685. DMA_Handle->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  686. DMA_Handle->Init.Direction = DMA_MEMORY_TO_PERIPH;
  687. DMA_Handle->Init.Mode = DMA_NORMAL;
  688. DMA_Handle->Init.Priority = DMA_PRIORITY_MEDIUM;
  689. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32MP1)
  690. DMA_Handle->Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  691. #endif
  692. if (HAL_DMA_DeInit(DMA_Handle) != HAL_OK)
  693. {
  694. RT_ASSERT(0);
  695. }
  696. if (HAL_DMA_Init(DMA_Handle) != HAL_OK)
  697. {
  698. RT_ASSERT(0);
  699. }
  700. /* DMA irq should set in DMA TX mode, or HAL_UART_TxCpltCallback function will not be called */
  701. HAL_NVIC_SetPriority(dma_config->dma_irq, 0, 0);
  702. HAL_NVIC_EnableIRQ(dma_config->dma_irq);
  703. }
  704. /**
  705. * @brief UART error callbacks
  706. * @param huart: UART handle
  707. * @note This example shows a simple way to report transfer error, and you can
  708. * add your own implementation.
  709. * @retval None
  710. */
  711. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  712. {
  713. struct stm32_uart *uart;
  714. RT_ASSERT(huart != NULL);
  715. uart = (struct stm32_uart *)huart;
  716. LOG_D("%s: %s %d\n", __FUNCTION__, uart->uart_config->name, huart->ErrorCode);
  717. UNUSED(uart);
  718. }
  719. /**
  720. * @brief Rx Transfer completed callback
  721. * @param huart: UART handle
  722. * @note This example shows a simple way to report end of DMA Rx transfer, and
  723. * you can add your own implementation.
  724. * @retval None
  725. */
  726. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  727. {
  728. struct stm32_uart *uart;
  729. RT_ASSERT(huart != NULL);
  730. uart = (struct stm32_uart *)huart;
  731. dma_isr(&uart->serial);
  732. }
  733. /**
  734. * @brief Rx Half transfer completed callback
  735. * @param huart: UART handle
  736. * @note This example shows a simple way to report end of DMA Rx Half transfer,
  737. * and you can add your own implementation.
  738. * @retval None
  739. */
  740. void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  741. {
  742. struct stm32_uart *uart;
  743. RT_ASSERT(huart != NULL);
  744. uart = (struct stm32_uart *)huart;
  745. dma_isr(&uart->serial);
  746. }
  747. /**
  748. * @brief HAL_UART_TxCpltCallback
  749. * @param huart: UART handle
  750. * @note This callback can be called by two functions, first in UART_EndTransmit_IT when
  751. * UART Tx complete and second in UART_DMATransmitCplt function in DMA Circular mode.
  752. * @retval None
  753. */
  754. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  755. {
  756. struct stm32_uart *uart;
  757. rt_size_t dma_cnt;
  758. RT_ASSERT(huart != NULL);
  759. uart = (struct stm32_uart *)huart;
  760. dma_cnt = __HAL_DMA_GET_COUNTER(&(uart->dma_tx.handle));
  761. if (dma_cnt == 0)
  762. {
  763. rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_TX_DMADONE);
  764. }
  765. }
  766. #endif /* RT_SERIAL_USING_DMA */
  767. static const struct rt_uart_ops stm32_uart_ops =
  768. {
  769. .init = stm32_init,
  770. .configure = stm32_configure,
  771. .control = stm32_control,
  772. .putc = stm32_putc,
  773. .getc = stm32_getc,
  774. .flush = stm32_flush,
  775. .start_tx = stm32_start_tx,
  776. .stop_tx = stm32_stop_tx,
  777. #ifdef RT_SERIAL_USING_DMA
  778. .is_dma_txing = stm32_is_dma_txing,
  779. .start_dma_tx = stm32_start_dma_tx,
  780. .stop_dma_tx = stm32_stop_dma_tx,
  781. #endif
  782. .enable_interrupt = stm32_enable_interrupt,
  783. .disable_interrupt = stm32_disable_interrupt,
  784. };
  785. int rt_hw_usart_init(void)
  786. {
  787. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct stm32_uart);
  788. rt_err_t result = 0;
  789. #ifdef RT_SERIAL_USING_DMA
  790. stm32_uart_get_dma_config();
  791. #endif
  792. for (int i = 0; i < obj_num; i++)
  793. {
  794. /* init UART object */
  795. uart_obj[i].uart_config = &uart_config[i];
  796. uart_obj[i].serial.ops = &stm32_uart_ops;
  797. /* register UART device */
  798. result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].uart_config->name,
  799. RT_DEVICE_FLAG_RDWR
  800. | RT_DEVICE_FLAG_INT_RX
  801. | RT_DEVICE_FLAG_INT_TX
  802. #ifdef RT_SERIAL_USING_DMA
  803. | uart_obj[i].uart_dma_flag
  804. #endif
  805. , NULL);
  806. RT_ASSERT(result == RT_EOK);
  807. }
  808. return result;
  809. }
  810. #endif /* RT_USING_SERIAL */