serial.c
9.07 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <rthw.h>
#include <rtthread.h>
#include "io.h"
#include <asm/ppc4xx-intvec.h>
#define UART0_BASE 0xef600300
#define UART1_BASE 0xef600400
#define UCR0_MASK 0x0000007f
#define UCR1_MASK 0x00007f00
#define UCR0_UDIV_POS 0
#define UCR1_UDIV_POS 8
#define UDIV_MAX 127
#define UART_RBR 0x00
#define UART_THR 0x00
#define UART_IER 0x01
#define UART_IIR 0x02
#define UART_FCR 0x02
#define UART_LCR 0x03
#define UART_MCR 0x04
#define UART_LSR 0x05
#define UART_MSR 0x06
#define UART_SCR 0x07
#define UART_DLL 0x00
#define UART_DLM 0x01
/*-----------------------------------------------------------------------------+
| Line Status Register.
+-----------------------------------------------------------------------------*/
#define asyncLSRDataReady1 0x01
#define asyncLSROverrunError1 0x02
#define asyncLSRParityError1 0x04
#define asyncLSRFramingError1 0x08
#define asyncLSRBreakInterrupt1 0x10
#define asyncLSRTxHoldEmpty1 0x20
#define asyncLSRTxShiftEmpty1 0x40
#define asyncLSRRxFifoError1 0x80
/* PPC405 serial device */
struct rt_ppc405_serial
{
/* inherit from device */
struct rt_device parent;
rt_uint32_t hw_base;
rt_uint32_t irqno;
rt_uint32_t baudrate;
/* reception field */
rt_uint16_t save_index, read_index;
rt_uint8_t rx_buffer[RT_UART_RX_BUFFER_SIZE];
};
struct rt_ppc405_serial ppc405_serial;
/* serial character device */
static rt_err_t rt_serial_init (rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag)
{
struct rt_ppc405_serial* device;
device = (struct rt_ppc405_serial*) dev;
RT_ASSERT(device != RT_NULL);
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
{
/* Enable "RX Data Available" Interrupt on UART */
out_8((rt_uint8_t*)device->hw_base + UART_IER, 0x01);
/* Setup UART FIFO: RX trigger level: 1 byte, Enable FIFO */
out_8((rt_uint8_t*)device->hw_base + UART_FCR, 1);
/* init UART rx interrupt */
rt_hw_interrupt_unmask(device->irqno);
}
return RT_EOK;
}
static rt_err_t rt_serial_close(rt_device_t dev)
{
struct rt_ppc405_serial* device;
device = (struct rt_ppc405_serial*) dev;
RT_ASSERT(device != RT_NULL);
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
{
/* mask UART rx interrupt */
rt_hw_interrupt_mask(device->irqno);
}
return RT_EOK;
}
static rt_err_t rt_serial_control(rt_device_t dev, int cmd, void *args)
{
return RT_EOK;
}
static rt_size_t rt_serial_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
rt_uint8_t* ptr;
struct rt_ppc405_serial* device;
device = (struct rt_ppc405_serial*) dev;
RT_ASSERT(device != RT_NULL);
/* point to buffer */
ptr = (rt_uint8_t*) buffer;
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
{
while (size)
{
/* interrupt receive */
rt_base_t level;
/* disable interrupt */
level = rt_hw_interrupt_disable();
if (device->read_index != device->save_index)
{
*ptr = device->rx_buffer[device->read_index];
device->read_index ++;
if (device->read_index >= RT_UART_RX_BUFFER_SIZE)
device->read_index = 0;
}
else
{
/* no data in rx buffer */
/* enable interrupt */
rt_hw_interrupt_enable(level);
break;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
ptr ++; size --;
}
return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
}
else if (dev->flag & RT_DEVICE_FLAG_DMA_RX)
{
/* not support right now */
RT_ASSERT(0);
}
/* polling mode */
RT_ASSERT(0);
return (rt_size_t)ptr - (rt_size_t)buffer;
}
static rt_size_t rt_serial_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
char *ptr;
struct rt_ppc405_serial* device;
device = (struct rt_ppc405_serial*) dev;
RT_ASSERT(device != RT_NULL);
if (dev->flag & RT_DEVICE_FLAG_INT_TX)
{
/* not support */
RT_ASSERT(0);
}
else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
{
/* not support */
RT_ASSERT(0);
}
/* polling write */
ptr = (char *)buffer;
if (dev->flag & RT_DEVICE_FLAG_STREAM)
{
/* stream mode */
while (size)
{
if (*ptr == '\n')
{
while ((in_8((rt_uint8_t*)device->hw_base + UART_LSR) & 0x20) != 0x20);
out_8((rt_uint8_t*)device->hw_base + UART_THR, '\r');
}
while ((in_8((rt_uint8_t*)device->hw_base + UART_LSR) & 0x20) != 0x20);
out_8((rt_uint8_t*)device->hw_base + UART_THR, *ptr);
ptr ++;
size --;
}
}
else
{
while (size)
{
while ((in_8((rt_uint8_t*)device->hw_base + UART_LSR) & 0x20) != 0x20);
out_8((rt_uint8_t*)device->hw_base + UART_THR, *ptr);
ptr ++;
size --;
}
}
return (rt_size_t) ptr - (rt_size_t) buffer;
}
void rt_serial_set_baudrate(struct rt_ppc405_serial* device)
{
rt_uint32_t bdiv;
bdiv = 115200;
out_8((rt_uint8_t *)device->hw_base + UART_DLL, bdiv); /* set baudrate divisor */
out_8((rt_uint8_t *)device->hw_base + UART_DLM, bdiv >> 8); /* set baudrate divisor */
}
void rt_serial_isr(int irqno, void* param)
{
unsigned char status;
struct rt_ppc405_serial *device;
device = (struct rt_ppc405_serial*) param;
status = in_8((rt_uint8_t *)device->hw_base + UART_LSR);
if (status & 0x01)
{
rt_base_t level;
while (status & 0x01)
{
/* disable interrupt */
level = rt_hw_interrupt_disable();
/* read character */
device->rx_buffer[device->save_index] = (0xff & (int) in_8((rt_uint8_t *)device->hw_base));
device->save_index ++;
if (device->save_index >= RT_UART_RX_BUFFER_SIZE)
device->save_index = 0;
/* if the next position is read index, discard this 'read char' */
if (device->save_index == device->read_index)
{
device->read_index ++;
if (device->read_index >= RT_UART_RX_BUFFER_SIZE)
device->read_index = 0;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
/* check error */
if ((status & ( asyncLSRFramingError1 |
asyncLSROverrunError1 |
asyncLSRParityError1 |
asyncLSRBreakInterrupt1 )) != 0)
{
out_8((rt_uint8_t *)device->hw_base + UART_LSR,
asyncLSRFramingError1 |
asyncLSROverrunError1 |
asyncLSRParityError1 |
asyncLSRBreakInterrupt1);
}
status = in_8((rt_uint8_t *)device->hw_base + UART_LSR);
}
/* invoke callback */
if(device->parent.rx_indicate != RT_NULL)
{
device->parent.rx_indicate(&device->parent, 1);
}
}
}
void rt_hw_serial_init(void)
{
volatile rt_uint8_t val;
struct rt_ppc405_serial* device;
device = (struct rt_ppc405_serial*) &ppc405_serial;
device->parent.type = RT_Device_Class_Char;
device->hw_base = UART0_BASE;
device->baudrate = 115200;
device->irqno = VECNUM_U0;
rt_hw_interrupt_install(device->irqno, rt_serial_isr, device, "serial"); /* install isr */
rt_memset(device->rx_buffer, 0, sizeof(device->rx_buffer));
device->read_index = device->save_index = 0;
out_8((rt_uint8_t *)device->hw_base + UART_LCR, 0x80); /* set DLAB bit */
/* setup baudrate */
rt_serial_set_baudrate(device);
out_8((rt_uint8_t *)device->hw_base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
out_8((rt_uint8_t *)device->hw_base + UART_FCR, 0x00); /* disable FIFO */
out_8((rt_uint8_t *)device->hw_base + UART_MCR, 0x00); /* no modem control DTR RTS */
val = in_8((rt_uint8_t *)device->hw_base + UART_LSR); /* clear line status */
val = in_8((rt_uint8_t *)device->hw_base + UART_RBR); /* read receive buffer */
out_8((rt_uint8_t *)device->hw_base + UART_SCR, 0x00); /* set scratchpad */
out_8((rt_uint8_t *)device->hw_base + UART_IER, 0x00); /* set interrupt enable reg */
device->parent.type = RT_Device_Class_Char;
device->parent.init = rt_serial_init;
device->parent.open = rt_serial_open;
device->parent.close = rt_serial_close;
device->parent.read = rt_serial_read;
device->parent.write = rt_serial_write;
device->parent.control = rt_serial_control;
device->parent.user_data = RT_NULL;
rt_device_register(&device->parent,
"uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM);
}