-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresetArduinoToBootloader.c
More file actions
132 lines (115 loc) · 2.64 KB
/
resetArduinoToBootloader.c
File metadata and controls
132 lines (115 loc) · 2.64 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
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
#include <errno.h>
#include <fcntl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
void usage(char **argv)
{
printf("usage: %s <PORT>\n", argv[0]);
exit(1);
}
int setBaudrate(int fd, int baud)
{
struct termios tty;
if (tcgetattr (fd, &tty) != 0)
{
printf("error %d from tcgetattr", errno);
return -1;
}
int ret = cfsetospeed (&tty, baud);
if (ret) {
printf("Error setting ospeed: %s\n", strerror(errno));
return -1;
}
ret = cfsetispeed (&tty, baud);
if (ret) {
printf("Error setting ispeed: %s\n", strerror(errno));
return -1;
}
printf("setting baudrate to %i\n", baud);
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf("error %d from tcsetattr", errno);
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
if (argc != 2) {
usage(argv);
}
char *port = argv[1];
int fd, ret, serial;
printf("Opening port %s\n", port);
fd = open(port, O_RDWR | O_NOCTTY );
if (fd == -1) {
printf("Failure opening port %s. Error: %s\n", port, strerror(errno));
exit(-1);
}
ret = setBaudrate(fd, B1200);
if (ret) {
printf("Error setting baudrate\n");
exit(-1);
}
ret = ioctl(fd, TIOCMGET, &serial);
if (ret == 1) {
printf("Failure getting port information on %s: %s\n", port, strerror(errno));
exit(-1);
}
// Clear DTR pin
serial = serial & ~(TIOCM_DTR);
ret = ioctl(fd, TIOCMSET, serial);
if (ret == 1) {
printf("Failure to clear DTR bit on %s: %s\n", port, strerror(errno));
exit(-1);
}
close(fd);
// Wait for 500 ms
printf("Wait for %s to become available again...\n", port);
usleep(500 * 1000);
struct stat buffer;
/* counter = 100 -> 100ms * 100 = 10s max */
int status, found = 0, counter = 100;
errno = 0;
do {
status = stat(port, &buffer);
if (status) {
if (errno != ENOENT) {
printf("Error accessing %s: %s %i\n", port, strerror(errno), errno);
exit(-1);
} else {
usleep(100 * 1000);
}
} else {
found = 1;
// Wait for udev to change the uid if a udev rule exists
// This avoids root privileges for flashing
if ( buffer.st_uid != getuid()) {
usleep(100 * 1000);
} else {
break;
}
}
counter--;
} while (errno == ENOENT && counter);
if (!counter) {
// we ran in a timeout
if (found) {
printf("%s found again, but the user is buffer.st_uid. " \
"This will most probably require root privleges for " \
"flashing then.\n", port);
} else {
printf("%s not found after reset with uid == %u\n", port, getuid());
exit(-1);
}
}
printf("%s is available in bootloader mode!\n", port);
return 0;
}