summaryrefslogtreecommitdiff
path: root/bsp/amebaz/drivers/drv_uart.c
blob: 2ad6e12292ed424f55931550708c7babb6b44500 (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
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-05-30     Bernard      the first version
 * 2018-03-15     flyingcys    add amebaz
 */
#include <rtl8710b.h>
#include <serial_api.h>
#include "board.h"
#include "drv_uart.h"

struct device_uart
{
    serial_t serial;

    rt_uint32_t irqno;
};

extern int LOGUART_SetBaud(u32 BaudRate);

#ifdef BSP_USING_UART0
static struct rt_serial_device  serial0;
static struct device_uart       uart0;
#endif

static rt_err_t  ameba_uart_configure    (struct rt_serial_device *serial, struct serial_configure *cfg);
static rt_err_t  ameba_uart_control      (struct rt_serial_device *serial, int cmd, void *arg);
static int       ameba_uart_putc         (struct rt_serial_device *serial, char c);
static int       ameba_uart_getc         (struct rt_serial_device *serial);
static rt_ssize_t ameba_uart_dma_transmit (struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);

static void ameba_uart_irq(uint32_t id, SerialIrq event);

const struct rt_uart_ops _uart_ops =
{
    ameba_uart_configure,
    ameba_uart_control,
    ameba_uart_putc,
    ameba_uart_getc,
    ameba_uart_dma_transmit
};

/*
 * UART interface
 */
static rt_err_t ameba_uart_configure (struct rt_serial_device *serial, struct serial_configure *cfg)
{
     struct device_uart * uart;

     RT_ASSERT(serial != RT_NULL);
     serial->config = *cfg;

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

     /* Set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
     serial_format(&uart->serial, 8, ParityNone, 1);

     /* set baudrate */
     serial_baud(&uart->serial, 115200);

     return (RT_EOK);
}

static rt_err_t ameba_uart_control (struct rt_serial_device *serial, int cmd, void *arg)
{
    struct device_uart * uart;

    uart = serial->parent.user_data;

    RT_ASSERT(uart != RT_NULL);

    switch (cmd)
    {
    case RT_DEVICE_CTRL_CLR_INT:
        /* Disable the UART Interrupt */
        serial_irq_set(&uart->serial, RxIrq, 0);
        serial_irq_handler(&uart->serial, RT_NULL, 0);
        break;

    case RT_DEVICE_CTRL_SET_INT:
        /* install interrupt */
        serial_irq_handler(&uart->serial, ameba_uart_irq, (uint32_t)serial);

        /* Enable the UART Interrupt */
        serial_irq_set(&uart->serial, RxIrq, 1);
        break;
    }

    return (RT_EOK);
}

static int ameba_uart_putc (struct rt_serial_device *serial, char c)
{
    struct device_uart* uart;

    uart = serial->parent.user_data;

    /* FIFO status, contain valid data */
    /* write data */
    serial_putc(&uart->serial, c);

    return (1);
}

static int ameba_uart_getc (struct rt_serial_device *serial)
{
    struct device_uart* uart = serial->parent.user_data;

    if(!serial_readable(&uart->serial))
        return -1;

    /* Receive Data Available */
    return serial_getc(&uart->serial);
}

static rt_ssize_t ameba_uart_dma_transmit (struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
{
    return (0);
}

static void ameba_uart_irq(uint32_t id, SerialIrq event)
{
    struct rt_serial_device *serial = (struct rt_serial_device *)id;
    if(event == RxIrq)
    {
        rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
    }
    else if(event == TxIrq)
    {
    }
}

static rt_err_t dbg_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
static rt_err_t dbg_control(struct rt_serial_device *serial, int cmd, void *arg);
static int dbg_putc(struct rt_serial_device *serial, char c);
static int dbg_getc(struct rt_serial_device *serial);

static struct rt_serial_device ameba_dbg_serial;
const struct rt_uart_ops _ambed_dbg_ops =
{
    dbg_configure,
    dbg_control,
    dbg_putc,
    dbg_getc,
    RT_NULL,
};

static rt_err_t dbg_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
    LOGUART_SetBaud(115200);
    return RT_EOK;
}

void dbg_uart_irq_handler(void * data)
{
    u32 IrqEn = DiagGetIsrEnReg();

    DiagSetIsrEnReg(0);

    rt_hw_serial_isr(&ameba_dbg_serial, RT_SERIAL_EVENT_RX_IND);

    DiagSetIsrEnReg(IrqEn);
}

static rt_err_t dbg_control(struct rt_serial_device *serial, int cmd, void *arg)
{
    switch (cmd)
    {
    case RT_DEVICE_CTRL_CLR_INT:
        /* Disable the UART Interrupt */
        NVIC_DisableIRQ(UART_LOG_IRQ); /* this is rom_code_patch */
        break;

    case RT_DEVICE_CTRL_SET_INT:
        /* install interrupt */
        DIAG_UartReInit((IRQ_FUN) dbg_uart_irq_handler);
        /* Enable the UART Interrupt */
        NVIC_SetPriority(UART_LOG_IRQ, 10); /* this is rom_code_patch */
        break;
    }

    return (RT_EOK);
}

static int dbg_putc(struct rt_serial_device *serial, char c)
{
    DiagPutChar(c);

    return 1;
};

static int dbg_getc(struct rt_serial_device *serial)
{
    int c = -1;

    if(!UART_Readable(UART2_DEV))
        return -1;

    c = DiagGetChar(_FALSE);

    return c;
}

/*
 * UART Initiation
 */
int rt_hw_uart_init(void)
{
    struct rt_serial_device *serial;
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;

#ifdef BSP_USING_UART0
    {
        struct device_uart      *uart;

        serial  = &serial0;
        uart    = &uart0;

        /* Init UART Hardware */
        serial_init(&uart->serial, UART_TX, UART_RX);

        serial->ops              = &_uart_ops;
        serial->config           = config;
        serial->config.baud_rate = 115200;

        uart->irqno     = UART0_IRQ;

        rt_hw_serial_register(serial,
                              "uart0",
                              RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
                              uart);
    }
#endif

    {
        serial  = &ameba_dbg_serial;

        serial->ops = &_ambed_dbg_ops;
        serial->config = config;

        rt_hw_serial_register(serial,
                              RT_CONSOLE_DEVICE_NAME,
                              RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
                              RT_NULL);
    }
    return 0;
}