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