source: rtems/c/src/lib/libbsp/i386/pc386/console/console.c @ ab0df696

4.104.114.84.95
Last change on this file since ab0df696 was ab0df696, checked in by Joel Sherrill <joel.sherrill@…>, on 08/05/98 at 15:15:46

Automatic CPU type detection code from Eric Valette <valette@…>.
Enabled on the pc386.

  • Property mode set to 100644
File size: 12.7 KB
Line 
1/*-------------------------------------------------------------------------+
2| console.c v1.1 - PC386 BSP - 1997/08/07
3+--------------------------------------------------------------------------+
4| This file contains the PC386 console I/O package.
5+--------------------------------------------------------------------------+
6| (C) Copyright 1997 -
7| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
8|
9| http://pandora.ist.utl.pt
10|
11| Instituto Superior Tecnico * Lisboa * PORTUGAL
12+--------------------------------------------------------------------------+
13| Disclaimer:
14|
15| This file is provided "AS IS" without warranty of any kind, either
16| expressed or implied.
17+--------------------------------------------------------------------------+
18| This code is based on:
19|   console.c,v 1.4 1995/12/19 20:07:23 joel Exp - go32 BSP
20| With the following copyright notice:
21| **************************************************************************
22| *  COPYRIGHT (c) 1989-1998.
23| *  On-Line Applications Research Corporation (OAR).
24| *  Copyright assigned to U.S. Government, 1994.
25| *
26| *  The license and distribution terms for this file may be
27| *  found in found in the file LICENSE in this distribution or at
28| *  http://www.OARcorp.com/rtems/license.html.
29| **************************************************************************
30|
31|  $Id$
32+--------------------------------------------------------------------------*/
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <assert.h>
37
38#include <bsp.h>
39#include <irq.h>
40#include <rtems/libio.h>
41#include <termios.h>
42#include <pc386uart.h>
43
44int PC386ConsolePort = PC386_CONSOLE_PORT_CONSOLE;
45
46static int conSetAttr(int minor, const struct termios *);
47
48/*-------------------------------------------------------------------------+
49| External Prototypes
50+--------------------------------------------------------------------------*/
51extern void     _IBMPC_keyboard_isr(void);
52extern void     _IBMPC_keyboard_isr_on(const rtems_irq_connect_data*);
53extern void     _IBMPC_keyboard_isr_off(const rtems_irq_connect_data*);
54extern int      _IBMPC_keyboard_isr_is_on(const rtems_irq_connect_data*);
55
56static rtems_irq_connect_data console_isr_data = {PC_386_KEYBOARD,
57                                                   _IBMPC_keyboard_isr,
58                                                   _IBMPC_keyboard_isr_on,
59                                                   _IBMPC_keyboard_isr_off,
60                                                   _IBMPC_keyboard_isr_is_on};
61                                                   
62
63extern rtems_boolean _IBMPC_scankey(char *);  /* defined in 'inch.c' */
64
65void console_reserve_resources(rtems_configuration_table *conf)
66{
67  if(PC386ConsolePort != PC386_CONSOLE_PORT_CONSOLE)
68    {
69      rtems_termios_reserve_resources(conf, 1);
70    }
71  return;
72}
73
74void __assert(const char *file, int line, const char *msg)
75{
76  static   char buf[20];
77  static   char exit_msg[] = "EXECUTIVE SHUTDOWN! Any key to reboot...";
78  static   char assert_msg[] = "assert failed: ";
79  unsigned char  ch;
80  const    unsigned char *cp;
81       
82 
83  /*
84   * Note we cannot call exit or printf from here,
85   * assert can fail inside ISR too
86   */
87  if(PC386ConsolePort == PC386_CONSOLE_PORT_CONSOLE)
88    {
89      printk("\nassert failed: %s: ", file);
90      printk("%d: ", line);
91      printk("%s\n\n", msg);
92      printk(exit_msg);
93      while(!_IBMPC_scankey(&ch));
94      printk("\n\n");
95    }
96  else
97    {
98      PC386_uart_intr_ctrl(PC386ConsolePort, PC386_UART_INTR_CTRL_DISABLE);
99     
100      PC386_uart_polled_write(PC386ConsolePort, '\r');
101      PC386_uart_polled_write(PC386ConsolePort, '\n');
102     
103      for(cp=assert_msg; *cp!=0; cp++)
104        {
105          PC386_uart_polled_write(PC386ConsolePort, *cp);
106        }
107
108      for(cp=file; *cp!=0; cp++)
109        {
110          PC386_uart_polled_write(PC386ConsolePort, *cp);
111        }
112     
113      PC386_uart_polled_write(PC386ConsolePort, ':');
114      PC386_uart_polled_write(PC386ConsolePort, ' ');
115
116      sprintf(buf, "%d: ", line);
117
118      for(cp=buf; *cp!=0; cp++)
119        {
120          PC386_uart_polled_write(PC386ConsolePort, *cp);
121        }
122
123      for(cp=msg; *cp!=0; cp++)
124        {
125          PC386_uart_polled_write(PC386ConsolePort, *cp);
126        }
127
128      PC386_uart_polled_write(PC386ConsolePort, '\r');
129      PC386_uart_polled_write(PC386ConsolePort, '\n');
130      PC386_uart_polled_write(PC386ConsolePort, '\r');
131      PC386_uart_polled_write(PC386ConsolePort, '\n');
132         
133      for(cp=exit_msg; *cp != 0; cp++)
134        {
135          PC386_uart_polled_write(PC386ConsolePort, *cp);
136        }
137
138      PC386_uart_polled_read(PC386ConsolePort);
139
140      PC386_uart_polled_write(PC386ConsolePort, '\r');
141      PC386_uart_polled_write(PC386ConsolePort, '\n');
142      PC386_uart_polled_write(PC386ConsolePort, '\r');
143      PC386_uart_polled_write(PC386ConsolePort, '\n');
144
145    }
146
147  rtemsReboot();
148}
149
150
151/*-------------------------------------------------------------------------+
152| Console device driver INITIALIZE entry point.
153+--------------------------------------------------------------------------+
154| Initilizes the I/O console (keyboard + VGA display) driver.
155+--------------------------------------------------------------------------*/
156rtems_device_driver
157console_initialize(rtems_device_major_number major,
158                   rtems_device_minor_number minor,
159                   void                      *arg)
160{
161  rtems_status_code status;
162
163  /* Initialize video */
164  _IBMPC_initVideo();
165
166  if(PC386ConsolePort == PC386_CONSOLE_PORT_CONSOLE)
167    {
168
169      /* Install keyboard interrupt handler */
170  status = pc386_install_rtems_irq_handler(&console_isr_data);
171 
172  if (!status)
173        {
174          printk("Error installing keyboard interrupt handler!\n");
175          rtems_fatal_error_occurred(status);
176        }
177     
178      status = rtems_io_register_name("/dev/console", major, 0);
179      if (status != RTEMS_SUCCESSFUL)
180        {
181          printk("Error registering console device!\n");
182          rtems_fatal_error_occurred(status);
183        }
184      printk("Initialized console on port CONSOLE\n\n");
185    }
186  else
187    {
188      /*
189       * Set up TERMIOS
190       */
191      rtems_termios_initialize ();
192     
193      /*
194       * Do device-specific initialization
195       */
196     
197      /* 9600-8-N-1 */
198      PC386_uart_init(PC386ConsolePort, 9600, 0);
199     
200     
201      /* Set interrupt handler */
202      if(PC386ConsolePort == PC386_UART_COM1)
203        {
204          console_isr_data.name = PC386_UART_COM1_IRQ;
205          console_isr_data.hdl  = PC386_uart_termios_isr_com1;
206         
207        }
208      else
209        {
210          assert(PC386ConsolePort == PC386_UART_COM2);
211          console_isr_data.name = PC386_UART_COM2_IRQ;
212          console_isr_data.hdl  = PC386_uart_termios_isr_com2;
213        }
214
215      status =pc386_install_rtems_irq_handler(&console_isr_data);
216      /*
217       * Register the device
218       */
219      status = rtems_io_register_name ("/dev/console", major, 0);
220      if (status != RTEMS_SUCCESSFUL)
221        {
222          printk("Error registering console device!\n");
223          rtems_fatal_error_occurred (status);
224        }
225
226      if(PC386ConsolePort == PC386_UART_COM1)
227        {
228          printk("Initialized console on port COM1 9600-8-N-1\n\n");
229        }
230      else
231        {
232          printk("Initialized console on port COM2 9600-8-N-1\n\n");
233        }
234    }
235#define  DISPLAY_CPU_INFO
236#ifdef DISPLAY_CPU_INFO
237  printCpuInfo();
238#endif
239 
240  return RTEMS_SUCCESSFUL;
241} /* console_initialize */
242
243
244/*-------------------------------------------------------------------------+
245| Console device driver OPEN entry point
246+--------------------------------------------------------------------------*/
247rtems_device_driver
248console_open(rtems_device_major_number major,
249                rtems_device_minor_number minor,
250                void                      *arg)
251{
252  rtems_status_code              status;
253  static rtems_termios_callbacks cb =
254  {
255    NULL,                     /* firstOpen */
256    NULL,                     /* lastClose */
257    NULL,                     /* pollRead */
258    PC386_uart_termios_write_com1, /* write */
259    conSetAttr,               /* setAttributes */
260    NULL,                     /* stopRemoteTx */
261    NULL,                     /* startRemoteTx */
262    1                         /* outputUsesInterrupts */
263  };
264
265  if(PC386ConsolePort == PC386_CONSOLE_PORT_CONSOLE)
266    {
267      return RTEMS_SUCCESSFUL;
268    }
269
270  if(PC386ConsolePort == PC386_UART_COM2)
271    {
272      cb.write = PC386_uart_termios_write_com2;
273    }
274
275  status = rtems_termios_open (major, minor, arg, &cb);
276
277  if(status != RTEMS_SUCCESSFUL)
278    {
279      printk("Error openning console device\n");
280      return status;
281    }
282
283  /*
284   * Pass data area info down to driver
285   */
286  PC386_uart_termios_set(PC386ConsolePort,
287                         ((rtems_libio_open_close_args_t *)arg)->iop->data1);
288 
289  /* Enable interrupts  on channel */
290  PC386_uart_intr_ctrl(PC386ConsolePort, PC386_UART_INTR_CTRL_TERMIOS);
291
292  return RTEMS_SUCCESSFUL;
293}
294
295/*-------------------------------------------------------------------------+
296| Console device driver CLOSE entry point
297+--------------------------------------------------------------------------*/
298rtems_device_driver
299console_close(rtems_device_major_number major,
300              rtems_device_minor_number minor,
301              void                      *arg)
302{
303  rtems_device_driver res = RTEMS_SUCCESSFUL;
304 
305  if(PC386ConsolePort != PC386_CONSOLE_PORT_CONSOLE)
306    {
307      res =  rtems_termios_close (arg);
308    }
309  pc386_remove_rtems_irq_handler (&console_isr_data);
310 
311  return res;
312} /* console_close */
313
314 
315/*-------------------------------------------------------------------------+
316| Console device driver READ entry point.
317+--------------------------------------------------------------------------+
318| Read characters from the I/O console. We only have stdin.
319+--------------------------------------------------------------------------*/
320rtems_device_driver
321console_read(rtems_device_major_number major,
322             rtems_device_minor_number minor,
323             void                      *arg)
324{
325  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg;
326  char                  *buffer  = rw_args->buffer;
327  int            count, maximum  = rw_args->count;
328
329  if(PC386ConsolePort != PC386_CONSOLE_PORT_CONSOLE)
330    {
331      return rtems_termios_read (arg);
332    }
333 
334  for (count = 0; count < maximum; count++)
335  {
336    /* Get character */
337    buffer[count] = _IBMPC_inch_sleep();
338
339    /* Echo character to screen */
340    _IBMPC_outch(buffer[count]);
341    if (buffer[count] == '\r')
342      {
343        _IBMPC_outch('\n');  /* CR = CR + LF */
344      }
345
346    if (buffer[count] == '\n' || buffer[count] == '\r')
347    {
348      /* What if this goes past the end of the buffer?  We're hosed. [bhc] */
349      buffer[count++]  = '\n';
350      buffer[count]    = '\0';
351      break;
352    }
353  }
354 
355  rw_args->bytes_moved = count;
356  return ((count >= 0) ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED);
357} /* console_read */
358 
359
360/*-------------------------------------------------------------------------+
361| Console device driver WRITE entry point.
362+--------------------------------------------------------------------------+
363| Write characters to the I/O console. Stderr and stdout are the same.
364+--------------------------------------------------------------------------*/
365rtems_device_driver
366console_write(rtems_device_major_number major,
367              rtems_device_minor_number minor,
368              void                    * arg)
369{
370  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg;
371  char                  *buffer  = rw_args->buffer;
372  int            count, maximum  = rw_args->count;
373
374  if(PC386ConsolePort != PC386_CONSOLE_PORT_CONSOLE)
375    {
376      return rtems_termios_write (arg);
377    }
378 
379  for (count = 0; count < maximum; count++)
380  {
381    _IBMPC_outch(buffer[count]);
382    if (buffer[count] == '\n')
383      _IBMPC_outch('\r');            /* LF = LF + CR */
384  }
385
386  rw_args->bytes_moved = maximum;
387  return RTEMS_SUCCESSFUL;
388} /* console_write */
389
390
391 
392/*
393 * Handle ioctl request.
394 */
395rtems_device_driver
396console_control(rtems_device_major_number major,
397                rtems_device_minor_number minor,
398                void                      * arg
399)
400{
401  if(PC386ConsolePort != PC386_CONSOLE_PORT_CONSOLE)
402    {
403      return rtems_termios_ioctl (arg);
404    }
405
406  return RTEMS_SUCCESSFUL;
407}
408
409static int
410conSetAttr(int minor, const struct termios *t)
411{
412  int baud;
413
414  switch (t->c_cflag & CBAUD)
415    {
416    case B50:   
417      baud = 50;
418      break;
419    case B75:   
420      baud = 75;       
421      break;
422    case B110: 
423      baud = 110;       
424      break;
425    case B134: 
426      baud = 134;       
427      break;
428    case B150: 
429      baud = 150;       
430      break;
431    case B200:
432      baud = 200;       
433      break;
434    case B300: 
435      baud = 300;
436      break;
437    case B600: 
438      baud = 600;       
439      break;
440    case B1200:
441      baud = 1200;
442      break;
443    case B1800:
444      baud = 1800;     
445      break;
446    case B2400:
447      baud = 2400;
448      break;
449    case B4800:
450      baud = 4800;
451      break;
452    case B9600:
453      baud = 9600;
454      break;
455    case B19200:
456      baud = 19200;
457      break;
458    case B38400:
459      baud = 38400;
460      break;
461    case B57600:       
462      baud = 57600;
463      break;
464    case B115200:
465      baud = 115200;
466      break;
467    default:
468      baud = 0;
469      rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR);
470      return 0;
471    }
472
473  PC386_uart_set_baud(PC386ConsolePort, baud);
474
475  return 0;
476}
477
478
479
480
481
482
483
484
485
Note: See TracBrowser for help on using the repository browser.