summaryrefslogtreecommitdiff
path: root/bsp/ck802/drivers/board_uart.c
blob: 3d6f20e820af2d9af3b087fd0db7081518ef4ef1 (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
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-09-23     Urey         first implementation
 */

#include <rtthread.h>

#ifdef RT_USING_CONSOLE

#include <rthw.h>
#include <rtdevice.h>

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#include "board.h"
#include "soc.h"
#include "dw_usart.h"
#include "drv_usart.h"
#include "pin_name.h"
#include "pinmux.h"

#if CONFIG_USART
struct
{
    uint32_t base;
    uint32_t irq;
} const sg_usart_config[CONFIG_USART_NUM] =
{
    { CSKY_UART0_BASE, UART0_IRQn },
    { CSKY_UART1_BASE, UART1_IRQn },
    { CSKY_UART2_BASE, UART2_IRQn },
    { CSKY_UART3_BASE, UART3_IRQn }
};

typedef struct
{
    pin_t    tx;
    pin_t    rx;
    uint16_t cfg_idx;    //idx of sg_usart_config[]
    uint16_t function;
} usart_pin_map_t;
const static usart_pin_map_t s_usart_pin_map[] =
{
    {
        PA0_TXD0_PWM0_XX_SIROUT0,
        PA1_RXD0_PWM1_XX_SIRIN0,
        0,
        0
    },
    {
        PA10_TXD1_PWM1_XX_SIROUT1,
        PA11_RXD1_PWM2_XX_SIRIN1,
        1,
        0
    },
    {
        PA23_TXD2_PWM5_XX_SIROUT2,
        PA22_RXD2_PWM4_XX_SIRIN2,
        2,
        0
    },
    {
        PA26_TXD3_PWMFAULT_XX_SIROUT3,
        PA27_RXD3_PWM0_XX_SIRIN3,
        3,
        0
    }
};

int32_t target_usart_init(pin_t tx, pin_t rx, uint32_t *base, uint32_t *irq)
{
    uint32_t idx;

    for (idx = 0; idx < sizeof(s_usart_pin_map) / sizeof(usart_pin_map_t); idx++)
    {
        if (s_usart_pin_map[idx].tx == tx && s_usart_pin_map[idx].rx == rx)
        {
            *base = sg_usart_config[s_usart_pin_map[idx].cfg_idx].base;
            *irq = sg_usart_config[s_usart_pin_map[idx].cfg_idx].irq;

            /*pinmux*/
            pin_mux(s_usart_pin_map[idx].tx, s_usart_pin_map[idx].function);
            pin_mux(s_usart_pin_map[idx].rx, s_usart_pin_map[idx].function);
            return s_usart_pin_map[idx].cfg_idx;
        }
    }
    return -1;
}
#endif

#ifdef RT_USING_UART1
#define UART_TXD1     PA10_TXD1_PWM1_XX_SIROUT1
#define UART_RXD1     PA11_RXD1_PWM2_XX_SIRIN1

static  usart_handle_t uart1_handle;
static struct rt_serial_device  serial1;
/*
static void usart1_event_cb(uint32_t event, void *cb_arg)
{
    switch (event)
    {
    case USART_EVENT_SEND_COMPLETE:
        rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_TX_DONE);
        break;

    case USART_EVENT_RECEIVED:
        rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_RX_IND);
        break;

    default:
        break;
    }
}
*/

__attribute__((isr)) void USART1_IRQHandler(void)
{
    rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_RX_IND);
}
#endif

/*
 * UART interface
 */
static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
    int ret;
    usart_handle_t uart;

    uint32_t bauds;
    usart_mode_e mode;
    usart_parity_e parity;
    usart_stop_bits_e stopbits;
    usart_data_bits_e databits;

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

    /* set baudrate parity...*/
    bauds = cfg->baud_rate;
    mode = USART_MODE_ASYNCHRONOUS;

    if (cfg->parity == PARITY_EVEN)
        parity = USART_PARITY_EVEN;
    else if (cfg->parity == PARITY_ODD)
        parity = USART_PARITY_ODD;
    else
        parity = USART_PARITY_NONE;

    stopbits = USART_STOP_BITS_1 ;
    databits = USART_DATA_BITS_8;

    ret = csi_usart_config(uart, SYSTEM_CLOCK, bauds, USART_MODE_ASYNCHRONOUS, parity, stopbits, databits);

    if (ret < 0)
    {
        return -RT_ERROR;
    }

    return RT_EOK;
}

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

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

    switch (cmd)
    {
    case RT_DEVICE_CTRL_CLR_INT:
        /* Disable the UART Interrupt */
        dw_usart_clr_int_flag(uart,IER_RDA_INT_ENABLE);
        break;

    case RT_DEVICE_CTRL_SET_INT:
        /* Enable the UART Interrupt */
        dw_usart_set_int_flag(uart,IER_RDA_INT_ENABLE);
        break;
    }

    return (RT_EOK);
}

static int uart_putc(struct rt_serial_device *serial, char c)
{
    usart_handle_t uart;

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

    dw_usart_putchar(uart,c);

    return (1);
}

static int uart_getc(struct rt_serial_device *serial)
{
    uint8_t ch;
    usart_handle_t uart;

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

    if (!dw_usart_getchar_no_poll(uart, &ch))
    {
        return (int)(ch);
    }
    else
    {
        return -1;
    }
}

const struct rt_uart_ops _uart_ops =
{
    uart_configure,
    uart_control,
    uart_putc,
    uart_getc,
};


int rt_hw_usart_init(void)
{
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;

#ifdef RT_USING_UART1
    serial1.ops                 = & _uart_ops;
    serial1.config              = config;
    serial1.config.bufsz        = 2048;
    serial1.config.baud_rate    = 115200;

    uart1_handle = csi_usart_initialize(UART_TXD1, UART_RXD1, NULL/*usart1_event_cb*/,
                                        (void *) 0);

    rt_hw_serial_register(&serial1,
                          "uart1",
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
                          uart1_handle);
#endif

    return 0;
}
INIT_BOARD_EXPORT(rt_hw_usart_init);
#endif