drv_usart_v2.c 4.5 KB
Newer Older
S
Sherman 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author            Notes
 * 2021-07-29     KyleChan          first version
 */

#include <drv_usart_v2.h>

#ifdef RT_USING_SERIAL_V2

//#define DRV_DEBUG
#define DBG_TAG              "drv.usart"
#ifdef DRV_DEBUG
S
Sherman 已提交
18
    #define DBG_LVL               DBG_LOG
S
Sherman 已提交
19
#else
S
Sherman 已提交
20
    #define DBG_LVL               DBG_INFO
S
Sherman 已提交
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
#endif /* DRV_DEBUG */
#include <rtdbg.h>

static struct ra_uart_config uart_config[] =
{
#ifdef BSP_USING_UART7
    UART7_CONFIG,
#endif

#ifdef BSP_USING_UART1
    UART1_CONFIG,
#endif
};

enum
{
#ifdef BSP_USING_UART7
    UART7_INDEX,
#endif

#ifdef BSP_USING_UART1
    UART1_INDEX,
#endif
};

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

static void ra_uart_get_config(void)
{
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;

#ifdef BSP_USING_UART7
    uart_obj[UART7_INDEX].serial.config = config;
    uart_obj[UART7_INDEX].uart_dma_flag = 0;

    uart_obj[UART7_INDEX].serial.config.rx_bufsz = BSP_UART7_RX_BUFSIZE;
    uart_obj[UART7_INDEX].serial.config.tx_bufsz = BSP_UART7_TX_BUFSIZE;
#endif

#ifdef BSP_USING_UART1
    uart_obj[UART1_INDEX].serial.config = config;
    uart_obj[UART1_INDEX].uart_dma_flag = 0;

    uart_obj[UART1_INDEX].serial.config.rx_bufsz = BSP_UART1_RX_BUFSIZE;
    uart_obj[UART1_INDEX].serial.config.tx_bufsz = BSP_UART1_TX_BUFSIZE;
#endif
}


/*
 * UART interface
 */
static rt_err_t ra_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
    struct ra_uart *uart;
    RT_ASSERT(serial != RT_NULL);
    RT_ASSERT(cfg != RT_NULL);

    fsp_err_t err = FSP_SUCCESS;

    uart = rt_container_of(serial, struct ra_uart, serial);
    RT_ASSERT(uart != RT_NULL);

S
Sherman 已提交
84
    err = R_SCI_UART_Open(uart->config->p_api_ctrl, uart->config->p_cfg);
S
Sherman 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    if (FSP_SUCCESS != err)
    {
        return RT_ERROR;
    }

    return RT_EOK;
}

static rt_err_t ra_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
{
    return RT_EOK;
}

static int ra_uart_putc(struct rt_serial_device *serial, char c)
{
    struct ra_uart *uart;
    RT_ASSERT(serial != RT_NULL);

    uart = rt_container_of(serial, struct ra_uart, serial);
    RT_ASSERT(uart != RT_NULL);

S
Sherman 已提交
106
    sci_uart_instance_ctrl_t *p_ctrl = (sci_uart_instance_ctrl_t *)uart->config->p_api_ctrl;
S
Sherman 已提交
107 108

    p_ctrl->p_reg->TDR = c;
S
Sherman 已提交
109
    while ((p_ctrl->p_reg->SSR_b.TEND) == 0);
S
Sherman 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    return RT_EOK;
}

static int ra_uart_getc(struct rt_serial_device *serial)
{
    return RT_EOK;
}


#ifdef BSP_USING_UART7
void uart7_isr_cb(uart_callback_args_t *p_args)
{
    rt_interrupt_enter();

    struct rt_serial_device *serial = &uart_obj[0].serial;
    RT_ASSERT(serial != RT_NULL);

S
Sherman 已提交
128
    if (UART_EVENT_RX_CHAR == p_args->event)
S
Sherman 已提交
129 130 131 132 133
    {
        struct rt_serial_rx_fifo *rx_fifo;
        rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
        RT_ASSERT(rx_fifo != RT_NULL);

S
Sherman 已提交
134
        rt_ringbuffer_putchar(&(rx_fifo->rb), (rt_uint8_t)p_args->data);
S
Sherman 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

        rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
    }

    rt_interrupt_leave();
}
#endif


#ifdef BSP_USING_UART1
void uart1_isr_cb(uart_callback_args_t *p_args)
{
    rt_interrupt_enter();

    struct rt_serial_device *serial = &uart_obj[1].serial;
    RT_ASSERT(serial != RT_NULL);

S
Sherman 已提交
152
    if (UART_EVENT_RX_CHAR == p_args->event)
S
Sherman 已提交
153 154 155 156 157
    {
        struct rt_serial_rx_fifo *rx_fifo;
        rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
        RT_ASSERT(rx_fifo != RT_NULL);

S
Sherman 已提交
158
        rt_ringbuffer_putchar(&(rx_fifo->rb), (rt_uint8_t)p_args->data);
S
Sherman 已提交
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

        rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
    }

    rt_interrupt_leave();
}
#endif


static const struct rt_uart_ops ra_uart_ops =
{
    .configure = ra_uart_configure,
    .control = ra_uart_control,
    .putc = ra_uart_putc,
    .getc = ra_uart_getc,
};


int rt_hw_usart_init(void)
{
    rt_err_t result = 0;
    rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ra_uart);

    ra_uart_get_config();
    for (int i = 0; i < obj_num; i++)
    {
        /* init UART object */
        uart_obj[i].config = &uart_config[i];
        uart_obj[i].serial.ops = &ra_uart_ops;
        /* register UART device */
        result = rt_hw_serial_register(&uart_obj[i].serial,
S
Sherman 已提交
190 191 192
                                       uart_obj[i].config->name,
                                       RT_DEVICE_FLAG_RDWR,
                                       NULL);
S
Sherman 已提交
193 194 195 196 197 198 199
        RT_ASSERT(result == RT_EOK);
    }

    return result;
}

#endif /* RT_USING_SERIAL_V2 */