source: rtems/c/src/tests/libtests/termios/init.c @ 8ef3818

4.104.114.84.95
Last change on this file since 8ef3818 was 8ef3818, checked in by Joel Sherrill <joel.sherrill@…>, on 06/12/00 at 19:57:02

Patch from John Cotton <john.cotton@…>, Charles-Antoine Gauthier
<charles.gauthier@…>, and Darlene A. Stewart
<Darlene.Stewart@…> to add support for a number of very
significant things:

+ BSPs for many variations on the Motorola MBX8xx board series
+ Cache Manager including initial support for m68040

and PowerPC

+ Rework of mpc8xx libcpu code so all mpc8xx CPUs now use

same code base.

+ Rework of eth_comm BSP to utiltize above.

John reports this works on the 821 and 860

  • Property mode set to 100644
File size: 17.0 KB
Line 
1/*
2 * RTEMS configuration/initialization
3 *
4 * This program may be distributed and used for any purpose.
5 * I ask only that you:
6 *  1. Leave this author information intact.
7 *  2. Document any changes you make.
8 *
9 * W. Eric Norum
10 * Saskatchewan Accelerator Laboratory
11 * University of Saskatchewan
12 * Saskatoon, Saskatchewan, CANADA
13 * eric@skatter.usask.ca
14 *
15 * Additions:
16 * Charles-Antoine Gauthier
17 * Software Engineering Group
18 * Institute for Information Technology
19 * National Research Council of Canada
20 * charles.gauthier@nrc.ca
21 *
22 *  $Id$
23 */
24
25#include <bsp.h>
26
27
28#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
29#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
30
31#define CONFIGURE_MAXIMUM_TASKS       1
32
33#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
34
35#define CONFIGURE_MICROSECONDS_PER_TICK 1000
36
37#define CONFIGURE_INIT
38
39rtems_task Init (rtems_task_argument argument);
40
41#include <confdefs.h>
42
43#include <stdio.h>
44#include <unistd.h>
45#include <termios.h>
46#include <errno.h>
47#include <string.h>
48
49#if !defined(fileno)
50int fileno( FILE *stream); /* beyond ANSI */
51#endif
52
53/* Some of the termios dumping code depends on bit positions! */
54
55void print_32bits( unsigned long bits, unsigned char size, char * names[] )
56{
57  unsigned char i;
58
59  for( i = 0; i < size; i++ ) {
60    if( (bits >> i) & 0x1 )
61      printf( "%s ", names[i] );
62  }
63}
64
65
66void print_c_iflag( struct termios * tp )
67{
68  char * c_iflag_bits [] = {
69    "IGNBRK",   /* 0000001 */
70    "BRKINT",   /* 0000002 */
71    "IGNPAR",   /* 0000004 */
72    "PARMRK",   /* 0000010 */
73    "INPCK",    /* 0000020 */
74    "ISTRIP",   /* 0000040 */
75    "INLCR",    /* 0000100 */
76    "IGNCR",    /* 0000200 */
77    "ICRNL",    /* 0000400 */
78    "IUCLC",    /* 0001000 */
79    "IXON",     /* 0002000 */
80    "IXANY",    /* 0004000 */
81    "IXOFF",    /* 0010000 */
82    "IMAXBEL",  /* 0020000 */
83    "unknown",  /* 0040000 */
84    "unknown",  /* 0100000 */
85    "unknown",  /* 0200000 */
86    "unknown",  /* 0400000 */
87    "unknown",  /* 1000000 */
88    "unknown",  /* 2000000 */
89    "unknown"   /* 4000000 */
90  };
91
92  printf( "c_iflag = 0x%08x\n\t", tp->c_iflag );
93  print_32bits( tp->c_iflag, sizeof( c_iflag_bits )/sizeof( char * ), c_iflag_bits );
94  printf( "\n" );
95}
96
97
98void print_c_oflag( struct termios * tp )
99{
100  printf( "c_oflag = 0x%08x\n\t", tp->c_oflag );
101
102  if( tp->c_oflag & OPOST )
103    printf( "OPOST " );
104   
105  if( tp->c_oflag & OLCUC )
106    printf( "OLCUC " );
107   
108  if( tp->c_oflag & ONLCR )
109    printf( "ONLCR " );
110   
111  if( tp->c_oflag & OCRNL )
112    printf( "OCRNL " );
113   
114  if( tp->c_oflag & ONOCR )
115    printf( "ONOCR " );
116   
117  if( tp->c_oflag & ONLRET )
118    printf( "ONLRET " );
119   
120  if( tp->c_oflag & OFILL )
121    printf( "OFILL " );
122   
123  if( tp->c_oflag & OFDEL )
124    printf( "OFDEL " );
125
126  switch( tp->c_oflag & NLDLY ) {
127    case NL0:
128      printf( "NL0 " );
129      break;
130     
131    case NL1:
132      printf( "NL1 " );
133      break;
134  }
135 
136  switch( tp->c_oflag & CRDLY ) {
137    case CR0:
138      printf( "CR0 " );
139      break;
140     
141    case CR1:
142      printf( "CR1 " );
143      break;
144     
145    case CR2:
146      printf( "CR2 " );
147      break;
148     
149    case CR3:
150      printf( "CR3 " );
151      break;
152  }
153 
154  switch( tp->c_oflag & TABDLY ) {
155    case TAB0:
156      printf( "TAB0 " );
157      break;
158     
159    case TAB1:
160      printf( "TAB1 " );
161      break;
162     
163    case TAB2:
164      printf( "TAB2 " );
165      break;
166     
167    case TAB3:
168      printf( "TAB3 " );
169      break;
170  }
171 
172  switch( tp->c_oflag & BSDLY ) {
173    case BS0:
174      printf( "BS0 " );
175      break;
176     
177    case BS1:
178      printf( "BS1 " );
179      break;
180  }
181 
182  switch( tp->c_oflag & VTDLY ) {
183    case VT0:
184      printf( "VT0 " );
185      break;
186     
187    case VT1:
188      printf( "VT1 " );
189      break;
190  }
191 
192  switch( tp->c_oflag & FFDLY ) {
193    case FF0:
194      printf( "FF0" );
195      break;
196     
197    case FF1:
198      printf( "FF1" );
199      break;
200  }
201  printf( "\n" );
202}
203
204
205void print_c_lflag( struct termios * tp )
206{
207  char * c_lflag_bits [] = {
208    "ISIG",        /* 0000001 */
209    "ICANON",      /* 0000002 */
210    "XCASE",       /* 0000004 */
211    "ECHO",        /* 0000010 */
212    "ECHOE",       /* 0000020 */
213    "ECHOK",       /* 0000040 */
214    "ECHONL",      /* 0000100 */
215    "NOFLSH",      /* 0000200 */
216    "TOSTOP",      /* 0000400 */
217    "ECHOCTL",     /* 0001000 */
218    "ECHOPRT",     /* 0002000 */
219    "ECHOKE",      /* 0004000 */
220    "FLUSHO",      /* 0010000 */
221    "unknown",     /* 0020000 */
222    "PENDIN",      /* 0040000 */
223    "IEXTEN",      /* 0100000 */
224    "unknown",     /* 0200000 */
225    "unknown",     /* 0400000 */
226    "unknown",     /* 1000000 */
227    "unknown",     /* 2000000 */
228    "unknown",     /* 4000000 */
229  };
230 
231  printf( "c_lflag = 0x%08x\n\t", tp->c_lflag );
232  print_32bits( tp->c_lflag, sizeof( c_lflag_bits )/sizeof( char * ), c_lflag_bits );
233  printf( "\n" );
234}
235
236
237void print_c_cflag( struct termios * tp )
238{
239  int baud;
240 
241  printf( "c_cflag = 0x%08x\n", tp->c_cflag );
242 
243  switch( baud = (tp->c_cflag & CBAUD) ) {
244    case B0:
245      printf( "\tCBAUD =\tB0\n" );
246      break;
247     
248    case B50:
249      printf( "\tCBAUD =\tB50\n" );
250      break;
251     
252    case B75:
253      printf( "\tCBAUD =\tB75\n" );
254      break;
255     
256    case B110:
257      printf( "\tCBAUD =\tB110\n" );
258      break;
259     
260    case B134:
261      printf( "\tCBAUD =\tB134\n" );
262      break;
263     
264    case B150:
265      printf( "\tCBAUD =\tB150\n" );
266      break;
267     
268    case B200:
269      printf( "\tCBAUD =\tB200\n" );
270      break;
271     
272    case B300:
273      printf( "\tCBAUD =\tB300\n" );
274      break;
275     
276    case B600:
277      printf( "\tCBAUD =\tB600\n" );
278      break;
279     
280    case B1200:
281      printf( "\tCBAUD =\tB1200\n" );
282      break;
283     
284    case B1800:
285      printf( "\tCBAUD =\tB1800\n" );
286      break;
287     
288    case B2400:
289      printf( "\tCBAUD =\tB2400\n" );
290      break;
291     
292    case B4800:
293      printf( "\tCBAUD =\tB4800\n" );
294      break;
295     
296    case B9600:
297      printf( "\tCBAUD =\tB9600\n" );
298      break;
299     
300    case B19200:
301      printf( "\tCBAUD =\tB19200\n" );
302      break;
303     
304    case B38400:
305      printf( "\tCBAUD =\tB38400\n" );
306      break;
307     
308    case B57600:
309      printf( "\tCBAUD =\tB57600\n" );
310      break;
311     
312    case B115200:
313      printf( "\tCBAUD =\tB115200\n" );
314      break;
315     
316    case B230400:
317      printf( "\tCBAUD =\tB230400\n" );
318      break;
319     
320    case B460800:
321      printf( "\tCBAUD =\tB460800\n" );
322      break;
323     
324    default:
325      printf( "\tCBAUD =\tunknown (0x%08x)\n", baud );
326      break;
327    }
328
329  switch( tp->c_cflag & CSIZE ) {
330    case CS5:
331      printf( "\tCSIZE =\tCS5\n" );
332      break;
333     
334    case CS6:
335      printf( "\tCSIZE =\tCS6\n" );
336      break;
337     
338    case CS7:
339      printf( "\tCSIZE =\tCS7\n" );
340      break;
341     
342    case CS8:
343      printf( "\tCSIZE =\tCS8\n" );
344      break;
345  }
346
347  if( tp->c_cflag & CSTOPB )
348    printf( "\tCSTOPB set: send 2 stop bits\n" );
349  else
350    printf( "\tCSTOPB clear: send 1 stop bit\n" );
351 
352  if( tp->c_cflag & PARENB )
353    printf( "\tPARENB set: parity enabled\n" );
354  else
355    printf( "\tPARENB clear: parity disabled\n" );
356 
357  if( tp->c_cflag & PARODD )
358    printf( "\tPARODD set: parity odd\n" );
359  else
360    printf( "\tPARODD clear: parity even\n" );
361 
362  if( tp->c_cflag & CREAD )
363    printf( "\tCREAD set: receiver enabled\n" );
364  else
365    printf( "\tCREAD clear: treceiver disabled\n" );
366 
367  if( tp->c_cflag & HUPCL )
368    printf( "\tHUPCL set: enabled\n" );
369  else
370    printf( "\tHUPCL clear: disabled\n" );
371 
372  if( tp->c_cflag & CLOCAL )
373    printf( "\tCLOCAL set: ignore modem lines\n" );
374  else
375    printf( "\tCLOCAL clear: don't ignore modem lines\n" );
376 
377#if defined(CBAUDEX)
378  if( tp->c_cflag & CBAUDEX )
379    printf( "\tCBAUDEX set: What does this do?\n" );
380  else
381    printf( "\tCBAUDEX clear: What does this do?\n" );
382#endif
383 
384  if( tp->c_cflag & CRTSCTS )
385    printf( "\tCRTSCTS: harware flow control enabled?\n" );
386  else
387    printf( "\tCRTSCTS: hardware flow control disabled?\n" );
388}
389
390
391void print_c_cc( struct termios * tp )
392{
393  int i;
394  char * cc_index_names [NCCS] = {
395    "[VINTR]   ",   /* 0 */
396    "[VQUIT]   ",   /* 1 */
397    "[VERASE]  ",   /* 2 */
398    "[VKILL]   ",   /* 3 */
399    "[VEOF]    ",   /* 4 */
400    "[VTIME]   ",   /* 5 */
401    "[VMIN]    ",   /* 6 */
402    "[VSWTC    ",   /* 7 */
403    "[VSTART]  ",   /* 8 */
404    "[VSTOP]   ",   /* 9 */
405    "[VSUSP]   ",   /* 10 */
406    "[VEOL]    ",   /* 11 */
407    "[VREPRINT]",   /* 12 */
408    "[VDISCARD]",   /* 13 */
409    "[VWERASE] ",   /* 14 */
410    "[VLNEXT   ",   /* 15 */
411    "[VEOL2]   ",   /* 16 */
412    "unknown   ",   /* 17 */
413    "unknown   ",   /* 18 */
414  };
415
416  for( i = 0; i < NCCS; i++ ) {
417    printf( "c_cc%s = 0x%08x\n", cc_index_names[i], tp->c_cc[i] );
418  }
419}
420
421
422void print_termios( struct termios *tp )
423{
424  printf( "\nLooking at the current termios settings:\n\n" );
425  print_c_iflag( tp );
426  print_c_oflag( tp );
427  print_c_cflag( tp );
428  print_c_lflag( tp );
429  print_c_cc( tp );
430  printf( "\n" );
431}
432
433
434unsigned long get_baud_rate( void )
435{
436  unsigned long baud_rate;
437 
438  while( TRUE ) {
439    printf( "Enter the numerical value for the new baud rate.\n" );
440    printf( "Choices are: 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800\n" );
441    printf( "2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800\n" );
442    printf( "\nYour choice: " );
443    scanf( "%lu", &baud_rate );
444    printf( "\n" );
445    switch( baud_rate ) {
446      case 50:     return B50;
447      case 75:     return B75;
448      case 110:    return B110;
449      case 134:    return B134;
450      case 150:    return B150;
451      case 200:    return B200;
452      case 300:    return B300;
453      case 600:    return B600;
454      case 1200:   return B1200;
455      case 1800:   return B1800;
456      case 2400:   return B2400;
457      case 4800:   return B4800;
458      case 9600:   return B9600;
459      case 19200:  return B19200;
460      case 38400:  return B38400;
461      case 57600:  return B57600;
462      case 115200: return B115200;
463      case 230400: return B230400;
464      case 460800: return B460800;
465
466      default:
467        printf( "%lu is not a valid choice. Try again.\n\n", baud_rate );
468        break;
469    }
470  }
471}
472
473
474unsigned long get_parity()
475{
476  int parity;
477 
478  while( TRUE ) {
479    printf( "Enter the numerical value for the new parity\n" );
480    printf( "Choices are: 0 for no parity, 1 for even parity, 2 for odd parity\n" );
481    printf( "\nYour choice: " );
482    scanf( "%d", &parity );
483    printf( "\n" );
484    switch( parity ) {
485      case 0:
486        return 0;
487
488      case 1:
489        return PARENB;
490
491      case 2:
492        return PARENB | PARODD;
493
494      default:
495        printf( "%d is not a valid choice. Try again.\n\n", parity );
496        break;
497    }
498  }
499}
500
501
502unsigned long get_stop_bits()
503{
504  int stop_bits;
505
506  while( TRUE ) {
507    printf( "Enter the numerical value for the new number of stop bits\n" );
508    printf( "Choices are: 1 or 2\n" );
509    printf( "\nYour choice: " );
510    scanf( "%d", &stop_bits );
511    printf( "\n" );
512    switch( stop_bits ) {
513      case 1:
514        return 0;
515       
516      case 2:
517        return CSTOPB;
518
519      default:
520        printf( "%d is not a valid choice. Try again.\n\n", stop_bits );
521        break;
522    }
523  }
524}
525
526
527unsigned long get_data_bits()
528{
529  int data_bits;
530
531  while( TRUE ) {
532    printf( "Enter the numerical value for the new number of data bits\n" );
533    printf( "Choices are: 5, 6, 7 or 8\n" );
534    printf( "\nYour choice: " );
535    scanf( "%d", &data_bits );
536    printf( "\n" );
537    switch( data_bits ) {
538      case 5:
539        return CS5;
540       
541      case 6:
542        return CS6;
543       
544      case 7:
545        return CS7;
546       
547      case 8:
548        return CS8;
549
550      default:
551        printf( "%d is not a valid choice. Try again.\n\n", data_bits );
552        break;
553    }
554  }
555}
556
557
558void change_line_settings( struct termios *tp )
559{
560  unsigned long baud_rate, parity, stop_bits, data_bits, sleep_time;
561   
562  printf( "\nSetting line characteristics\n\n" );
563 
564  baud_rate = get_baud_rate();
565  parity = get_parity();
566  stop_bits = get_stop_bits();
567  data_bits = get_data_bits();
568
569  printf( "NOTE: You will not see output until you switch your terminal settings!\n" );
570  printf( "WARNING: If you do not switch your terminal settings, your terminal may hang.\n" );
571  printf( "Enter the number of seconds the test will wait for you to switch your terminal\n" );
572  printf( "settings before it continues\n" );
573  printf( "Sleep time (in seconds): " );
574  scanf( "%lu", &sleep_time );
575  printf( "\n" );
576  printf( "Setting line to new termios settings in %lu seconds.\n", sleep_time );
577
578  sleep( sleep_time );
579 
580  tp->c_cflag = CLOCAL | CREAD | parity | stop_bits | data_bits | baud_rate;
581  if( tcsetattr( fileno( stdin ), TCSADRAIN, tp ) < 0 ) {
582    perror( "change_line_settings(): tcsetattr() failed" );
583    exit( 1 );
584  }
585  printf( "Line settings set.\n" );
586}
587
588
589void canonical_input( struct termios *tp )
590{
591    char buffer[256];
592    char c, first_time = TRUE;
593   
594  printf( "\nTesting canonical input\n\n" );
595
596  printf( "Setting line to canonical input mode.\n" );
597  tp->c_lflag = ISIG | ICANON | ECHO | ECHONL | ECHOK | ECHOE | ECHOPRT | ECHOCTL | IEXTEN;
598  tp->c_iflag = BRKINT | ICRNL | IXON | IMAXBEL;
599  if( tcsetattr( fileno( stdin ), TCSADRAIN, tp ) < 0 ) {
600    perror( "canonical_input(): tcsetattr() failed" );
601    exit( 1 );
602  }
603 
604  while ( ( c = getchar () ) != '\n');
605  printf( "Testing getchar(). Type some text followed by carriage return\n" );
606  printf( "Each character you entered will be echoed back to you\n\n" );
607  while ( ( c = getchar () ) != '\n') {
608    if( first_time ) {
609      printf( "\nYou typed:\n");
610      first_time = FALSE;
611    }
612    printf( "%c", c );
613  }
614  printf( "\n\nCanonical input test done.\n" );
615}
616
617
618/*
619 * Test raw (ICANON=0) input
620 */
621void do_raw_input( int vmin, int vtime )
622{
623  int i;
624  struct termios old, new;
625  rtems_interval ticksPerSecond, then, now;
626  unsigned int msec;
627  unsigned long count;
628  int nread;
629  unsigned char cbuf[100];
630
631  printf( "Raw input test with VMIN=%d  VTIME=%d\n", vmin, vtime );
632 
633  rtems_clock_get( RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond );
634  if ( tcgetattr( fileno ( stdin ), &old ) < 0 ) {
635    perror( "do_raw_input(): tcgetattr() failed" );
636    return;
637  }
638 
639  new = old;
640  new.c_lflag &= ~( ICANON | ECHO | ECHONL | ECHOK | ECHOE | ECHOPRT | ECHOCTL );
641  new.c_cc[VMIN] = vmin;
642  new.c_cc[VTIME] = vtime;
643  if( tcsetattr( fileno( stdin ), TCSADRAIN, &new ) < 0 ) {
644    perror ("do_raw_input(): tcsetattr() failed" );
645    return;
646  }
647 
648  do {
649    rtems_clock_get( RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then );
650    count = 0;
651    for(;;) {
652      nread = read( fileno( stdin ), cbuf, sizeof cbuf );
653      if( nread < 0 ) {
654        perror( "do_raw_input(): read() failed" );
655        goto out;
656      }
657      count++;
658      if( nread != 0 )
659        break;
660    }
661    rtems_clock_get( RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now );
662    msec = (now - then) * 1000 / ticksPerSecond;
663    printf( "Count:%-10lu  Interval:%3u.%3.3d  Char:",
664          count, msec / 1000, msec % 1000 );
665         
666    for( i = 0 ; i < nread ; i++ )
667      printf (" 0x%2.2x", cbuf[i]);
668    printf ("\n");
669   
670  } while( cbuf[0] != 'q' );
671 
672out:
673  if( tcsetattr( fileno( stdin ), TCSADRAIN, &old) < 0 )
674    perror("do_raw_input(): tcsetattr() failed: %s\n" );
675   
676  printf ("*** End of Raw input  VMIN=%d  VTIME=%d ***\n", vmin, vtime);
677}
678
679
680void raw_input( struct termios *tp )
681{
682  printf( "\nTesting raw input input\n\n" );
683  printf( "Hit 'q' to terminate the test\n" );
684
685  do_raw_input( 0, 0 );
686  do_raw_input( 0, 20 );
687  do_raw_input( 5, 0 );
688  do_raw_input( 5, 20 );
689 
690  printf( "\nRaw input test done.\n" );
691}
692
693
694void usage( void )
695{
696  printf( "\nYou have the following choices:\n" );
697  printf( "  1 - Reset the struct termios\n" );
698  printf( "  2 - Look at the current termios setting\n" );
699  printf( "  3 - Change the line characteristics\n" );
700  printf( "  4 - Test canonical input\n" );
701  printf( "  5 - Test raw input\n" );
702  printf( "  9 - Exit\n" );
703  printf( "Enter your choice (1 to 5 or 9, followed by a carriage return): " );
704}
705
706
707/*
708 * RTEMS Startup Task
709 */
710rtems_task
711Init (rtems_task_argument ignored)
712{
713  char c, done;
714  struct termios orig_termios, test_termios;
715 
716  printf( "\n\n*** TEST OF TERMIOS INPUT CAPABILITIES ***\n" );
717
718  if( tcgetattr( fileno( stdin ), &orig_termios ) < 0 ) {
719    perror( "tcgetattr() failed" );
720    exit( 0 );
721  }
722
723  test_termios = orig_termios;
724
725  usage();
726  for(;;) {
727    switch( c = getchar() ) {
728      case '1':
729        printf( "\nResetting the line to the original termios setting\n\n" );
730        test_termios = orig_termios;
731        if( tcsetattr( fileno( stdin ), TCSADRAIN, &test_termios ) < 0 ) {
732          perror( "tcsetattr() failed" );
733          exit( 1 );
734        }
735        usage();
736        break;
737
738      case '2':
739        print_termios( &test_termios );
740        usage();
741        break;
742
743      case '3':
744        change_line_settings( &test_termios );
745        usage();
746        break;
747
748      case '4':
749        canonical_input( &test_termios );
750        usage();
751        break;
752
753      case '5':
754        raw_input( &test_termios );
755        usage();
756        break;
757
758      case '9':
759        exit( 1 );
760
761      case '\n':
762        break;
763         
764      default:
765        printf( "\n%c is not a valid choice. Try again\n\n", c );
766        usage();
767        break;
768    }
769  }
770}
771
Note: See TracBrowser for help on using the repository browser.