fixed EEPROM user-area space checks for FT232R and FT245R chips in ftdi_eeprom_build()
[libftdi] / examples / serial_test.c
1 /* serial_test.c
2
3    Read/write data via serial I/O
4
5    This program is distributed under the GPL, version 2
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <getopt.h>
12 #include <signal.h>
13 #include <ftdi.h>
14
15 static int exitRequested = 0;
16 /*
17  * sigintHandler --
18  *
19  *    SIGINT handler, so we can gracefully exit when the user hits ctrl-C.
20  */
21 static void
22 sigintHandler(int signum)
23 {
24     exitRequested = 1;
25 }
26
27 int main(int argc, char **argv)
28 {
29     struct ftdi_context *ftdi;
30     unsigned char buf[1024];
31     int f = 0, i;
32     int vid = 0x403;
33     int pid = 0;
34     int baudrate = 115200;
35     int interface = INTERFACE_ANY;
36     int do_write = 0;
37     unsigned int pattern = 0xffff;
38     int retval = EXIT_FAILURE;
39
40     while ((i = getopt(argc, argv, "i:v:p:b:w::")) != -1)
41     {
42         switch (i)
43         {
44             case 'i': // 0=ANY, 1=A, 2=B, 3=C, 4=D
45                 interface = strtoul(optarg, NULL, 0);
46                 break;
47             case 'v':
48                 vid = strtoul(optarg, NULL, 0);
49                 break;
50             case 'p':
51                 pid = strtoul(optarg, NULL, 0);
52                 break;
53             case 'b':
54                 baudrate = strtoul(optarg, NULL, 0);
55                 break;
56             case 'w':
57                 do_write = 1;
58                 if (optarg)
59                     pattern = strtoul(optarg, NULL, 0);
60                 if (pattern > 0xff)
61                 {
62                     fprintf(stderr, "Please provide a 8 bit pattern\n");
63                     exit(-1);
64                 }
65                 break;
66             default:
67                 fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate] [-w [pattern]]\n", *argv);
68                 exit(-1);
69         }
70     }
71
72     // Init
73     if ((ftdi = ftdi_new()) == 0)
74     {
75         fprintf(stderr, "ftdi_new failed\n");
76         return EXIT_FAILURE;
77     }
78
79     if (!vid && !pid && (interface == INTERFACE_ANY))
80     {
81         ftdi_set_interface(ftdi, INTERFACE_ANY);
82         struct ftdi_device_list *devlist;
83         int res;
84         if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0)
85         {
86             fprintf(stderr, "No FTDI with default VID/PID found\n");
87             goto do_deinit;
88         }
89         if (res == 1)
90         {
91             f = ftdi_usb_open_dev(ftdi,  devlist[0].dev);
92             if (f<0)
93             {
94                 fprintf(stderr, "Unable to open device %d: (%s)",
95                         i, ftdi_get_error_string(ftdi));
96             }
97         }
98         ftdi_list_free(&devlist);
99         if (res > 1)
100         {
101             fprintf(stderr, "%d Devices found, please select Device with VID/PID\n", res);
102             /* TODO: List Devices*/
103             goto do_deinit;
104         }
105         if (res == 0)
106         {
107             fprintf(stderr, "No Devices found with default VID/PID\n");
108             goto do_deinit;
109         }
110     }
111     else
112     {
113         // Select interface
114         ftdi_set_interface(ftdi, interface);
115         
116         // Open device
117         f = ftdi_usb_open(ftdi, vid, pid);
118     }
119     if (f < 0)
120     {
121         fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
122         exit(-1);
123     }
124
125     // Set baudrate
126     f = ftdi_set_baudrate(ftdi, baudrate);
127     if (f < 0)
128     {
129         fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(ftdi));
130         exit(-1);
131     }
132     
133     /* Set line parameters
134      *
135      * TODO: Make these parameters settable from the command line
136      *
137      * Parameters are choosen that sending a continous stream of 0x55 
138      * should give a square wave
139      *
140      */
141     f = ftdi_set_line_property(ftdi, 8, STOP_BIT_1, NONE);
142     if (f < 0)
143     {
144         fprintf(stderr, "unable to set line parameters: %d (%s)\n", f, ftdi_get_error_string(ftdi));
145         exit(-1);
146     }
147     
148     if (do_write)
149         for(i=0; i<1024; i++)
150             buf[i] = pattern;
151
152     signal(SIGINT, sigintHandler);
153     while (!exitRequested)
154     {
155         if (do_write)
156             f = ftdi_write_data(ftdi, buf, 
157                                 (baudrate/512 >sizeof(buf))?sizeof(buf):
158                                 (baudrate/512)?baudrate/512:1);
159         else
160             f = ftdi_read_data(ftdi, buf, sizeof(buf));
161         if (f<0)
162             usleep(1 * 1000000);
163         else if(f> 0 && !do_write)
164         {
165             fprintf(stderr, "read %d bytes\n", f);
166             fwrite(buf, f, 1, stdout);
167             fflush(stderr);
168             fflush(stdout);
169         }
170     }
171     signal(SIGINT, SIG_DFL);
172     retval =  EXIT_SUCCESS;
173             
174     ftdi_usb_close(ftdi);
175     do_deinit:
176     ftdi_free(ftdi);
177
178     return retval;
179 }