forked from derekstavis/nrf24le1-libbcm2835
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
430 lines (363 loc) · 9.61 KB
/
main.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "nrf24le1.h"
typedef enum {
CMD_UNKNOWN,
CMD_SHOW,
CMD_RESET,
CMD_READ_INFO,
CMD_READ_NVM,
CMD_READ_FIRMWARE,
CMD_WRITE_INFO,
CMD_WRITE_NVM,
CMD_WRITE_FIRMWARE,
} cmd_e;
static int usage(void)
{
fprintf(stderr, "Usage: nrf24le1 [cmds...]\n");
fprintf(stderr, "Commands:\n");
fprintf(stderr, "\tshow -- show FSR register and ability to modify it\n");
fprintf(stderr, "\tread infopage [filename] -- Read infopage and save to filename or to stdout if no filename given\n");
fprintf(stderr, "\tread nvm [filename] -- Read NVRAM area\n");
fprintf(stderr, "\tread firmware [filename] -- Read firmware area\n");
fprintf(stderr, "\twrite infopage [filename] -- Write infopage from file or stdout if no filename given\n");
fprintf(stderr, "\twrite nvm [filename] -- Write NVRAM area\n");
fprintf(stderr, "\twrite firmware [filename] -- Write firmware area\n");
fprintf(stderr, "\treset -- Reset the MCU, useful to restart the code from scratch if it stuck\n");
fprintf(stderr, "\n");
fprintf(stderr, "filename is taken to be in Intel Hex format if its suffix is .ihx or .hex\n");
fprintf(stderr, "if no filename is given the data is read or written from stdout and is expected in Intel Hex format\n");
return 1;
}
static cmd_e args_to_cmd(int argc, char **argv, char **filename)
{
int cmd_read;
if (argc < 2)
return CMD_UNKNOWN;
if (strcmp(argv[1], "show") == 0)
return CMD_SHOW;
if (strcmp(argv[1], "reset") == 0)
return CMD_RESET;
if (argc < 3)
return CMD_UNKNOWN;
if (strcmp(argv[1], "read") == 0)
cmd_read = 1;
else if (strcmp(argv[1], "write") == 0)
cmd_read = 0;
else
return CMD_UNKNOWN;
if (argc >= 4)
*filename = argv[3];
if (strcmp(argv[2], "info") == 0 || strcmp(argv[2], "infopage") == 0)
return (cmd_read ? CMD_READ_INFO : CMD_WRITE_INFO);
else if (strcmp(argv[2], "nvm") == 0)
return (cmd_read ? CMD_READ_NVM : CMD_WRITE_NVM);
else if (strcmp(argv[2], "firmware") == 0 || strcmp(argv[2], "fw") == 0)
return (cmd_read ? CMD_READ_FIRMWARE : CMD_WRITE_FIRMWARE);
return CMD_UNKNOWN;
}
static int filename_is_hex(char *filename)
{
int len;
if (filename == NULL)
return 1;
len = strlen(filename);
if (len < 4)
return 0;
if (strcmp(filename + len - 4, ".hex") == 0 ||
strcmp(filename + len - 4, ".ihx") == 0)
{
return 1;
}
else
{
return 0;
}
}
static void real_time_schedule(void)
{
// Set real-time FIFO schedule to have utmost chance to have exact delays
struct sched_param sp;
memset(&sp, 0, sizeof(sp));
sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
sched_setscheduler(0, SCHED_FIFO, &sp);
// Make us unpageable, lock all pages to RAM with no swapping allowed
mlockall(MCL_CURRENT | MCL_FUTURE);
}
static void save_line_hex(FILE *f, uint16_t addr, uint8_t *buf, uint16_t count)
{
uint16_t i;
uint16_t to_skip_bytes = 0;
uint16_t to_save_bytes = 0;
uint8_t checksum = 0;
/* We want to save the shortest line, we ignore 0xFF parts at the start
* and end of the buffer */
for (i = 0; i < count; i++) {
if (buf[i] != 0xFF)
to_save_bytes = i + 1;
else if (buf[i] == 0xFF && to_save_bytes == 0)
to_skip_bytes = i + 1;
}
if (to_save_bytes == 0)
return; // Nothing to save
if (to_save_bytes - to_skip_bytes > count / 2) {
// Unless we saved half the line, just keep form and save the entire line
to_save_bytes = 32;
to_skip_bytes = 0;
}
fprintf(f, ":%02X%04X00", (uint8_t)(to_save_bytes - to_skip_bytes), addr + to_skip_bytes);
checksum += to_save_bytes - to_skip_bytes;
checksum += (addr + to_skip_bytes) >> 8;
checksum += (addr + to_skip_bytes) & 0xFF;
for (i = to_skip_bytes; i < to_save_bytes; i++) {
fprintf(f, "%02X", buf[i]);
checksum += buf[i];
}
fprintf(f, "%02X\n", (uint8_t)((~checksum) + 1));
}
static void save_data_hex(FILE *f, uint8_t *buf, uint16_t count)
{
int i;
for (i = 0; i < count; i += 32) {
uint16_t left = 32;
if (count - i < 32)
left = count - i;
save_line_hex(f, i, buf+i, left);
}
fprintf(f, ":00000001FF\n"); // EOF
}
static void save_data_bin(FILE *f, uint8_t *buf, uint16_t count)
{
fwrite(buf, 1, count, f);
}
static void save_data(char *filename, uint8_t *buf, uint16_t count)
{
FILE *f = stdout;
int is_hex = filename_is_hex(filename);
if (filename) {
f = fopen(filename, "wb");
if (f == NULL) {
fprintf(stderr, "Error opening file '%s', saving as hex to stdout.\n", filename);
f = stdout;
is_hex = 1;
}
} else {
is_hex = 1;
f = stdout;
}
if (is_hex)
save_data_hex(f, buf, count);
else
save_data_bin(f, buf, count);
}
static uint16_t read_data_bin(FILE *f, uint8_t *buf, uint16_t size)
{
return fread(buf, 1, size, f);
}
static int parse_hex(char *line, uint8_t *data)
{
int i;
int first_nibble = 1;
uint8_t nibble;
// Start after the colon ':'
for (i = 1; line[i] != 0 && line[i] != '\n' && line[i] != '\r'; i++) {
if (line[i] >= '0' && line[i] <= '9')
nibble = line[i] - '0';
else if (line[i] >= 'a' && line[i] <= 'f')
nibble = line[i] - 'a' + 0xA;
else if (line[i] >= 'A' && line[i] <= 'F')
nibble = line[i] - 'A' + 0xA;
else {
// Invalid hex character
return i;
}
if (first_nibble) {
*data = nibble << 4;
first_nibble = 0;
} else {
*data |= nibble;
first_nibble = 1;
data++;
}
}
if (!first_nibble)
return i;
return -1;
}
static uint16_t read_data_hex(FILE *f, uint8_t *buf, uint16_t size)
{
char line[1024]; // Maximum likely size is 1 + 2 + 4 + 2 + 255*2 + 2 + 2 = 523
uint8_t data[512];
int i;
uint16_t max_data = 0;
uint8_t num_data;
uint8_t num_bytes;
uint16_t addr;
uint8_t rec_type;
uint8_t checksum;
memset(buf, 0xFF, size);
while (!feof(f) && !ferror(f)) {
if (fgets(line, sizeof(line), f) == NULL)
break;
line[sizeof(line)-1] = 0;
if (line[0] != ':') {
fprintf(stderr, "Invalid data in hex file (Intel Hex requires a colon at start)\n");
return 0;
}
int err_index = parse_hex(line, data);
if (err_index >= 0) {
fprintf(stderr, "Error parsing hex at character %d\n", err_index);
fprintf(stderr, "Line: '%s'\n", line);
fprintf(stderr, "Err : ");
for (i = 0; i < err_index; i++)
fprintf(stderr, " ");
fprintf(f, "^\n");
return 0;
}
num_data = data[0] + 5;
num_bytes = data[0];
addr = (data[1] << 8) | data[2];
rec_type = data[3];
checksum = 0;
for (i = 0; i < num_data; i++)
checksum += data[i];
if (checksum != 0) {
fprintf(stderr, "Incorrect checksum in line: '%s'\n", line);
return 0;
}
switch (rec_type) {
case 0:
if (addr + num_bytes > size) {
fprintf(stderr, "Data is larger than allowed buffer\n");
return 0;
}
memcpy(buf + addr, data + 4, num_bytes);
if (addr + num_bytes > max_data)
max_data = addr + num_bytes;
printf("Processed %u bytes at address %u, new max data is %u\n", num_bytes, addr, max_data);
break;
case 1:
fprintf(stderr, "EOF\n");
goto Exit;
default:
fprintf(stderr, "Unknown record type %d\n", rec_type);
return 0;
}
}
Exit:
return max_data;
}
static uint16_t read_data(char *filename, uint8_t *buf, uint16_t size)
{
FILE *f = stdout;
int is_hex = filename_is_hex(filename);
if (filename) {
f = fopen(filename, "rb");
if (f == NULL) {
fprintf(stderr, "Error opening file '%s', saving as hex to stdout.\n", filename);
f = stdin;
is_hex = 1;
}
} else {
is_hex = 1;
f = stdin;
}
if (is_hex)
return read_data_hex(f, buf, size);
else
return read_data_bin(f, buf, size);
}
static void read_info(char *filename)
{
ssize_t ret;
uint8_t buf[NRF_PAGE_SIZE];
ret = da_infopage_show(buf);
if (ret < 0) {
printf("Error reading infopage, ret=%d\n", ret);
return;
}
save_data(filename, buf, sizeof(buf));
}
static void read_nvm(char *filename)
{
ssize_t ret;
uint8_t buf[NVM_NORMAL_MEM_SIZE];
ret = da_nvm_normal_show(buf);
if (ret < 0) {
printf("Error reading nvm, ret=%d\n", ret);
return;
}
save_data(filename, buf, sizeof(buf));
}
int main(int argc, char **argv)
{
uint8_t bufread[16*1024];
unsigned long off = 0;
size_t count = sizeof(bufread);
cmd_e cmd;
char *filename = NULL;
cmd = args_to_cmd(argc, argv, &filename);
if (cmd == CMD_UNKNOWN)
return usage();
memset(bufread, 0, sizeof(bufread));
nrf24le1_init();
real_time_schedule();
da_enable_program_store(1);
if (cmd == CMD_SHOW) {
da_test_show(1);
} else {
// First we make sure we have proper SPI connectivity
if (da_test_show(0) == 0) {
// Now we run the command
switch (cmd) {
case CMD_RESET:
da_reset();
break;
case CMD_WRITE_FIRMWARE:
count = read_data(filename, bufread, sizeof(bufread));
if (count > 0)
uhet_write(bufread, count, &off);
else
fprintf(stderr, "Failed to read data to program\n");
break;
case CMD_WRITE_INFO:
count = read_data(filename, bufread, INFO_PAGE_SIZE);
if (count > 0)
da_infopage_store(bufread, count);
else
fprintf(stderr, "Failed to read data to program\n");
break;
case CMD_WRITE_NVM:
count = read_data(filename, bufread, NVM_NORMAL_MEM_SIZE);
if (count > 0)
da_nvm_normal_store(bufread, count);
else
fprintf(stderr, "Failed to read data to program\n");
break;
case CMD_READ_FIRMWARE:
count = uhet_read(bufread, count, &off);
if (count > 0)
save_data(filename, bufread, count);
break;
case CMD_READ_INFO:
read_info(filename);
break;
case CMD_READ_NVM:
read_nvm(filename);
break;
default:
break;
}
}
}
da_enable_program_store(0);
nrf24le1_cleanup();
return 0;
}