FreeCalypso > hg > fc-pcm-if
comparison sw/libserial/initflush.c @ 6:a10657f8024e
sw: move initflush.c module into libserial
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 12 Oct 2024 19:42:19 +0000 |
parents | sw/mcsi-rx/initflush.c@de85c3680d7e |
children |
comparison
equal
deleted
inserted
replaced
5:3ae4a6ca5639 | 6:a10657f8024e |
---|---|
1 /* | |
2 * FTDI chip+driver combo (specifically the combination of FTDI chips and | |
3 * ftdi_sio driver in Linux, not sure who is the actual culprit) exhibits | |
4 * this unpleasant behaviour: even though we request "please flush all | |
5 * previous input" when we set our termios params, old accumulated serial | |
6 * Rx bytes still remain in some buffer somewhere, and a newly started | |
7 * serial application receives this stale garbage. As a workaround, | |
8 * we do an additional flush of our own: we put the fd in non-blocking mode | |
9 * and keep reading and discarding data until we get EAGAIN or EWOULDBLOCK. | |
10 */ | |
11 | |
12 #include <sys/types.h> | |
13 #include <sys/errno.h> | |
14 #include <stdio.h> | |
15 #include <stdlib.h> | |
16 #include <unistd.h> | |
17 | |
18 extern int target_fd; | |
19 | |
20 void | |
21 init_serial_flush() | |
22 { | |
23 u_char buf[512]; | |
24 int cc; | |
25 | |
26 set_serial_nonblock(1); | |
27 for (;;) { | |
28 cc = read(target_fd, buf, sizeof buf); | |
29 if (cc <= 0) | |
30 break; | |
31 } | |
32 if (cc == 0) { | |
33 fprintf(stderr, | |
34 "read EOF from serial port during initial flush\n"); | |
35 exit(1); | |
36 } | |
37 if (errno == EAGAIN || errno == EWOULDBLOCK) | |
38 return; /* success */ | |
39 perror("serial port read"); | |
40 exit(1); | |
41 } |