-
Notifications
You must be signed in to change notification settings - Fork 6
/
serialecho.c
307 lines (244 loc) · 6.39 KB
/
serialecho.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright (C) 2015 Jumpnow Technologies, LLC - http://jumpnowtek.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* Echo test a serial port with rx and tx jumpered.
* Used for debugging boards.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <time.h>
#include <signal.h>
void register_sig_handler();
void sigint_handler(int sig);
int open_port(const char *device, int speed, struct termios *oldopts);
int map_speed_to_unix(int speed);
void run_test(int fd, unsigned char byte);
void msleep(int ms);
volatile int done;
#ifdef __linux__
#define DEFAULT_PORT "/dev/ttyO4"
#else
#define DEFAULT_PORT "/dev/ttyu2"
#endif
#define DEFAULT_SPEED 115200
void usage(char *argv_0)
{
printf("\nUsage: %s [-d <device>] [-s <speed>] [-h]\n", argv_0);
printf(" -d <device> Serial device, default is /dev/ttyO4\n");
printf(" -s <speed> Speed, default is 115200\n");
printf(" -b <pattern> Send this byte only, example -b 0xaa\n");
printf(" -h Show this help\n\n");
exit(1);
}
int main(int argc, char **argv)
{
int opt;
char device[64];
int speed;
unsigned char byte;
struct termios oldopts;
strcpy(device, DEFAULT_PORT);
speed = DEFAULT_SPEED;
byte = 0;
while ((opt = getopt(argc, argv, "d:s:b:h")) != -1) {
switch (opt) {
case 'd':
if (strlen(optarg) > sizeof(device) - 1) {
printf("Device name too long: %s\n", optarg);
usage(argv[0]);
}
strcpy(device, optarg);
break;
case 's':
speed = strtol(optarg, NULL, 0);
if (map_speed_to_unix(speed) < 0) {
printf("Invalid speed: %s\n", optarg);
usage(argv[0]);
}
break;
case 'b':
byte = strtol(optarg, NULL, 0);
break;
case 'h':
default:
usage(argv[0]);
break;
}
}
int fd = open_port(device, speed, &oldopts);
if (fd < 0)
exit(1);
run_test(fd, byte);
tcsetattr(fd, TCSANOW, &oldopts);
close(fd);
return 0;
}
void run_test(int fd, unsigned char byte)
{
int len, txlen, pos;
int retries;
char tx[64];
char rx[64];
if (byte) {
memset(tx, byte, sizeof(tx));
txlen = sizeof(tx);
}
else {
strcpy(tx, "ABCDEFJHIJKLMNOPQRSTUVWXYZ1234567890abcdefjhijklmnopqrstuvwxyz");
txlen = strlen(tx);
}
printf("\n--- ctrl-c to stop ---\n");
while (!done) {
memset(rx, 0, sizeof(rx));
len = write(fd, tx, txlen);
if (len != txlen) {
perror("write");
break;
}
if (byte)
printf("Wrote %d bytes of 0x%02X\n", txlen, byte);
else
printf("\nWrote: %s\n", tx);
pos = 0;
retries = 0;
while (pos < txlen && !done) {
msleep(500);
len = read(fd, rx + pos, txlen - pos);
if (len < 0) {
perror("read");
done = 1;
break;
}
pos += len;
if (pos < txlen)
printf("Partial read: %d bytes\n", len);
if (retries++ > 10) {
printf("Giving up\n");
done = 1;
}
}
if (!done) {
if (byte) {
if (!memcmp(rx, tx, txlen))
printf("Read %d matching bytes\n", txlen);
else
printf("Read did not match\n");
}
else {
printf("Read : %s\n", rx);
}
msleep(2000);
}
}
}
int open_port(const char *device, int speed, struct termios *oldopts)
{
int fd, unix_speed;
struct termios opts;
unix_speed = map_speed_to_unix(speed);
if (unix_speed < 0)
return -1;
fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0) {
perror("open");
return -1;
}
// no delay
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &opts);
if (oldopts)
memcpy(oldopts, &opts, sizeof(struct termios));
opts.c_cflag &= ~CSIZE;
opts.c_cflag &= ~CSTOPB;
opts.c_cflag &= ~PARENB;
opts.c_cflag |= CLOCAL | CREAD | CS8;
opts.c_iflag = IGNPAR;
opts.c_oflag = 0;
opts.c_lflag = 0;
opts.c_cc[VTIME] = 5;
opts.c_cc[VMIN] = 0;
cfsetispeed(&opts, unix_speed);
cfsetospeed(&opts, unix_speed);
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &opts);
return fd;
}
int map_speed_to_unix(int speed)
{
int unix_speed;
switch (speed) {
case 50: unix_speed = B50; break;
case 75: unix_speed = B75; break;
case 110: unix_speed = B110; break;
case 134: unix_speed = B134; break;
case 150: unix_speed = B150; break;
case 300: unix_speed = B300; break;
case 600: unix_speed = B600; break;
case 1200: unix_speed = B1200; break;
case 1800: unix_speed = B1800; break;
case 2400: unix_speed = B2400; break;
case 4800: unix_speed = B4800; break;
case 9600: unix_speed = B9600; break;
case 19200: unix_speed = B19200; break;
case 38400: unix_speed = B38400; break;
case 57600: unix_speed = B57600; break;
case 115200: unix_speed = B115200; break;
case 230400: unix_speed = B230400; break;
case 460800: unix_speed = B460800; break;
case 500000: unix_speed = B500000; break;
case 576000: unix_speed = B576000; break;
case 921600: unix_speed = B921600; break;
case 1000000: unix_speed = B1000000; break;
case 1152000: unix_speed = B1152000; break;
case 1500000: unix_speed = B1500000; break;
case 2000000: unix_speed = B2000000; break;
case 2500000: unix_speed = B2500000; break;
case 3000000: unix_speed = B3000000; break;
case 3500000: unix_speed = B3500000; break;
case 4000000: unix_speed = B4000000; break;
default: unix_speed = -1; break;
}
return unix_speed;
}
void msleep(int ms)
{
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}
void register_sig_handler()
{
struct sigaction sia;
bzero(&sia, sizeof sia);
sia.sa_handler = sigint_handler;
if (sigaction(SIGINT, &sia, NULL) < 0) {
perror("sigaction(SIGINT)");
exit(1);
}
}
void sigint_handler(int sig)
{
done = 1;
}