source: rtems/c/src/lib/libbsp/sparc/erc32/console/console.c @ 52dd75d

4.104.114.84.95
Last change on this file since 52dd75d was 52dd75d, checked in by Joel Sherrill <joel.sherrill@…>, on 02/03/98 at 18:29:05

Corrected spelling error so interrupt driven console would work.

  • Property mode set to 100644
File size: 10.8 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-1997.
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#ifdef 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/*
353 *  Console Termios Support Entry Points
354 *
355 */
356
357int console_write_support (int minor, const char *buf, int len)
358{
359  int nwrite = 0;
360
361  while (nwrite < len) {
362#if defined(CONSOLE_USE_INTERRUPTS)
363    console_outbyte_interrupt( minor, *buf++ );
364#else
365    console_outbyte_polled( minor, *buf++ );
366#endif
367    nwrite++;
368  }
369  return nwrite;
370}
371
372void console_reserve_resources(
373  rtems_configuration_table *configuration
374)
375{
376  rtems_termios_reserve_resources( configuration, 2 );
377}
378
379/*
380 *  Console Device Driver Entry Points
381 *
382 */
383 
384rtems_device_driver console_initialize(
385  rtems_device_major_number  major,
386  rtems_device_minor_number  minor,
387  void                      *arg
388)
389{
390  rtems_status_code status;
391
392  rtems_termios_initialize();
393
394  /*
395   *  Register Device Names
396   */
397
398  status = rtems_io_register_name( "/dev/console", major, 0 );
399  if (status != RTEMS_SUCCESSFUL)
400    rtems_fatal_error_occurred(status);
401
402  status = rtems_io_register_name( "/dev/console_b", major, 1 );
403  if (status != RTEMS_SUCCESSFUL)
404    rtems_fatal_error_occurred(status);
405
406  /*
407   *  Initialize Hardware
408   */
409 
410#ifdef CONSOLE_USE_INTERRUPTS
411  console_initialize_interrupts();
412#endif
413
414  return RTEMS_SUCCESSFUL;
415}
416
417rtems_device_driver console_open(
418  rtems_device_major_number major,
419  rtems_device_minor_number minor,
420  void                    * arg
421)
422{
423        rtems_status_code sc;
424#if defined(CONSOLE_USE_INTERRUPTS)
425        rtems_libio_open_close_args_t *args = arg;
426#endif
427
428        assert( minor <= 1 );
429        if ( minor > 2 )
430          return RTEMS_INVALID_NUMBER;
431 
432#if defined(CONSOLE_USE_INTERRUPTS)
433        sc = rtems_termios_open (major, minor, arg,
434                        NULL,
435                        NULL,
436                        NULL,
437                        console_write_support,
438                        0);
439
440        console_termios_data[ minor ] = args->iop->data1;
441#else
442        sc = rtems_termios_open (major, minor, arg,
443                        NULL,
444                        NULL,
445                        console_inbyte_nonblocking,
446                        console_write_support,
447                        0);
448#endif
449
450  return RTEMS_SUCCESSFUL;
451}
452 
453rtems_device_driver console_close(
454  rtems_device_major_number major,
455  rtems_device_minor_number minor,
456  void                    * arg
457)
458{
459  return rtems_termios_close (arg);
460}
461 
462rtems_device_driver console_read(
463  rtems_device_major_number major,
464  rtems_device_minor_number minor,
465  void                    * arg
466)
467{
468  return rtems_termios_read (arg);
469}
470 
471rtems_device_driver console_write(
472  rtems_device_major_number major,
473  rtems_device_minor_number minor,
474  void                    * arg
475)
476{
477  return rtems_termios_write (arg);
478}
479 
480rtems_device_driver console_control(
481  rtems_device_major_number major,
482  rtems_device_minor_number minor,
483  void                    * arg
484)
485{
486  return rtems_termios_ioctl (arg);
487}
488
Note: See TracBrowser for help on using the repository browser.