summaryrefslogtreecommitdiff
path: root/bsp/wch/risc-v/libraries/ch32_drivers/drv_usart.c
blob: 9daa586c355f51f8ccb35352261230dc33c87d3e (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-08-27     liYony       the first version
 */

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

#ifdef RT_USING_SERIAL

//#define DRV_DEBUG
#define LOG_TAG              "drv.uart"
#include <drv_log.h>

#if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) && !defined(BSP_USING_UART4) && \
    !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && !defined(BSP_USING_UART7) && !defined(BSP_USING_UART8)
    #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_UART1
    UART1_INDEX,
#endif
#ifdef BSP_USING_UART2
    UART2_INDEX,
#endif
#ifdef BSP_USING_UART3
    UART3_INDEX,
#endif
#ifdef BSP_USING_UART4
    UART4_INDEX,
#endif
#ifdef BSP_USING_UART5
    UART5_INDEX,
#endif
#ifdef BSP_USING_UART6
    UART6_INDEX,
#endif
#ifdef BSP_USING_UART7
    UART7_INDEX,
#endif
#ifdef BSP_USING_UART8
    UART8_INDEX,
#endif
};

/* If you want to use other serial ports, please follow UART1 to complete other
  serial ports. For clock configuration,  */
static struct ch32_uart_hw_config uart_hw_config[] =
{
#ifdef BSP_USING_UART1
    {
        /* clock configuration, please refer to ch32v30x_rcc.h */
        RCC_APB2Periph_USART1, RCC_APB2Periph_GPIOA,
        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
        GPIOA, GPIO_Pin_9, /* Tx */GPIOA, GPIO_Pin_10, /* Rx */
        /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
        for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
        GPIO_Remap_NONE,
    },
#endif
#ifdef BSP_USING_UART2
    {
        /* clock configuration, please refer to ch32v30x_rcc.h */
        RCC_APB1Periph_USART2, RCC_APB2Periph_GPIOA,
        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
        GPIOA, GPIO_Pin_2, /* Tx */GPIOA, GPIO_Pin_3, /* Rx */
        /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
        for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
        GPIO_Remap_NONE,
    },
#endif
#ifdef BSP_USING_UART3
    {
        /* clock configuration, please refer to ch32v30x_rcc.h */
        RCC_APB1Periph_USART3, RCC_APB2Periph_GPIOB,
        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
        GPIOB, GPIO_Pin_10, /* Tx */GPIOB, GPIO_Pin_11, /* Rx */
        /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
        for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
        GPIO_Remap_NONE,
    },
#endif
#ifdef BSP_USING_UART4
    {
        /* clock configuration, please refer to ch32v30x_rcc.h */
        RCC_APB1Periph_UART4, RCC_APB2Periph_GPIOC,
        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
        GPIOC, GPIO_Pin_10, /* Tx */GPIOC, GPIO_Pin_11, /* Rx */
        /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
        for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
        GPIO_Remap_NONE,
    },
#endif
#ifdef BSP_USING_UART5
    {
        /* clock configuration, please refer to ch32v30x_rcc.h */
        RCC_APB1Periph_UART5, RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,
        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
        GPIOC, GPIO_Pin_12, /* Tx */GPIOD, GPIO_Pin_2, /* Rx */
        /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
        for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
        GPIO_Remap_NONE,
    },
#endif
#ifdef BSP_USING_UART6
    {
        /* clock configuration, please refer to ch32v30x_rcc.h */
        RCC_APB1Periph_UART6, RCC_APB2Periph_GPIOC,
        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
        GPIOC, GPIO_Pin_0, /* Tx */GPIOC, GPIO_Pin_1, /* Rx */
        /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
        for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
        GPIO_Remap_NONE,
    },
#endif
#ifdef BSP_USING_UART7
    {
        /* clock configuration, please refer to ch32v30x_rcc.h */
        RCC_APB1Periph_UART7, RCC_APB2Periph_GPIOC,
        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
        GPIOC, GPIO_Pin_2, /* Tx */GPIOC, GPIO_Pin_3, /* Rx */
        /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
        for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
        GPIO_Remap_NONE,
    },
#endif
#ifdef BSP_USING_UART8
    {
        /* clock configuration, please refer to ch32v30x_rcc.h */
        RCC_APB1Periph_UART8, RCC_APB2Periph_GPIOC,
        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
        GPIOC, GPIO_Pin_4, /* Tx */GPIOC, GPIO_Pin_5, /* Rx */
        /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
        for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
        GPIO_Remap_NONE,
    },
#endif
};

static struct ch32_uart_config uart_config[] =
{
#ifdef BSP_USING_UART1
    {
        "uart1",
        USART1,
        USART1_IRQn,
    },
#endif
#ifdef BSP_USING_UART2
    {
        "uart2",
        USART2,
        USART2_IRQn,
    },
#endif
#ifdef BSP_USING_UART3
    {
        "uart3",
        USART3,
        USART3_IRQn,
    },
#endif
#ifdef BSP_USING_UART4
    {
        "uart4",
        UART4,
        UART4_IRQn,
    },
#endif
#ifdef BSP_USING_UART5
    {
        "uart5",
        UART5,
        UART5_IRQn,
    },
#endif
#ifdef BSP_USING_UART6
    {
        "uart6",
        UART6,
        UART6_IRQn,
    },
#endif
#ifdef BSP_USING_UART7
    {
        "uart7",
        UART7,
        UART7_IRQn,
    },
#endif
#ifdef BSP_USING_UART8
    {
        "uart8",
        UART8,
        UART8_IRQn,
    },
#endif
};

static struct ch32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};

static rt_err_t ch32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
    struct ch32_uart *uart;
    GPIO_InitTypeDef GPIO_InitStructure={0};

    RT_ASSERT(serial != RT_NULL);
    RT_ASSERT(cfg != RT_NULL);

    uart = (struct ch32_uart *) serial->parent.user_data;

    uart->Init.USART_BaudRate             = cfg->baud_rate;
    uart->Init.USART_HardwareFlowControl  = USART_HardwareFlowControl_None;
    uart->Init.USART_Mode                 = USART_Mode_Rx|USART_Mode_Tx;

    switch (cfg->data_bits)
    {
    case DATA_BITS_8:
        uart->Init.USART_WordLength = USART_WordLength_8b;
        break;
    case DATA_BITS_9:
        uart->Init.USART_WordLength = USART_WordLength_9b;
        break;
    default:
        uart->Init.USART_WordLength = USART_WordLength_8b;
        break;
    }

    switch (cfg->stop_bits)
    {
    case STOP_BITS_1:
        uart->Init.USART_StopBits   = USART_StopBits_1;
        break;
    case STOP_BITS_2:
        uart->Init.USART_StopBits   = USART_StopBits_2;
        break;
    default:
        uart->Init.USART_StopBits   = USART_StopBits_1;
        break;
    }
    switch (cfg->parity)
    {
    case PARITY_NONE:
        uart->Init.USART_Parity    = USART_Parity_No;
        break;
    case PARITY_ODD:
        uart->Init.USART_Parity    = USART_Parity_Odd;
        break;
    case PARITY_EVEN:
        uart->Init.USART_Parity    = USART_Parity_Even;
        break;
    default:
        uart->Init.USART_Parity     = USART_Parity_No;
        break;
    }

    /* UART hardware configuration, including clock and GPIO, etc. */
    RCC_APB2PeriphClockCmd(uart->hw_config->gpio_periph_clock, ENABLE);
    if(uart->config->Instance == USART1)
    {
        RCC_APB2PeriphClockCmd(uart->hw_config->uart_periph_clock, ENABLE);
    }
    else
    {
        RCC_APB1PeriphClockCmd(uart->hw_config->uart_periph_clock, ENABLE);
    }

    if(uart->hw_config->remap != GPIO_Remap_NONE)
    {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
        GPIO_PinRemapConfig(uart->hw_config->remap, ENABLE);
    }
    GPIO_InitStructure.GPIO_Pin = uart->hw_config->tx_gpio_pin;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(uart->hw_config->tx_gpio_port, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = uart->hw_config->rx_gpio_pin;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(uart->hw_config->rx_gpio_port, &GPIO_InitStructure);

    USART_Init(uart->config->Instance,&uart->Init);
    USART_Cmd(uart->config->Instance, ENABLE);

    return RT_EOK;
}

static rt_err_t ch32_control(struct rt_serial_device *serial, int cmd, void *arg)
{
    struct ch32_uart *uart;
    RT_ASSERT(serial != RT_NULL);
    uart = (struct ch32_uart *)serial->parent.user_data;
    switch (cmd)
    {
    /* disable interrupt */
    case RT_DEVICE_CTRL_CLR_INT:
        /* disable rx irq */
        NVIC_DisableIRQ(uart->config->irq_type);
        /* disable interrupt */
        USART_ITConfig(uart->config->Instance,USART_IT_RXNE,DISABLE);
        break;
    /* enable interrupt */
    case RT_DEVICE_CTRL_SET_INT:
        /* enable rx irq */
        NVIC_EnableIRQ(uart->config->irq_type);
        /* enable interrupt */
        USART_ITConfig(uart->config->Instance, USART_IT_RXNE,ENABLE);
        break;
    }
    return RT_EOK;
}

static int ch32_putc(struct rt_serial_device *serial, char c)
{
    struct ch32_uart *uart;
    RT_ASSERT(serial != RT_NULL);
    uart = (struct ch32_uart *)serial->parent.user_data;
    while (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_TC) == RESET);
    uart->config->Instance->DATAR = c;
    return 1;
}

static int ch32_getc(struct rt_serial_device *serial)
{
    int ch;
    struct ch32_uart *uart;
    RT_ASSERT(serial != RT_NULL);
    uart = (struct ch32_uart *)serial->parent.user_data;
    ch = -1;
    if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_RXNE) != RESET)
    {
        ch = uart->config->Instance->DATAR & 0xff;
    }
    return ch;
}

static rt_ssize_t ch32dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
{
    return -RT_EIO;
}

static void uart_isr(struct rt_serial_device *serial)
{
    struct ch32_uart *uart = (struct ch32_uart *) serial->parent.user_data;
    RT_ASSERT(uart != RT_NULL);
    if (USART_GetITStatus(uart->config->Instance, USART_IT_RXNE) != RESET)
    {
        rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
        USART_ClearITPendingBit(uart->config->Instance, USART_IT_RXNE);
    }
}

static const struct rt_uart_ops ch32_uart_ops =
{
    ch32_configure,
    ch32_control,
    ch32_putc,
    ch32_getc,
    ch32dma_transmit
};

#ifdef BSP_USING_UART1
#if defined (SOC_RISCV_SERIES_CH32V2)
void USART1_IRQHandler(void) __attribute__((interrupt()));
#else
void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
void USART1_IRQHandler(void)
{
    GET_INT_SP();
    rt_interrupt_enter();
    uart_isr(&(uart_obj[UART1_INDEX].serial));
    rt_interrupt_leave();
    FREE_INT_SP();
}
#endif

#ifdef BSP_USING_UART2
#if defined (SOC_RISCV_SERIES_CH32V2)
void USART2_IRQHandler(void) __attribute__((interrupt()));
#else
void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
void USART2_IRQHandler(void)
{
    GET_INT_SP();
    rt_interrupt_enter();
    uart_isr(&(uart_obj[UART2_INDEX].serial));
    rt_interrupt_leave();
    FREE_INT_SP();
}
#endif

#ifdef BSP_USING_UART3
#if defined (SOC_RISCV_SERIES_CH32V2)
void USART3_IRQHandler(void) __attribute__((interrupt()));
#else
void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
void USART3_IRQHandler(void)
{
    GET_INT_SP();
    rt_interrupt_enter();
    uart_isr(&(uart_obj[UART3_INDEX].serial));
    rt_interrupt_leave();
    FREE_INT_SP();
}
#endif

#ifdef BSP_USING_UART4
#if defined (SOC_RISCV_SERIES_CH32V2)
void UART4_IRQHandler(void) __attribute__((interrupt()));
#else
void UART4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
void UART4_IRQHandler(void)
{
    GET_INT_SP();
    rt_interrupt_enter();
    uart_isr(&(uart_obj[UART4_INDEX].serial));
    rt_interrupt_leave();
    FREE_INT_SP();
}
#endif

#ifdef BSP_USING_UART5
#if defined (SOC_RISCV_SERIES_CH32V2)
void UART5_IRQHandler(void) __attribute__((interrupt()));
#else
void UART5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
void UART5_IRQHandler(void)
{
    GET_INT_SP();
    rt_interrupt_enter();
    uart_isr(&(uart_obj[UART5_INDEX].serial));
    rt_interrupt_leave();
    FREE_INT_SP();
}
#endif

#ifdef BSP_USING_UART6
#if defined (SOC_RISCV_SERIES_CH32V2)
void UART6_IRQHandler(void) __attribute__((interrupt()));
#else
void UART6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
void UART6_IRQHandler(void)
{
    GET_INT_SP();
    rt_interrupt_enter();
    uart_isr(&(uart_obj[UART6_INDEX].serial));
    rt_interrupt_leave();
    FREE_INT_SP();
}
#endif

#ifdef BSP_USING_UART7
#if defined (SOC_RISCV_SERIES_CH32V2)
void UART7_IRQHandler(void) __attribute__((interrupt()));
#else
void UART7_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
void UART7_IRQHandler(void)
{
    GET_INT_SP();
    rt_interrupt_enter();
    uart_isr(&(uart_obj[UART7_INDEX].serial));
    rt_interrupt_leave();
    FREE_INT_SP();
}
#endif

#ifdef BSP_USING_UART8
#if defined (SOC_RISCV_SERIES_CH32V2)
void UART8_IRQHandler(void) __attribute__((interrupt()));
#else
void UART8_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
void UART8_IRQHandler(void)
{
    GET_INT_SP();
    rt_interrupt_enter();
    uart_isr(&(uart_obj[UART8_INDEX].serial));
    rt_interrupt_leave();
    FREE_INT_SP();
}
#endif

int rt_hw_usart_init(void)
{
    rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ch32_uart);
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
    rt_err_t result = 0;

    for (int i = 0; i < obj_num; i++)
    {
        /* init UART object */
        uart_obj[i].config        = &uart_config[i];
        uart_obj[i].hw_config     = &uart_hw_config[i];
        uart_obj[i].serial.ops    = &ch32_uart_ops;
        uart_obj[i].serial.config = config;
        /* Hardware initialization is required, otherwise it
        will not be registered into the device framework */
        if(uart_obj[i].hw_config->gpio_periph_clock == 0)
        {
            LOG_E("You did not perform hardware initialization for %s", uart_obj[i].config->name);
            continue;
        }
        if(uart_obj[i].hw_config->uart_periph_clock == 0)
        {
            LOG_E("You did not perform hardware initialization for %s", uart_obj[i].config->name);
            continue;
        }
        /* register UART device */
        result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
                                       RT_DEVICE_FLAG_RDWR
                                       | RT_DEVICE_FLAG_INT_RX
                                       , &uart_obj[i]);
        RT_ASSERT(result == RT_EOK);
    }

    return result;
}

#endif /* RT_USING_SERIAL */