drv_uart.c 7.6 KB
Newer Older
W
wangyq2018 已提交
1 2 3 4 5 6 7 8
/*
 * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2019-01-23     wangyq        the first version
9
 * 2019-11-01     wangyq        update libraries
W
wangyq2018 已提交
10 11 12 13 14 15
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
W
wangyq2018 已提交
16
#include "drv_uart.h"
W
wangyq2018 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <ald_gpio.h>
#include <ald_uart.h>

#ifdef RT_USING_SERIAL

/* es32 uart driver */
struct es32_uart
{
    uart_handle_t huart;
    IRQn_Type irq;
};

static rt_err_t es32f0x_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
W
wangyq2018 已提交
31
    gpio_init_t gpio_initstructure;
W
wangyq2018 已提交
32 33 34 35 36 37
    struct es32_uart *uart;
    RT_ASSERT(serial != RT_NULL);
    RT_ASSERT(cfg != RT_NULL);
    uart = (struct es32_uart *)serial->parent.user_data;

    /* Initialize tx pin */
W
wangyq2018 已提交
38 39 40 41 42 43
    gpio_initstructure.mode = GPIO_MODE_OUTPUT;
    gpio_initstructure.odos = GPIO_PUSH_PULL;
    gpio_initstructure.pupd = GPIO_PUSH_UP;
    gpio_initstructure.odrv = GPIO_OUT_DRIVE_NORMAL;
    gpio_initstructure.flt  = GPIO_FILTER_DISABLE;
    gpio_initstructure.type = GPIO_TYPE_TTL;
W
wangyq2018 已提交
44 45

#ifdef BSP_USING_UART0
W
wangyq2018 已提交
46
    gpio_initstructure.func = GPIO_FUNC_3;
47
    ald_gpio_init(GPIOB, GPIO_PIN_10, &gpio_initstructure);
W
wangyq2018 已提交
48

49
    /* Initialize rx pin ,the same as txpin except mode */
W
wangyq2018 已提交
50
    gpio_initstructure.mode = GPIO_MODE_INPUT;
51
    ald_gpio_init(GPIOB, GPIO_PIN_11, &gpio_initstructure);
W
wangyq2018 已提交
52 53 54
#endif /* uart0 gpio init */

#ifdef BSP_USING_UART1
W
wangyq2018 已提交
55
    gpio_initstructure.func = GPIO_FUNC_3;
56
    ald_gpio_init(GPIOC, GPIO_PIN_10, &gpio_initstructure);
W
wangyq2018 已提交
57

58
    /* Initialize rx pin ,the same as txpin except mode */
W
wangyq2018 已提交
59
    gpio_initstructure.mode = GPIO_MODE_INPUT;
60
    ald_gpio_init(GPIOC, GPIO_PIN_11, &gpio_initstructure);
W
wangyq2018 已提交
61 62 63
#endif /* uart1 gpio init */

#ifdef BSP_USING_UART2
W
wangyq2018 已提交
64
    gpio_initstructure.func = GPIO_FUNC_5;
65
    ald_gpio_init(GPIOC, GPIO_PIN_12, &gpio_initstructure);
W
wangyq2018 已提交
66

67
    /* Initialize rx pin ,the same as txpin except mode */
W
wangyq2018 已提交
68
    gpio_initstructure.mode = GPIO_MODE_INPUT;
69
    ald_gpio_init(GPIOD, GPIO_PIN_2, &gpio_initstructure);
W
wangyq2018 已提交
70 71 72
#endif /* uart2 gpio init */

#ifdef BSP_USING_UART3
W
wangyq2018 已提交
73
    gpio_initstructure.func = GPIO_FUNC_4;
74
    ald_gpio_init(GPIOC, GPIO_PIN_4, &gpio_initstructure);
W
wangyq2018 已提交
75

76
    /* Initialize rx pin ,the same as txpin except mode */
W
wangyq2018 已提交
77
    gpio_initstructure.mode = GPIO_MODE_INPUT;
78
    ald_gpio_init(GPIOC, GPIO_PIN_5, &gpio_initstructure);
W
wangyq2018 已提交
79 80 81 82 83 84 85
#endif /* uart3 gpio init */

    uart->huart.init.mode        = UART_MODE_UART;
    uart->huart.init.baud        = cfg->baud_rate;
    uart->huart.init.word_length = (uart_word_length_t)(cfg->data_bits - 5);
    uart->huart.init.parity = (uart_parity_t)(cfg->parity == PARITY_EVEN ? UART_PARITY_EVEN : cfg->parity);
    uart->huart.init.fctl        = UART_HW_FLOW_CTL_DISABLE;
86
    ald_uart_init(&uart->huart);
W
wangyq2018 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    if (cfg->bit_order == BIT_ORDER_MSB)
    {
        UART_MSB_FIRST_ENABLE(&uart->huart);
    }
    else
    {
        UART_MSB_FIRST_DISABLE(&uart->huart);
    }

    if (cfg->invert == NRZ_INVERTED)
    {
        UART_DATA_INV_ENABLE(&uart->huart);
    }
    else
    {
        UART_DATA_INV_DISABLE(&uart->huart);
    }

106
    /* enable rx int */
107
    ald_uart_interrupt_config(&uart->huart, UART_IT_RXRD, ENABLE);
W
wangyq2018 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

    return RT_EOK;
}

static rt_err_t es32f0x_control(struct rt_serial_device *serial, int cmd, void *arg)
{
    struct es32_uart *uart;
    RT_ASSERT(serial != RT_NULL);

    uart = (struct es32_uart *)serial->parent.user_data;
    switch (cmd)
    {
    case RT_DEVICE_CTRL_CLR_INT:
        /* disable rx irq */
        NVIC_DisableIRQ(uart->irq);
        /* disable interrupt */
124
        ald_uart_interrupt_config(&uart->huart, UART_IT_RXRD, DISABLE);
W
wangyq2018 已提交
125 126 127 128 129 130
        break;

    case RT_DEVICE_CTRL_SET_INT:
        /* enable rx irq */
        NVIC_EnableIRQ(uart->irq);
        /* enable interrupt */
131
        ald_uart_interrupt_config(&uart->huart, UART_IT_RXRD, ENABLE);
W
wangyq2018 已提交
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
        break;
    }

    return RT_EOK;
}

static int es32f0x_putc(struct rt_serial_device *serial, char c)
{
    struct es32_uart *uart;
    RT_ASSERT(serial != RT_NULL);
    uart = (struct es32_uart *)serial->parent.user_data;

    while (!(uart->huart.perh->SR & 0x40)) ;
    WRITE_REG(uart->huart.perh->TBR, c);

    return 1;
}

static int es32f0x_getc(struct rt_serial_device *serial)
{
    int ch = -1;
    struct es32_uart *uart;

    RT_ASSERT(serial != RT_NULL);
    uart = (struct es32_uart *)serial->parent.user_data;

    if (uart->huart.perh->SR & 0x01)
    {
        ch = (uint8_t)(uart->huart.perh->RBR & 0xFF);
    }

    return ch;
}

static const struct rt_uart_ops es32f0x_uart_ops =
{
    es32f0x_configure,
    es32f0x_control,
    es32f0x_putc,
    es32f0x_getc,
};

#ifdef BSP_USING_UART0
/* UART0 device driver structure */
struct es32_uart uart0 =
{
    {UART0},
    UART0_IRQn
};

struct rt_serial_device serial0;

void UART0_Handler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    if (UART0->RIF & 0x01)
    {
        rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* BSP_USING_UART0 */

#ifdef BSP_USING_UART1
/* UART1 device driver structure */
struct es32_uart uart1 =
{
    {UART1},
    UART1_IRQn
};

struct rt_serial_device serial1;

void UART1_Handler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    if (UART1->RIF & 0x01)
    {
        rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* BSP_USING_UART1 */

#ifdef BSP_USING_UART2
/* UART2 device driver structure */
struct es32_uart uart2 =
{
    {UART2},
    BS16T1_UART2_IRQn
};

struct rt_serial_device serial2;

void BS16T1_UART2_Handler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    if (UART2->RIF & 0x01)
    {
        rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* BSP_USING_UART2 */

#ifdef BSP_USING_UART3
/* UART3 device driver structure */
struct es32_uart uart3 =
{
    {UART3},
    BS16T2_UART3_IRQn
};

struct rt_serial_device serial3;

void BS16T2_UART3_Handler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    if (UART3->RIF & 0x01)
    {
        rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
    }
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* BSP_USING_UART3 */

W
wangyq2018 已提交
270
int rt_hw_uart_init(void)
W
wangyq2018 已提交
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
{
    struct es32_uart *uart;
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;

#ifdef BSP_USING_UART0
    uart = &uart0;
    serial0.ops = &es32f0x_uart_ops;
    serial0.config = config;

    /* register UART0 device */
    rt_hw_serial_register(&serial0, "uart0",
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
                          uart);
#endif /* BSP_USING_UART0 */

#ifdef BSP_USING_UART1
    uart = &uart1;
    serial1.ops = &es32f0x_uart_ops;
    serial1.config = config;

    /* register UART1 device */
    rt_hw_serial_register(&serial1, "uart1",
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
                          uart);
#endif /* BSP_USING_UART1 */

#ifdef BSP_USING_UART2
    uart = &uart2;
    serial2.ops = &es32f0x_uart_ops;
    serial2.config = config;

    /* register UART2 device */
    rt_hw_serial_register(&serial2, "uart2",
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
                          uart);
#endif /* BSP_USING_UART2 */

#ifdef BSP_USING_UART3
    uart = &uart3;
    serial3.ops = &es32f0x_uart_ops;
    serial3.config = config;

    /* register UART3 device */
    rt_hw_serial_register(&serial3, "uart3",
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
                          uart);
#endif /* BSP_USING_UART3 */

    return 0;
}
W
wangyq2018 已提交
321
INIT_BOARD_EXPORT(rt_hw_uart_init);
W
wangyq2018 已提交
322 323

#endif