drv_usart.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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_ssize_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. #if defined(BSP_UART1_RX_USING_DMA) || defined(BSP_UART2_RX_USING_DMA) || defined(BSP_UART3_RX_USING_DMA) || \
  527. defined(BSP_UART4_RX_USING_DMA) || defined(BSP_UART6_RX_USING_DMA) || defined(BSP_UART7_RX_USING_DMA)
  528. static void hc32_uart_dma_rx_irq_handler(struct hc32_uart *uart)
  529. {
  530. struct rt_serial_device *serial;
  531. rt_size_t recv_len;
  532. rt_base_t level;
  533. RT_ASSERT(RT_NULL != uart);
  534. RT_ASSERT(RT_NULL != uart->config->Instance);
  535. serial = &uart->serial;
  536. level = rt_hw_interrupt_disable();
  537. recv_len = serial->config.bufsz - uart->dma_rx_last_index;
  538. uart->dma_rx_last_index = 0UL;
  539. rt_hw_interrupt_enable(level);
  540. if (recv_len)
  541. {
  542. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
  543. }
  544. }
  545. static void hc32_uart_rxto_irq_handler(struct hc32_uart *uart)
  546. {
  547. rt_base_t level;
  548. rt_size_t cnt;
  549. rt_size_t recv_len;
  550. rt_size_t recv_total_index;
  551. cnt = DMA_TRANS_CNT(uart->config->dma_rx->Instance, uart->config->dma_rx->channel);
  552. recv_total_index = uart->serial.config.bufsz - cnt;
  553. if (0UL != recv_total_index)
  554. {
  555. level = rt_hw_interrupt_disable();
  556. recv_len = recv_total_index - uart->dma_rx_last_index;
  557. uart->dma_rx_last_index = recv_total_index;
  558. rt_hw_interrupt_enable(level);
  559. if (recv_len)
  560. {
  561. rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
  562. }
  563. }
  564. TMR0_Stop(uart->config->rx_timeout->TMR0_Instance, uart->config->rx_timeout->channel);
  565. USART_ClearStatus(uart->config->Instance, USART_FLAG_RX_TIMEOUT);
  566. }
  567. #endif
  568. #if defined(BSP_UART1_TX_USING_DMA) || defined(BSP_UART2_TX_USING_DMA) || defined(BSP_UART3_TX_USING_DMA) || \
  569. defined(BSP_UART4_TX_USING_DMA) || defined(BSP_UART6_TX_USING_DMA) || defined(BSP_UART7_TX_USING_DMA)
  570. static void hc32_uart_tc_irq_handler(struct hc32_uart *uart)
  571. {
  572. RT_ASSERT(uart != RT_NULL);
  573. DMA_ClearTransCompleteStatus(uart->config->dma_tx->Instance, (DMA_FLAG_TC_CH0 | DMA_FLAG_BTC_CH0) << uart->config->dma_tx->channel);
  574. USART_FuncCmd(uart->config->Instance, (USART_TX | USART_INT_TX_CPLT), DISABLE);
  575. if (uart->serial.parent.open_flag & RT_DEVICE_FLAG_DMA_TX)
  576. {
  577. rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_TX_DMADONE);
  578. }
  579. }
  580. #endif
  581. #endif
  582. #if defined(BSP_USING_UART1)
  583. static void hc32_uart1_rx_irq_handler(void)
  584. {
  585. /* enter interrupt */
  586. rt_interrupt_enter();
  587. hc32_uart_rx_irq_handler(&uart_obj[UART1_INDEX]);
  588. /* leave interrupt */
  589. rt_interrupt_leave();
  590. }
  591. static void hc32_uart1_tx_irq_handler(void)
  592. {
  593. /* enter interrupt */
  594. rt_interrupt_enter();
  595. hc32_uart_tx_irq_handler(&uart_obj[UART1_INDEX]);
  596. /* leave interrupt */
  597. rt_interrupt_leave();
  598. }
  599. static void hc32_uart1_rxerr_irq_handler(void)
  600. {
  601. /* enter interrupt */
  602. rt_interrupt_enter();
  603. hc32_uart_rxerr_irq_handler(&uart_obj[UART1_INDEX]);
  604. /* leave interrupt */
  605. rt_interrupt_leave();
  606. }
  607. #if defined(RT_SERIAL_USING_DMA)
  608. #if defined(BSP_UART1_RX_USING_DMA)
  609. static void hc32_uart1_rxto_irq_handler(void)
  610. {
  611. /* enter interrupt */
  612. rt_interrupt_enter();
  613. hc32_uart_rxto_irq_handler(&uart_obj[UART1_INDEX]);
  614. /* leave interrupt */
  615. rt_interrupt_leave();
  616. }
  617. static void hc32_uart1_dma_rx_irq_handler(void)
  618. {
  619. /* enter interrupt */
  620. rt_interrupt_enter();
  621. hc32_uart_dma_rx_irq_handler(&uart_obj[UART1_INDEX]);
  622. /* leave interrupt */
  623. rt_interrupt_leave();
  624. }
  625. #endif /* BSP_UART1_RX_USING_DMA */
  626. #if defined(BSP_UART1_TX_USING_DMA)
  627. static void hc32_uart1_tc_irq_handler(void)
  628. {
  629. /* enter interrupt */
  630. rt_interrupt_enter();
  631. hc32_uart_tc_irq_handler(&uart_obj[UART1_INDEX]);
  632. /* leave interrupt */
  633. rt_interrupt_leave();
  634. }
  635. #endif /* BSP_UART1_RX_USING_DMA */
  636. #endif /* RT_SERIAL_USING_DMA */
  637. #endif /* BSP_USING_UART1 */
  638. #if defined(BSP_USING_UART2)
  639. static void hc32_uart2_rx_irq_handler(void)
  640. {
  641. /* enter interrupt */
  642. rt_interrupt_enter();
  643. hc32_uart_rx_irq_handler(&uart_obj[UART2_INDEX]);
  644. /* leave interrupt */
  645. rt_interrupt_leave();
  646. }
  647. static void hc32_uart2_tx_irq_handler(void)
  648. {
  649. /* enter interrupt */
  650. rt_interrupt_enter();
  651. hc32_uart_tx_irq_handler(&uart_obj[UART2_INDEX]);
  652. /* leave interrupt */
  653. rt_interrupt_leave();
  654. }
  655. static void hc32_uart2_rxerr_irq_handler(void)
  656. {
  657. /* enter interrupt */
  658. rt_interrupt_enter();
  659. hc32_uart_rxerr_irq_handler(&uart_obj[UART2_INDEX]);
  660. /* leave interrupt */
  661. rt_interrupt_leave();
  662. }
  663. #if defined(RT_SERIAL_USING_DMA)
  664. #if defined(BSP_UART2_TX_USING_DMA)
  665. static void hc32_uart2_tc_irq_handler(void)
  666. {
  667. /* enter interrupt */
  668. rt_interrupt_enter();
  669. hc32_uart_tc_irq_handler(&uart_obj[UART2_INDEX]);
  670. /* leave interrupt */
  671. rt_interrupt_leave();
  672. }
  673. #endif /* BSP_UART2_TX_USING_DMA */
  674. #if defined(BSP_UART2_RX_USING_DMA)
  675. static void hc32_uart2_rxto_irq_handler(void)
  676. {
  677. /* enter interrupt */
  678. rt_interrupt_enter();
  679. hc32_uart_rxto_irq_handler(&uart_obj[UART2_INDEX]);
  680. /* leave interrupt */
  681. rt_interrupt_leave();
  682. }
  683. static void hc32_uart2_dma_rx_irq_handler(void)
  684. {
  685. /* enter interrupt */
  686. rt_interrupt_enter();
  687. hc32_uart_dma_rx_irq_handler(&uart_obj[UART2_INDEX]);
  688. /* leave interrupt */
  689. rt_interrupt_leave();
  690. }
  691. #endif /* BSP_UART2_RX_USING_DMA */
  692. #endif /* RT_SERIAL_USING_DMA */
  693. #endif /* BSP_USING_UART2 */
  694. #if defined(BSP_USING_UART3)
  695. static void hc32_uart3_rx_irq_handler(void)
  696. {
  697. /* enter interrupt */
  698. rt_interrupt_enter();
  699. hc32_uart_rx_irq_handler(&uart_obj[UART3_INDEX]);
  700. /* leave interrupt */
  701. rt_interrupt_leave();
  702. }
  703. static void hc32_uart3_tx_irq_handler(void)
  704. {
  705. /* enter interrupt */
  706. rt_interrupt_enter();
  707. hc32_uart_tx_irq_handler(&uart_obj[UART3_INDEX]);
  708. /* leave interrupt */
  709. rt_interrupt_leave();
  710. }
  711. static void hc32_uart3_rxerr_irq_handler(void)
  712. {
  713. /* enter interrupt */
  714. rt_interrupt_enter();
  715. hc32_uart_rxerr_irq_handler(&uart_obj[UART3_INDEX]);
  716. /* leave interrupt */
  717. rt_interrupt_leave();
  718. }
  719. #if defined(RT_SERIAL_USING_DMA)
  720. #if defined(BSP_UART3_TX_USING_DMA)
  721. static void hc32_uart3_tc_irq_handler(void)
  722. {
  723. /* enter interrupt */
  724. rt_interrupt_enter();
  725. hc32_uart_tc_irq_handler(&uart_obj[UART3_INDEX]);
  726. /* leave interrupt */
  727. rt_interrupt_leave();
  728. }
  729. #endif /* BSP_UART3_TX_USING_DMA */
  730. #if defined(BSP_UART3_RX_USING_DMA)
  731. static void hc32_uart3_rxto_irq_handler(void)
  732. {
  733. /* enter interrupt */
  734. rt_interrupt_enter();
  735. hc32_uart_rxto_irq_handler(&uart_obj[UART3_INDEX]);
  736. /* leave interrupt */
  737. rt_interrupt_leave();
  738. }
  739. static void hc32_uart3_dma_rx_irq_handler(void)
  740. {
  741. /* enter interrupt */
  742. rt_interrupt_enter();
  743. hc32_uart_dma_rx_irq_handler(&uart_obj[UART3_INDEX]);
  744. /* leave interrupt */
  745. rt_interrupt_leave();
  746. }
  747. #endif /* BSP_UART3_RX_USING_DMA */
  748. #endif /* RT_SERIAL_USING_DMA */
  749. #endif /* BSP_USING_UART3 */
  750. #if defined(BSP_USING_UART4)
  751. static void hc32_uart4_rx_irq_handler(void)
  752. {
  753. /* enter interrupt */
  754. rt_interrupt_enter();
  755. hc32_uart_rx_irq_handler(&uart_obj[UART4_INDEX]);
  756. /* leave interrupt */
  757. rt_interrupt_leave();
  758. }
  759. static void hc32_uart4_tx_irq_handler(void)
  760. {
  761. /* enter interrupt */
  762. rt_interrupt_enter();
  763. hc32_uart_tx_irq_handler(&uart_obj[UART4_INDEX]);
  764. /* leave interrupt */
  765. rt_interrupt_leave();
  766. }
  767. static void hc32_uart4_rxerr_irq_handler(void)
  768. {
  769. /* enter interrupt */
  770. rt_interrupt_enter();
  771. hc32_uart_rxerr_irq_handler(&uart_obj[UART4_INDEX]);
  772. /* leave interrupt */
  773. rt_interrupt_leave();
  774. }
  775. #if defined(RT_SERIAL_USING_DMA)
  776. #if defined(BSP_UART4_TX_USING_DMA)
  777. static void hc32_uart4_tc_irq_handler(void)
  778. {
  779. /* enter interrupt */
  780. rt_interrupt_enter();
  781. hc32_uart_tc_irq_handler(&uart_obj[UART4_INDEX]);
  782. /* leave interrupt */
  783. rt_interrupt_leave();
  784. }
  785. #endif /* BSP_UART4_TX_USING_DMA */
  786. #if defined(BSP_UART4_RX_USING_DMA)
  787. static void hc32_uart4_rxto_irq_handler(void)
  788. {
  789. /* enter interrupt */
  790. rt_interrupt_enter();
  791. hc32_uart_rxto_irq_handler(&uart_obj[UART4_INDEX]);
  792. /* leave interrupt */
  793. rt_interrupt_leave();
  794. }
  795. static void hc32_uart4_dma_rx_irq_handler(void)
  796. {
  797. /* enter interrupt */
  798. rt_interrupt_enter();
  799. hc32_uart_dma_rx_irq_handler(&uart_obj[UART4_INDEX]);
  800. /* leave interrupt */
  801. rt_interrupt_leave();
  802. }
  803. #endif /* BSP_UART4_RX_USING_DMA */
  804. #endif /* RT_SERIAL_USING_DMA */
  805. #endif /* BSP_USING_UART4 */
  806. #if defined(BSP_USING_UART5)
  807. static void hc32_uart5_rx_irq_handler(void)
  808. {
  809. /* enter interrupt */
  810. rt_interrupt_enter();
  811. hc32_uart_rx_irq_handler(&uart_obj[UART5_INDEX]);
  812. /* leave interrupt */
  813. rt_interrupt_leave();
  814. }
  815. static void hc32_uart5_tx_irq_handler(void)
  816. {
  817. /* enter interrupt */
  818. rt_interrupt_enter();
  819. hc32_uart_tx_irq_handler(&uart_obj[UART5_INDEX]);
  820. /* leave interrupt */
  821. rt_interrupt_leave();
  822. }
  823. static void hc32_uart5_rxerr_irq_handler(void)
  824. {
  825. /* enter interrupt */
  826. rt_interrupt_enter();
  827. hc32_uart_rxerr_irq_handler(&uart_obj[UART5_INDEX]);
  828. /* leave interrupt */
  829. rt_interrupt_leave();
  830. }
  831. #endif /* BSP_USING_UART5 */
  832. #if defined(BSP_USING_UART6)
  833. static void hc32_uart6_rx_irq_handler(void)
  834. {
  835. /* enter interrupt */
  836. rt_interrupt_enter();
  837. hc32_uart_rx_irq_handler(&uart_obj[UART6_INDEX]);
  838. /* leave interrupt */
  839. rt_interrupt_leave();
  840. }
  841. static void hc32_uart6_tx_irq_handler(void)
  842. {
  843. /* enter interrupt */
  844. rt_interrupt_enter();
  845. hc32_uart_tx_irq_handler(&uart_obj[UART6_INDEX]);
  846. /* leave interrupt */
  847. rt_interrupt_leave();
  848. }
  849. static void hc32_uart6_rxerr_irq_handler(void)
  850. {
  851. /* enter interrupt */
  852. rt_interrupt_enter();
  853. hc32_uart_rxerr_irq_handler(&uart_obj[UART6_INDEX]);
  854. /* leave interrupt */
  855. rt_interrupt_leave();
  856. }
  857. #if defined(RT_SERIAL_USING_DMA)
  858. #if defined(BSP_UART6_TX_USING_DMA)
  859. static void hc32_uart6_tc_irq_handler(void)
  860. {
  861. /* enter interrupt */
  862. rt_interrupt_enter();
  863. hc32_uart_tc_irq_handler(&uart_obj[UART6_INDEX]);
  864. /* leave interrupt */
  865. rt_interrupt_leave();
  866. }
  867. #endif /* BSP_UART6_TX_USING_DMA */
  868. #if defined(BSP_UART6_RX_USING_DMA)
  869. static void hc32_uart6_rxto_irq_handler(void)
  870. {
  871. /* enter interrupt */
  872. rt_interrupt_enter();
  873. hc32_uart_rxto_irq_handler(&uart_obj[UART6_INDEX]);
  874. /* leave interrupt */
  875. rt_interrupt_leave();
  876. }
  877. static void hc32_uart6_dma_rx_irq_handler(void)
  878. {
  879. /* enter interrupt */
  880. rt_interrupt_enter();
  881. hc32_uart_dma_rx_irq_handler(&uart_obj[UART6_INDEX]);
  882. /* leave interrupt */
  883. rt_interrupt_leave();
  884. }
  885. #endif /* BSP_UART6_RX_USING_DMA */
  886. #endif /* RT_SERIAL_USING_DMA */
  887. #endif /* BSP_USING_UART6 */
  888. #if defined(BSP_USING_UART7)
  889. static void hc32_uart7_rx_irq_handler(void)
  890. {
  891. /* enter interrupt */
  892. rt_interrupt_enter();
  893. hc32_uart_rx_irq_handler(&uart_obj[UART7_INDEX]);
  894. /* leave interrupt */
  895. rt_interrupt_leave();
  896. }
  897. static void hc32_uart7_tx_irq_handler(void)
  898. {
  899. /* enter interrupt */
  900. rt_interrupt_enter();
  901. hc32_uart_tx_irq_handler(&uart_obj[UART7_INDEX]);
  902. /* leave interrupt */
  903. rt_interrupt_leave();
  904. }
  905. static void hc32_uart7_rxerr_irq_handler(void)
  906. {
  907. /* enter interrupt */
  908. rt_interrupt_enter();
  909. hc32_uart_rxerr_irq_handler(&uart_obj[UART7_INDEX]);
  910. /* leave interrupt */
  911. rt_interrupt_leave();
  912. }
  913. #if defined(RT_SERIAL_USING_DMA)
  914. #if defined(BSP_UART7_TX_USING_DMA)
  915. static void hc32_uart7_tc_irq_handler(void)
  916. {
  917. /* enter interrupt */
  918. rt_interrupt_enter();
  919. hc32_uart_tc_irq_handler(&uart_obj[UART7_INDEX]);
  920. /* leave interrupt */
  921. rt_interrupt_leave();
  922. }
  923. #endif /* BSP_UART7_TX_USING_DMA */
  924. #if defined(BSP_UART7_RX_USING_DMA)
  925. static void hc32_uart7_rxto_irq_handler(void)
  926. {
  927. /* enter interrupt */
  928. rt_interrupt_enter();
  929. hc32_uart_rxto_irq_handler(&uart_obj[UART7_INDEX]);
  930. /* leave interrupt */
  931. rt_interrupt_leave();
  932. }
  933. static void hc32_uart7_dma_rx_irq_handler(void)
  934. {
  935. /* enter interrupt */
  936. rt_interrupt_enter();
  937. hc32_uart_dma_rx_irq_handler(&uart_obj[UART7_INDEX]);
  938. /* leave interrupt */
  939. rt_interrupt_leave();
  940. }
  941. #endif /* BSP_UART7_RX_USING_DMA */
  942. #endif /* RT_SERIAL_USING_DMA */
  943. #endif /* BSP_USING_UART7 */
  944. #if defined(BSP_USING_UART8)
  945. static void hc32_uart8_rx_irq_handler(void)
  946. {
  947. /* enter interrupt */
  948. rt_interrupt_enter();
  949. hc32_uart_rx_irq_handler(&uart_obj[UART8_INDEX]);
  950. /* leave interrupt */
  951. rt_interrupt_leave();
  952. }
  953. static void hc32_uart8_tx_irq_handler(void)
  954. {
  955. /* enter interrupt */
  956. rt_interrupt_enter();
  957. hc32_uart_tx_irq_handler(&uart_obj[UART8_INDEX]);
  958. /* leave interrupt */
  959. rt_interrupt_leave();
  960. }
  961. static void hc32_uart8_rxerr_irq_handler(void)
  962. {
  963. /* enter interrupt */
  964. rt_interrupt_enter();
  965. hc32_uart_rxerr_irq_handler(&uart_obj[UART8_INDEX]);
  966. /* leave interrupt */
  967. rt_interrupt_leave();
  968. }
  969. #endif /* BSP_USING_UART8 */
  970. #if defined(BSP_USING_UART9)
  971. static void hc32_uart9_rx_irq_handler(void)
  972. {
  973. /* enter interrupt */
  974. rt_interrupt_enter();
  975. hc32_uart_rx_irq_handler(&uart_obj[UART9_INDEX]);
  976. /* leave interrupt */
  977. rt_interrupt_leave();
  978. }
  979. static void hc32_uart9_tx_irq_handler(void)
  980. {
  981. /* enter interrupt */
  982. rt_interrupt_enter();
  983. hc32_uart_tx_irq_handler(&uart_obj[UART9_INDEX]);
  984. /* leave interrupt */
  985. rt_interrupt_leave();
  986. }
  987. static void hc32_uart9_rxerr_irq_handler(void)
  988. {
  989. /* enter interrupt */
  990. rt_interrupt_enter();
  991. hc32_uart_rxerr_irq_handler(&uart_obj[UART9_INDEX]);
  992. /* leave interrupt */
  993. rt_interrupt_leave();
  994. }
  995. #endif /* BSP_USING_UART9 */
  996. #if defined(BSP_USING_UART10)
  997. static void hc32_uart10_rx_irq_handler(void)
  998. {
  999. /* enter interrupt */
  1000. rt_interrupt_enter();
  1001. hc32_uart_rx_irq_handler(&uart_obj[UART10_INDEX]);
  1002. /* leave interrupt */
  1003. rt_interrupt_leave();
  1004. }
  1005. static void hc32_uart10_tx_irq_handler(void)
  1006. {
  1007. /* enter interrupt */
  1008. rt_interrupt_enter();
  1009. hc32_uart_tx_irq_handler(&uart_obj[UART10_INDEX]);
  1010. /* leave interrupt */
  1011. rt_interrupt_leave();
  1012. }
  1013. static void hc32_uart10_rxerr_irq_handler(void)
  1014. {
  1015. /* enter interrupt */
  1016. rt_interrupt_enter();
  1017. hc32_uart_rxerr_irq_handler(&uart_obj[UART10_INDEX]);
  1018. /* leave interrupt */
  1019. rt_interrupt_leave();
  1020. }
  1021. #endif /* BSP_USING_UART10 */
  1022. /**
  1023. * @brief This function gets dma witch uart used infomation include unit,
  1024. * channel, interrupt etc.
  1025. * @param None
  1026. * @retval None
  1027. */
  1028. static void hc32_uart_get_dma_info(void)
  1029. {
  1030. #ifdef BSP_USING_UART1
  1031. uart_obj[UART1_INDEX].uart_dma_flag = 0;
  1032. #ifdef BSP_UART1_RX_USING_DMA
  1033. uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1034. static struct dma_config uart1_dma_rx = UART1_DMA_RX_CONFIG;
  1035. static struct hc32_uart_rxto uart1_rx_timeout = UART1_RXTO_CONFIG;
  1036. uart1_dma_rx.irq_callback = hc32_uart1_dma_rx_irq_handler;
  1037. uart1_rx_timeout.irq_callback = hc32_uart1_rxto_irq_handler;
  1038. uart_config[UART1_INDEX].rx_timeout = &uart1_rx_timeout;
  1039. uart_config[UART1_INDEX].dma_rx = &uart1_dma_rx;
  1040. #endif
  1041. #ifdef BSP_UART1_TX_USING_DMA
  1042. uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1043. static struct dma_config uart1_dma_tx = UART1_DMA_TX_CONFIG;
  1044. uart_config[UART1_INDEX].dma_tx = &uart1_dma_tx;
  1045. static struct hc32_uart_irq_config uart1_tc_irq = UART1_TX_CPLT_CONFIG;
  1046. uart1_tc_irq.irq_callback = hc32_uart1_tc_irq_handler;
  1047. uart_config[UART1_INDEX].tc_irq = &uart1_tc_irq;
  1048. #endif
  1049. #endif
  1050. #ifdef BSP_USING_UART2
  1051. uart_obj[UART2_INDEX].uart_dma_flag = 0;
  1052. #ifdef BSP_UART2_RX_USING_DMA
  1053. uart_obj[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1054. static struct dma_config uart2_dma_rx = UART2_DMA_RX_CONFIG;
  1055. static struct hc32_uart_rxto uart2_rx_timeout = UART2_RXTO_CONFIG;
  1056. uart2_dma_rx.irq_callback = hc32_uart2_dma_rx_irq_handler;
  1057. uart2_rx_timeout.irq_callback = hc32_uart2_rxto_irq_handler;
  1058. uart_config[UART2_INDEX].rx_timeout = &uart2_rx_timeout;
  1059. uart_config[UART2_INDEX].dma_rx = &uart2_dma_rx;
  1060. #endif
  1061. #ifdef BSP_UART2_TX_USING_DMA
  1062. uart_obj[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1063. static struct dma_config uart2_dma_tx = UART2_DMA_TX_CONFIG;
  1064. uart_config[UART2_INDEX].dma_tx = &uart2_dma_tx;
  1065. static struct hc32_uart_irq_config uart2_tc_irq = UART2_TX_CPLT_CONFIG;
  1066. uart2_tc_irq.irq_callback = hc32_uart2_tc_irq_handler;
  1067. uart_config[UART2_INDEX].tc_irq = &uart2_tc_irq;
  1068. #endif
  1069. #endif
  1070. #ifdef BSP_USING_UART3
  1071. uart_obj[UART3_INDEX].uart_dma_flag = 0;
  1072. #ifdef BSP_UART3_RX_USING_DMA
  1073. uart_obj[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1074. static struct dma_config uart3_dma_rx = UART3_DMA_RX_CONFIG;
  1075. static struct hc32_uart_rxto uart3_rx_timeout = UART3_RXTO_CONFIG;
  1076. uart3_dma_rx.irq_callback = hc32_uart3_dma_rx_irq_handler;
  1077. uart3_rx_timeout.irq_callback = hc32_uart3_rxto_irq_handler;
  1078. uart_config[UART3_INDEX].rx_timeout = &uart3_rx_timeout;
  1079. uart_config[UART3_INDEX].dma_rx = &uart3_dma_rx;
  1080. #endif
  1081. #ifdef BSP_UART3_TX_USING_DMA
  1082. uart_obj[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1083. static struct dma_config uart3_dma_tx = UART3_DMA_TX_CONFIG;
  1084. uart_config[UART3_INDEX].dma_tx = &uart3_dma_tx;
  1085. static struct hc32_uart_irq_config uart3_tc_irq = UART3_TX_CPLT_CONFIG;
  1086. uart3_tc_irq.irq_callback = hc32_uart3_tc_irq_handler;
  1087. uart_config[UART3_INDEX].tc_irq = &uart3_tc_irq;
  1088. #endif
  1089. #endif
  1090. #ifdef BSP_USING_UART4
  1091. uart_obj[UART4_INDEX].uart_dma_flag = 0;
  1092. #ifdef BSP_UART4_RX_USING_DMA
  1093. uart_obj[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1094. static struct dma_config uart4_dma_rx = UART4_DMA_RX_CONFIG;
  1095. static struct hc32_uart_rxto uart4_rx_timeout = UART4_RXTO_CONFIG;
  1096. uart4_dma_rx.irq_callback = hc32_uart4_dma_rx_irq_handler;
  1097. uart4_rx_timeout.irq_callback = hc32_uart4_rxto_irq_handler;
  1098. uart_config[UART4_INDEX].rx_timeout = &uart4_rx_timeout;
  1099. uart_config[UART4_INDEX].dma_rx = &uart4_dma_rx;
  1100. #endif
  1101. #ifdef BSP_UART4_TX_USING_DMA
  1102. uart_obj[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1103. static struct dma_config uart4_dma_tx = UART4_DMA_TX_CONFIG;
  1104. uart_config[UART4_INDEX].dma_tx = &uart4_dma_tx;
  1105. static struct hc32_uart_irq_config uart4_tc_irq = UART4_TX_CPLT_CONFIG;
  1106. uart4_tc_irq.irq_callback = hc32_uart4_tc_irq_handler;
  1107. uart_config[UART4_INDEX].tc_irq = &uart4_tc_irq;
  1108. #endif
  1109. #endif
  1110. #ifdef BSP_USING_UART6
  1111. uart_obj[UART6_INDEX].uart_dma_flag = 0;
  1112. #ifdef BSP_UART6_RX_USING_DMA
  1113. uart_obj[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1114. static struct dma_config uart6_dma_rx = UART6_DMA_RX_CONFIG;
  1115. static struct hc32_uart_rxto uart6_rx_timeout = UART6_RXTO_CONFIG;
  1116. uart6_dma_rx.irq_callback = hc32_uart6_dma_rx_irq_handler;
  1117. uart6_rx_timeout.irq_callback = hc32_uart6_rxto_irq_handler;
  1118. uart_config[UART6_INDEX].rx_timeout = &uart6_rx_timeout;
  1119. uart_config[UART6_INDEX].dma_rx = &uart6_dma_rx;
  1120. #endif
  1121. #ifdef BSP_UART6_TX_USING_DMA
  1122. uart_obj[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1123. static struct dma_config uart6_dma_tx = UART6_DMA_TX_CONFIG;
  1124. uart_config[UART6_INDEX].dma_tx = &uart6_dma_tx;
  1125. static struct hc32_uart_irq_config uart6_tc_irq = UART6_TX_CPLT_CONFIG;
  1126. uart6_tc_irq.irq_callback = hc32_uart6_tc_irq_handler;
  1127. uart_config[UART6_INDEX].tc_irq = &uart6_tc_irq;
  1128. #endif
  1129. #endif
  1130. #ifdef BSP_USING_UART7
  1131. uart_obj[UART7_INDEX].uart_dma_flag = 0;
  1132. #ifdef BSP_UART7_RX_USING_DMA
  1133. uart_obj[UART7_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1134. static struct dma_config uart7_dma_rx = UART7_DMA_RX_CONFIG;
  1135. static struct hc32_uart_rxto uart7_rx_timeout = UART7_RXTO_CONFIG;
  1136. uart7_dma_rx.irq_callback = hc32_uart7_dma_rx_irq_handler;
  1137. uart7_rx_timeout.irq_callback = hc32_uart7_rxto_irq_handler;
  1138. uart_config[UART7_INDEX].rx_timeout = &uart7_rx_timeout;
  1139. uart_config[UART7_INDEX].dma_rx = &uart7_dma_rx;
  1140. #endif
  1141. #ifdef BSP_UART7_TX_USING_DMA
  1142. uart_obj[UART7_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1143. static struct dma_config uart7_dma_tx = UART7_DMA_TX_CONFIG;
  1144. uart_config[UART7_INDEX].dma_tx = &uart7_dma_tx;
  1145. static struct hc32_uart_irq_config uart7_tc_irq = UART7_TX_CPLT_CONFIG;
  1146. uart7_tc_irq.irq_callback = hc32_uart7_tc_irq_handler;
  1147. uart_config[UART7_INDEX].tc_irq = &uart7_tc_irq;
  1148. #endif
  1149. #endif
  1150. }
  1151. /**
  1152. * @brief This function gets uart irq handle.
  1153. * @param None
  1154. * @retval None
  1155. */
  1156. static void hc32_get_uart_callback(void)
  1157. {
  1158. #ifdef BSP_USING_UART1
  1159. uart_config[UART1_INDEX].rxerr_irq.irq_callback = hc32_uart1_rxerr_irq_handler;
  1160. uart_config[UART1_INDEX].rx_irq.irq_callback = hc32_uart1_rx_irq_handler;
  1161. uart_config[UART1_INDEX].tx_irq.irq_callback = hc32_uart1_tx_irq_handler;
  1162. #endif
  1163. #ifdef BSP_USING_UART2
  1164. uart_config[UART2_INDEX].rxerr_irq.irq_callback = hc32_uart2_rxerr_irq_handler;
  1165. uart_config[UART2_INDEX].rx_irq.irq_callback = hc32_uart2_rx_irq_handler;
  1166. uart_config[UART2_INDEX].tx_irq.irq_callback = hc32_uart2_tx_irq_handler;
  1167. #endif
  1168. #ifdef BSP_USING_UART3
  1169. uart_config[UART3_INDEX].rxerr_irq.irq_callback = hc32_uart3_rxerr_irq_handler;
  1170. uart_config[UART3_INDEX].rx_irq.irq_callback = hc32_uart3_rx_irq_handler;
  1171. uart_config[UART3_INDEX].tx_irq.irq_callback = hc32_uart3_tx_irq_handler;
  1172. #endif
  1173. #ifdef BSP_USING_UART4
  1174. uart_config[UART4_INDEX].rxerr_irq.irq_callback = hc32_uart4_rxerr_irq_handler;
  1175. uart_config[UART4_INDEX].rx_irq.irq_callback = hc32_uart4_rx_irq_handler;
  1176. uart_config[UART4_INDEX].tx_irq.irq_callback = hc32_uart4_tx_irq_handler;
  1177. #endif
  1178. #ifdef BSP_USING_UART5
  1179. uart_config[UART5_INDEX].rxerr_irq.irq_callback = hc32_uart5_rxerr_irq_handler;
  1180. uart_config[UART5_INDEX].rx_irq.irq_callback = hc32_uart5_rx_irq_handler;
  1181. uart_config[UART5_INDEX].tx_irq.irq_callback = hc32_uart5_tx_irq_handler;
  1182. #endif
  1183. #ifdef BSP_USING_UART6
  1184. uart_config[UART6_INDEX].rxerr_irq.irq_callback = hc32_uart6_rxerr_irq_handler;
  1185. uart_config[UART6_INDEX].rx_irq.irq_callback = hc32_uart6_rx_irq_handler;
  1186. uart_config[UART6_INDEX].tx_irq.irq_callback = hc32_uart6_tx_irq_handler;
  1187. #endif
  1188. #ifdef BSP_USING_UART7
  1189. uart_config[UART7_INDEX].rxerr_irq.irq_callback = hc32_uart7_rxerr_irq_handler;
  1190. uart_config[UART7_INDEX].rx_irq.irq_callback = hc32_uart7_rx_irq_handler;
  1191. uart_config[UART7_INDEX].tx_irq.irq_callback = hc32_uart7_tx_irq_handler;
  1192. #endif
  1193. #ifdef BSP_USING_UART8
  1194. uart_config[UART8_INDEX].rxerr_irq.irq_callback = hc32_uart8_rxerr_irq_handler;
  1195. uart_config[UART8_INDEX].rx_irq.irq_callback = hc32_uart8_rx_irq_handler;
  1196. uart_config[UART8_INDEX].tx_irq.irq_callback = hc32_uart8_tx_irq_handler;
  1197. #endif
  1198. #ifdef BSP_USING_UART9
  1199. uart_config[UART9_INDEX].rxerr_irq.irq_callback = hc32_uart9_rxerr_irq_handler;
  1200. uart_config[UART9_INDEX].rx_irq.irq_callback = hc32_uart9_rx_irq_handler;
  1201. uart_config[UART9_INDEX].tx_irq.irq_callback = hc32_uart9_tx_irq_handler;
  1202. #endif
  1203. #ifdef BSP_USING_UART10
  1204. uart_config[UART10_INDEX].rxerr_irq.irq_callback = hc32_uart10_rxerr_irq_handler;
  1205. uart_config[UART10_INDEX].rx_irq.irq_callback = hc32_uart10_rx_irq_handler;
  1206. uart_config[UART10_INDEX].tx_irq.irq_callback = hc32_uart10_tx_irq_handler;
  1207. #endif
  1208. }
  1209. static const struct rt_uart_ops hc32_uart_ops =
  1210. {
  1211. .configure = hc32_configure,
  1212. .control = hc32_control,
  1213. .putc = hc32_putc,
  1214. .getc = hc32_getc,
  1215. .dma_transmit = hc32_dma_transmit
  1216. };
  1217. int hc32_hw_uart_init(void)
  1218. {
  1219. rt_err_t result = RT_EOK;
  1220. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct hc32_uart);
  1221. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  1222. hc32_uart_get_dma_info();
  1223. hc32_get_uart_callback();
  1224. for (int i = 0; i < obj_num; i++)
  1225. {
  1226. /* init UART object */
  1227. uart_obj[i].serial.ops = &hc32_uart_ops;
  1228. uart_obj[i].serial.config = config;
  1229. uart_obj[i].config = &uart_config[i];
  1230. /* register the handle */
  1231. hc32_install_irq_handler(&uart_config[i].rxerr_irq.irq_config, uart_config[i].rxerr_irq.irq_callback, RT_FALSE);
  1232. #ifdef RT_SERIAL_USING_DMA
  1233. if (uart_obj[i].uart_dma_flag & RT_DEVICE_FLAG_DMA_RX)
  1234. {
  1235. hc32_install_irq_handler(&uart_config[i].dma_rx->irq_config, uart_config[i].dma_rx->irq_callback, RT_FALSE);
  1236. hc32_install_irq_handler(&uart_config[i].rx_timeout->irq_config, uart_config[i].rx_timeout->irq_callback, RT_FALSE);
  1237. }
  1238. if (uart_obj[i].uart_dma_flag & RT_DEVICE_FLAG_DMA_TX)
  1239. {
  1240. hc32_install_irq_handler(&uart_config[i].tc_irq->irq_config, uart_config[i].tc_irq->irq_callback, RT_FALSE);
  1241. }
  1242. #endif
  1243. /* register UART device */
  1244. result = rt_hw_serial_register(&uart_obj[i].serial,
  1245. uart_obj[i].config->name,
  1246. (RT_DEVICE_FLAG_RDWR |
  1247. RT_DEVICE_FLAG_INT_RX |
  1248. RT_DEVICE_FLAG_INT_TX |
  1249. uart_obj[i].uart_dma_flag),
  1250. &uart_obj[i]);
  1251. RT_ASSERT(result == RT_EOK);
  1252. }
  1253. return result;
  1254. }
  1255. INIT_BOARD_EXPORT(hc32_hw_uart_init);
  1256. #endif
  1257. #endif /* RT_USING_SERIAL */
  1258. /*******************************************************************************
  1259. * EOF (not truncated)
  1260. ******************************************************************************/