-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathidfmon.c
78 lines (70 loc) · 1.85 KB
/
idfmon.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
#define _GNU_SOURCE
// Connect to ESP and monitor.
// Keep reconnecting
// Exit and abort when we find it is waiting for code (i.e. invalid header: 0xffffffff)
// Output what it sends otherwise
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
int
main (int argc, const char *argv[])
{
if (argc <= 1)
errx (1, "Specify port");
while (1)
{
int fd = open (argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0)
{
putchar ('.');
fflush (stdout);
sleep (1);
continue;
}
fcntl (fd, F_SETFL, 0);
struct termios t;
tcgetattr (fd, &t);
cfmakeraw (&t);
//cfsetispeed (&t, B460800);
//cfsetospeed (&t, B460800);
t.c_cflag &= ~HUPCL; // disable hangup logic
t.c_cflag &= ~CRTSCTS; // disable hardware flow control
t.c_cflag |= CLOCAL | CREAD; // ignore modem controls
t.c_iflag &= ~(IXON | IXOFF | IXANY); //disable software flow control
tcsetattr (fd, TCSANOW, &t);
// RTS: EN
// DTR: GPIO0
int status = 0;
ioctl (fd, TIOCMSET, &status);
usleep (100000);
status |= TIOCM_RTS; // RTS (low)
ioctl (fd, TIOCMSET, &status);
usleep (100000);
status &= ~TIOCM_RTS; // RTS (high)
ioctl (fd, TIOCMSET, &status);
char line[1024];
while (1)
{
ssize_t l = read (fd, line, sizeof (line) - 1);
if (l <= 0)
break;
line[l] = 0;
if (strstr (line, "invalid header: 0xffffffff"))
{
printf ("Empty chip, ready to flash\n");
return 0;
}
printf ("%s", line);
}
close (fd);
}
return 0;
}