source: rtems/c/src/lib/libbsp/arm/rtl22xx/console/uart.c @ 0afac6a

4.115
Last change on this file since 0afac6a was 23c3b3b2, checked in by Joel Sherrill <joel.sherrill@…>, on 10/14/14 at 21:50:54

c/src/lib/libbsp/arm/rtl22xx/console/uart.c

  • Property mode set to 100644
File size: 8.1 KB
Line 
1/*
2 *  console driver for RTL22xx UARTs
3 *
4 *  If you want the driver to be interrupt driven, you
5 *  need to write the ISR, and in the ISR insert the
6 *  chars into termios's queue.
7 */
8
9/*
10 *  Copyright (c) By ray <rayx.cn@gmail.com>
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#include <bsp.h>                /* Must be before libio.h */
18#include <rtems/libio.h>
19#include <termios.h>
20#include <rtems/bspIo.h>
21
22/* Put the CPU (or UART) specific header file #include here */
23#include <lpc22xx.h>
24#include "lpc22xx_uart.h"
25
26#include <libchip/serial.h>
27#include <libchip/sersupp.h>
28
29/* How many serial ports? */
30#define NUM_DEVS       1
31
32int dbg_dly;
33
34/* static function prototypes */
35static int     uart_first_open(int major, int minor, void *arg);
36static int     uart_last_close(int major, int minor, void *arg);
37static int     uart_read(int minor);
38static ssize_t uart_write(int minor, const char *buf, size_t len);
39static void    uart_init(int minor);
40static void    uart_write_polled(int minor, char c);
41static int     uart_set_attributes(int minor, const struct termios *t);
42
43/* These are used by code in console.c */
44unsigned long Console_Configuration_Count = NUM_DEVS;
45
46/* Pointers to functions for handling the UART. */
47const console_fns uart_fns = {
48  libchip_serial_default_probe,
49  uart_first_open,
50  uart_last_close,
51  uart_read,
52  uart_write,
53  uart_init,
54  uart_write_polled,   /* not used in this driver */
55  uart_set_attributes,
56  FALSE                /* TRUE if interrupt driven, FALSE if not. */
57};
58
59/*
60 * There's one item in array for each UART.
61 *
62 * Some of these fields are marked "NOT USED". They are not used
63 * by console.c, but may be used by drivers in libchip
64 */
65console_tbl Console_Configuration_Ports[] = {
66{
67    "/dev/com0",                      /* sDeviceName */
68    SERIAL_CUSTOM,                    /* deviceType */
69    &uart_fns,                        /* pDeviceFns */
70    NULL,                             /* deviceProbe */
71    NULL,                             /* pDeviceFlow */
72    0,                                /* ulMargin - NOT USED */
73    0,                                /* ulHysteresis - NOT USED */
74    NULL,                             /* pDeviceParams */
75    0,                                /* ulCtrlPort1  - NOT USED */
76    0,                                /* ulCtrlPort2  - NOT USED */
77    0,                                /* ulDataPort  - NOT USED */
78    NULL,                             /* getRegister - NOT USED */
79    NULL,                             /* setRegister - NOT USED */
80    NULL,                             /* getData - NOT USED */
81    NULL,                             /* setData - NOT USED */
82    0,                                /* ulClock - NOT USED */
83    0                                 /* ulIntVector - NOT USED */
84}
85#if 0
86{
87    "/dev/com1",                      /* sDeviceName */
88    SERIAL_CUSTOM,                    /* deviceType */
89    &uart_fns,                        /* pDeviceFns */
90    NULL,                             /* deviceProbe */
91    NULL,                             /* pDeviceFlow */
92    0,                                /* ulMargin - NOT USED */
93    0,                                /* ulHysteresis - NOT USED */
94    NULL,                             /* pDeviceParams */
95    0,                                /* ulCtrlPort1  - NOT USED */
96    0,                                /* ulCtrlPort2  - NOT USED */
97    0,                                /* ulDataPort  - NOT USED */
98    NULL,                             /* getRegister - NOT USED */
99    NULL,                             /* setRegister - NOT USED */
100    NULL,                             /* getData - NOT USED */
101    NULL,                             /* setData - NOT USED */
102    0,                                /* ulClock - NOT USED */
103    0                                 /* ulIntVector - NOT USED */
104}
105#endif
106};
107
108/*
109 * This is called the first time each device is opened. If the driver
110 * is interrupt driven, you should enable interrupts here. Otherwise,
111 * it's probably safe to do nothing.
112 *
113 * Since micromonitor already set up the UART, we do nothing.
114 */
115static int uart_first_open(int major, int minor, void *arg)
116{
117  return 0;
118}
119
120/*
121 * This is called the last time each device is closed. If the driver
122 * is interrupt driven, you should disable interrupts here. Otherwise,
123 * it's probably safe to do nothing.
124 */
125static int uart_last_close(int major, int minor, void *arg)
126{
127  return 0;
128}
129
130/*
131 * Read one character from UART.
132 *
133 * Return -1 if there's no data, otherwise return
134 * the character in lowest 8 bits of returned int.
135 */
136static int uart_read(int minor)
137{
138  char c;
139
140  switch (minor) {
141    case 0:
142      if (U0LSR & ULSR_RDR) {
143        c = U0RBR;
144        return c;
145      }
146      return -1;
147    case 1:
148      if (U1LSR & ULSR_RDR) {
149        c = U1RBR;
150        return c;
151      }
152      return -1;
153    default:
154      break;
155  }
156  printk("Unknown console minor number %d\n", minor);
157  return -1;
158}
159
160/*
161 * Write buffer to UART
162 *
163 * return 1 on success, -1 on error
164 */
165static ssize_t uart_write(int minor, const char *buf, size_t len)
166{
167  size_t i;
168
169  switch (minor) {
170    case 0:
171      for (i = 0; i < len; i++) {
172        while (!(U0LSR & ULSR_THRE))   /* wait for TX buffer to empty*/
173          continue;                    /* also either WDOG() or swap()*/
174        U0THR = (char) buf[i];
175      }
176      break;
177    case 1:
178      for (i = 0; i < len; i++) {
179        while (!(U0LSR & ULSR_THRE))   /* wait for TX buffer to empty*/
180          continue;                    /* also either WDOG() or swap()*/
181        U0THR = (char) buf[i];
182      }
183      break;
184    default:
185      printk("Unknown console minor number %d\n", minor);
186      return -1;
187  }
188
189  return len;
190}
191
192/* Set up the UART. */
193static void uart_init(int minor)
194{
195#if 0 //init will be done in bspstart.c
196  int baud=6;
197  int mode =0x03;
198  if(minor==0){
199    // set port pins for UART0
200    PINSEL0 =  (PINSEL0 & ~U0_PINMASK) | U0_PINSEL;
201
202    U0IER = 0x00;                         // disable all interrupts
203
204    // set the baudrate
205    U0LCR = 1<<7;             // select divisor latches
206    U0DLL = (uint8_t)baud;                // set for baud low byte
207    U0DLM = (uint8_t)(baud >> 8);         // set for baud high byte
208
209    // set the number of characters and other
210    // user specified operating parameters
211    U0LCR = (mode & ~ULCR_DLAB_ENABLE);
212    U0FCR = mode>>8; /*fifo mode*/
213
214    // set port pins for UART1
215    PINSEL0 = (PINSEL0 & ~U1_PINMASK) | U1_PINSEL;
216
217    U1IER = 0x00;              // disable all interrupts
218  }else if(minor==1){
219    // set the baudrate
220    U1LCR = ULCR_DLAB_ENABLE;        // select divisor latches
221    U1DLL = (uint8_t)baud;          // set for baud low byte
222    U1DLM = (uint8_t)(baud >> 8);      // set for baud high byte
223
224    // set the number of characters and other
225    // user specified operating parameters
226    U1LCR = (mode & ~ULCR_DLAB_ENABLE);
227    U1FCR = mode>>8;/*fifo mode*/
228  }
229
230#endif
231}
232
233/* I'm not sure this is needed for the shared console driver. */
234static void uart_write_polled(int minor, char c)
235{
236  uart_write(minor, &c, 1);
237}
238
239/* This is for setting baud rate, bits, etc. */
240static int uart_set_attributes(int minor, const struct termios *t)
241{
242  return 0;
243}
244
245/*
246 * Write a character to the console. This is used by printk() and
247 * maybe other low level functions. It should not use interrupts or any
248 * RTEMS system calls. It needs to be very simple
249 */
250static void _BSP_put_char( char c )
251{
252  uart_write_polled(0, c);
253  if (c == '\n') {
254    uart_write_polled(0, '\r');
255  }
256}
257
258BSP_output_char_function_type BSP_output_char = _BSP_put_char;
259
260static int _BSP_get_char(void)
261{
262  return uart_read(0);
263}
264
265BSP_polling_getchar_function_type BSP_poll_char = _BSP_get_char;
266
267/*
268 * init USART 0¡£8 bit, 1 Stop,No checkout, BPS115200
269 */
270void UART0_Ini(void)
271{
272  long Fdiv;
273  int i;
274
275  PINSEL0 = 0x00000005;        // I/O to UART0
276  U0LCR = 0x83;                // DLAB = 1
277  Fdiv = (Fpclk >>4) / UART_BPS;  // configure BPS
278  U0DLM = Fdiv/256;
279  U0DLL = Fdiv%256;
280  U0LCR = 0x03;
281
282  for(i=0;i<10;i++){
283    U0THR = 67;    //send a C to see if is OK
284    while ( (U0LSR&0x40)==0 );
285  }
286}
Note: See TracBrowser for help on using the repository browser.