source: rtems/c/src/lib/libbsp/sparc/leon2/console/console.c @ dab21360

4.104.115
Last change on this file since dab21360 was dab21360, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/10/09 at 14:43:52

2009-12-10 Ralf Corsépius <ralf.corsepius@…>

  • console/console.c: Reflect changes to rtems_termios_callbacks->write.
  • Property mode set to 100644
File size: 10.3 KB
Line 
1/*
2 *  This file contains the TTY driver for the serial ports on the LEON.
3 *
4 *  This driver uses the termios pseudo driver.
5 *
6 *  COPYRIGHT (c) 1989-1998.
7 *  On-Line Applications Research Corporation (OAR).
8 *
9 *  The license and distribution terms for this file may be
10 *  found in the file LICENSE in this distribution or at
11 *  http://www.rtems.com/license/LICENSE.
12 *
13 *  $Id$
14 */
15
16#include <bsp.h>
17#include <rtems/libio.h>
18#include <stdlib.h>
19#include <assert.h>
20#include <rtems/bspIo.h>
21
22/*
23 *  Should we use a polled or interrupt drived console?
24 *
25 *  NOTE: This is defined in the custom/leon.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/* body is in debugputs.c */
51
52/*
53 *  console_inbyte_nonblocking
54 *
55 *  This routine polls for a character.
56 */
57
58int console_inbyte_nonblocking( int port );
59
60/* body is in debugputs.c */
61
62/*
63 *  Interrupt driven console IO
64 */
65
66#if (CONSOLE_USE_INTERRUPTS)
67
68/*
69 *  Buffers between task and ISRs
70 */
71
72#include <ringbuf.h>
73
74Ring_buffer_t  TX_Buffer[ 2 ];
75bool           Is_TX_active[ 2 ];
76
77void *console_termios_data[ 2 ];
78
79/*
80 *  console_isr_a
81 *
82 *  This routine is the console interrupt handler for Channel 1.
83 *
84 *  Input parameters:
85 *    vector - vector number
86 *
87 *  Output parameters: NONE
88 *
89 *  Return values:     NONE
90 */
91
92rtems_isr console_isr_a(
93  rtems_vector_number vector
94)
95{
96  char ch;
97  int UStat;
98
99  if ( (UStat = LEON_REG.UART_Status_1) & LEON_REG_UART_STATUS_DR ) {
100    if (UStat & LEON_REG_UART_STATUS_ERR) {
101      LEON_REG.UART_Status_1 = LEON_REG_UART_STATUS_CLR;
102    }
103    ch = LEON_REG.UART_Channel_1;
104
105    rtems_termios_enqueue_raw_characters( console_termios_data[ 0 ], &ch, 1 );
106  }
107
108  if ( LEON_REG.UART_Status_1 & LEON_REG_UART_STATUS_THE ) {
109    if ( !Ring_buffer_Is_empty( &TX_Buffer[ 0 ] ) ) {
110      Ring_buffer_Remove_character( &TX_Buffer[ 0 ], ch );
111      LEON_REG.UART_Channel_1 = (uint32_t) ch;
112    } else
113     Is_TX_active[ 0 ] = false;
114  }
115
116  LEON_Clear_interrupt( LEON_INTERRUPT_UART_1_RX_TX );
117}
118
119/*
120 *  console_isr_b
121 *
122 *  This routine is the console interrupt handler for Channel 2.
123 *
124 *  Input parameters:
125 *    vector - vector number
126 *
127 *  Output parameters: NONE
128 *
129 *  Return values:     NONE
130 */
131
132rtems_isr console_isr_b(
133  rtems_vector_number vector
134)
135{
136  char ch;
137  int UStat;
138
139  if ( (UStat = LEON_REG.UART_Status_2) & LEON_REG_UART_STATUS_DR ) {
140    if (UStat & LEON_REG_UART_STATUS_ERR) {
141      LEON_REG.UART_Status_2 = LEON_REG_UART_STATUS_CLR;
142    }
143    ch = LEON_REG.UART_Channel_2;
144    rtems_termios_enqueue_raw_characters( console_termios_data[ 1 ], &ch, 1 );
145
146  }
147
148  if ( LEON_REG.UART_Status_2 & LEON_REG_UART_STATUS_THE ) {
149    if ( !Ring_buffer_Is_empty( &TX_Buffer[ 1 ] ) ) {
150      Ring_buffer_Remove_character( &TX_Buffer[ 1 ], ch );
151      LEON_REG.UART_Channel_2 = (uint32_t) ch;
152    } else
153     Is_TX_active[ 1 ] = false;
154  }
155
156  LEON_Clear_interrupt( LEON_INTERRUPT_UART_2_RX_TX );
157}
158
159/*
160 *  console_exit
161 *
162 *  This routine allows the console to exit by masking its associated interrupt
163 *  vectors.
164 *
165 *  Input parameters:  NONE
166 *
167 *  Output parameters: NONE
168 *
169 *  Return values:     NONE
170 */
171
172void console_exit()
173{
174  uint32_t port;
175  uint32_t ch;
176
177  /*
178   *  Although the interrupts for the UART are unmasked, the PIL is set to
179   *  disable all external interrupts.  So we might as well do this first.
180   */
181
182  LEON_Mask_interrupt( LEON_INTERRUPT_UART_1_RX_TX );
183  LEON_Mask_interrupt( LEON_INTERRUPT_UART_2_RX_TX );
184
185  for ( port=0 ; port <= 1 ; port++ ) {
186    while ( !Ring_buffer_Is_empty( &TX_Buffer[ port ] ) ) {
187      Ring_buffer_Remove_character( &TX_Buffer[ port ], ch );
188      console_outbyte_polled( port, ch );
189    }
190  }
191
192  /*
193   *  Now wait for all the data to actually get out ... the send register
194   *  should be empty.
195   */
196
197  while ( (LEON_REG.UART_Status_1 & LEON_REG_UART_STATUS_THE) !=
198          LEON_REG_UART_STATUS_THE );
199
200  while ( (LEON_REG.UART_Status_2 & LEON_REG_UART_STATUS_THE) !=
201          LEON_REG_UART_STATUS_THE );
202
203  LEON_REG.UART_Control_1 = 0;
204  LEON_REG.UART_Control_2 = 0;
205  LEON_REG.UART_Status_1 = 0;
206  LEON_REG.UART_Status_2 = 0;
207
208
209}
210
211#define CONSOLE_UART_1_TRAP  LEON_TRAP_TYPE( LEON_INTERRUPT_UART_1_RX_TX )
212#define CONSOLE_UART_2_TRAP  LEON_TRAP_TYPE( LEON_INTERRUPT_UART_2_RX_TX )
213
214/*
215 *  console_initialize_interrupts
216 *
217 *  This routine initializes the console's receive and transmit
218 *  ring buffers and loads the appropriate vectors to handle the interrupts.
219 *
220 *  Input parameters:  NONE
221 *
222 *  Output parameters: NONE
223 *
224 *  Return values:     NONE
225 */
226
227#ifdef RDB_BREAK_IN
228  extern uint32_t trap_table[];
229#endif
230
231void console_initialize_interrupts( void )
232{
233  Ring_buffer_Initialize( &TX_Buffer[ 0 ] );
234  Ring_buffer_Initialize( &TX_Buffer[ 1 ] );
235
236  Is_TX_active[ 0 ] = false;
237  Is_TX_active[ 1 ] = false;
238
239  atexit( console_exit );
240
241  LEON_REG.UART_Control_1 |= LEON_REG_UART_CTRL_RI | LEON_REG_UART_CTRL_TI;
242  LEON_REG.UART_Control_2 |= LEON_REG_UART_CTRL_RI | LEON_REG_UART_CTRL_TI;
243
244  set_vector( console_isr_a, CONSOLE_UART_1_TRAP, 1 );
245#ifdef RDB_BREAK_IN
246  if (trap_table[0x150/4] == 0x91d02000)
247#endif
248  set_vector( console_isr_b, CONSOLE_UART_2_TRAP, 1 );
249}
250
251/*
252 *  console_outbyte_interrupt
253 *
254 *  This routine transmits a character out.
255 *
256 *  Input parameters:
257 *    port - port to transmit character to
258 *    ch  - character to be transmitted
259 *
260 *  Output parameters:  NONE
261 *
262 *  Return values:      NONE
263 */
264
265void console_outbyte_interrupt(
266  int   port,
267  char  ch
268)
269{
270  /*
271   *  If this is the first character then we need to prime the pump
272   */
273
274  if ( Is_TX_active[ port ] == false ) {
275    Is_TX_active[ port ] = true;
276    console_outbyte_polled( port, ch );
277    return;
278  }
279
280  while ( Ring_buffer_Is_full( &TX_Buffer[ port ] ) );
281
282  Ring_buffer_Add_character( &TX_Buffer[ port ], ch );
283}
284
285#endif /* CONSOLE_USE_INTERRUPTS */
286
287/*
288 *  Console Termios Support Entry Points
289 *
290 */
291
292ssize_t console_write_support (int minor, const char *buf, size_t len)
293{
294  int nwrite = 0;
295
296  while (nwrite < len) {
297#if (CONSOLE_USE_INTERRUPTS)
298    console_outbyte_interrupt( minor, *buf++ );
299#else
300    console_outbyte_polled( minor, *buf++ );
301#endif
302    nwrite++;
303  }
304  return nwrite;
305}
306
307/*
308 *  Console Device Driver Entry Points
309 *
310 */
311
312rtems_device_driver console_initialize(
313  rtems_device_major_number  major,
314  rtems_device_minor_number  minor,
315  void                      *arg
316)
317{
318  rtems_status_code status;
319
320  rtems_termios_initialize();
321
322  /*
323   *  Register Device Names
324   */
325
326  status = rtems_io_register_name( "/dev/console", major, 0 );
327  if (status != RTEMS_SUCCESSFUL)
328    rtems_fatal_error_occurred(status);
329
330  status = rtems_io_register_name( "/dev/console_b", major, 1 );
331  if (status != RTEMS_SUCCESSFUL)
332    rtems_fatal_error_occurred(status);
333
334  /*
335   *  Initialize Hardware
336   */
337
338  LEON_REG.UART_Control_1 |= LEON_REG_UART_CTRL_RE | LEON_REG_UART_CTRL_TE;
339  LEON_REG.UART_Control_2 |= LEON_REG_UART_CTRL_RE | LEON_REG_UART_CTRL_TE |
340        LEON_REG_UART_CTRL_RI;  /* rx irq default enable for remote debugger */
341  LEON_REG.UART_Status_1 = 0;
342  LEON_REG.UART_Status_2 = 0;
343#if (CONSOLE_USE_INTERRUPTS)
344  console_initialize_interrupts();
345#endif
346
347  return RTEMS_SUCCESSFUL;
348}
349
350rtems_device_driver console_open(
351  rtems_device_major_number major,
352  rtems_device_minor_number minor,
353  void                    * arg
354)
355{
356  rtems_status_code sc;
357#if (CONSOLE_USE_INTERRUPTS)
358  rtems_libio_open_close_args_t *args = arg;
359  static const rtems_termios_callbacks intrCallbacks = {
360    NULL,                        /* firstOpen */
361    NULL,                        /* lastClose */
362    NULL,                        /* pollRead */
363    console_write_support,       /* write */
364    NULL,                        /* setAttributes */
365    NULL,                        /* stopRemoteTx */
366    NULL,                        /* startRemoteTx */
367    0                            /* outputUsesInterrupts */
368  };
369#else
370  static const rtems_termios_callbacks pollCallbacks = {
371    NULL,                        /* firstOpen */
372    NULL,                        /* lastClose */
373    console_inbyte_nonblocking,  /* pollRead */
374    console_write_support,       /* write */
375    NULL,                        /* setAttributes */
376    NULL,                        /* stopRemoteTx */
377    NULL,                        /* startRemoteTx */
378    0                            /* outputUsesInterrupts */
379  };
380#endif
381
382  assert( minor <= 1 );
383  if ( minor > 2 )
384    return RTEMS_INVALID_NUMBER;
385
386#if (CONSOLE_USE_INTERRUPTS)
387  sc = rtems_termios_open (major, minor, arg, &intrCallbacks);
388
389  console_termios_data[ minor ] = args->iop->data1;
390#else
391  sc = rtems_termios_open (major, minor, arg, &pollCallbacks);
392#endif
393
394  return RTEMS_SUCCESSFUL;
395}
396
397rtems_device_driver console_close(
398  rtems_device_major_number major,
399  rtems_device_minor_number minor,
400  void                    * arg
401)
402{
403  return rtems_termios_close (arg);
404}
405
406rtems_device_driver console_read(
407  rtems_device_major_number major,
408  rtems_device_minor_number minor,
409  void                    * arg
410)
411{
412  return rtems_termios_read (arg);
413}
414
415rtems_device_driver console_write(
416  rtems_device_major_number major,
417  rtems_device_minor_number minor,
418  void                    * arg
419)
420{
421  return rtems_termios_write (arg);
422}
423
424rtems_device_driver console_control(
425  rtems_device_major_number major,
426  rtems_device_minor_number minor,
427  void                    * arg
428)
429{
430  return rtems_termios_ioctl (arg);
431}
432
433/* putchar/getchar for printk */
434
435static void bsp_out_char (char c)
436{
437  console_outbyte_polled(0, c);
438}
439
440BSP_output_char_function_type BSP_output_char = bsp_out_char;
441
442static int bsp_in_char(void)
443{
444  int tmp;
445
446  while ((tmp = console_inbyte_nonblocking(0)) < 0);
447  return tmp;
448}
449
450BSP_polling_getchar_function_type BSP_poll_char = bsp_in_char;
Note: See TracBrowser for help on using the repository browser.