]> rtime.felk.cvut.cz Git - sysless.git/blob - arch/arm/mach-lpc21xx/tools/tolpc/rs232_lt.c
cmdproc: Make backspace work even in sterm
[sysless.git] / arch / arm / mach-lpc21xx / tools / tolpc / rs232_lt.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <termios.h>
5 #include <ncurses.h>
6 #include <sys/time.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <getopt.h>
11 #include "tolpc_fn.h"
12
13 #define DEBUG 0
14 #define HAS_GETOPT_LONG 1
15
16 #define MEM_BUF_LEN 0x40000
17
18 unsigned char mem_buf[MEM_BUF_LEN];
19
20 int rs232_loop_test(char *sdev, int baud, int flowc);
21
22 struct termios init_saved_termios;
23
24
25 int flowc;
26
27 static void
28 usage(void)
29 {
30   printf("usage: tohit <parameters> <send_file>\n");
31   printf("  -d, --sdev <name>        name of RS232 device [%s]\n",tohit_sdev);
32   printf("  -B, --baud <num>         RS232 baudrate [%d]\n",tohit_baud);
33   printf("  -f, --flowc-rts          flow control\n");
34   printf("  -V, --version            show version\n");
35   printf("  -h, --help               this usage screen\n");
36 }
37
38 int main(int argc, char **argv) 
39 {
40   /* FILE *F; */
41   
42   static struct option long_opts[] = {
43     { "sdev",  1, 0, 'd' },
44     { "baud",  1, 0, 'B' },
45     { "flowc-rts",0, 0, 'f' },
46     { "version",0,0, 'V' },
47     { "help",  0, 0, 'h' },
48     { 0, 0, 0, 0}
49   };
50   int opt;
51   int ret;
52
53   tohit_baud=9600;
54
55  #ifndef HAS_GETOPT_LONG
56   while ((opt = getopt(argc, argv, "d:B:fVh")) != EOF)
57  #else
58   while ((opt = getopt_long(argc, argv, "d:B:fVh",
59                             &long_opts[0], NULL)) != EOF)
60  #endif
61   switch (opt) {
62     case 'd':
63       tohit_sdev=optarg;
64       break;
65     case 'B':
66       tohit_baud = strtol(optarg,NULL,0);
67       break;
68     case 'f':
69       flowc=1;
70       break;
71     case 'V':
72       fputs("tohit pre alpha\n", stdout);
73       exit(0);
74     case 'h':
75     default:
76       usage();
77       exit(opt == 'h' ? 0 : 1);
78   }
79
80   def_shell_mode();
81   savetty();
82   /*tcgetattr(0, &init_saved_termios);*/
83   initscr(); cbreak(); noecho();
84   nonl(); intrflush(stdscr, FALSE); keypad(stdscr, TRUE);
85   nodelay(stdscr, TRUE);
86   
87   ret=rs232_loop_test(tohit_sdev,tohit_baud,flowc);
88
89   endwin();
90   
91   return ret;
92 }
93
94
95 int rs232_loop_test(char *sdev, int baud, int flowc)
96 {
97   int fd;
98   int c;
99   unsigned char *pout=NULL, *pin=NULL, uc;
100   int cntout=0,cntin=0, cnt;
101   int i, test_loop_fl=0;
102   int errorcnt=0;
103   int idle;
104   int stopin=0;
105   /* int stopout=0; */
106   
107   /* Open RS232 device */
108   if ((fd = open(sdev, O_RDWR | O_NONBLOCK)) == -1) {
109     printf("Error openning %s\n",sdev);
110     return -1;
111   }     
112
113   /* Set RS232 device mode and speed */
114   if(rs232_setmode(fd,baud,0,flowc)<0){
115     printf("Error in rs232_setmode\n");
116     return -1;
117   }
118
119 /*
120   rs232_sendch(int fd,unsigned char c);
121   rs232_recch(int fd);
122   rs232_test(int fd,int time);
123 */
124
125   mvprintw(/*y*/2,/*x*/2,"Test of RS-232 transfers");
126
127   do{
128     c=getch();
129     idle=(c==ERR);
130     
131     switch(c) {
132       case 't' :
133         cnt=20000;
134         if(cnt>MEM_BUF_LEN) cnt=MEM_BUF_LEN;
135         for(i=0;i<cnt;i++)
136           mem_buf[i]=i^(i>>7);
137         test_loop_fl=1;
138         cntout=cntin=cnt;
139         pout=pin=mem_buf;
140         errorcnt=0;
141         mvprintw(/*y*/11,/*x*/0,"Loop test : %s",
142                  "Running");
143         mvprintw(/*y*/9,/*x*/0,"                   ");
144         mvprintw(/*y*/10,/*x*/0,"                   ");
145         break;
146      case 's' :
147         stopin=!stopin;
148         break;
149     }
150     
151     if(test_loop_fl) {
152       if(cntout)
153         if(write(fd, pout, 1) == 1) {
154           pout++;
155           cntout--;
156           idle=0;
157         }
158       if(cntin&&!stopin)
159         if(read(fd, &uc, 1) == 1) {
160           if(*pin!=uc) {
161             errorcnt++;
162             mvprintw(/*y*/9,/*x*/0,"Diff   : %02X != %02X",uc,*pin);
163             mvprintw(/*y*/10,/*x*/0,"Errors : %4d",errorcnt);
164           }
165           pin++;
166           cntin--;
167           idle=0;
168         }
169       if(!cntin&&!cntout) {
170         mvprintw(/*y*/11,/*x*/0,"Loop test : %s",
171                  errorcnt?"Failed ":"Passed ");
172         test_loop_fl=0;
173       }
174     }
175     
176     if(idle) {
177       mvprintw(/*y*/8,/*x*/0,"Cnt Out: %6d In : %6d  %s",
178                cntout,cntin,stopin?"Stop":"    ");
179     }
180   } while(c!=KEY_F(10));
181
182   return 0;  
183 }
184