Repository navigation
Expand file tree
/
Copy pathuart.c
More file actions
102 lines (98 loc) · 2.81 KB
/
Copy pathuart.c
File metadata and controls
102 lines (98 loc) · 2.81 KB
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
// SPDX-License-Identifier: MIT
/*
* uart: open a serial port raw at a given baud rate (termios), send a line
* and print whatever comes back within a second. With TX wired to RX it is
* a loopback test. The port's settings are put back on exit. Not on the
* serial console a login is running on: it would read the console's input.
*
* uart /dev/ttyAMLn [baud] [text]
*
* Build: cc -O2 -Wall -o uart uart.c
* The debug console is the first port; extra ports on the 40-pin header are
* enabled by a device-tree overlay (libretech-wiring-tool).
*/
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
static speed_t baud(long b)
{
switch (b) {
case 9600: return B9600;
case 19200: return B19200;
case 38400: return B38400;
case 57600: return B57600;
case 115200: return B115200;
case 230400: return B230400;
case 460800: return B460800;
case 921600: return B921600;
case 1000000: return B1000000;
case 1500000: return B1500000;
default: return 0;
}
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: uart /dev/ttyXXX [baud] [text]\n");
return 2;
}
long b = argc > 2 ? strtol(argv[2], NULL, 0) : 115200;
speed_t sp = baud(b);
if (!sp) {
fprintf(stderr, "unsupported baud %ld\n", b);
return 2;
}
int fd = open(argv[1], O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC);
if (fd < 0) {
perror(argv[1]);
return 1;
}
if (ioctl(fd, TIOCEXCL) < 0) /* nobody else opens it while we have it */
perror("TIOCEXCL");
struct termios saved, t;
if (tcgetattr(fd, &saved) < 0) {
perror("tcgetattr");
return 1;
}
t = saved;
cfmakeraw(&t); /* no echo, no line editing */
t.c_cflag &= ~(CRTSCTS | CSTOPB); /* 8N1, no hardware flow control */
t.c_cflag |= CLOCAL | CREAD;
cfsetispeed(&t, sp);
cfsetospeed(&t, sp);
if (tcsetattr(fd, TCSANOW, &t) < 0) {
perror("tcsetattr");
return 1;
}
tcflush(fd, TCIOFLUSH);
const char *msg = argc > 3 ? argv[3] : "hello from platform-guide\r\n";
size_t len = strlen(msg), sent = 0;
struct pollfd w = { .fd = fd, .events = POLLOUT };
while (sent < len && poll(&w, 1, 1000) > 0) { /* bounded: a stuck line cannot hang it */
ssize_t r = write(fd, msg + sent, len - sent);
if (r <= 0)
break;
sent += r;
}
printf("%s at %ld baud: sent %zu of %zu bytes\n", argv[1], b, sent, len);
char buf[256];
size_t got = 0;
struct pollfd p = { .fd = fd, .events = POLLIN };
while (got < sizeof(buf) - 1 && poll(&p, 1, 1000) > 0) {
ssize_t r = read(fd, buf + got, sizeof(buf) - 1 - got);
if (r <= 0)
break;
got += r;
}
buf[got] = 0;
printf("received %zu bytes%s%s\n", got, got ? ": " : "", buf);
tcsetattr(fd, TCSADRAIN, &saved); /* the port's settings as we found them, once sent */
ioctl(fd, TIOCNXCL);
close(fd);
return sent == len ? 0 : 1;
}