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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/*
* File : usart.c
* mb9bf506r uart driver
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2012-11-30 lgnq first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "usart.h"
#include "uart_18xx_43xx.h"
#include "scu_18xx_43xx.h"
#if defined(RT_USING_UART0)
/* UART0 device driver structure */
struct serial_ringbuffer uart0_int_rx;
struct uart_device uart0 =
{
LPC_USART0,
USART0_IRQn,
};
struct rt_serial_device serial0;
void UART0_IRQHandler(void)
{
UART_Int_Status status;
/* enter interrupt */
rt_interrupt_enter();
status = Chip_UART_GetIntStatus(LPC_USART0);
/* error */
if (status == UART_ERROR)
{
return;
}
/* ready for Read Data */
if (status & READY_TO_RECEIVE)
{
rt_hw_serial_isr(&serial0);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#if defined(RT_USING_UART1)
/* UART1 device driver structure */
struct serial_ringbuffer uart1_int_rx;
struct uart_device uart1 =
{
LPC_UART1,
USART1_IRQn,
};
struct rt_serial_device serial1;
void UART1_IRQHandler(void)
{
UART_Int_Status status;
/* enter interrupt */
rt_interrupt_enter();
status = Chip_UART_GetIntStatus(LPC_UART1);
/* error */
if (status == UART_ERROR)
{
return;
}
/* ready for Read Data */
if (status & READY_TO_RECEIVE)
{
rt_hw_serial_isr(&serial1);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#if defined(RT_USING_UART2)
/* UART2 device driver structure */
struct serial_ringbuffer uart2_int_rx;
struct uart_device uart2 =
{
LPC_USART2,
USART2_IRQn,
};
struct rt_serial_device serial2;
void UART2_IRQHandler(void)
{
UART_Int_Status status;
/* enter interrupt */
rt_interrupt_enter();
status = Chip_UART_GetIntStatus(LPC_USART2);
/* error */
if (status == UART_ERROR)
{
return;
}
/* ready for Read Data */
if (status & READY_TO_RECEIVE)
{
rt_hw_serial_isr(&serial2);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#if defined(RT_USING_UART3)
/* UART3 device driver structure */
struct serial_ringbuffer uart3_int_rx;
struct uart_device uart3 =
{
LPC_USART3,
USART3_IRQn,
};
struct rt_serial_device serial3;
void UART3_IRQHandler(void)
{
UART_Int_Status status;
/* enter interrupt */
rt_interrupt_enter();
status = Chip_UART_GetIntStatus(LPC_USART3);
/* error */
if (status == UART_ERROR)
{
return;
}
/* ready for Read Data */
if (status & READY_TO_RECEIVE)
{
rt_hw_serial_isr(&serial3);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif
void uart_pin_setup(void)
{
#if defined(RT_USING_UART0)
Chip_SCU_PinMux(0x6, 4, MD_PDN, FUNC2); /* P6.5 : UART0_TXD */
Chip_SCU_PinMux(0x6, 5, MD_PLN | MD_EZI | MD_ZI, FUNC2); /* P6.4 : UART0_RXD */
#endif
#if defined(RT_USING_UART1)
Chip_SCU_PinMux(0x1, 13, MD_PDN, FUNC2); /* P1.13 : UART1_TXD */
Chip_SCU_PinMux(0x1, 14, MD_PLN | MD_EZI | MD_ZI, FUNC2); /* P1.14 : UART1_RX */
#endif
}
static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
struct uart_device *uart;
UART_DATABIT_Type databit;
UART_STOPBIT_Type stopbit;
UART_PARITY_Type parity;
/* UART FIFO configuration Struct variable */
UART_FIFO_CFG_Type UARTFIFOConfigStruct;
RT_ASSERT(serial != RT_NULL);
uart = (struct uart_device *)serial->parent.user_data;
Chip_UART_Init(uart->uart_regs);
Chip_UART_SetBaud(uart->uart_regs, cfg->baud_rate);
/* set stop bits */
switch (cfg->stop_bits)
{
case STOP_BITS_1:
stopbit = UART_STOPBIT_1;
break;
case STOP_BITS_2:
stopbit = UART_STOPBIT_2;
break;
default:
return RT_ERROR;
}
/* set data bits */
switch (cfg->data_bits)
{
case DATA_BITS_5:
databit = UART_DATABIT_5;
break;
case DATA_BITS_6:
databit = UART_DATABIT_6;
break;
case DATA_BITS_7:
databit = UART_DATABIT_7;
break;
case DATA_BITS_8:
databit = UART_DATABIT_8;
break;
default:
return RT_ERROR;
}
/* set parity */
switch (cfg->parity)
{
case PARITY_NONE:
parity = UART_PARITY_NONE;
break;
case PARITY_EVEN:
parity = UART_PARITY_EVEN;
break;
case PARITY_ODD:
parity = UART_PARITY_ODD;
break;
default:
return RT_ERROR;
}
Chip_UART_ConfigData(uart->uart_regs, databit, parity, stopbit);
/* Enable UART Transmit */
Chip_UART_TxCmd(uart->uart_regs, ENABLE);
Chip_UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
/* Enable DMA mode in UART */
UARTFIFOConfigStruct.FIFO_DMAMode = ENABLE;
/* Initialize FIFO for UART0 peripheral */
Chip_UART_FIFOConfig(uart->uart_regs, &UARTFIFOConfigStruct);
/* Enable UART Rx interrupt */
Chip_UART_IntConfig(uart->uart_regs, UART_INTCFG_RBR, ENABLE);
/* Enable UART line status interrupt */
Chip_UART_IntConfig(uart->uart_regs, UART_INTCFG_RLS, ENABLE);
/* Enable Interrupt for UART channel */
/* Priority = 1 */
NVIC_SetPriority(uart->irq_num, 1);
return RT_EOK;
}
static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
{
struct uart_device *uart;
RT_ASSERT(serial != RT_NULL);
uart = (struct uart_device *)serial->parent.user_data;
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
/* disable rx irq */
UART_DISABLE_IRQ(uart->irq_num);
break;
case RT_DEVICE_CTRL_SET_INT:
/* enable rx irq */
UART_ENABLE_IRQ(uart->irq_num);
break;
}
return (RT_EOK);
}
static int uart_putc(struct rt_serial_device *serial, char c)
{
struct uart_device *uart;
RT_ASSERT(serial != RT_NULL);
uart = (struct uart_device *)serial->parent.user_data;
/* wait send buffer is empty */
while (!(uart->uart_regs->LSR & UART_LSR_THRE))
;
/* write to send buffer */
uart->uart_regs->THR = c & UART_THR_MASKBIT;
return (1);
}
static int uart_getc(struct rt_serial_device *serial)
{
struct uart_device *uart;
uint8_t ch;
RT_ASSERT(serial != RT_NULL);
uart = (struct uart_device *)serial->parent.user_data;
/* receive buffer is full */
if (uart->uart_regs->LSR & UART_LSR_RDR)
{
ch = uart->uart_regs->RBR & UART_RBR_MASKBIT;
return (ch);
}
else
return (-1);
}
static struct rt_uart_ops uart_ops =
{
uart_configure,
uart_control,
uart_putc,
uart_getc,
};
void rt_hw_serial_init(void)
{
struct serial_configure config;
config.baud_rate = BAUD_RATE_115200;
config.data_bits = DATA_BITS_8;
config.parity = PARITY_NONE;
config.stop_bits = STOP_BITS_1;
uart_pin_setup();
#if defined(RT_USING_UART0)
serial0.ops = &uart_ops;
serial0.int_rx = &uart0_int_rx;
serial0.config = config;
/* register UART0 device */
rt_hw_serial_register(&serial0,
"uart0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
&uart0);
#endif
#if defined(RT_USING_UART1)
serial1.ops = &uart_ops;
serial1.int_rx = &uart1_int_rx;
serial1.config = config;
/* register UART1 device */
rt_hw_serial_register(&serial1,
"uart1",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
&uart1);
#endif
#if defined(RT_USING_UART2)
serial2.ops = &uart_ops;
serial2.int_rx = &uart2_int_rx;
serial2.config = config;
/* register UART2 device */
rt_hw_serial_register(&serial2,
"uart2",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
&uart2);
#endif
#if defined(RT_USING_UART3)
serial3.ops = &uart_ops;
serial3.int_rx = &uart3_int_rx;
serial3.config = config;
/* register UART3 device */
rt_hw_serial_register(&serial3,
"uart3",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
&uart3);
#endif
}
|