1 /*
2  * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-04-28     CDT          first version
9  */
10 
11 #ifndef __DRV_USART_H__
12 #define __DRV_USART_H__
13 
14 /*******************************************************************************
15  * Include files
16  ******************************************************************************/
17 #include <rtthread.h>
18 #include "rtdevice.h"
19 #include "drv_irq.h"
20 #include "drv_dma.h"
21 
22 /* C binding of definitions if building with C++ compiler */
23 #ifdef __cplusplus
24 extern "C"
25 {
26 #endif
27 
28 /*******************************************************************************
29  * Global type definitions ('typedef')
30  ******************************************************************************/
31 struct hc32_uart_irq_config
32 {
33     struct hc32_irq_config irq_config;
34     func_ptr_t             irq_callback;
35 };
36 
37 /* HC32 config Rx timeout */
38 struct hc32_uart_rxto
39 {
40     CM_TMR0_TypeDef             *TMR0_Instance;
41     rt_uint32_t                 channel;
42     rt_uint32_t                 clock;
43     rt_size_t                   timeout_bits;
44 #if defined (HC32F460) || defined (HC32F4A0) || defined (HC32F4A8)
45     struct hc32_irq_config      irq_config;
46     func_ptr_t                  irq_callback;
47 #endif
48 };
49 
50 /* HC32 config uart class */
51 struct hc32_uart_config
52 {
53     const char                  *name;
54     CM_USART_TypeDef            *Instance;
55     rt_uint32_t                 clock;
56 #if defined (HC32F460) || defined (HC32F4A0) || defined (HC32F4A8)
57     struct hc32_uart_irq_config rxerr_irq;
58     struct hc32_uart_irq_config rx_irq;
59     struct hc32_uart_irq_config tx_irq;
60 #elif defined (HC32F448) || defined (HC32F472) || defined (HC32F334)
61     IRQn_Type                   irq_num;
62     en_int_src_t                rxerr_int_src;
63     en_int_src_t                tx_int_src;
64     en_int_src_t                rx_int_src;
65 #ifdef RT_SERIAL_USING_DMA
66     en_int_src_t                rxto_int_src;
67 #endif
68 #endif
69 
70 #ifdef RT_SERIAL_USING_DMA
71     struct hc32_uart_rxto       *rx_timeout;
72     stc_dma_llp_descriptor_t    llp_desc[2U];
73     struct dma_config           *dma_rx;
74     struct hc32_uart_irq_config *tc_irq;
75     struct dma_config           *dma_tx;
76 #endif
77 };
78 
79 /* HC32 uart driver class */
80 struct hc32_uart
81 {
82     struct hc32_uart_config *config;
83 #ifdef RT_SERIAL_USING_DMA
84     rt_size_t               dma_rx_remaining_cnt;
85 #endif
86     rt_uint16_t             uart_dma_flag;
87     struct rt_serial_device serial;
88 };
89 
90 /*******************************************************************************
91  * Global pre-processor symbols/macros ('#define')
92  ******************************************************************************/
93 
94 /*******************************************************************************
95  * Global variable definitions ('extern')
96  ******************************************************************************/
97 
98 /*******************************************************************************
99  * Global function prototypes (definition in C source)
100  ******************************************************************************/
101 int rt_hw_usart_init(void);
102 
103 #ifdef __cplusplus
104 }
105 #endif
106 
107 #endif /* __DRV_USART_H__ */
108 
109 /*******************************************************************************
110  * EOF (not truncated)
111  ******************************************************************************/
112