drv_usart.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. * Copyright (c) 2022, Xiaohua Semiconductor Co., Ltd.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2022-04-28 CDT first version
  10. */
  11. /*******************************************************************************
  12. * Include files
  13. ******************************************************************************/
  14. #include <rtdevice.h>
  15. #include <rthw.h>
  16. #ifdef RT_USING_SERIAL
  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_UART9) || \
  20. defined(BSP_USING_UART10)
  21. #include "drv_usart.h"
  22. #include "board_config.h"
  23. /*******************************************************************************
  24. * Local type definitions ('typedef')
  25. ******************************************************************************/
  26. /*******************************************************************************
  27. * Local pre-processor symbols/macros ('#define')
  28. ******************************************************************************/
  29. #define DMA_CH_REG(reg_base, ch) \
  30. (*(uint32_t *)((uint32_t)(&(reg_base)) + ((ch) * 0x40UL)))
  31. #define DMA_TRANS_CNT(unit, ch) \
  32. (READ_REG32(DMA_CH_REG((unit)->MONDTCTL0, (ch))) >> DMA_DTCTL_CNT_POS)
  33. #define USART_TCI_ENABLE(unit) \
  34. SET_REG32_BIT(unit->CR1, USART_INT_TX_CPLT)
  35. #define UART_BAUDRATE_ERR_MAX (0.025F)
  36. #if defined (HC32F460)
  37. #define FCG_USART_CLK FCG_Fcg1PeriphClockCmd
  38. #elif defined (HC32F4A0)
  39. #define FCG_USART_CLK FCG_Fcg3PeriphClockCmd
  40. #endif
  41. #define FCG_TMR0_CLK FCG_Fcg2PeriphClockCmd
  42. #define FCG_DMA_CLK FCG_Fcg0PeriphClockCmd
  43. /*******************************************************************************
  44. * Global variable definitions (declared in header file with 'extern')
  45. ******************************************************************************/
  46. extern rt_err_t rt_hw_board_uart_init(CM_USART_TypeDef *USARTx);
  47. /*******************************************************************************
  48. * Local function prototypes ('static')
  49. ******************************************************************************/
  50. #ifdef RT_SERIAL_USING_DMA
  51. static void hc32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag);
  52. #endif
  53. /*******************************************************************************
  54. * Local variable definitions ('static')
  55. ******************************************************************************/
  56. enum
  57. {
  58. #ifdef BSP_USING_UART1
  59. UART1_INDEX,
  60. #endif
  61. #ifdef BSP_USING_UART2
  62. UART2_INDEX,
  63. #endif
  64. #ifdef BSP_USING_UART3
  65. UART3_INDEX,
  66. #endif
  67. #ifdef BSP_USING_UART4
  68. UART4_INDEX,
  69. #endif
  70. #ifdef BSP_USING_UART5
  71. UART5_INDEX,
  72. #endif
  73. #ifdef BSP_USING_UART6
  74. UART6_INDEX,
  75. #endif
  76. #ifdef BSP_USING_UART7
  77. UART7_INDEX,
  78. #endif
  79. #ifdef BSP_USING_UART8
  80. UART8_INDEX,
  81. #endif
  82. #ifdef BSP_USING_UART9
  83. UART9_INDEX,
  84. #endif
  85. #ifdef BSP_USING_UART10
  86. UART10_INDEX,
  87. #endif
  88. };
  89. static struct hc32_uart_config uart_config[] =
  90. {
  91. #ifdef BSP_USING_UART1
  92. UART1_CONFIG,
  93. #endif
  94. #ifdef BSP_USING_UART2
  95. UART2_CONFIG,
  96. #endif
  97. #ifdef BSP_USING_UART3
  98. UART3_CONFIG,
  99. #endif
  100. #ifdef BSP_USING_UART4
  101. UART4_CONFIG,
  102. #endif
  103. #ifdef BSP_USING_UART5
  104. UART5_CONFIG,
  105. #endif
  106. #ifdef BSP_USING_UART6
  107. UART6_CONFIG,
  108. #endif
  109. #ifdef BSP_USING_UART7
  110. UART7_CONFIG,
  111. #endif
  112. #ifdef BSP_USING_UART8
  113. UART8_CONFIG,
  114. #endif
  115. #ifdef BSP_USING_UART9
  116. UART9_CONFIG,
  117. #endif
  118. #ifdef BSP_USING_UART10
  119. UART10_CONFIG,
  120. #endif
  121. };
  122. static struct hc32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
  123. /*******************************************************************************
  124. * Function implementation - global ('extern') and local ('static')
  125. ******************************************************************************/
  126. static rt_err_t hc32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  127. {
  128. struct hc32_uart *uart;
  129. stc_usart_uart_init_t uart_init;
  130. RT_ASSERT(RT_NULL != cfg);
  131. RT_ASSERT(RT_NULL != serial);
  132. uart = rt_container_of(serial, struct hc32_uart, serial);
  133. USART_UART_StructInit(&uart_init);
  134. uart_init.u32OverSampleBit = USART_OVER_SAMPLE_8BIT;
  135. uart_init.u32Baudrate = cfg->baud_rate;
  136. uart_init.u32ClockSrc = USART_CLK_SRC_INTERNCLK;
  137. #if defined (HC32F4A0)
  138. if ((CM_USART1 == uart->config->Instance) || (CM_USART2 == uart->config->Instance) || \
  139. (CM_USART6 == uart->config->Instance) || (CM_USART7 == uart->config->Instance))
  140. #elif defined (HC32F460)
  141. if ((CM_USART1 == uart->config->Instance) || (CM_USART2 == uart->config->Instance) || \
  142. (CM_USART3 == uart->config->Instance) || (CM_USART4 == uart->config->Instance))
  143. #endif
  144. {
  145. uart_init.u32CKOutput = USART_CK_OUTPUT_ENABLE;
  146. }
  147. switch (cfg->data_bits)
  148. {
  149. case DATA_BITS_8:
  150. uart_init.u32DataWidth = USART_DATA_WIDTH_8BIT;
  151. break;
  152. case DATA_BITS_9:
  153. uart_init.u32DataWidth = USART_DATA_WIDTH_9BIT;
  154. break;
  155. default:
  156. uart_init.u32DataWidth = USART_DATA_WIDTH_8BIT;
  157. break;
  158. }
  159. switch (cfg->stop_bits)
  160. {
  161. case STOP_BITS_1:
  162. uart_init.u32StopBit = USART_STOPBIT_1BIT;
  163. break;
  164. case STOP_BITS_2:
  165. uart_init.u32StopBit = USART_STOPBIT_2BIT;
  166. break;
  167. default:
  168. uart_init.u32StopBit = USART_STOPBIT_1BIT;
  169. break;
  170. }
  171. switch (cfg->parity)
  172. {
  173. case PARITY_NONE:
  174. uart_init.u32Parity = USART_PARITY_NONE;
  175. break;
  176. case PARITY_EVEN:
  177. uart_init.u32Parity = USART_PARITY_EVEN;
  178. break;
  179. case PARITY_ODD:
  180. uart_init.u32Parity = USART_PARITY_ODD;
  181. break;
  182. default:
  183. uart_init.u32Parity = USART_PARITY_NONE;
  184. break;
  185. }
  186. if (BIT_ORDER_LSB == cfg->bit_order)
  187. {
  188. uart_init.u32FirstBit = USART_FIRST_BIT_LSB;
  189. }
  190. else
  191. {
  192. uart_init.u32FirstBit = USART_FIRST_BIT_MSB;
  193. }
  194. #if defined (HC32F4A0)
  195. switch (cfg->flowcontrol)
  196. {
  197. case RT_SERIAL_FLOWCONTROL_NONE:
  198. uart_init.u32HWFlowControl = USART_HW_FLOWCTRL_NONE;
  199. break;
  200. case RT_SERIAL_FLOWCONTROL_CTSRTS:
  201. uart_init.u32HWFlowControl = USART_HW_FLOWCTRL_RTS_CTS;
  202. break;
  203. default:
  204. uart_init.u32HWFlowControl = USART_HW_FLOWCTRL_NONE;
  205. break;
  206. }
  207. #endif
  208. #ifdef RT_SERIAL_USING_DMA
  209. uart->dma_rx_last_index = 0;
  210. #endif
  211. /* Enable USART clock */
  212. FCG_USART_CLK(uart->config->clock, ENABLE);
  213. if (RT_EOK != rt_hw_board_uart_init(uart->config->Instance))
  214. {
  215. return -RT_ERROR;
  216. }
  217. /* Configure UART */
  218. uint32_t u32Div;
  219. float32_t f32Error;
  220. int32_t i32Ret = LL_ERR;
  221. USART_DeInit(uart->config->Instance);
  222. USART_UART_Init(uart->config->Instance, &uart_init, NULL);
  223. for (u32Div = 0UL; u32Div <= USART_CLK_DIV64; u32Div++)
  224. {
  225. USART_SetClockDiv(uart->config->Instance, u32Div);
  226. if ((LL_OK == USART_SetBaudrate(uart->config->Instance, uart_init.u32Baudrate, &f32Error)) &&
  227. ((-UART_BAUDRATE_ERR_MAX <= f32Error) && (f32Error <= UART_BAUDRATE_ERR_MAX)))
  228. {
  229. i32Ret = LL_OK;
  230. break;
  231. }
  232. }
  233. if (i32Ret != LL_OK)
  234. {
  235. return -RT_ERROR;
  236. }
  237. /* Enable error interrupt */
  238. NVIC_EnableIRQ(uart->config->rxerr_irq.irq_config.irq_num);
  239. USART_FuncCmd(uart->config->Instance, USART_TX | USART_RX | USART_INT_RX, ENABLE);
  240. return RT_EOK;
  241. }
  242. static rt_err_t hc32_control(struct rt_serial_device *serial, int cmd, void *arg)
  243. {
  244. struct hc32_uart *uart;
  245. rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
  246. RT_ASSERT(RT_NULL != serial);
  247. uart = rt_container_of(serial, struct hc32_uart, serial);
  248. RT_ASSERT(RT_NULL != uart->config->Instance);
  249. switch (cmd)
  250. {
  251. /* Disable interrupt */
  252. case RT_DEVICE_CTRL_CLR_INT:
  253. if (RT_DEVICE_FLAG_INT_RX == ctrl_arg)
  254. {
  255. NVIC_DisableIRQ(uart->config->rx_irq.irq_config.irq_num);
  256. USART_FuncCmd(uart->config->Instance, USART_INT_RX, DISABLE);
  257. INTC_IrqSignOut(uart->config->rx_irq.irq_config.irq_num);
  258. }
  259. else if (RT_DEVICE_FLAG_INT_TX == ctrl_arg)
  260. {
  261. NVIC_DisableIRQ(uart->config->tx_irq.irq_config.irq_num);
  262. USART_FuncCmd(uart->config->Instance, USART_INT_TX_EMPTY, DISABLE);
  263. INTC_IrqSignOut(uart->config->tx_irq.irq_config.irq_num);
  264. }
  265. #ifdef RT_SERIAL_USING_DMA
  266. else if (RT_DEVICE_FLAG_DMA_RX == ctrl_arg)
  267. {
  268. NVIC_DisableIRQ(uart->config->dma_rx->irq_config.irq_num);
  269. }
  270. else if (RT_DEVICE_FLAG_DMA_TX == ctrl_arg)
  271. {
  272. NVIC_DisableIRQ(uart->config->dma_tx->irq_config.irq_num);
  273. }
  274. #endif
  275. break;
  276. /* Enable interrupt */
  277. case RT_DEVICE_CTRL_SET_INT:
  278. if (RT_DEVICE_FLAG_INT_RX == ctrl_arg)
  279. {
  280. hc32_install_irq_handler(&uart->config->rx_irq.irq_config, uart->config->rx_irq.irq_callback, RT_TRUE);
  281. USART_FuncCmd(uart->config->Instance, USART_INT_RX, ENABLE);
  282. }
  283. else
  284. {
  285. hc32_install_irq_handler(&uart->config->tx_irq.irq_config, uart->config->tx_irq.irq_callback, RT_TRUE);
  286. USART_FuncCmd(uart->config->Instance, USART_TX, DISABLE);
  287. USART_FuncCmd(uart->config->Instance, USART_TX | USART_INT_TX_EMPTY, ENABLE);
  288. }
  289. break;
  290. #ifdef RT_SERIAL_USING_DMA
  291. case RT_DEVICE_CTRL_CONFIG:
  292. hc32_dma_config(serial, ctrl_arg);
  293. break;
  294. #endif
  295. case RT_DEVICE_CTRL_CLOSE:
  296. USART_DeInit(uart->config->Instance);
  297. break;
  298. }
  299. return RT_EOK;
  300. }
  301. static int hc32_putc(struct rt_serial_device *serial, char c)
  302. {
  303. struct hc32_uart *uart;
  304. RT_ASSERT(RT_NULL != serial);
  305. uart = rt_container_of(serial, struct hc32_uart, serial);
  306. RT_ASSERT(RT_NULL != uart->config->Instance);
  307. if (serial->parent.open_flag & RT_DEVICE_FLAG_INT_TX)
  308. {
  309. if (USART_GetStatus(uart->config->Instance, USART_FLAG_TX_EMPTY) != SET)
  310. {
  311. return -1;
  312. }
  313. }
  314. else
  315. {
  316. /* Polling mode. */
  317. while (USART_GetStatus(uart->config->Instance, USART_FLAG_TX_EMPTY) != SET);
  318. }
  319. USART_WriteData(uart->config->Instance, c);
  320. return 1;
  321. }
  322. static int hc32_getc(struct rt_serial_device *serial)
  323. {
  324. int ch = -1;
  325. struct hc32_uart *uart;
  326. RT_ASSERT(RT_NULL != serial);
  327. uart = rt_container_of(serial, struct hc32_uart, serial);
  328. RT_ASSERT(RT_NULL != uart->config->Instance);
  329. if (SET == USART_GetStatus(uart->config->Instance, USART_FLAG_RX_FULL))
  330. {
  331. ch = (rt_uint8_t)USART_ReadData(uart->config->Instance);
  332. }
  333. return ch;
  334. }
  335. static rt_size_t hc32_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  336. {
  337. #ifdef RT_SERIAL_USING_DMA
  338. struct hc32_uart *uart;
  339. struct dma_config *uart_dma;
  340. RT_ASSERT(RT_NULL != serial);
  341. RT_ASSERT(RT_NULL != buf);
  342. if (size == 0)
  343. {
  344. return 0;
  345. }
  346. uart = rt_container_of(serial, struct hc32_uart, serial);
  347. if (RT_SERIAL_DMA_TX == direction)
  348. {
  349. uart_dma = uart->config->dma_tx;
  350. if (RESET == USART_GetStatus(uart->config->Instance, USART_FLAG_TX_CPLT))
  351. {
  352. RT_ASSERT(0);
  353. }
  354. DMA_SetSrcAddr(uart_dma->Instance, uart_dma->channel, (uint32_t)buf);
  355. DMA_SetTransCount(uart_dma->Instance, uart_dma->channel, size);
  356. DMA_ChCmd(uart_dma->Instance, uart_dma->channel, ENABLE);
  357. USART_FuncCmd(uart->config->Instance, USART_TX, ENABLE);
  358. USART_FuncCmd(uart->config->Instance, USART_INT_TX_CPLT, ENABLE);
  359. return size;
  360. }
  361. #endif
  362. return 0;
  363. }
  364. static void hc32_uart_rx_irq_handler(struct hc32_uart *uart)
  365. {
  366. RT_ASSERT(RT_NULL != uart);
  367. rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_RX_IND);
  368. }
  369. static void hc32_uart_tx_irq_handler(struct hc32_uart *uart)
  370. {
  371. RT_ASSERT(RT_NULL != uart);
  372. if (uart->serial.parent.open_flag & RT_DEVICE_FLAG_INT_TX)
  373. {
  374. rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_TX_DONE);
  375. }
  376. }
  377. static void hc32_uart_rxerr_irq_handler(struct hc32_uart *uart)
  378. {
  379. RT_ASSERT(RT_NULL != uart);
  380. RT_ASSERT(RT_NULL != uart->config->Instance);
  381. if (SET == USART_GetStatus(uart->config->Instance, (USART_FLAG_OVERRUN | USART_FLAG_PARITY_ERR | USART_FLAG_FRAME_ERR)))
  382. {
  383. USART_ReadData(uart->config->Instance);
  384. }
  385. USART_ClearStatus(uart->config->Instance, (USART_FLAG_PARITY_ERR | USART_FLAG_FRAME_ERR | USART_FLAG_OVERRUN));
  386. }
  387. #ifdef RT_SERIAL_USING_DMA
  388. static void hc32_uart_rx_timeout(struct rt_serial_device *serial)
  389. {
  390. struct hc32_uart *uart;
  391. uint32_t cmp_val;
  392. CM_TMR0_TypeDef *TMR0_Instance;
  393. uint8_t ch;
  394. uint32_t timeout_bits;
  395. stc_tmr0_init_t stcTmr0Init;
  396. RT_ASSERT(RT_NULL != serial);
  397. uart = rt_container_of(serial, struct hc32_uart, serial);
  398. RT_ASSERT(RT_NULL != uart->config->Instance);
  399. TMR0_Instance = uart->config->rx_timeout->TMR0_Instance;
  400. ch = uart->config->rx_timeout->channel;
  401. timeout_bits = uart->config->rx_timeout->timeout_bits;
  402. #if defined (HC32F460)
  403. if ((CM_USART1 == uart->config->Instance) || (CM_USART3 == uart->config->Instance))
  404. {
  405. RT_ASSERT(TMR0_CH_A == ch);
  406. }
  407. else if ((CM_USART2 == uart->config->Instance) || (CM_USART4 == uart->config->Instance))
  408. {
  409. RT_ASSERT(TMR0_CH_B == ch);
  410. }
  411. #elif defined (HC32F4A0)
  412. if ((CM_USART1 == uart->config->Instance) || (CM_USART6 == uart->config->Instance))
  413. {
  414. RT_ASSERT(TMR0_CH_A == ch);
  415. }
  416. else if ((CM_USART2 == uart->config->Instance) || (CM_USART7 == uart->config->Instance))
  417. {
  418. RT_ASSERT(TMR0_CH_B == ch);
  419. }
  420. #endif
  421. FCG_TMR0_CLK(uart->config->rx_timeout->clock, ENABLE);
  422. /* TIMER0 basetimer function initialize */
  423. TMR0_DeInit(TMR0_Instance);
  424. TMR0_SetCountValue(TMR0_Instance, ch, 0U);
  425. TMR0_StructInit(&stcTmr0Init);
  426. stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV1;
  427. stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_XTAL32;
  428. if (TMR0_CLK_DIV1 == stcTmr0Init.u32ClockDiv)
  429. {
  430. cmp_val = (timeout_bits - 4UL);
  431. }
  432. else if (TMR0_CLK_DIV2 == stcTmr0Init.u32ClockDiv)
  433. {
  434. cmp_val = (timeout_bits / 2UL - 2UL);
  435. }
  436. else
  437. {
  438. cmp_val = (timeout_bits / (1UL << (stcTmr0Init.u32ClockDiv >> TMR0_BCONR_CKDIVA_POS)) - 1UL);
  439. }
  440. DDL_ASSERT(cmp_val <= 0xFFFFUL);
  441. stcTmr0Init.u16CompareValue = (uint16_t)(cmp_val);
  442. TMR0_Init(TMR0_Instance, ch, &stcTmr0Init);
  443. TMR0_HWStartCondCmd(TMR0_Instance, ch, ENABLE);
  444. TMR0_HWClearCondCmd(TMR0_Instance, ch, ENABLE);
  445. /* Clear compare flag */
  446. TMR0_ClearStatus(TMR0_Instance, (uint32_t)(0x1UL << ch));
  447. NVIC_EnableIRQ(uart->config->rx_timeout->irq_config.irq_num);
  448. USART_ClearStatus(uart->config->Instance, USART_FLAG_RX_TIMEOUT);
  449. USART_FuncCmd(uart->config->Instance, (USART_RX_TIMEOUT | USART_INT_RX_TIMEOUT), ENABLE);
  450. }
  451. static void hc32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag)
  452. {
  453. struct hc32_uart *uart;
  454. stc_dma_init_t dma_init;
  455. struct dma_config *uart_dma;
  456. RT_ASSERT(RT_NULL != serial);
  457. uart = rt_container_of(serial, struct hc32_uart, serial);
  458. RT_ASSERT(RT_NULL != uart->config->Instance);
  459. if (RT_DEVICE_FLAG_DMA_RX == flag)
  460. {
  461. stc_dma_llp_init_t llp_init;
  462. struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  463. RT_ASSERT(RT_NULL != uart->config->rx_timeout->TMR0_Instance);
  464. RT_ASSERT(RT_NULL != uart->config->dma_rx->Instance);
  465. uart_dma = uart->config->dma_rx;
  466. /* Initialization uart rx timeout for DMA */
  467. hc32_uart_rx_timeout(serial);
  468. /* Enable DMA clock */
  469. FCG_DMA_CLK(uart_dma->clock, ENABLE);
  470. DMA_ChCmd(uart_dma->Instance, uart_dma->channel, DISABLE);
  471. /* Initialize DMA */
  472. DMA_StructInit(&dma_init);
  473. dma_init.u32IntEn = DMA_INT_ENABLE;
  474. dma_init.u32SrcAddr = ((uint32_t)(&uart->config->Instance->DR) + 2UL);
  475. dma_init.u32DestAddr = (uint32_t)rx_fifo->buffer;
  476. dma_init.u32DataWidth = DMA_DATAWIDTH_8BIT;
  477. dma_init.u32BlockSize = 1UL;
  478. dma_init.u32TransCount = serial->config.bufsz;
  479. dma_init.u32SrcAddrInc = DMA_SRC_ADDR_FIX;
  480. dma_init.u32DestAddrInc = DMA_DEST_ADDR_INC;
  481. DMA_Init(uart_dma->Instance, uart_dma->channel, &dma_init);
  482. /* Initialize LLP */
  483. llp_init.u32State = DMA_LLP_ENABLE;
  484. llp_init.u32Mode = DMA_LLP_WAIT;
  485. llp_init.u32Addr = (uint32_t)&uart->config->llp_desc;
  486. DMA_LlpInit(uart_dma->Instance, uart_dma->channel, &llp_init);
  487. /* Configure LLP descriptor */
  488. uart->config->llp_desc.SARx = dma_init.u32SrcAddr;
  489. uart->config->llp_desc.DARx = dma_init.u32DestAddr;
  490. uart->config->llp_desc.DTCTLx = (dma_init.u32TransCount << DMA_DTCTL_CNT_POS) | (dma_init.u32BlockSize << DMA_DTCTL_BLKSIZE_POS);
  491. uart->config->llp_desc.LLPx = (uint32_t)&uart->config->llp_desc;
  492. uart->config->llp_desc.CHCTLx = (dma_init.u32SrcAddrInc | dma_init.u32DestAddrInc | dma_init.u32DataWidth | \
  493. llp_init.u32State | llp_init.u32Mode | dma_init.u32IntEn);
  494. /* Enable DMA interrupt */
  495. NVIC_EnableIRQ(uart->config->dma_rx->irq_config.irq_num);
  496. /* Enable DMA module */
  497. DMA_Cmd(uart_dma->Instance, ENABLE);
  498. AOS_SetTriggerEventSrc(uart_dma->trigger_select, uart_dma->trigger_event);
  499. DMA_ChCmd(uart_dma->Instance, uart_dma->channel, ENABLE);
  500. }
  501. else if (RT_DEVICE_FLAG_DMA_TX == flag)
  502. {
  503. RT_ASSERT(RT_NULL != uart->config->dma_tx->Instance);
  504. uart_dma = uart->config->dma_tx;
  505. /* Enable DMA clock */
  506. FCG_DMA_CLK(uart_dma->clock, ENABLE);
  507. DMA_ChCmd(uart_dma->Instance, uart_dma->channel, DISABLE);
  508. /* Initialize DMA */
  509. DMA_StructInit(&dma_init);
  510. dma_init.u32IntEn = DMA_INT_DISABLE;
  511. dma_init.u32SrcAddr = 0UL;
  512. dma_init.u32DestAddr = (uint32_t)(&uart->config->Instance->DR);
  513. dma_init.u32DataWidth = DMA_DATAWIDTH_8BIT;
  514. dma_init.u32BlockSize = 1UL;
  515. dma_init.u32TransCount = 0UL;
  516. dma_init.u32SrcAddrInc = DMA_SRC_ADDR_INC;
  517. dma_init.u32DestAddrInc = DMA_DEST_ADDR_FIX;
  518. DMA_Init(uart_dma->Instance, uart_dma->channel, &dma_init);
  519. /* Enable DMA module */
  520. DMA_Cmd(uart_dma->Instance, ENABLE);
  521. AOS_SetTriggerEventSrc(uart_dma->trigger_select, uart_dma->trigger_event);
  522. USART_FuncCmd(uart->config->Instance, USART_TX | USART_INT_TX_CPLT, DISABLE);
  523. NVIC_EnableIRQ(uart->config->tc_irq->irq_config.irq_num);
  524. }
  525. }
  526. static void hc32_uart_dma_rx_irq_handler(struct hc32_uart *uart)
  527. {
  528. struct rt_serial_device *serial;
  529. rt_size_t recv_len;
  530. rt_base_t level;
  531. RT_ASSERT(RT_NULL != uart);
  532. RT_ASSERT(RT_NULL != uart->config->Instance);
  533. serial = &uart->serial;
  534. level = rt_hw_interrupt_disable();
  535. recv_len = serial->config.bufsz - uart->dma_rx_last_index;
  536. uart->dma_rx_last_index = 0UL;
  537. rt_hw_interrupt_enable(level);
  538. if (recv_len)
  539. {
  540. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
  541. }
  542. }
  543. static void hc32_uart_rxto_irq_handler(struct hc32_uart *uart)
  544. {
  545. rt_base_t level;
  546. rt_size_t cnt;
  547. rt_size_t recv_len;
  548. rt_size_t recv_total_index;
  549. cnt = DMA_TRANS_CNT(uart->config->dma_rx->Instance, uart->config->dma_rx->channel);
  550. recv_total_index = uart->serial.config.bufsz - cnt;
  551. if (0UL != recv_total_index)
  552. {
  553. level = rt_hw_interrupt_disable();
  554. recv_len = recv_total_index - uart->dma_rx_last_index;
  555. uart->dma_rx_last_index = recv_total_index;
  556. rt_hw_interrupt_enable(level);
  557. if (recv_len)
  558. {
  559. rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
  560. }
  561. }
  562. TMR0_Stop(uart->config->rx_timeout->TMR0_Instance, uart->config->rx_timeout->channel);
  563. USART_ClearStatus(uart->config->Instance, USART_FLAG_RX_TIMEOUT);
  564. }
  565. static void hc32_uart_tc_irq_handler(struct hc32_uart *uart)
  566. {
  567. RT_ASSERT(uart != RT_NULL);
  568. DMA_ClearTransCompleteStatus(uart->config->dma_tx->Instance, (DMA_FLAG_TC_CH0 | DMA_FLAG_BTC_CH0) << uart->config->dma_tx->channel);
  569. USART_FuncCmd(uart->config->Instance, (USART_TX | USART_INT_TX_CPLT), DISABLE);
  570. if (uart->serial.parent.open_flag & RT_DEVICE_FLAG_DMA_TX)
  571. {
  572. rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_TX_DMADONE);
  573. }
  574. }
  575. #endif
  576. #if defined(BSP_USING_UART1)
  577. static void hc32_uart1_rx_irq_handler(void)
  578. {
  579. /* enter interrupt */
  580. rt_interrupt_enter();
  581. hc32_uart_rx_irq_handler(&uart_obj[UART1_INDEX]);
  582. /* leave interrupt */
  583. rt_interrupt_leave();
  584. }
  585. static void hc32_uart1_tx_irq_handler(void)
  586. {
  587. /* enter interrupt */
  588. rt_interrupt_enter();
  589. hc32_uart_tx_irq_handler(&uart_obj[UART1_INDEX]);
  590. /* leave interrupt */
  591. rt_interrupt_leave();
  592. }
  593. static void hc32_uart1_rxerr_irq_handler(void)
  594. {
  595. /* enter interrupt */
  596. rt_interrupt_enter();
  597. hc32_uart_rxerr_irq_handler(&uart_obj[UART1_INDEX]);
  598. /* leave interrupt */
  599. rt_interrupt_leave();
  600. }
  601. #if defined(RT_SERIAL_USING_DMA)
  602. #if defined(BSP_UART1_RX_USING_DMA)
  603. static void hc32_uart1_rxto_irq_handler(void)
  604. {
  605. /* enter interrupt */
  606. rt_interrupt_enter();
  607. hc32_uart_rxto_irq_handler(&uart_obj[UART1_INDEX]);
  608. /* leave interrupt */
  609. rt_interrupt_leave();
  610. }
  611. static void hc32_uart1_dma_rx_irq_handler(void)
  612. {
  613. /* enter interrupt */
  614. rt_interrupt_enter();
  615. hc32_uart_dma_rx_irq_handler(&uart_obj[UART1_INDEX]);
  616. /* leave interrupt */
  617. rt_interrupt_leave();
  618. }
  619. #endif /* BSP_UART1_RX_USING_DMA */
  620. #if defined(BSP_UART1_TX_USING_DMA)
  621. static void hc32_uart1_tc_irq_handler(void)
  622. {
  623. /* enter interrupt */
  624. rt_interrupt_enter();
  625. hc32_uart_tc_irq_handler(&uart_obj[UART1_INDEX]);
  626. /* leave interrupt */
  627. rt_interrupt_leave();
  628. }
  629. #endif /* BSP_UART1_RX_USING_DMA */
  630. #endif /* RT_SERIAL_USING_DMA */
  631. #endif /* BSP_USING_UART1 */
  632. #if defined(BSP_USING_UART2)
  633. static void hc32_uart2_rx_irq_handler(void)
  634. {
  635. /* enter interrupt */
  636. rt_interrupt_enter();
  637. hc32_uart_rx_irq_handler(&uart_obj[UART2_INDEX]);
  638. /* leave interrupt */
  639. rt_interrupt_leave();
  640. }
  641. static void hc32_uart2_tx_irq_handler(void)
  642. {
  643. /* enter interrupt */
  644. rt_interrupt_enter();
  645. hc32_uart_tx_irq_handler(&uart_obj[UART2_INDEX]);
  646. /* leave interrupt */
  647. rt_interrupt_leave();
  648. }
  649. static void hc32_uart2_rxerr_irq_handler(void)
  650. {
  651. /* enter interrupt */
  652. rt_interrupt_enter();
  653. hc32_uart_rxerr_irq_handler(&uart_obj[UART2_INDEX]);
  654. /* leave interrupt */
  655. rt_interrupt_leave();
  656. }
  657. #if defined(RT_SERIAL_USING_DMA)
  658. #if defined(BSP_UART2_TX_USING_DMA)
  659. static void hc32_uart2_tc_irq_handler(void)
  660. {
  661. /* enter interrupt */
  662. rt_interrupt_enter();
  663. hc32_uart_tc_irq_handler(&uart_obj[UART2_INDEX]);
  664. /* leave interrupt */
  665. rt_interrupt_leave();
  666. }
  667. #endif /* BSP_UART2_TX_USING_DMA */
  668. #if defined(BSP_UART2_RX_USING_DMA)
  669. static void hc32_uart2_rxto_irq_handler(void)
  670. {
  671. /* enter interrupt */
  672. rt_interrupt_enter();
  673. hc32_uart_rxto_irq_handler(&uart_obj[UART2_INDEX]);
  674. /* leave interrupt */
  675. rt_interrupt_leave();
  676. }
  677. static void hc32_uart2_dma_rx_irq_handler(void)
  678. {
  679. /* enter interrupt */
  680. rt_interrupt_enter();
  681. hc32_uart_dma_rx_irq_handler(&uart_obj[UART2_INDEX]);
  682. /* leave interrupt */
  683. rt_interrupt_leave();
  684. }
  685. #endif /* BSP_UART2_RX_USING_DMA */
  686. #endif /* RT_SERIAL_USING_DMA */
  687. #endif /* BSP_USING_UART2 */
  688. #if defined(BSP_USING_UART3)
  689. static void hc32_uart3_rx_irq_handler(void)
  690. {
  691. /* enter interrupt */
  692. rt_interrupt_enter();
  693. hc32_uart_rx_irq_handler(&uart_obj[UART3_INDEX]);
  694. /* leave interrupt */
  695. rt_interrupt_leave();
  696. }
  697. static void hc32_uart3_tx_irq_handler(void)
  698. {
  699. /* enter interrupt */
  700. rt_interrupt_enter();
  701. hc32_uart_tx_irq_handler(&uart_obj[UART3_INDEX]);
  702. /* leave interrupt */
  703. rt_interrupt_leave();
  704. }
  705. static void hc32_uart3_rxerr_irq_handler(void)
  706. {
  707. /* enter interrupt */
  708. rt_interrupt_enter();
  709. hc32_uart_rxerr_irq_handler(&uart_obj[UART3_INDEX]);
  710. /* leave interrupt */
  711. rt_interrupt_leave();
  712. }
  713. #if defined(RT_SERIAL_USING_DMA)
  714. #if defined(BSP_UART3_TX_USING_DMA)
  715. static void hc32_uart3_tc_irq_handler(void)
  716. {
  717. /* enter interrupt */
  718. rt_interrupt_enter();
  719. hc32_uart_tc_irq_handler(&uart_obj[UART3_INDEX]);
  720. /* leave interrupt */
  721. rt_interrupt_leave();
  722. }
  723. #endif /* BSP_UART3_TX_USING_DMA */
  724. #if defined(BSP_UART3_RX_USING_DMA)
  725. static void hc32_uart3_rxto_irq_handler(void)
  726. {
  727. /* enter interrupt */
  728. rt_interrupt_enter();
  729. hc32_uart_rxto_irq_handler(&uart_obj[UART3_INDEX]);
  730. /* leave interrupt */
  731. rt_interrupt_leave();
  732. }
  733. static void hc32_uart3_dma_rx_irq_handler(void)
  734. {
  735. /* enter interrupt */
  736. rt_interrupt_enter();
  737. hc32_uart_dma_rx_irq_handler(&uart_obj[UART3_INDEX]);
  738. /* leave interrupt */
  739. rt_interrupt_leave();
  740. }
  741. #endif /* BSP_UART3_RX_USING_DMA */
  742. #endif /* RT_SERIAL_USING_DMA */
  743. #endif /* BSP_USING_UART3 */
  744. #if defined(BSP_USING_UART4)
  745. static void hc32_uart4_rx_irq_handler(void)
  746. {
  747. /* enter interrupt */
  748. rt_interrupt_enter();
  749. hc32_uart_rx_irq_handler(&uart_obj[UART4_INDEX]);
  750. /* leave interrupt */
  751. rt_interrupt_leave();
  752. }
  753. static void hc32_uart4_tx_irq_handler(void)
  754. {
  755. /* enter interrupt */
  756. rt_interrupt_enter();
  757. hc32_uart_tx_irq_handler(&uart_obj[UART4_INDEX]);
  758. /* leave interrupt */
  759. rt_interrupt_leave();
  760. }
  761. static void hc32_uart4_rxerr_irq_handler(void)
  762. {
  763. /* enter interrupt */
  764. rt_interrupt_enter();
  765. hc32_uart_rxerr_irq_handler(&uart_obj[UART4_INDEX]);
  766. /* leave interrupt */
  767. rt_interrupt_leave();
  768. }
  769. #if defined(RT_SERIAL_USING_DMA)
  770. #if defined(BSP_UART4_TX_USING_DMA)
  771. static void hc32_uart4_tc_irq_handler(void)
  772. {
  773. /* enter interrupt */
  774. rt_interrupt_enter();
  775. hc32_uart_tc_irq_handler(&uart_obj[UART4_INDEX]);
  776. /* leave interrupt */
  777. rt_interrupt_leave();
  778. }
  779. #endif /* BSP_UART4_TX_USING_DMA */
  780. #if defined(BSP_UART4_RX_USING_DMA)
  781. static void hc32_uart4_rxto_irq_handler(void)
  782. {
  783. /* enter interrupt */
  784. rt_interrupt_enter();
  785. hc32_uart_rxto_irq_handler(&uart_obj[UART4_INDEX]);
  786. /* leave interrupt */
  787. rt_interrupt_leave();
  788. }
  789. static void hc32_uart4_dma_rx_irq_handler(void)
  790. {
  791. /* enter interrupt */
  792. rt_interrupt_enter();
  793. hc32_uart_dma_rx_irq_handler(&uart_obj[UART4_INDEX]);
  794. /* leave interrupt */
  795. rt_interrupt_leave();
  796. }
  797. #endif /* BSP_UART4_RX_USING_DMA */
  798. #endif /* RT_SERIAL_USING_DMA */
  799. #endif /* BSP_USING_UART4 */
  800. #if defined(BSP_USING_UART5)
  801. static void hc32_uart5_rx_irq_handler(void)
  802. {
  803. /* enter interrupt */
  804. rt_interrupt_enter();
  805. hc32_uart_rx_irq_handler(&uart_obj[UART5_INDEX]);
  806. /* leave interrupt */
  807. rt_interrupt_leave();
  808. }
  809. static void hc32_uart5_tx_irq_handler(void)
  810. {
  811. /* enter interrupt */
  812. rt_interrupt_enter();
  813. hc32_uart_tx_irq_handler(&uart_obj[UART5_INDEX]);
  814. /* leave interrupt */
  815. rt_interrupt_leave();
  816. }
  817. static void hc32_uart5_rxerr_irq_handler(void)
  818. {
  819. /* enter interrupt */
  820. rt_interrupt_enter();
  821. hc32_uart_rxerr_irq_handler(&uart_obj[UART5_INDEX]);
  822. /* leave interrupt */
  823. rt_interrupt_leave();
  824. }
  825. #endif /* BSP_USING_UART5 */
  826. #if defined(BSP_USING_UART6)
  827. static void hc32_uart6_rx_irq_handler(void)
  828. {
  829. /* enter interrupt */
  830. rt_interrupt_enter();
  831. hc32_uart_rx_irq_handler(&uart_obj[UART6_INDEX]);
  832. /* leave interrupt */
  833. rt_interrupt_leave();
  834. }
  835. static void hc32_uart6_tx_irq_handler(void)
  836. {
  837. /* enter interrupt */
  838. rt_interrupt_enter();
  839. hc32_uart_tx_irq_handler(&uart_obj[UART6_INDEX]);
  840. /* leave interrupt */
  841. rt_interrupt_leave();
  842. }
  843. static void hc32_uart6_rxerr_irq_handler(void)
  844. {
  845. /* enter interrupt */
  846. rt_interrupt_enter();
  847. hc32_uart_rxerr_irq_handler(&uart_obj[UART6_INDEX]);
  848. /* leave interrupt */
  849. rt_interrupt_leave();
  850. }
  851. #if defined(RT_SERIAL_USING_DMA)
  852. #if defined(BSP_UART6_TX_USING_DMA)
  853. static void hc32_uart6_tc_irq_handler(void)
  854. {
  855. /* enter interrupt */
  856. rt_interrupt_enter();
  857. hc32_uart_tc_irq_handler(&uart_obj[UART6_INDEX]);
  858. /* leave interrupt */
  859. rt_interrupt_leave();
  860. }
  861. #endif /* BSP_UART6_TX_USING_DMA */
  862. #if defined(BSP_UART6_RX_USING_DMA)
  863. static void hc32_uart6_rxto_irq_handler(void)
  864. {
  865. /* enter interrupt */
  866. rt_interrupt_enter();
  867. hc32_uart_rxto_irq_handler(&uart_obj[UART6_INDEX]);
  868. /* leave interrupt */
  869. rt_interrupt_leave();
  870. }
  871. static void hc32_uart6_dma_rx_irq_handler(void)
  872. {
  873. /* enter interrupt */
  874. rt_interrupt_enter();
  875. hc32_uart_dma_rx_irq_handler(&uart_obj[UART6_INDEX]);
  876. /* leave interrupt */
  877. rt_interrupt_leave();
  878. }
  879. #endif /* BSP_UART6_RX_USING_DMA */
  880. #endif /* RT_SERIAL_USING_DMA */
  881. #endif /* BSP_USING_UART6 */
  882. #if defined(BSP_USING_UART7)
  883. static void hc32_uart7_rx_irq_handler(void)
  884. {
  885. /* enter interrupt */
  886. rt_interrupt_enter();
  887. hc32_uart_rx_irq_handler(&uart_obj[UART7_INDEX]);
  888. /* leave interrupt */
  889. rt_interrupt_leave();
  890. }
  891. static void hc32_uart7_tx_irq_handler(void)
  892. {
  893. /* enter interrupt */
  894. rt_interrupt_enter();
  895. hc32_uart_tx_irq_handler(&uart_obj[UART7_INDEX]);
  896. /* leave interrupt */
  897. rt_interrupt_leave();
  898. }
  899. static void hc32_uart7_rxerr_irq_handler(void)
  900. {
  901. /* enter interrupt */
  902. rt_interrupt_enter();
  903. hc32_uart_rxerr_irq_handler(&uart_obj[UART7_INDEX]);
  904. /* leave interrupt */
  905. rt_interrupt_leave();
  906. }
  907. #if defined(RT_SERIAL_USING_DMA)
  908. #if defined(BSP_UART7_TX_USING_DMA)
  909. static void hc32_uart7_tc_irq_handler(void)
  910. {
  911. /* enter interrupt */
  912. rt_interrupt_enter();
  913. hc32_uart_tc_irq_handler(&uart_obj[UART7_INDEX]);
  914. /* leave interrupt */
  915. rt_interrupt_leave();
  916. }
  917. #endif /* BSP_UART7_TX_USING_DMA */
  918. #if defined(BSP_UART7_RX_USING_DMA)
  919. static void hc32_uart7_rxto_irq_handler(void)
  920. {
  921. /* enter interrupt */
  922. rt_interrupt_enter();
  923. hc32_uart_rxto_irq_handler(&uart_obj[UART7_INDEX]);
  924. /* leave interrupt */
  925. rt_interrupt_leave();
  926. }
  927. static void hc32_uart7_dma_rx_irq_handler(void)
  928. {
  929. /* enter interrupt */
  930. rt_interrupt_enter();
  931. hc32_uart_dma_rx_irq_handler(&uart_obj[UART7_INDEX]);
  932. /* leave interrupt */
  933. rt_interrupt_leave();
  934. }
  935. #endif /* BSP_UART7_RX_USING_DMA */
  936. #endif /* RT_SERIAL_USING_DMA */
  937. #endif /* BSP_USING_UART7 */
  938. #if defined(BSP_USING_UART8)
  939. static void hc32_uart8_rx_irq_handler(void)
  940. {
  941. /* enter interrupt */
  942. rt_interrupt_enter();
  943. hc32_uart_rx_irq_handler(&uart_obj[UART8_INDEX]);
  944. /* leave interrupt */
  945. rt_interrupt_leave();
  946. }
  947. static void hc32_uart8_tx_irq_handler(void)
  948. {
  949. /* enter interrupt */
  950. rt_interrupt_enter();
  951. hc32_uart_tx_irq_handler(&uart_obj[UART8_INDEX]);
  952. /* leave interrupt */
  953. rt_interrupt_leave();
  954. }
  955. static void hc32_uart8_rxerr_irq_handler(void)
  956. {
  957. /* enter interrupt */
  958. rt_interrupt_enter();
  959. hc32_uart_rxerr_irq_handler(&uart_obj[UART8_INDEX]);
  960. /* leave interrupt */
  961. rt_interrupt_leave();
  962. }
  963. #endif /* BSP_USING_UART8 */
  964. #if defined(BSP_USING_UART9)
  965. static void hc32_uart9_rx_irq_handler(void)
  966. {
  967. /* enter interrupt */
  968. rt_interrupt_enter();
  969. hc32_uart_rx_irq_handler(&uart_obj[UART9_INDEX]);
  970. /* leave interrupt */
  971. rt_interrupt_leave();
  972. }
  973. static void hc32_uart9_tx_irq_handler(void)
  974. {
  975. /* enter interrupt */
  976. rt_interrupt_enter();
  977. hc32_uart_tx_irq_handler(&uart_obj[UART9_INDEX]);
  978. /* leave interrupt */
  979. rt_interrupt_leave();
  980. }
  981. static void hc32_uart9_rxerr_irq_handler(void)
  982. {
  983. /* enter interrupt */
  984. rt_interrupt_enter();
  985. hc32_uart_rxerr_irq_handler(&uart_obj[UART9_INDEX]);
  986. /* leave interrupt */
  987. rt_interrupt_leave();
  988. }
  989. #endif /* BSP_USING_UART9 */
  990. #if defined(BSP_USING_UART10)
  991. static void hc32_uart10_rx_irq_handler(void)
  992. {
  993. /* enter interrupt */
  994. rt_interrupt_enter();
  995. hc32_uart_rx_irq_handler(&uart_obj[UART10_INDEX]);
  996. /* leave interrupt */
  997. rt_interrupt_leave();
  998. }
  999. static void hc32_uart10_tx_irq_handler(void)
  1000. {
  1001. /* enter interrupt */
  1002. rt_interrupt_enter();
  1003. hc32_uart_tx_irq_handler(&uart_obj[UART10_INDEX]);
  1004. /* leave interrupt */
  1005. rt_interrupt_leave();
  1006. }
  1007. static void hc32_uart10_rxerr_irq_handler(void)
  1008. {
  1009. /* enter interrupt */
  1010. rt_interrupt_enter();
  1011. hc32_uart_rxerr_irq_handler(&uart_obj[UART10_INDEX]);
  1012. /* leave interrupt */
  1013. rt_interrupt_leave();
  1014. }
  1015. #endif /* BSP_USING_UART10 */
  1016. /**
  1017. * @brief This function gets dma witch uart used infomation include unit,
  1018. * channel, interrupt etc.
  1019. * @param None
  1020. * @retval None
  1021. */
  1022. static void hc32_uart_get_dma_info(void)
  1023. {
  1024. #ifdef BSP_USING_UART1
  1025. uart_obj[UART1_INDEX].uart_dma_flag = 0;
  1026. #ifdef BSP_UART1_RX_USING_DMA
  1027. uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1028. static struct dma_config uart1_dma_rx = UART1_DMA_RX_CONFIG;
  1029. static struct hc32_uart_rxto uart1_rx_timeout = UART1_RXTO_CONFIG;
  1030. uart1_dma_rx.irq_callback = hc32_uart1_dma_rx_irq_handler;
  1031. uart1_rx_timeout.irq_callback = hc32_uart1_rxto_irq_handler;
  1032. uart_config[UART1_INDEX].rx_timeout = &uart1_rx_timeout;
  1033. uart_config[UART1_INDEX].dma_rx = &uart1_dma_rx;
  1034. #endif
  1035. #ifdef BSP_UART1_TX_USING_DMA
  1036. uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1037. static struct dma_config uart1_dma_tx = UART1_DMA_TX_CONFIG;
  1038. uart_config[UART1_INDEX].dma_tx = &uart1_dma_tx;
  1039. static struct hc32_uart_irq_config uart1_tc_irq = UART1_TX_CPLT_CONFIG;
  1040. uart1_tc_irq.irq_callback = hc32_uart1_tc_irq_handler;
  1041. uart_config[UART1_INDEX].tc_irq = &uart1_tc_irq;
  1042. #endif
  1043. #endif
  1044. #ifdef BSP_USING_UART2
  1045. uart_obj[UART2_INDEX].uart_dma_flag = 0;
  1046. #ifdef BSP_UART2_RX_USING_DMA
  1047. uart_obj[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1048. static struct dma_config uart2_dma_rx = UART2_DMA_RX_CONFIG;
  1049. static struct hc32_uart_rxto uart2_rx_timeout = UART2_RXTO_CONFIG;
  1050. uart2_dma_rx.irq_callback = hc32_uart2_dma_rx_irq_handler;
  1051. uart2_rx_timeout.irq_callback = hc32_uart2_rxto_irq_handler;
  1052. uart_config[UART2_INDEX].rx_timeout = &uart2_rx_timeout;
  1053. uart_config[UART2_INDEX].dma_rx = &uart2_dma_rx;
  1054. #endif
  1055. #ifdef BSP_UART2_TX_USING_DMA
  1056. uart_obj[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1057. static struct dma_config uart2_dma_tx = UART2_DMA_TX_CONFIG;
  1058. uart_config[UART2_INDEX].dma_tx = &uart2_dma_tx;
  1059. static struct hc32_uart_irq_config uart2_tc_irq = UART2_TX_CPLT_CONFIG;
  1060. uart2_tc_irq.irq_callback = hc32_uart2_tc_irq_handler;
  1061. uart_config[UART2_INDEX].tc_irq = &uart2_tc_irq;
  1062. #endif
  1063. #endif
  1064. #ifdef BSP_USING_UART3
  1065. uart_obj[UART3_INDEX].uart_dma_flag = 0;
  1066. #ifdef BSP_UART3_RX_USING_DMA
  1067. uart_obj[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1068. static struct dma_config uart3_dma_rx = UART3_DMA_RX_CONFIG;
  1069. static struct hc32_uart_rxto uart3_rx_timeout = UART3_RXTO_CONFIG;
  1070. uart3_dma_rx.irq_callback = hc32_uart3_dma_rx_irq_handler;
  1071. uart3_rx_timeout.irq_callback = hc32_uart3_rxto_irq_handler;
  1072. uart_config[UART3_INDEX].rx_timeout = &uart3_rx_timeout;
  1073. uart_config[UART3_INDEX].dma_rx = &uart3_dma_rx;
  1074. #endif
  1075. #ifdef BSP_UART3_TX_USING_DMA
  1076. uart_obj[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1077. static struct dma_config uart3_dma_tx = UART3_DMA_TX_CONFIG;
  1078. uart_config[UART3_INDEX].dma_tx = &uart3_dma_tx;
  1079. static struct hc32_uart_irq_config uart3_tc_irq = UART3_TX_CPLT_CONFIG;
  1080. uart3_tc_irq.irq_callback = hc32_uart3_tc_irq_handler;
  1081. uart_config[UART3_INDEX].tc_irq = &uart3_tc_irq;
  1082. #endif
  1083. #endif
  1084. #ifdef BSP_USING_UART4
  1085. uart_obj[UART4_INDEX].uart_dma_flag = 0;
  1086. #ifdef BSP_UART4_RX_USING_DMA
  1087. uart_obj[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1088. static struct dma_config uart4_dma_rx = UART4_DMA_RX_CONFIG;
  1089. static struct hc32_uart_rxto uart4_rx_timeout = UART4_RXTO_CONFIG;
  1090. uart4_dma_rx.irq_callback = hc32_uart4_dma_rx_irq_handler;
  1091. uart4_rx_timeout.irq_callback = hc32_uart4_rxto_irq_handler;
  1092. uart_config[UART4_INDEX].rx_timeout = &uart4_rx_timeout;
  1093. uart_config[UART4_INDEX].dma_rx = &uart4_dma_rx;
  1094. #endif
  1095. #ifdef BSP_UART4_TX_USING_DMA
  1096. uart_obj[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1097. static struct dma_config uart4_dma_tx = UART4_DMA_TX_CONFIG;
  1098. uart_config[UART4_INDEX].dma_tx = &uart4_dma_tx;
  1099. static struct hc32_uart_irq_config uart4_tc_irq = UART4_TX_CPLT_CONFIG;
  1100. uart4_tc_irq.irq_callback = hc32_uart4_tc_irq_handler;
  1101. uart_config[UART4_INDEX].tc_irq = &uart4_tc_irq;
  1102. #endif
  1103. #endif
  1104. #ifdef BSP_USING_UART6
  1105. uart_obj[UART6_INDEX].uart_dma_flag = 0;
  1106. #ifdef BSP_UART6_RX_USING_DMA
  1107. uart_obj[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1108. static struct dma_config uart6_dma_rx = UART6_DMA_RX_CONFIG;
  1109. static struct hc32_uart_rxto uart6_rx_timeout = UART6_RXTO_CONFIG;
  1110. uart6_dma_rx.irq_callback = hc32_uart6_dma_rx_irq_handler;
  1111. uart6_rx_timeout.irq_callback = hc32_uart6_rxto_irq_handler;
  1112. uart_config[UART6_INDEX].rx_timeout = &uart6_rx_timeout;
  1113. uart_config[UART6_INDEX].dma_rx = &uart6_dma_rx;
  1114. #endif
  1115. #ifdef BSP_UART6_TX_USING_DMA
  1116. uart_obj[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1117. static struct dma_config uart6_dma_tx = UART6_DMA_TX_CONFIG;
  1118. uart_config[UART6_INDEX].dma_tx = &uart6_dma_tx;
  1119. static struct hc32_uart_irq_config uart6_tc_irq = UART6_TX_CPLT_CONFIG;
  1120. uart6_tc_irq.irq_callback = hc32_uart6_tc_irq_handler;
  1121. uart_config[UART6_INDEX].tc_irq = &uart6_tc_irq;
  1122. #endif
  1123. #endif
  1124. #ifdef BSP_USING_UART7
  1125. uart_obj[UART7_INDEX].uart_dma_flag = 0;
  1126. #ifdef BSP_UART7_RX_USING_DMA
  1127. uart_obj[UART7_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1128. static struct dma_config uart7_dma_rx = UART7_DMA_RX_CONFIG;
  1129. static struct hc32_uart_rxto uart7_rx_timeout = UART7_RXTO_CONFIG;
  1130. uart7_dma_rx.irq_callback = hc32_uart7_dma_rx_irq_handler;
  1131. uart7_rx_timeout.irq_callback = hc32_uart7_rxto_irq_handler;
  1132. uart_config[UART7_INDEX].rx_timeout = &uart7_rx_timeout;
  1133. uart_config[UART7_INDEX].dma_rx = &uart7_dma_rx;
  1134. #endif
  1135. #ifdef BSP_UART7_TX_USING_DMA
  1136. uart_obj[UART7_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1137. static struct dma_config uart7_dma_tx = UART7_DMA_TX_CONFIG;
  1138. uart_config[UART7_INDEX].dma_tx = &uart7_dma_tx;
  1139. static struct hc32_uart_irq_config uart7_tc_irq = UART7_TX_CPLT_CONFIG;
  1140. uart7_tc_irq.irq_callback = hc32_uart7_tc_irq_handler;
  1141. uart_config[UART7_INDEX].tc_irq = &uart7_tc_irq;
  1142. #endif
  1143. #endif
  1144. }
  1145. /**
  1146. * @brief This function gets uart irq handle.
  1147. * @param None
  1148. * @retval None
  1149. */
  1150. static void hc32_get_uart_callback(void)
  1151. {
  1152. #ifdef BSP_USING_UART1
  1153. uart_config[UART1_INDEX].rxerr_irq.irq_callback = hc32_uart1_rxerr_irq_handler;
  1154. uart_config[UART1_INDEX].rx_irq.irq_callback = hc32_uart1_rx_irq_handler;
  1155. uart_config[UART1_INDEX].tx_irq.irq_callback = hc32_uart1_tx_irq_handler;
  1156. #endif
  1157. #ifdef BSP_USING_UART2
  1158. uart_config[UART2_INDEX].rxerr_irq.irq_callback = hc32_uart2_rxerr_irq_handler;
  1159. uart_config[UART2_INDEX].rx_irq.irq_callback = hc32_uart2_rx_irq_handler;
  1160. uart_config[UART2_INDEX].tx_irq.irq_callback = hc32_uart2_tx_irq_handler;
  1161. #endif
  1162. #ifdef BSP_USING_UART3
  1163. uart_config[UART3_INDEX].rxerr_irq.irq_callback = hc32_uart3_rxerr_irq_handler;
  1164. uart_config[UART3_INDEX].rx_irq.irq_callback = hc32_uart3_rx_irq_handler;
  1165. uart_config[UART3_INDEX].tx_irq.irq_callback = hc32_uart3_tx_irq_handler;
  1166. #endif
  1167. #ifdef BSP_USING_UART4
  1168. uart_config[UART4_INDEX].rxerr_irq.irq_callback = hc32_uart4_rxerr_irq_handler;
  1169. uart_config[UART4_INDEX].rx_irq.irq_callback = hc32_uart4_rx_irq_handler;
  1170. uart_config[UART4_INDEX].tx_irq.irq_callback = hc32_uart4_tx_irq_handler;
  1171. #endif
  1172. #ifdef BSP_USING_UART5
  1173. uart_config[UART5_INDEX].rxerr_irq.irq_callback = hc32_uart5_rxerr_irq_handler;
  1174. uart_config[UART5_INDEX].rx_irq.irq_callback = hc32_uart5_rx_irq_handler;
  1175. uart_config[UART5_INDEX].tx_irq.irq_callback = hc32_uart5_tx_irq_handler;
  1176. #endif
  1177. #ifdef BSP_USING_UART6
  1178. uart_config[UART6_INDEX].rxerr_irq.irq_callback = hc32_uart6_rxerr_irq_handler;
  1179. uart_config[UART6_INDEX].rx_irq.irq_callback = hc32_uart6_rx_irq_handler;
  1180. uart_config[UART6_INDEX].tx_irq.irq_callback = hc32_uart6_tx_irq_handler;
  1181. #endif
  1182. #ifdef BSP_USING_UART7
  1183. uart_config[UART7_INDEX].rxerr_irq.irq_callback = hc32_uart7_rxerr_irq_handler;
  1184. uart_config[UART7_INDEX].rx_irq.irq_callback = hc32_uart7_rx_irq_handler;
  1185. uart_config[UART7_INDEX].tx_irq.irq_callback = hc32_uart7_tx_irq_handler;
  1186. #endif
  1187. #ifdef BSP_USING_UART8
  1188. uart_config[UART8_INDEX].rxerr_irq.irq_callback = hc32_uart8_rxerr_irq_handler;
  1189. uart_config[UART8_INDEX].rx_irq.irq_callback = hc32_uart8_rx_irq_handler;
  1190. uart_config[UART8_INDEX].tx_irq.irq_callback = hc32_uart8_tx_irq_handler;
  1191. #endif
  1192. #ifdef BSP_USING_UART9
  1193. uart_config[UART9_INDEX].rxerr_irq.irq_callback = hc32_uart9_rxerr_irq_handler;
  1194. uart_config[UART9_INDEX].rx_irq.irq_callback = hc32_uart9_rx_irq_handler;
  1195. uart_config[UART9_INDEX].tx_irq.irq_callback = hc32_uart9_tx_irq_handler;
  1196. #endif
  1197. #ifdef BSP_USING_UART10
  1198. uart_config[UART10_INDEX].rxerr_irq.irq_callback = hc32_uart10_rxerr_irq_handler;
  1199. uart_config[UART10_INDEX].rx_irq.irq_callback = hc32_uart10_rx_irq_handler;
  1200. uart_config[UART10_INDEX].tx_irq.irq_callback = hc32_uart10_tx_irq_handler;
  1201. #endif
  1202. }
  1203. static const struct rt_uart_ops hc32_uart_ops =
  1204. {
  1205. .configure = hc32_configure,
  1206. .control = hc32_control,
  1207. .putc = hc32_putc,
  1208. .getc = hc32_getc,
  1209. .dma_transmit = hc32_dma_transmit
  1210. };
  1211. int hc32_hw_uart_init(void)
  1212. {
  1213. rt_err_t result = RT_EOK;
  1214. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct hc32_uart);
  1215. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  1216. hc32_uart_get_dma_info();
  1217. hc32_get_uart_callback();
  1218. for (int i = 0; i < obj_num; i++)
  1219. {
  1220. /* init UART object */
  1221. uart_obj[i].serial.ops = &hc32_uart_ops;
  1222. uart_obj[i].serial.config = config;
  1223. uart_obj[i].config = &uart_config[i];
  1224. /* register the handle */
  1225. hc32_install_irq_handler(&uart_config[i].rxerr_irq.irq_config, uart_config[i].rxerr_irq.irq_callback, RT_FALSE);
  1226. #ifdef RT_SERIAL_USING_DMA
  1227. if (uart_obj[i].uart_dma_flag & RT_DEVICE_FLAG_DMA_RX)
  1228. {
  1229. hc32_install_irq_handler(&uart_config[i].dma_rx->irq_config, uart_config[i].dma_rx->irq_callback, RT_FALSE);
  1230. hc32_install_irq_handler(&uart_config[i].rx_timeout->irq_config, uart_config[i].rx_timeout->irq_callback, RT_FALSE);
  1231. }
  1232. if (uart_obj[i].uart_dma_flag & RT_DEVICE_FLAG_DMA_TX)
  1233. {
  1234. hc32_install_irq_handler(&uart_config[i].tc_irq->irq_config, uart_config[i].tc_irq->irq_callback, RT_FALSE);
  1235. }
  1236. #endif
  1237. /* register UART device */
  1238. result = rt_hw_serial_register(&uart_obj[i].serial,
  1239. uart_obj[i].config->name,
  1240. (RT_DEVICE_FLAG_RDWR |
  1241. RT_DEVICE_FLAG_INT_RX |
  1242. RT_DEVICE_FLAG_INT_TX |
  1243. uart_obj[i].uart_dma_flag),
  1244. &uart_obj[i]);
  1245. RT_ASSERT(result == RT_EOK);
  1246. }
  1247. return result;
  1248. }
  1249. INIT_BOARD_EXPORT(hc32_hw_uart_init);
  1250. #endif
  1251. #endif /* RT_USING_SERIAL */
  1252. /*******************************************************************************
  1253. * EOF (not truncated)
  1254. ******************************************************************************/