source: rtems/c/src/lib/libcpu/powerpc/ppc403/console/console.c.polled @ 39d08d55

4.104.114.9
Last change on this file since 39d08d55 was 39d08d55, checked in by Ralf Corsepius <ralf.corsepius@…>, on Sep 6, 2008 at 5:36:55 PM

Convert to "bool".

  • Property mode set to 100644
File size: 9.6 KB
Line 
1/*
2 *  This file contains the PowerPC 403GA console IO package.
3 *
4 *  Author:     Andrew Bray <andy@i-cubed.co.uk>
5 *
6 *  COPYRIGHT (c) 1995 by i-cubed ltd.
7 *
8 *  To anyone who acknowledges that this file is provided "AS IS"
9 *  without any express or implied warranty:
10 *      permission to use, copy, modify, and distribute this file
11 *      for any purpose is hereby granted without fee, provided that
12 *      the above copyright notice and this notice appears in all
13 *      copies, and that the name of i-cubed limited not be used in
14 *      advertising or publicity pertaining to distribution of the
15 *      software without specific, written prior permission.
16 *      i-cubed limited makes no representations about the suitability
17 *      of this software for any purpose.
18 *
19 *  Derived from c/src/lib/libbsp/no_cpu/no_bsp/console/console.c:
20 *
21 *  COPYRIGHT (c) 1989-2007.
22 *  On-Line Applications Research Corporation (OAR).
23 *
24 *  The license and distribution terms for this file may be
25 *  found in the file LICENSE in this distribution or at
26 *  http://www.rtems.com/license/LICENSE.
27 *
28 *  $Id$
29 */
30
31#define NO_BSP_INIT
32
33#include <rtems.h>
34#include <rtems/libio.h>
35
36struct async {
37/*-----------------------------------------------------------------------------+
38| Line Status Register.
39+-----------------------------------------------------------------------------*/
40    unsigned char SPLS;
41    unsigned char SPLSset;
42#define LSRDataReady             0x80
43#define LSRFramingError          0x40
44#define LSROverrunError          0x20
45#define LSRParityError           0x10
46#define LSRBreakInterrupt        0x08
47#define LSRTxHoldEmpty           0x04
48#define LSRTxShiftEmpty          0x02
49
50/*-----------------------------------------------------------------------------+
51| Handshake Status Register.
52+-----------------------------------------------------------------------------*/
53    unsigned char SPHS;
54    unsigned char SPHSset;
55#define HSRDsr                   0x80
56#define HSRCts                   0x40
57
58/*-----------------------------------------------------------------------------+
59| Baud rate divisor registers
60+-----------------------------------------------------------------------------*/
61    unsigned char BRDH;
62    unsigned char BRDL;
63
64/*-----------------------------------------------------------------------------+
65| Control Register.
66+-----------------------------------------------------------------------------*/
67    unsigned char SPCTL;
68#define CRNormal                      0x00
69#define CRLoopback                    0x40
70#define CRAutoEcho                    0x80
71#define CRDtr                    0x20
72#define CRRts                    0x10
73#define CRWordLength7            0x00
74#define CRWordLength8            0x08
75#define CRParityDisable          0x00
76#define CRParityEnable           0x04
77#define CREvenParity             0x00
78#define CROddParity           0x02
79#define CRStopBitsOne            0x00
80#define CRStopBitsTwo            0x01
81#define CRDisableDtrRts       0x00
82
83/*-----------------------------------------------------------------------------+
84| Receiver Command Register.
85+-----------------------------------------------------------------------------*/
86    unsigned char SPRC;
87#define RCRDisable                    0x00
88#define RCREnable                     0x80
89#define RCRIntDisable         0x00
90#define RCRIntEnabled         0x20
91#define RCRDMACh2                     0x40
92#define RCRDMACh3                     0x60
93#define RCRErrorInt           0x10
94#define RCRPauseEnable        0x08
95
96/*-----------------------------------------------------------------------------+
97| Transmitter Command Register.
98+-----------------------------------------------------------------------------*/
99    unsigned char SPTC;
100#define TCRDisable                    0x00
101#define TCREnable                     0x80
102#define TCRIntDisable         0x00
103#define TCRIntEnabled         0x20
104#define TCRDMACh2                     0x40
105#define TCRDMACh3                     0x60
106#define TCRTxEmpty                    0x10
107#define TCRErrorInt           0x08
108#define TCRStopPause          0x04
109#define TCRBreakGen           0x02
110
111/*-----------------------------------------------------------------------------+
112| Miscellanies defines.
113+-----------------------------------------------------------------------------*/
114    unsigned char SPTB;
115#define SPRB    SPTB
116};
117
118#define XOFFchar                      0x13
119#define XONchar                       0x11
120
121typedef volatile struct async *pasync;
122static const pasync port = (pasync)0x40000000;
123
124/*  console_initialize
125 *
126 *  This routine initializes the console IO driver.
127 *
128 *  Input parameters: NONE
129 *
130 *  Output parameters:  NONE
131 *
132 *  Return values:
133 */
134
135rtems_device_driver console_initialize(
136  rtems_device_major_number  major,
137  rtems_device_minor_number  minor,
138  void                      *arg
139)
140{
141  rtems_status_code status;
142  register unsigned tmp;
143  extern uint32_t bsp_serial_per_sec;
144  extern bool bsp_serial_external_clock;
145  extern bool bsp_serial_cts_rts;
146  extern uint32_t bsp_serial_rate;
147
148  /* Initialise the serial port */
149  asm volatile ("mfdcr %0, 0xa0" : "=r" (tmp)); /* IOCR */
150  tmp &= ~3;
151  tmp |= (bsp_serial_external_clock ? 2 : 0) |
152      (bsp_serial_cts_rts ? 1 : 0);
153  asm volatile ("mtdcr 0xa0, %0" : "=r" (tmp) : "0" (tmp)); /* IOCR */
154  port->SPLS = (LSRDataReady | LSRFramingError | LSROverrunError |
155         LSRParityError | LSRBreakInterrupt);
156  tmp = bsp_serial_per_sec / bsp_get_serial_rate;
157#if 0 /* replaced by IMD... */
158  tmp = ((tmp + 8) >> 4) - 1;
159  port->BRDL = tmp & 0x255;
160  port->BRDH = tmp >> 8;
161#else
162  tmp = ((tmp) >> 4) - 1;
163  port->BRDL = tmp & 0xff;
164  port->BRDH = tmp >> 8;
165#endif
166  port->SPCTL = (CRNormal | CRDtr | CRRts | CRWordLength8 | CRParityDisable |
167     CRStopBitsOne);
168  port->SPRC = (RCREnable | RCRIntDisable | RCRPauseEnable);
169  port->SPTC = (TCREnable | TCRIntDisable);
170  port->SPHS = (HSRDsr | HSRCts);
171
172  status = rtems_io_register_name(
173    "/dev/console",
174    major,
175    (rtems_device_minor_number) 0
176  );
177 
178  if (status != RTEMS_SUCCESSFUL)
179    rtems_fatal_error_occurred(status);
180 
181  return RTEMS_SUCCESSFUL;
182}
183
184
185/*  is_character_ready
186 *
187 *  This routine returns TRUE if a character is available.
188 *
189 *  Input parameters: NONE
190 *
191 *  Output parameters:  NONE
192 *
193 *  Return values:
194 */
195
196bool is_character_ready(
197  char *ch
198)
199{
200  unsigned char status;
201
202  if ((status = port->SPLS) & LSRDataReady)
203    {
204      *ch = port->SPRB; 
205      return true;
206    }
207
208  /* Clean any dodgy status */
209  if ((status & (LSRFramingError | LSROverrunError | LSRParityError |
210                 LSRBreakInterrupt)) != 0)
211    {
212      port->SPLS = (LSRFramingError | LSROverrunError | LSRParityError |
213                 LSRBreakInterrupt);
214    }
215
216  return false;
217}
218
219/*  inbyte
220 *
221 *  This routine reads a character from the SOURCE.
222 *
223 *  Input parameters: NONE
224 *
225 *  Output parameters:  NONE
226 *
227 *  Return values:
228 *    character read from SOURCE
229 */
230
231char inbyte( void )
232{
233  unsigned char status;
234
235  while (1)
236    {
237      if ((status = port->SPLS) & LSRDataReady)
238              break;
239
240      /* Clean any dodgy status */
241      if ((status & (LSRFramingError | LSROverrunError | LSRParityError |
242                     LSRBreakInterrupt)) != 0)
243            {
244              port->SPLS = (LSRFramingError | LSROverrunError | LSRParityError |
245                            LSRBreakInterrupt);
246            }
247    }
248
249  return port->SPRB; 
250}
251
252/*  outbyte
253 *
254 *  This routine transmits a character out the SOURCE.  It may support
255 *  XON/XOFF flow control.
256 *
257 *  Input parameters:
258 *    ch  - character to be transmitted
259 *
260 *  Output parameters:  NONE
261 */
262
263void outbyte(
264  char ch
265)
266{
267  unsigned char status;
268  extern bool bsp_serial_xon_xoff;
269
270  while (port->SPHS)
271    port->SPHS = (HSRDsr | HSRCts);
272
273  while (1)
274    {
275      status = port->SPLS;
276
277      if (port->SPHS)
278        port->SPHS = (HSRDsr | HSRCts);
279      else if (status & LSRTxHoldEmpty)
280              break;
281    }
282
283  if (bsp_serial_xon_xoff)
284    while (is_character_ready(&status))
285    {
286            if (status == XOFFchar)
287              do {
288                while (!is_character_ready(&status));
289              } while (status != XONchar);
290    }
291
292  port->SPTB = ch;
293}
294
295/*
296 *  Open entry point
297 */
298 
299rtems_device_driver console_open(
300  rtems_device_major_number major,
301  rtems_device_minor_number minor,
302  void                    * arg
303)
304{
305  return RTEMS_SUCCESSFUL;
306}
307 
308/*
309 *  Close entry point
310 */
311 
312rtems_device_driver console_close(
313  rtems_device_major_number major,
314  rtems_device_minor_number minor,
315  void                    * arg
316)
317{
318  return RTEMS_SUCCESSFUL;
319}
320 
321/*
322 * read bytes from the serial port. We only have stdin.
323 */
324 
325rtems_device_driver console_read(
326  rtems_device_major_number major,
327  rtems_device_minor_number minor,
328  void                    * arg
329)
330{
331  rtems_libio_rw_args_t *rw_args;
332  char *buffer;
333  int maximum;
334  int count = 0;
335 
336  rw_args = (rtems_libio_rw_args_t *) arg;
337 
338  buffer = rw_args->buffer;
339  maximum = rw_args->count;
340 
341  for (count = 0; count < maximum; count++) {
342    buffer[ count ] = inbyte();
343    if (buffer[ count ] == '\n' || buffer[ count ] == '\r') {
344      buffer[ count++ ]  = '\n';
345      buffer[ count ]  = 0;
346      break;
347    }
348  }
349 
350  rw_args->bytes_moved = count;
351  return (count >= 0) ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED;
352}
353 
354/*
355 * write bytes to the serial port. Stdout and stderr are the same.
356 */
357 
358rtems_device_driver console_write(
359  rtems_device_major_number major,
360  rtems_device_minor_number minor,
361  void                    * arg
362)
363{
364  int count;
365  int maximum;
366  rtems_libio_rw_args_t *rw_args;
367  char *buffer;
368 
369  rw_args = (rtems_libio_rw_args_t *) arg;
370 
371  buffer = rw_args->buffer;
372  maximum = rw_args->count;
373 
374  for (count = 0; count < maximum; count++) {
375    if ( buffer[ count ] == '\n') {
376      outbyte('\r');
377    }
378    outbyte( buffer[ count ] );
379  }
380  return maximum;
381}
382 
383/*
384 *  IO Control entry point
385 */
386 
387rtems_device_driver console_control(
388  rtems_device_major_number major,
389  rtems_device_minor_number minor,
390  void                    * arg
391)
392{
393  return RTEMS_SUCCESSFUL;
394}
395
Note: See TracBrowser for help on using the repository browser.