summaryrefslogtreecommitdiff
path: root/bsp/fh8620/drivers/uart.c
blob: 4b264d9206f05ef1bfb535e56f0347800b799456 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 *  This file is part of FH8620 BSP for RT-Thread distribution.
 *
 *	Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd. 
 *	All rights reserved
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *	Visit http://www.fullhan.com to get contact with Fullhan.
 *
 * Change Logs:
 * Date           Author       Notes
 */
 
#include <board.h>
#include <rtdevice.h>
#include "fh_arch.h"
#include "libraries/inc/fh_uart.h"


void rt_fh_uart_handler(int vector, void *param)
{
	int status;
	unsigned int ret;
	struct fh_uart *uart;
	unsigned int reg_status;
	rt_device_t dev = (rt_device_t)param;
	uart = (struct fh_uart *)dev->user_data;
	status = uart_get_iir_status(uart->uart_port);
	if (status & UART_IIR_NOINT)
	{
		return;
	}
	if(status & UART_IIR_THREMPTY){
		//first close tx isr
		uart_disable_irq(uart->uart_port,UART_IER_ETBEI);

		rt_hw_serial_isr((struct rt_serial_device *)dev, RT_SERIAL_EVENT_TX_DONE);
	}
	else if((status & UART_IIR_CHRTOUT)==UART_IIR_CHRTOUT){
		//bug....
		//if no data in rx fifo
		reg_status = uart_get_status(uart->uart_port);
		if((reg_status & 1<<3) == 0)
			ret = uart_getc(uart->uart_port);
	}
	else{
		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 fh_uart_configure(struct rt_serial_device *serial,
                                struct serial_configure *cfg)
{
	int div;
	enum data_bits data_mode;
	enum stop_bits stop_mode;
	enum parity parity_mode;
	struct fh_uart *uart;

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

	switch (cfg->data_bits)
	{
	case DATA_BITS_8:
		data_mode = UART_DATA_BIT8;
		break;
	case DATA_BITS_7:
		data_mode = UART_DATA_BIT7;
		break;
	case DATA_BITS_6:
		data_mode = UART_DATA_BIT6;
		break;
	case DATA_BITS_5:
		data_mode = UART_DATA_BIT5;
		break;
	default:
		data_mode = UART_DATA_BIT8;
		break;
	}

	switch (cfg->stop_bits)
	{
	case STOP_BITS_2:
		stop_mode = UART_STOP_BIT2;
		break;
	case STOP_BITS_1:
	default:
		stop_mode = UART_STOP_BIT1;
		break;
	}

	switch (cfg->parity)
	{
	case PARITY_ODD:
		parity_mode = UART_PARITY_ODD;
		break;
	case PARITY_EVEN:
		parity_mode = UART_PARITY_EVEN;
		break;
	case PARITY_NONE:
	default:
		parity_mode = UART_PARITY_NONE;
		break;
	}

    uart_disable_irq(uart->uart_port, UART_IER_ERBFI);

	uart_configure(uart->uart_port, data_mode,
					stop_mode, parity_mode,
					cfg->baud_rate, UART_CLOCK_FREQ);

	uart_enable_irq(uart->uart_port, UART_IER_ERBFI);

    return RT_EOK;
}

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

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

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

    return RT_EOK;
}

static int fh_uart_putc(struct rt_serial_device *serial, char c)
{
	struct fh_uart *uart = serial->parent.user_data;
	unsigned int ret;
	ret = uart_get_status(uart->uart_port);
	if(serial->parent.open_flag & RT_DEVICE_FLAG_INT_TX){
		//RT_DEVICE_FLAG_INT_TX

		if(c == '\n'){
			fh_uart_putc(serial,'\r');
		}
		if(ret & UART_USR_TFNF){
			uart_putc(uart->uart_port, c);
			return 1;
		}
		//open tx isr here..
		uart_enable_irq(uart->uart_port,UART_IER_ETBEI);
	    return -1;
	}
	//poll mode
	else{

		while(!(uart_get_status(uart->uart_port) & UART_USR_TFNF))
			;
		uart_putc(uart->uart_port, c);
		return 1;


	}



}

static int fh_uart_getc(struct rt_serial_device *serial)
{
    int result;
	struct fh_uart *uart = serial->parent.user_data;

	if (uart_is_rx_ready(uart->uart_port))
	{
		result = uart_getc(uart->uart_port);
	}
	else
	{
		result = -1;
	}

    return result;
}

static const struct rt_uart_ops fh_uart_ops =
{
    fh_uart_configure,
    fh_uart_control,
    fh_uart_putc,
    fh_uart_getc,
};


#if defined(RT_USING_UART0)
static struct rt_serial_device serial0;
struct fh_uart uart0 = {
	(uart *)UART0_REG_BASE,
	UART0_IRQn
};

#endif

#if defined(RT_USING_UART1)
static struct rt_serial_device serial1;
struct fh_uart uart1 = {
	(uart *)UART1_REG_BASE,
	UART1_IRQn
};

#endif




/**
 * This function will handle init uart
 */
void rt_hw_uart_init(void)
{
	struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;

#if defined(RT_USING_UART0)
#if(0)
	serial0.ops = &fh_uart_ops;
	serial0.config = config;

	/* register vcom device */
	rt_hw_serial_register(&serial0, "uart0",
			RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_STANDALONE,
			&uart0);
	rt_hw_interrupt_install(uart0.irq, rt_fh_uart_handler,
			(void *)&(serial0.parent), "UART0");
	rt_hw_interrupt_umask(uart0.irq);
#endif
	serial0.ops = &fh_uart_ops;
	serial0.config = config;

	/* register vcom device */
	rt_hw_serial_register(&serial0, "uart0",
			RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM ,
			&uart0);
	rt_hw_interrupt_install(uart0.irq, rt_fh_uart_handler,
			(void *)&(serial0.parent), "UART0");
	rt_hw_interrupt_umask(uart0.irq);

#endif

#if defined(RT_USING_UART1)
	serial1.ops = &fh_uart_ops;
	serial1.config = config;

	/* register vcom device */
	rt_hw_serial_register(&serial1, "uart1",
			RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM ,
			&uart1);
	rt_hw_interrupt_install(uart1.irq, rt_fh_uart_handler,
			(void *)&(serial1.parent), "UART1");
	rt_hw_interrupt_umask(uart1.irq);

#endif

}