summaryrefslogtreecommitdiff
path: root/bsp/asm9260t/drivers/usart.c
blob: 3a17f97b382bf3d2d2941c24d7d89b2ffb3c8411 (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
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-01-13     weety       first version
 * 2013-07-21     weety       using serial component
 * 2015-05-02     ArdaFu      Port from AT91SAM9260 BSP
 */

#include <rtthread.h>
#include <rthw.h>
#include <rtdevice.h>
#include "interrupt.h"
#include <asm9260t.h>
#include "gpio.h"

#include "uart.h"

typedef struct
{
    HW_USART_TypeDef *port;
    int irq;
} asm_uart_t;


/**
 * This function will handle serial
 */
void rt_asm_usart_handler(int vector, void *param)
{
    rt_uint32_t status;
    asm_uart_t *uart;
    rt_device_t dev = (rt_device_t)param;
    uart = (asm_uart_t *)dev->user_data;
    status = uart->port->INTR[R_VAL];

    if(!(status & (ASM_UART_INTR_RXIS | ASM_UART_INTR_RTIS)))
        return;
    uart->port->INTR[R_CLR] = ASM_UART_INTR_RXIS|ASM_UART_INTR_RTIS;
    //rt_interrupt_enter();
    rt_hw_serial_isr((struct rt_serial_device *)dev, RT_SERIAL_EVENT_RX_IND);
    //rt_interrupt_leave();

}


/**
* UART device in RT-Thread
*/
static rt_err_t asm_usart_configure(struct rt_serial_device *serial,
                                    struct serial_configure *cfg)
{
    asm_uart_t *uart;

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

    Hw_UartDisable(uart->port);

    Hw_UartReset(uart->port);

    Hw_UartConfig(uart->port, cfg->baud_rate, cfg->data_bits,
                  cfg->stop_bits, cfg->parity);

    Hw_UartEnable(uart->port);

    return RT_EOK;
}

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

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

    switch (cmd)
    {
    case RT_DEVICE_CTRL_CLR_INT:
        /* disable rx irq */
        rt_hw_interrupt_mask(uart->irq);
        break;
    case RT_DEVICE_CTRL_SET_INT:
        /* enable rx irq */
        rt_hw_interrupt_umask(uart->irq);
        break;
    }

    return RT_EOK;
}


static int asm_usart_putc(struct rt_serial_device *serial, char c)
{
    //rt_uint32_t level;
    asm_uart_t *uart = serial->parent.user_data;

    while ((uart->port->STAT[R_VAL] & ASM_UART_STAT_TXFF));
    uart->port->DATA[R_VAL] = c;

    return 1;
}

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

    if (uart->port->STAT[R_VAL] & ASM_UART_STAT_RXFE)
        return -1;
    return  uart->port->DATA[R_VAL] & 0xff;
}

static const struct rt_uart_ops asm_usart_ops =
{
    asm_usart_configure,
    asm_usart_control,
    asm_usart_putc,
    asm_usart_getc,
};


#if defined(RT_USING_UART0)
static struct rt_serial_device serial0;
asm_uart_t uart0 =
{
    USART0,
    INT_UART0
};
#endif

#if defined(RT_USING_UART3)
static struct rt_serial_device serial3;
asm_uart_t uart3 =
{
    USART3,
    INT_UART3
};
#endif

#if defined(RT_USING_UART4)
static struct rt_serial_device serial4;
asm_uart_t uart4 =
{
    USART4,
    INT_UART4
};
#endif

//USART0 PINS TX=GP14_0:5 , RX=GP14_1:5
//USART3 PINS TX=GP8_6:2 , RX=GP8_7:2
//USART4 PINS TX=GP3_0:2 , RX=GP3_1:2

void asm_usart_gpio_init(void)
{
    // enable IOCONFIG GPIO
    outl(((1UL<<25) | (1UL<<4)) ,REG_SET(HW_AHBCLKCTRL0));
#ifdef RT_USING_UART0
    HW_SetPinMux(14, 0, 5);
    HW_SetPinMux(14, 1, 5);
#endif

#ifdef RT_USING_UART3
    HW_SetPinMux(8, 6, 2);
    HW_SetPinMux(8, 7, 2);
#endif

#ifdef RT_USING_UART4
    HW_SetPinMux(3, 0, 2);
    HW_SetPinMux(3, 1, 2);
#endif
}

void asm_serial_config_set_default(struct rt_serial_device* serial)
{
    serial->ops = &asm_usart_ops;

    serial->config.baud_rate = BAUD_RATE_115200;
    serial->config.bit_order = BIT_ORDER_LSB;
    serial->config.data_bits = DATA_BITS_8;
    serial->config.parity    = PARITY_NONE;
    serial->config.stop_bits = STOP_BITS_1;
    serial->config.invert    = NRZ_NORMAL;
    serial->config.bufsz     = RT_SERIAL_RB_BUFSZ;
}
#define DRV_REG_OPS (RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM)
/**
 * This function will handle init uart
 */
int rt_hw_uart_init(void)
{
    asm_usart_gpio_init();

#if defined(RT_USING_UART0)
    Hw_UartInit(0);
    asm_serial_config_set_default(&serial0);

    /* register uart device */
    rt_hw_serial_register(&serial0, "uart0", DRV_REG_OPS, &uart0);
    rt_hw_interrupt_install(uart0.irq, rt_asm_usart_handler,
                            (void *)&(serial0.parent), "UART0");
    rt_hw_interrupt_umask(uart0.irq);
#endif

#if defined(RT_USING_UART3)
    Hw_UartInit(3);
    asm_serial_config_set_default(&serial3);

    /* register uart device */
    rt_hw_serial_register(&serial3, "uart3",  DRV_REG_OPS, &uart3);
    rt_hw_interrupt_install(uart3.irq, rt_asm_usart_handler,
                            (void *)&(serial3.parent), "UART3");
    rt_hw_interrupt_umask(uart3.irq);
#endif

#if defined(RT_USING_UART4)
    Hw_UartInit(4);
    asm_serial_config_set_default(&serial4);

    /* register uart device */
    rt_hw_serial_register(&serial4, "uart4", DRV_REG_OPS, &uart4);
    rt_hw_interrupt_install(uart4.irq, rt_asm_usart_handler,
                            (void *)&(serial4.parent), "UART4");
    rt_hw_interrupt_umask(uart4.irq);
#endif

    return 0;
}
INIT_BOARD_EXPORT(rt_hw_uart_init);