source: rtems/c/src/lib/libbsp/sparc/erc32/console/console.c @ 0090c1a

4.104.114.84.95
Last change on this file since 0090c1a was 0090c1a, checked in by Joel Sherrill <joel.sherrill@…>, on 05/18/99 at 17:31:27

Spacing correction.

  • Property mode set to 100644
File size: 11.3 KB
Line 
1/*
2 *  This file contains the TTY driver for the serial ports on the erc32.
3 *
4 *  This driver uses the termios pseudo driver.
5 *
6 *  COPYRIGHT (c) 1989-1998.
7 *  On-Line Applications Research Corporation (OAR).
8 *  Copyright assigned to U.S. Government, 1994.
9 *
10 *  The license and distribution terms for this file may be
11 *  found in the file LICENSE in this distribution or at
12 *  http://www.OARcorp.com/rtems/license.html.
13 *
14 *  $Id$
15 */
16
17#include <bsp.h>
18#include <rtems/libio.h>
19#include <stdlib.h>
20#include <assert.h>
21
22/*
23 *  Should we use a polled or interrupt drived console?
24 * 
25 *  NOTE: This is defined in the custom/erc32.cfg file.
26 *
27 *  WARNING:  In sis 1.6, it did not appear that the UART interrupts
28 *            worked in a desirable fashion.  Immediately upon writing
29 *            a character into the TX buffer, an interrupt was generated.
30 *            This did not allow enough time for the program to put more
31 *            characters in the buffer.  So every character resulted in
32 *            "priming" the transmitter.   This effectively results in
33 *            in a polled console with a useless interrupt per character
34 *            on output.  It is reasonable to assume that input does not
35 *            share this problem although it was not investigated.
36 *
37 */
38
39/*
40 *  console_outbyte_polled
41 *
42 *  This routine transmits a character using polling.
43 */
44
45void console_outbyte_polled(
46  int  port,
47  char ch
48)
49{
50  if ( port == 0 ) {
51    while ( (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEA) == 0 );
52    ERC32_MEC.UART_Channel_A = (int) ch;
53    return;
54  }
55
56  while ( (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEB) == 0 );
57  ERC32_MEC.UART_Channel_B = (int) ch;
58}
59
60/*
61 *  console_inbyte_nonblocking
62 *
63 *  This routine polls for a character.
64 */
65
66int console_inbyte_nonblocking( int port )
67{
68  int UStat;
69
70  UStat = ERC32_MEC.UART_Status;
71
72  switch (port) {
73
74    case 0:
75      if (UStat & ERC32_MEC_UART_STATUS_ERRA) {
76        ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRA;
77        ERC32_MEC.Control = ERC32_MEC.Control;
78      }
79
80      if ((UStat & ERC32_MEC_UART_STATUS_DRA) == 0)
81         return -1;
82      return (int) ERC32_MEC.UART_Channel_A;
83      return 1;
84
85    case 1:
86      if (UStat & ERC32_MEC_UART_STATUS_ERRB) {
87        ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRB;
88        ERC32_MEC.Control = ERC32_MEC.Control;
89      }
90
91      if ((UStat & ERC32_MEC_UART_STATUS_DRB) == 0)
92        return -1;
93      return (int) ERC32_MEC.UART_Channel_B;
94
95    default:
96      assert( 0 );
97  }
98
99  return -1;
100}
101
102/*
103 *  Interrupt driven console IO
104 */
105
106#if (CONSOLE_USE_INTERRUPTS)
107
108/*
109 *  Buffers between task and ISRs
110 */
111
112#include <ringbuf.h>
113 
114Ring_buffer_t  TX_Buffer[ 2 ];
115boolean        Is_TX_active[ 2 ];
116 
117void *console_termios_data[ 2 ];
118
119/*
120 *  console_isr_a
121 *
122 *  This routine is the console interrupt handler for Channel A.
123 *
124 *  Input parameters:
125 *    vector - vector number
126 *
127 *  Output parameters: NONE
128 *
129 *  Return values:     NONE
130 */
131
132rtems_isr console_isr_a(
133  rtems_vector_number vector
134)
135{
136  char ch;
137  int UStat;
138 
139  if ( (UStat = ERC32_MEC.UART_Status) & ERC32_MEC_UART_STATUS_DRA ) {
140    if (UStat & ERC32_MEC_UART_STATUS_ERRA) {
141      ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRA;
142      ERC32_MEC.Control = ERC32_MEC.Control;
143    }
144    ch = ERC32_MEC.UART_Channel_A;
145
146    rtems_termios_enqueue_raw_characters( console_termios_data[ 0 ], &ch, 1 );
147  }
148 
149  if ( ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEA ) {
150    if ( !Ring_buffer_Is_empty( &TX_Buffer[ 0 ] ) ) {
151      Ring_buffer_Remove_character( &TX_Buffer[ 0 ], ch );
152      ERC32_MEC.UART_Channel_A = (unsigned32) ch;
153    } else
154     Is_TX_active[ 0 ] = FALSE;
155  }
156 
157  ERC32_Clear_interrupt( ERC32_INTERRUPT_UART_A_RX_TX );
158}
159
160/*
161 *  console_isr_b
162 *
163 *  This routine is the console interrupt handler for Channel B.
164 *
165 *  Input parameters:
166 *    vector - vector number
167 *
168 *  Output parameters: NONE
169 *
170 *  Return values:     NONE
171 */
172 
173rtems_isr console_isr_b(
174  rtems_vector_number vector
175)
176{
177  char ch;
178  int UStat;
179
180  if ( (UStat = ERC32_MEC.UART_Status) & ERC32_MEC_UART_STATUS_DRB ) {
181    if (UStat & ERC32_MEC_UART_STATUS_ERRB) {
182      ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRB;
183      ERC32_MEC.Control = ERC32_MEC.Control;
184    }
185    ch = ERC32_MEC.UART_Channel_B;
186    rtems_termios_enqueue_raw_characters( console_termios_data[ 1 ], &ch, 1 );
187
188  }
189
190  if ( ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEB ) {
191    if ( !Ring_buffer_Is_empty( &TX_Buffer[ 1 ] ) ) {
192      Ring_buffer_Remove_character( &TX_Buffer[ 1 ], ch );
193      ERC32_MEC.UART_Channel_B = (unsigned32) ch;
194    } else
195     Is_TX_active[ 1 ] = FALSE;
196  }
197
198  ERC32_Clear_interrupt( ERC32_INTERRUPT_UART_B_RX_TX );
199}
200
201/*
202 *  console_exit
203 *
204 *  This routine allows the console to exit by masking its associated interrupt
205 *  vectors.
206 *
207 *  Input parameters:  NONE
208 *
209 *  Output parameters: NONE
210 *
211 *  Return values:     NONE
212 */
213
214void console_exit()
215{
216  rtems_unsigned32 port;
217  rtems_unsigned32 ch;
218
219  /*
220   *  Although the interrupts for the UART are unmasked, the PIL is set to
221   *  disable all external interrupts.  So we might as well do this first.
222   */
223
224  ERC32_Mask_interrupt( ERC32_INTERRUPT_UART_A_RX_TX );
225  ERC32_Mask_interrupt( ERC32_INTERRUPT_UART_B_RX_TX );
226
227  for ( port=0 ; port <= 1 ; port++ ) {
228    while ( !Ring_buffer_Is_empty( &TX_Buffer[ port ] ) ) {
229      Ring_buffer_Remove_character( &TX_Buffer[ port ], ch );
230      console_outbyte_polled( port, ch );
231    }
232  }
233
234  /*
235   *  Now wait for all the data to actually get out ... the send register
236   *  should be empty.
237   */
238 
239  while ( (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEA) !=
240          ERC32_MEC_UART_STATUS_THEA );
241
242  while ( (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEB) !=
243          ERC32_MEC_UART_STATUS_THEB );
244
245}
246
247#define CONSOLE_UART_A_TRAP  ERC32_TRAP_TYPE( ERC32_INTERRUPT_UART_A_RX_TX )
248#define CONSOLE_UART_B_TRAP  ERC32_TRAP_TYPE( ERC32_INTERRUPT_UART_B_RX_TX )
249
250/*
251 *  console_initialize_interrupts
252 *
253 *  This routine initializes the console's receive and transmit
254 *  ring buffers and loads the appropriate vectors to handle the interrupts.
255 *
256 *  Input parameters:  NONE
257 *
258 *  Output parameters: NONE
259 *
260 *  Return values:     NONE
261 */
262
263#ifdef RDB_BREAK_IN
264  extern unsigned32 trap_table[];
265#endif
266
267void console_initialize_interrupts( void )
268{
269  Ring_buffer_Initialize( &TX_Buffer[ 0 ] );
270  Ring_buffer_Initialize( &TX_Buffer[ 1 ] );
271
272  Is_TX_active[ 0 ] = FALSE;
273  Is_TX_active[ 1 ] = FALSE;
274
275  atexit( console_exit );
276
277  set_vector( console_isr_a, CONSOLE_UART_A_TRAP, 1 );
278#ifdef RDB_BREAK_IN
279  if (trap_table[0x150/4] == 0x91d02000)
280#endif
281  set_vector( console_isr_b, CONSOLE_UART_B_TRAP, 1 );
282}
283
284/*
285 *  console_outbyte_interrupt
286 *
287 *  This routine transmits a character out.
288 *
289 *  Input parameters:
290 *    port - port to transmit character to
291 *    ch  - character to be transmitted
292 *
293 *  Output parameters:  NONE
294 *
295 *  Return values:      NONE
296 */
297 
298void console_outbyte_interrupt(
299  int   port,
300  char  ch
301)
302{
303  /*
304   *  If this is the first character then we need to prime the pump
305   */
306
307  if ( Is_TX_active[ port ] == FALSE ) {
308    Is_TX_active[ port ] = TRUE;
309    console_outbyte_polled( port, ch );
310    return;
311  }
312
313  while ( Ring_buffer_Is_full( &TX_Buffer[ port ] ) );
314
315  Ring_buffer_Add_character( &TX_Buffer[ port ], ch );
316}
317
318#endif /* CONSOLE_USE_INTERRUPTS */
319
320/*
321 *  DEBUG_puts
322 *
323 *  This should be safe in the event of an error.  It attempts to insure
324 *  that no TX empty interrupts occur while it is doing polled IO.  Then
325 *  it restores the state of that external interrupt.
326 *
327 *  Input parameters:
328 *    string  - pointer to debug output string
329 *
330 *  Output parameters:  NONE
331 *
332 *  Return values:      NONE
333 */
334
335void DEBUG_puts(
336  char *string
337)
338{
339  char *s;
340  unsigned32 old_level;
341
342  ERC32_Disable_interrupt( ERC32_INTERRUPT_UART_A_RX_TX, old_level );
343    for ( s = string ; *s ; s++ )
344      console_outbyte_polled( 0, *s );
345
346    console_outbyte_polled( 0, '\r' );
347    console_outbyte_polled( 0, '\n' );
348  ERC32_Restore_interrupt( ERC32_INTERRUPT_UART_A_RX_TX, old_level );
349}
350
351/*
352 *  Console Termios Support Entry Points
353 *
354 */
355
356int console_write_support (int minor, const char *buf, int len)
357{
358  int nwrite = 0;
359
360  while (nwrite < len) {
361#if (CONSOLE_USE_INTERRUPTS)
362    console_outbyte_interrupt( minor, *buf++ );
363#else
364    console_outbyte_polled( minor, *buf++ );
365#endif
366    nwrite++;
367  }
368  return nwrite;
369}
370
371void console_reserve_resources(
372  rtems_configuration_table *configuration
373)
374{
375  rtems_termios_reserve_resources( configuration, 2 );
376}
377
378/*
379 *  Console Device Driver Entry Points
380 *
381 */
382 
383rtems_device_driver console_initialize(
384  rtems_device_major_number  major,
385  rtems_device_minor_number  minor,
386  void                      *arg
387)
388{
389  rtems_status_code status;
390
391  rtems_termios_initialize();
392
393  /*
394   *  Register Device Names
395   */
396
397  status = rtems_io_register_name( "/dev/console", major, 0 );
398  if (status != RTEMS_SUCCESSFUL)
399    rtems_fatal_error_occurred(status);
400
401  status = rtems_io_register_name( "/dev/console_b", major, 1 );
402  if (status != RTEMS_SUCCESSFUL)
403    rtems_fatal_error_occurred(status);
404
405  /*
406   *  Initialize Hardware
407   */
408 
409#if (CONSOLE_USE_INTERRUPTS)
410  console_initialize_interrupts();
411#endif
412
413  return RTEMS_SUCCESSFUL;
414}
415
416rtems_device_driver console_open(
417  rtems_device_major_number major,
418  rtems_device_minor_number minor,
419  void                    * arg
420)
421{
422  rtems_status_code sc;
423#if (CONSOLE_USE_INTERRUPTS)
424  rtems_libio_open_close_args_t *args = arg;
425  static const rtems_termios_callbacks intrCallbacks = {
426    NULL,                        /* firstOpen */
427    NULL,                        /* lastClose */
428    NULL,                        /* pollRead */
429    console_write_support,       /* write */
430    NULL,                        /* setAttributes */
431    NULL,                        /* stopRemoteTx */
432    NULL,                        /* startRemoteTx */
433    0                            /* outputUsesInterrupts */
434  };
435#else
436  static const rtems_termios_callbacks pollCallbacks = {
437    NULL,                        /* firstOpen */
438    NULL,                        /* lastClose */
439    console_inbyte_nonblocking,  /* pollRead */
440    console_write_support,       /* write */
441    NULL,                        /* setAttributes */
442    NULL,                        /* stopRemoteTx */
443    NULL,                        /* startRemoteTx */
444    0                            /* outputUsesInterrupts */
445  };
446#endif
447
448  assert( minor <= 1 );
449  if ( minor > 2 )
450    return RTEMS_INVALID_NUMBER;
451 
452#if (CONSOLE_USE_INTERRUPTS)
453  sc = rtems_termios_open (major, minor, arg, &intrCallbacks);
454
455  console_termios_data[ minor ] = args->iop->data1;
456#else
457  sc = rtems_termios_open (major, minor, arg, &pollCallbacks);
458#endif
459
460  return RTEMS_SUCCESSFUL;
461}
462 
463rtems_device_driver console_close(
464  rtems_device_major_number major,
465  rtems_device_minor_number minor,
466  void                    * arg
467)
468{
469  return rtems_termios_close (arg);
470}
471 
472rtems_device_driver console_read(
473  rtems_device_major_number major,
474  rtems_device_minor_number minor,
475  void                    * arg
476)
477{
478  return rtems_termios_read (arg);
479}
480 
481rtems_device_driver console_write(
482  rtems_device_major_number major,
483  rtems_device_minor_number minor,
484  void                    * arg
485)
486{
487  return rtems_termios_write (arg);
488}
489 
490rtems_device_driver console_control(
491  rtems_device_major_number major,
492  rtems_device_minor_number minor,
493  void                    * arg
494)
495{
496  return rtems_termios_ioctl (arg);
497}
498
Note: See TracBrowser for help on using the repository browser.