summaryrefslogtreecommitdiff
path: root/bsp/hc32l196/drivers/drv_usart.c
blob: cef082c91e9a54e3cd7cfb23b3bb6ba6e6c91c39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-9-1       DongBowen    first version
 */

#include "drv_usart.h"
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"

#ifdef RT_USING_SERIAL
#ifdef BSP_USING_UART

#if !defined(BSP_USING_UART0) && !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && \
    !defined(BSP_USING_UART3)
    #error "Please define at least one BSP_USING_UARTx"
    /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
#endif

enum
{
#ifdef BSP_USING_UART0
    UART0_INDEX,
#endif
#ifdef BSP_USING_UART1
    UART1_INDEX,
#endif
#ifdef BSP_USING_UART2
    UART2_INDEX,
#endif
#ifdef BSP_USING_UART3
    UART3_INDEX,
#endif
};

static struct hc_uart_cfg uart_cfg[] =
{
#ifdef BSP_USING_UART0
    UART0_CFG,
#endif
#ifdef BSP_USING_UART1
    UART1_CFG,
#endif
#ifdef BSP_USING_UART2
    UART2_CFG,
#endif
#ifdef BSP_USING_UART3
    UART3_CFG,
#endif
};

static struct hc_uart uart_drv[sizeof(uart_cfg) / sizeof(uart_cfg[0])] = {0};

static rt_err_t _uart_init(struct rt_serial_device *serial_device, struct serial_configure *configure)
{
    stc_gpio_cfg_t stcGpioCfg;
    stc_uart_cfg_t stcCfg;

    struct hc_uart_cfg *cfg;
    RT_ASSERT(serial_device != RT_NULL);
    RT_ASSERT(configure != RT_NULL);
    cfg = serial_device->parent.user_data;

    /* configure rx and tx gpio */
    rt_memset(&stcGpioCfg, 0, sizeof(stcGpioCfg));
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
    stcGpioCfg.enDir = GpioDirOut;
    Gpio_Init(cfg->tx_port, cfg->tx_pin, &stcGpioCfg);
    Gpio_SetAfMode(cfg->tx_port, cfg->tx_pin, cfg->tx_af);
    stcGpioCfg.enDir = GpioDirIn;
    Gpio_Init(cfg->rx_port, cfg->rx_pin, &stcGpioCfg);
    Gpio_SetAfMode(cfg->rx_port, cfg->rx_pin, cfg->rx_af);

    /* configure uart */
    rt_memset(&stcCfg, 0, sizeof(stcCfg));

    Sysctrl_SetPeripheralGate(cfg->uart_periph, TRUE);

    stcCfg.stcBaud.u32Baud = configure->baud_rate;
    stcCfg.stcBaud.enClkDiv = UartMsk8Or16Div;
    stcCfg.stcBaud.u32Pclk = Sysctrl_GetPClkFreq();

    switch (configure->data_bits)
    {
    case DATA_BITS_8:
        break;
    default:
        break;
    }

    switch (configure->stop_bits)
    {
    case STOP_BITS_1:
        stcCfg.enStopBit = UartMsk1bit;
        break;
    case STOP_BITS_2:
        stcCfg.enStopBit = UartMsk2bit;
        break;
    default:
        stcCfg.enStopBit = UartMsk1bit;
        break;
    }

    switch (configure->parity)
    {
    case PARITY_NONE:
        stcCfg.enMmdorCk = UartMskDataOrAddr;
        stcCfg.enRunMode = UartMskMode1;
        break;
    case PARITY_ODD:
        stcCfg.enMmdorCk = UartMskOdd;
        stcCfg.enRunMode = UartMskMode3;
        break;
    case PARITY_EVEN:
        stcCfg.enMmdorCk = UartMskEven;
        stcCfg.enRunMode = UartMskMode3;
        break;
    default:
        stcCfg.enMmdorCk = UartMskDataOrAddr;
        stcCfg.enRunMode = UartMskMode1;
        break;
    }

    Uart_Init(cfg->uart, &stcCfg);

    Uart_ClrStatus(cfg->uart, UartRC);
    Uart_ClrStatus(cfg->uart, UartTC);

    rt_hw_us_delay(2);
    return RT_EOK;
}

static rt_err_t _uart_control(struct rt_serial_device *serial_device, int cmd, void *arg)
{
    struct hc_uart_cfg *cfg;
    RT_ASSERT(serial_device != RT_NULL);
    cfg = serial_device->parent.user_data;

    switch (cmd)
    {
    case RT_DEVICE_CTRL_CLR_INT:
        /* disable rx irq */
        Uart_DisableIrq(cfg->uart, UartRxIrq);
        EnableNvic(cfg->irqn, IrqLevel3, FALSE);
        break;
    case RT_DEVICE_CTRL_SET_INT:
        /* enable rx irq */
        Uart_EnableIrq(cfg->uart, UartRxIrq);
        EnableNvic(cfg->irqn, IrqLevel3, TRUE);
        break;
    }
    return RT_EOK;
}

static int _uart_putc(struct rt_serial_device *serial_device, char c)
{
    struct hc_uart_cfg *cfg;
    RT_ASSERT(serial_device != RT_NULL);
    cfg = serial_device->parent.user_data;

    Uart_SendDataPoll(cfg->uart, (uint8_t)c);
    return 1;
}

static int _uart_getc(struct rt_serial_device *serial_device)
{
    int ch;
    struct hc_uart_cfg *cfg;
    RT_ASSERT(serial_device != RT_NULL);
    cfg = serial_device->parent.user_data;

    ch = -1;

    if (Uart_GetStatus(cfg->uart, UartRC))
    {
        Uart_ClrStatus(cfg->uart, UartRC);
        ch = Uart_ReceiveData(cfg->uart);
    }

    return ch;
}

static const struct rt_uart_ops _uart_ops =
{
    .configure = _uart_init,
    .control = _uart_control,
    .putc = _uart_putc,
    .getc = _uart_getc,
    .dma_transmit = RT_NULL
};

/**
 * Uart common interrupt process. This need add to uart ISR.
 *
 * @param serial serial device
 */
static void rt_hw_uart_isr(struct rt_serial_device *serial_device)
{
    struct hc_uart_cfg *cfg;
    uint32_t status;
    RT_ASSERT(serial_device != RT_NULL);

    cfg = serial_device->parent.user_data;
    status = cfg->uart->ISR;

    /* UART in mode Receiver -------------------------------------------------*/
    if (status & (1 << UartFE))
    {
        Uart_ClrStatus(cfg->uart, UartFE);
    }
    if (status & (1 << UartPE))
    {
        Uart_ClrStatus(cfg->uart, UartPE);
    }
    if (status & (1 << UartRC))
    {
        rt_hw_serial_isr(serial_device, RT_SERIAL_EVENT_RX_IND);
    }
}

#if defined(BSP_USING_UART0)
void Uart0_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    rt_hw_uart_isr(&(uart_drv[UART0_INDEX].serial_device));

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* BSP_USING_UART0 */

#if defined(BSP_USING_UART1)
void Uart1_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    rt_hw_uart_isr(&(uart_drv[UART1_INDEX].serial_device));

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* BSP_USING_UART1 */

#if defined(BSP_USING_UART2)
void Uart2_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    rt_hw_uart_isr(&(uart_drv[UART2_INDEX].serial_device));

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* BSP_USING_UART2 */

#if defined(BSP_USING_UART3)
void Uart3_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    rt_hw_uart_isr(&(uart_drv[UART3_INDEX].serial_device));

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* BSP_USING_UART3 */

int rt_hw_uart_init(void)
{
    struct serial_configure cfg = RT_SERIAL_CONFIG_DEFAULT;
    int i = 0;
    rt_err_t result = RT_EOK;

    for (i = 0; i < sizeof(uart_cfg) / sizeof(uart_cfg[0]); i++)
    {
        uart_drv[i].cfg = &uart_cfg[i];
        uart_drv[i].serial_device.ops = &_uart_ops;
        uart_drv[i].serial_device.config = cfg;
        /* register UART device */
        result = rt_hw_serial_register(&uart_drv[i].serial_device, uart_drv[i].cfg->name,
                                       RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart_drv[i].cfg);
        RT_ASSERT(result == RT_EOK);
    }

    return result;
}
INIT_BOARD_EXPORT(rt_hw_uart_init);

#endif /* BSP_USING_UART */
#endif /* RT_USING_SERIAL */