fixed EEPROM user-area space checks for FT232R and FT245R chips in ftdi_eeprom_build()
[libftdi] / examples / serial_test.c
CommitLineData
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
JP
10#include <unistd.h>
11#include <getopt.h>
8b0b694f 12#include <signal.h>
d69dbd9e
JP
13#include <ftdi.h>
14
8b0b694f
UB
15static int exitRequested = 0;
16/*
17 * sigintHandler --
18 *
19 * SIGINT handler, so we can gracefully exit when the user hits ctrl-C.
20 */
21static void
22sigintHandler(int signum)
23{
24 exitRequested = 1;
25}
26
d69dbd9e
JP
27int main(int argc, char **argv)
28{
8b0b694f 29 struct ftdi_context *ftdi;
97c6b5f6 30 unsigned char buf[1024];
b9c1e317 31 int f = 0, i;
2e9cc5be 32 int vid = 0x403;
8b0b694f 33 int pid = 0;
d69dbd9e
JP
34 int baudrate = 115200;
35 int interface = INTERFACE_ANY;
8b0b694f 36 int do_write = 0;
53a561d3 37 unsigned int pattern = 0xffff;
8b0b694f 38 int retval = EXIT_FAILURE;
d69dbd9e 39
8b0b694f 40 while ((i = getopt(argc, argv, "i:v:p:b:w::")) != -1)
d69dbd9e
JP
41 {
42 switch (i)
43 {
05c2e40a
TJ
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;
8b0b694f
UB
56 case 'w':
57 do_write = 1;
53a561d3
UB
58 if (optarg)
59 pattern = strtoul(optarg, NULL, 0);
8b0b694f 60 if (pattern > 0xff)
53a561d3 61 {
8b0b694f 62 fprintf(stderr, "Please provide a 8 bit pattern\n");
53a561d3
UB
63 exit(-1);
64 }
8b0b694f 65 break;
05c2e40a 66 default:
8b0b694f 67 fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate] [-w [pattern]]\n", *argv);
05c2e40a 68 exit(-1);
d69dbd9e
JP
69 }
70 }
71
72 // Init
8b0b694f 73 if ((ftdi = ftdi_new()) == 0)
d69dbd9e 74 {
8b0b694f 75 fprintf(stderr, "ftdi_new failed\n");
d69dbd9e
JP
76 return EXIT_FAILURE;
77 }
78
8b0b694f
UB
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 }
d69dbd9e
JP
119 if (f < 0)
120 {
8b0b694f 121 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
d69dbd9e
JP
122 exit(-1);
123 }
124
125 // Set baudrate
8b0b694f 126 f = ftdi_set_baudrate(ftdi, baudrate);
d69dbd9e
JP
127 if (f < 0)
128 {
8b0b694f 129 fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_string(ftdi));
d69dbd9e
JP
130 exit(-1);
131 }
8b0b694f 132
4f95e48a
UB
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
8b0b694f
UB
148 if (do_write)
149 for(i=0; i<1024; i++)
150 buf[i] = pattern;
d69dbd9e 151
8b0b694f
UB
152 signal(SIGINT, sigintHandler);
153 while (!exitRequested)
05c2e40a 154 {
8b0b694f 155 if (do_write)
275e7cae
UB
156 f = ftdi_write_data(ftdi, buf,
157 (baudrate/512 >sizeof(buf))?sizeof(buf):
158 (baudrate/512)?baudrate/512:1);
8b0b694f
UB
159 else
160 f = ftdi_read_data(ftdi, buf, sizeof(buf));
161 if (f<0)
8ace1862 162 usleep(1 * 1000000);
8b0b694f
UB
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 }
d69dbd9e 170 }
8b0b694f
UB
171 signal(SIGINT, SIG_DFL);
172 retval = EXIT_SUCCESS;
173
174 ftdi_usb_close(ftdi);
175 do_deinit:
176 ftdi_free(ftdi);
d69dbd9e 177
8b0b694f 178 return retval;
d69dbd9e 179}