-
Notifications
You must be signed in to change notification settings - Fork 6
/
loader.cpp
139 lines (130 loc) · 3.41 KB
/
loader.cpp
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
#include <fcntl.h>
#include <tclap/ArgException.h>
#include <tclap/CmdLine.h>
#include <tclap/SwitchArg.h>
#include <tclap/ValueArg.h>
#include <termios.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include "states.h"
int main(int argc, char *argv[]) {
/* parse command-line options */
std::string port;
std::string file;
bool run;
bool reboot;
bool debug;
std::vector<TCLAP::Arg*> xorList;
try {
TCLAP::CmdLine cmd("PIC bootloader loader", ' ', "0.1");
TCLAP::ValueArg<std::string> port_arg("p", "port", "PIC serial port",
true, "/dev/ttyO1", "serial port");
cmd.add(port_arg);
TCLAP::ValueArg<std::string> file_arg("f", "file", "HEX file", false,
"", "HEX file");
xorList.push_back(&file_arg);
TCLAP::SwitchArg run_arg("x", "run", "Run program", false);
xorList.push_back(&run_arg);
TCLAP::SwitchArg reboot_arg("r", "reboot", "Reboot PIC", false);
xorList.push_back(&reboot_arg);
cmd.xorAdd(xorList);
TCLAP::SwitchArg debug_arg("d", "debug", "Print debug messages", false);
cmd.add(debug_arg);
cmd.parse(argc, argv);
port = port_arg.getValue();
file = file_arg.getValue();
run = run_arg.getValue();
reboot = reboot_arg.getValue();
debug = debug_arg.getValue();
} catch (TCLAP::ArgException &e) {
std::cerr << "error: " << e.error() << " for arg " << e.argId()
<< std::endl;
}
/* open serial port */
int fd = open(port.c_str(), O_RDWR | O_NOCTTY);
if (fd == -1) {
fprintf(stderr, "%s: unable to open %s\n", __FUNCTION__, port.c_str());
exit(EXIT_FAILURE);
}
/* backup terminal settings */
struct termios termios_original;
tcgetattr(fd, &termios_original);
/* configure terminal settings */
struct termios termios = termios_original;
termios.c_iflag = 0;
termios.c_oflag = 0;
termios.c_cflag = B115200 | CS8 | CREAD | CLOCAL;
termios.c_lflag = 0;
termios.c_lflag &= ~ICANON;
termios.c_cc[VMIN] = 1;
termios.c_cc[VTIME] = 0;
tcsetattr(fd, TCSANOW, &termios);
char c;
if (run) {
/* execute previously loaded program */
c = STATE_BOOT;
write(fd, &c, 1);
read(fd, &c, 1);
printf("run status = %c\n", c);
} else if (reboot) {
/* reboot PIC */
c = STATE_REBOOT;
write(fd, &c, 1);
read(fd, &c, 1);
printf("reboot status = %c\n", c);
} else {
/* erase flash */
c = STATE_ERASE_FLASH;
write(fd, &c, 1);
read(fd, &c, 1);
printf("erase status = %c\n", c);
/* load new program */
static const unsigned int buffer_size = 1 + 2 + 4 + 2 + 256 * 2 + 2 + 1;
char buffer[buffer_size];
FILE *fp = fopen(file.c_str(), "r");
while (fgets(buffer, buffer_size, fp)) {
if (buffer[0] == ':') {
/* found record line */
static const unsigned int num_tries = 10;
bool failed = true;
for (unsigned int count = 0; count < num_tries; count++) {
if (debug) {
printf("Sending %s", buffer);
}
/* send record line */
for (char *ptr = buffer; ((*ptr != '\n') && (*ptr != '\0'));
ptr++) {
write(fd, ptr, 1);
}
/* receive checksum status */
read(fd, &c, 1);
if (c == '0') {
/* checksum OK */
if (debug) {
printf("OK\n");
}
failed = false;
break;
} else {
if (debug) {
printf("KO\n");
}
}
}
if (failed) {
goto end;
}
}
}
fclose(fp);
}
end:
/* restore terminal settings */
tcsetattr(fd, TCSANOW, &termios_original);
close(fd);
exit(EXIT_SUCCESS);
}