|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, ATL Electronics |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#define DT_DRV_COMPAT gd_gd32_usart |
| 7 | + |
| 8 | +#include <drivers/uart.h> |
| 9 | + |
| 10 | +struct gd32_usart_config { |
| 11 | + uint32_t reg; |
| 12 | + uint32_t rcu_periph_clock; |
| 13 | +}; |
| 14 | + |
| 15 | +struct gd32_usart_data { |
| 16 | + uint32_t baud_rate; |
| 17 | +}; |
| 18 | + |
| 19 | +static int usart_gd32_init(const struct device *dev) |
| 20 | +{ |
| 21 | + const struct gd32_usart_config *const cfg = dev->config; |
| 22 | + struct gd32_usart_data *const data = dev->data; |
| 23 | + |
| 24 | + /* NOTE: pins are configured at board_init till pinctrl be available */ |
| 25 | + |
| 26 | + rcu_periph_clock_enable(cfg->rcu_periph_clock); |
| 27 | + usart_deinit(cfg->reg); |
| 28 | + usart_baudrate_set(cfg->reg, data->baud_rate); |
| 29 | + usart_word_length_set(cfg->reg, USART_WL_8BIT); |
| 30 | + usart_parity_config(cfg->reg, USART_PM_NONE); |
| 31 | + usart_stop_bit_set(cfg->reg, USART_STB_1BIT); |
| 32 | + usart_parity_config(cfg->reg, USART_PM_NONE); |
| 33 | + usart_receive_config(cfg->reg, USART_RECEIVE_ENABLE); |
| 34 | + usart_transmit_config(cfg->reg, USART_TRANSMIT_ENABLE); |
| 35 | + usart_enable(cfg->reg); |
| 36 | + |
| 37 | + return 0; |
| 38 | +} |
| 39 | + |
| 40 | +static int usart_gd32_poll_in(const struct device *dev, unsigned char *c) |
| 41 | +{ |
| 42 | + const struct gd32_usart_config *const cfg = dev->config; |
| 43 | + uint32_t status; |
| 44 | + |
| 45 | + status = usart_flag_get(cfg->reg, USART_FLAG_RBNE); |
| 46 | + |
| 47 | + if (!status) { |
| 48 | + return -EPERM; |
| 49 | + } |
| 50 | + |
| 51 | + *c = usart_data_receive(cfg->reg); |
| 52 | + |
| 53 | + return 0; |
| 54 | +} |
| 55 | + |
| 56 | +static void usart_gd32_poll_out(const struct device *dev, unsigned char c) |
| 57 | +{ |
| 58 | + const struct gd32_usart_config *const cfg = dev->config; |
| 59 | + |
| 60 | + usart_data_transmit(cfg->reg, c); |
| 61 | + |
| 62 | + while (usart_flag_get(cfg->reg, USART_FLAG_TBE) == RESET) { |
| 63 | + ; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +static int usart_gd32_err_check(const struct device *dev) |
| 68 | +{ |
| 69 | + const struct gd32_usart_config *const cfg = dev->config; |
| 70 | + uint32_t status = USART_STAT0(cfg->reg); |
| 71 | + int errors = 0; |
| 72 | + |
| 73 | + if (status & USART_FLAG_ORERR) { |
| 74 | + usart_flag_clear(cfg->reg, USART_FLAG_ORERR); |
| 75 | + |
| 76 | + errors |= UART_ERROR_OVERRUN; |
| 77 | + } |
| 78 | + |
| 79 | + if (status & USART_FLAG_PERR) { |
| 80 | + usart_flag_clear(cfg->reg, USART_FLAG_PERR); |
| 81 | + |
| 82 | + errors |= UART_ERROR_PARITY; |
| 83 | + } |
| 84 | + |
| 85 | + if (status & USART_FLAG_FERR) { |
| 86 | + usart_flag_clear(cfg->reg, USART_FLAG_FERR); |
| 87 | + |
| 88 | + errors |= UART_ERROR_FRAMING; |
| 89 | + } |
| 90 | + |
| 91 | + usart_flag_clear(cfg->reg, USART_FLAG_NERR); |
| 92 | + |
| 93 | + return errors; |
| 94 | +} |
| 95 | + |
| 96 | +static const struct uart_driver_api usart_gd32_driver_api = { |
| 97 | + .poll_in = usart_gd32_poll_in, |
| 98 | + .poll_out = usart_gd32_poll_out, |
| 99 | + .err_check = usart_gd32_err_check, |
| 100 | +}; |
| 101 | + |
| 102 | +#define GD32_USART_INIT(n) \ |
| 103 | + static struct gd32_usart_data usart##n##_gd32_data = { \ |
| 104 | + .baud_rate = DT_INST_PROP(n, current_speed), \ |
| 105 | + }; \ |
| 106 | + static const struct gd32_usart_config usart##n##_gd32_config = { \ |
| 107 | + .reg = DT_INST_REG_ADDR(n), \ |
| 108 | + .rcu_periph_clock = DT_INST_PROP(n, rcu_periph_clock), \ |
| 109 | + }; \ |
| 110 | + DEVICE_DT_INST_DEFINE(n, &usart_gd32_init, \ |
| 111 | + NULL, \ |
| 112 | + &usart##n##_gd32_data, \ |
| 113 | + &usart##n##_gd32_config, PRE_KERNEL_1, \ |
| 114 | + CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \ |
| 115 | + &usart_gd32_driver_api); |
| 116 | + |
| 117 | +DT_INST_FOREACH_STATUS_OKAY(GD32_USART_INIT) |
0 commit comments