forked from sergev/pic32prog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic32prog.c
712 lines (660 loc) · 21.9 KB
/
pic32prog.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
* Flash memory programmer for Microchip PIC32 microcontrollers.
*
* Copyright (C) 2011-2012 Serge Vakulenko
*
* This file is part of PIC32PROG project, which is distributed
* under the terms of the GNU General Public License (GPL).
* See the accompanying file "COPYING" for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <getopt.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <time.h>
#include <libgen.h>
#include <locale.h>
#include "target.h"
#include "localize.h"
#define VERSION "1."SVNVERSION
#define BLOCKSZ 1024
#define FLASHV_BASE 0x9d000000
#define BOOTV_BASE 0x9fc00000
#define FLASHP_BASE 0x1d000000
#define BOOTP_BASE 0x1fc00000
#define FLASH_BYTES (512 * 1024)
#define BOOT_BYTES (12 * 1024)
/* Macros for converting between hex and binary. */
#define NIBBLE(x) (isdigit(x) ? (x)-'0' : tolower(x)+10-'a')
#define HEX(buffer) ((NIBBLE((buffer)[0])<<4) + NIBBLE((buffer)[1]))
/* Data to write */
unsigned char boot_data [BOOT_BYTES];
unsigned char flash_data [FLASH_BYTES];
unsigned char boot_dirty [BOOT_BYTES / BLOCKSZ];
unsigned char flash_dirty [FLASH_BYTES / BLOCKSZ];
unsigned boot_used;
unsigned flash_used;
unsigned boot_bytes;
unsigned flash_bytes;
int total_bytes;
#define DEVCFG3_ADDR(base) (base + boot_bytes - 16)
#define DEVCFG2_ADDR(base) (base + boot_bytes - 12)
#define DEVCFG1_ADDR(base) (base + boot_bytes - 8)
#define DEVCFG0_ADDR(base) (base + boot_bytes - 4)
unsigned progress_count;
int verify_only;
int skip_verify = 0;
int debug_level;
int power_on;
target_t *target;
char *progname;
const char *copyright;
void *fix_time ()
{
static struct timeval t0;
gettimeofday (&t0, 0);
return &t0;
}
unsigned mseconds_elapsed (void *arg)
{
struct timeval t1, *t0 = arg;
unsigned mseconds;
gettimeofday (&t1, 0);
mseconds = (t1.tv_sec - t0->tv_sec) * 1000 +
(t1.tv_usec - t0->tv_usec) / 1000;
if (mseconds < 1)
mseconds = 1;
return mseconds;
}
void store_data (unsigned address, unsigned byte)
{
unsigned offset;
if (address >= BOOTV_BASE && address < BOOTV_BASE + BOOT_BYTES) {
/* Boot code, virtual. */
offset = address - BOOTV_BASE;
boot_data [offset] = byte;
if (offset < BOOT_BYTES - 16)
boot_dirty [offset / 1024] = 1;
boot_used = 1;
} else if (address >= BOOTP_BASE && address < BOOTP_BASE + BOOT_BYTES) {
/* Boot code, physical. */
offset = address - BOOTP_BASE;
boot_data [offset] = byte;
if (offset < BOOT_BYTES - 16)
boot_dirty [offset / 1024] = 1;
boot_used = 1;
} else if (address >= FLASHV_BASE && address < FLASHV_BASE + FLASH_BYTES) {
/* Main flash memory, virtual. */
offset = address - FLASHV_BASE;
flash_data [offset] = byte;
flash_dirty [offset / 1024] = 1;
flash_used = 1;
} else if (address >= FLASHP_BASE && address < FLASHP_BASE + FLASH_BYTES) {
/* Main flash memory, physical. */
offset = address - FLASHP_BASE;
flash_data [offset] = byte;
flash_dirty [offset / 1024] = 1;
flash_used = 1;
} else {
/* Ignore incorrect data. */
//fprintf (stderr, _("%08X: address out of flash memory\n"), address);
return;
}
total_bytes++;
}
/*
* Read the S record file.
*/
int read_srec (char *filename)
{
FILE *fd;
unsigned char buf [256];
unsigned char *data;
unsigned address;
int bytes;
fd = fopen (filename, "r");
if (! fd) {
perror (filename);
exit (1);
}
while (fgets ((char*) buf, sizeof(buf), fd)) {
if (buf[0] == '\n')
continue;
if (buf[0] != 'S') {
fclose (fd);
return 0;
}
if (buf[1] == '7' || buf[1] == '8' || buf[1] == '9')
break;
/* Starting an S-record. */
if (! isxdigit (buf[2]) || ! isxdigit (buf[3])) {
fprintf (stderr, _("%s: bad SREC record: %s\n"), filename, buf);
exit (1);
}
bytes = HEX (buf + 2);
/* Ignore the checksum byte. */
--bytes;
address = 0;
data = buf + 4;
switch (buf[1]) {
case '3':
address = HEX (data);
data += 2;
--bytes;
/* Fall through. */
case '2':
address = (address << 8) | HEX (data);
data += 2;
--bytes;
/* Fall through. */
case '1':
address = (address << 8) | HEX (data);
data += 2;
address = (address << 8) | HEX (data);
data += 2;
bytes -= 2;
while (bytes-- > 0) {
store_data (address++, HEX (data));
data += 2;
}
break;
}
}
fclose (fd);
return 1;
}
/*
* Read HEX file.
*/
int read_hex (char *filename)
{
FILE *fd;
unsigned char buf [256], data[16], record_type, sum;
unsigned address, high;
int bytes, i;
fd = fopen (filename, "r");
if (! fd) {
perror (filename);
exit (1);
}
high = 0;
while (fgets ((char*) buf, sizeof(buf), fd)) {
if (buf[0] == '\n')
continue;
if (buf[0] != ':') {
fclose (fd);
return 0;
}
if (! isxdigit (buf[1]) || ! isxdigit (buf[2]) ||
! isxdigit (buf[3]) || ! isxdigit (buf[4]) ||
! isxdigit (buf[5]) || ! isxdigit (buf[6]) ||
! isxdigit (buf[7]) || ! isxdigit (buf[8])) {
fprintf (stderr, _("%s: bad HEX record: %s\n"), filename, buf);
exit (1);
}
record_type = HEX (buf+7);
if (record_type == 1) {
/* End of file. */
break;
}
if (record_type == 5) {
/* Start address, ignore. */
continue;
}
bytes = HEX (buf+1);
if (strlen ((char*) buf) < bytes * 2 + 11) {
fprintf (stderr, _("%s: too short hex line\n"), filename);
exit (1);
}
address = high << 16 | HEX (buf+3) << 8 | HEX (buf+5);
if (address & 3) {
fprintf (stderr, _("%s: odd address\n"), filename);
exit (1);
}
sum = 0;
for (i=0; i<bytes; ++i) {
data [i] = HEX (buf+9 + i + i);
sum += data [i];
}
sum += record_type + bytes + (address & 0xff) + (address >> 8 & 0xff);
if (sum != (unsigned char) - HEX (buf+9 + bytes + bytes)) {
fprintf (stderr, _("%s: bad HEX checksum\n"), filename);
exit (1);
}
if (record_type == 4) {
/* Extended address. */
if (bytes != 2) {
fprintf (stderr, _("%s: invalid HEX linear address record length\n"),
filename);
exit (1);
}
high = data[0] << 8 | data[1];
continue;
}
if (record_type != 0) {
fprintf (stderr, _("%s: unknown HEX record type: %d\n"),
filename, record_type);
exit (1);
}
//printf ("%08x: %u bytes\n", address, bytes);
for (i=0; i<bytes; i++) {
store_data (address++, data [i]);
}
}
fclose (fd);
return 1;
}
void print_symbols (char symbol, int cnt)
{
while (cnt-- > 0)
putchar (symbol);
}
void progress (unsigned step)
{
++progress_count;
if (progress_count % step == 0) {
putchar ('#');
fflush (stdout);
}
}
void quit (void)
{
if (target != 0) {
target_close (target, power_on);
free (target);
target = 0;
}
}
void interrupted (int signum)
{
fprintf (stderr, _("\nInterrupted.\n"));
quit();
_exit (-1);
}
void do_probe ()
{
unsigned boot_bytes;
/* Open and detect the device. */
atexit (quit);
target = target_open ();
if (! target) {
fprintf (stderr, _("Error detecting device -- check cable!\n"));
exit (1);
}
boot_bytes = target_boot_bytes (target);
printf (_(" Processor: %s (id %08X)\n"), target_cpu_name (target),
target_idcode (target));
printf (_(" Flash memory: %d kbytes\n"), target_flash_bytes (target) / 1024);
if (boot_bytes != 0)
printf (_(" Boot memory: %d kbytes\n"), boot_bytes / 1024);
target_print_devcfg (target);
}
/*
* Write flash memory.
*/
void program_block (target_t *mc, unsigned addr)
{
unsigned char *data;
unsigned offset;
unsigned flash_bytes = target_flash_bytes (mc);
unsigned boot_bytes = target_boot_bytes (mc);
if (addr >= BOOTV_BASE && addr < BOOTV_BASE + boot_bytes) {
data = boot_data;
offset = addr - BOOTV_BASE;
} else if (addr >= BOOTP_BASE && addr < BOOTP_BASE + boot_bytes) {
data = boot_data;
offset = addr - BOOTP_BASE;
} else if (addr >= FLASHV_BASE && addr < FLASHV_BASE + flash_bytes) {
data = flash_data;
offset = addr - FLASHV_BASE;
} else {
data = flash_data;
offset = addr - FLASHP_BASE;
}
target_program_block (mc, addr, BLOCKSZ/4, (unsigned*) (data + offset));
}
int verify_block (target_t *mc, unsigned addr)
{
unsigned char *data;
unsigned offset;
unsigned flash_bytes = target_flash_bytes (mc);
unsigned boot_bytes = target_boot_bytes (mc);
if (addr >= BOOTV_BASE && addr < BOOTV_BASE + boot_bytes) {
data = boot_data;
offset = addr - BOOTV_BASE;
} else if (addr >= BOOTP_BASE && addr < BOOTP_BASE + boot_bytes) {
data = boot_data;
offset = addr - BOOTP_BASE;
} else if (addr >= FLASHV_BASE && addr < FLASHV_BASE + flash_bytes) {
data = flash_data;
offset = addr - FLASHV_BASE;
} else {
data = flash_data;
offset = addr - FLASHP_BASE;
}
target_verify_block (mc, addr, BLOCKSZ/4, (unsigned*) (data + offset));
return 1;
}
void do_program (char *filename)
{
unsigned addr;
int progress_len, progress_step;
void *t0;
unsigned flash_bytes, boot_bytes;
/* Open and detect the device. */
atexit (quit);
target = target_open ();
if (! target) {
fprintf (stderr, _("Error detecting device -- check cable!\n"));
exit (1);
}
flash_bytes = target_flash_bytes (target);
boot_bytes = target_boot_bytes (target);
printf (_(" Processor: %s\n"), target_cpu_name (target));
printf (_(" Flash memory: %d kbytes\n"), flash_bytes / 1024);
if (boot_bytes != 0)
printf (_(" Boot memory: %d kbytes\n"), boot_bytes / 1024);
printf (_(" Data: %d bytes\n"), total_bytes);
if (*(unsigned*) DEVCFG0_ADDR(boot_data) & 0x80000000) {
/* Default configuration. */
*(unsigned*) DEVCFG0_ADDR(boot_data) = 0x7ffffffd;
*(unsigned*) DEVCFG1_ADDR(boot_data) = 0xff6afd5b;
*(unsigned*) DEVCFG2_ADDR(boot_data) = 0xfff879d9;
*(unsigned*) DEVCFG3_ADDR(boot_data) = 0x3affffff;
}
if (! verify_only) {
/* Erase flash. */
target_erase (target);
}
target_use_executable (target);
for (progress_step=1; ; progress_step<<=1) {
progress_len = 1 + total_bytes / progress_step / BLOCKSZ;
if (progress_len < 64)
break;
}
progress_count = 0;
t0 = fix_time ();
if (! verify_only) {
if (flash_used) {
printf (_("Program flash: "));
print_symbols ('.', progress_len);
print_symbols ('\b', progress_len);
fflush (stdout);
for (addr=FLASHV_BASE; addr-FLASHV_BASE<flash_bytes; addr+=BLOCKSZ) {
if (flash_dirty [(addr-FLASHV_BASE) / 1024]) {
program_block (target, addr);
progress (progress_step);
}
}
printf (_("# done\n"));
}
if (boot_used) {
printf (_(" Program boot: ............\b\b\b\b\b\b\b\b\b\b\b\b"));
fflush (stdout);
for (addr=BOOTV_BASE; addr-BOOTV_BASE<boot_bytes; addr+=BLOCKSZ) {
if (boot_dirty [(addr-BOOTV_BASE) / 1024]) {
program_block (target, addr);
progress (1);
}
}
printf (_("# done \n"));
if (! boot_dirty [boot_bytes / 1024 - 1]) {
/* Write chip configuration. */
target_program_word (target, DEVCFG3_ADDR(BOOTV_BASE),
*(unsigned*) DEVCFG3_ADDR(boot_data));
target_program_word (target, DEVCFG2_ADDR(BOOTV_BASE),
*(unsigned*) DEVCFG2_ADDR(boot_data));
target_program_word (target, DEVCFG1_ADDR(BOOTV_BASE),
*(unsigned*) DEVCFG1_ADDR(boot_data));
target_program_word (target, DEVCFG0_ADDR(BOOTV_BASE),
*(unsigned*) DEVCFG0_ADDR(boot_data));
boot_dirty [boot_bytes / 1024 - 1] = 1;
}
}
}
if (flash_used && !skip_verify) {
printf (_(" Verify flash: "));
print_symbols ('.', progress_len);
print_symbols ('\b', progress_len);
fflush (stdout);
for (addr=FLASHV_BASE; addr-FLASHV_BASE<flash_bytes; addr+=BLOCKSZ) {
if (flash_dirty [(addr-FLASHV_BASE) / 1024]) {
progress (progress_step);
if (! verify_block (target, addr))
exit (0);
}
}
printf (_("# done\n"));
}
if (boot_used && !skip_verify) {
printf (_(" Verify boot: ............\b\b\b\b\b\b\b\b\b\b\b\b"));
fflush (stdout);
for (addr=BOOTV_BASE; addr-BOOTV_BASE<boot_bytes; addr+=BLOCKSZ) {
if (boot_dirty [(addr-BOOTV_BASE) / 1024]) {
progress (1);
if (! verify_block (target, addr))
exit (0);
}
}
printf (_(" done \n"));
}
if (boot_used || flash_used)
printf (_("Rate: %ld bytes per second\n"),
total_bytes * 1000L / mseconds_elapsed (t0));
}
void do_read (char *filename, unsigned base, unsigned nbytes)
{
FILE *fd;
unsigned len, addr, data [BLOCKSZ/4], progress_step;
void *t0;
fd = fopen (filename, "wb");
if (! fd) {
perror (filename);
exit (1);
}
printf (_(" Memory: total %d bytes\n"), nbytes);
/* Open and detect the device. */
atexit (quit);
target = target_open ();
if (! target) {
fprintf (stderr, _("Error detecting device -- check cable!\n"));
exit (1);
}
target_use_executable (target);
for (progress_step=1; ; progress_step<<=1) {
len = 1 + nbytes / progress_step / BLOCKSZ;
if (len < 64)
break;
}
printf (" Read: " );
print_symbols ('.', len);
print_symbols ('\b', len);
fflush (stdout);
progress_count = 0;
t0 = fix_time ();
for (addr=base; addr-base<nbytes; addr+=BLOCKSZ) {
progress (progress_step);
target_read_block (target, addr, BLOCKSZ/4, data);
if (fwrite (data, 1, BLOCKSZ, fd) != BLOCKSZ) {
fprintf (stderr, "%s: write error!\n", filename);
exit (1);
}
}
printf (_("# done\n"));
printf (_(" Rate: %ld bytes per second\n"),
nbytes * 1000L / mseconds_elapsed (t0));
fclose (fd);
}
/*
* Print copying part of license
*/
static void gpl_show_copying (void)
{
printf ("%s.\n\n", copyright);
printf ("This program is free software; you can redistribute it and/or modify\n");
printf ("it under the terms of the GNU General Public License as published by\n");
printf ("the Free Software Foundation; either version 2 of the License, or\n");
printf ("(at your option) any later version.\n");
printf ("\n");
printf ("This program is distributed in the hope that it will be useful,\n");
printf ("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
printf ("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
printf ("GNU General Public License for more details.\n");
printf ("\n");
}
/*
* Print NO WARRANTY part of license
*/
static void gpl_show_warranty (void)
{
printf ("%s.\n\n", copyright);
printf ("BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n");
printf ("FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN\n");
printf ("OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n");
printf ("PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n");
printf ("OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n");
printf ("MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS\n");
printf ("TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE\n");
printf ("PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n");
printf ("REPAIR OR CORRECTION.\n");
printf("\n");
printf ("IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n");
printf ("WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n");
printf ("REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n");
printf ("INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n");
printf ("OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n");
printf ("TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n");
printf ("YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n");
printf ("PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n");
printf ("POSSIBILITY OF SUCH DAMAGES.\n");
printf("\n");
}
int main (int argc, char **argv)
{
int ch, read_mode = 0;
unsigned base, nbytes;
static const struct option long_options[] = {
{ "help", 0, 0, 'h' },
{ "warranty", 0, 0, 'W' },
{ "copying", 0, 0, 'C' },
{ "version", 0, 0, 'V' },
{ "skip-verify", 0, 0, 'S' },
{ NULL, 0, 0, 0 },
};
/* Set locale and message catalogs. */
setlocale (LC_ALL, "");
#if defined (__CYGWIN32__) || defined (MINGW32)
/* Files with localized messages should be placed in
* the current directory or in c:/Program Files/pic32prog. */
if (access ("./ru/LC_MESSAGES/pic32prog.mo", R_OK) == 0)
bindtextdomain ("pic32prog", ".");
else
bindtextdomain ("pic32prog", "c:/Program Files/pic32prog");
#else
bindtextdomain ("pic32prog", "/usr/local/share/locale");
#endif
textdomain ("pic32prog");
setvbuf (stdout, (char *)NULL, _IOLBF, 0);
setvbuf (stderr, (char *)NULL, _IOLBF, 0);
printf (_("Programmer for Microchip PIC32 microcontrollers, Version %s\n"), VERSION);
progname = argv[0];
copyright = _(" Copyright: (C) 2011-2012 Serge Vakulenko");
signal (SIGINT, interrupted);
#ifdef __linux__
signal (SIGHUP, interrupted);
#endif
signal (SIGTERM, interrupted);
while ((ch = getopt_long (argc, argv, "vDhrpCVWS",
long_options, 0)) != -1) {
switch (ch) {
case 'v':
++verify_only;
continue;
case 'D':
++debug_level;
continue;
case 'r':
++read_mode;
continue;
case 'p':
++power_on;
continue;
case 'h':
break;
case 'V':
/* Version already printed above. */
return 0;
case 'C':
gpl_show_copying ();
return 0;
case 'W':
gpl_show_warranty ();
return 0;
case 'S':
++skip_verify;
continue;
}
usage:
printf ("%s.\n\n", copyright);
printf ("PIC32prog comes with ABSOLUTELY NO WARRANTY; for details\n");
printf ("use `--warranty' option. This is Open Source software. You are\n");
printf ("welcome to redistribute it under certain conditions. Use the\n");
printf ("'--copying' option for details.\n\n");
printf ("Probe:\n");
printf (" pic32prog\n");
printf ("\nWrite flash memory:\n");
printf (" pic32prog [-v] file.srec\n");
printf (" pic32prog [-v] file.hex\n");
printf ("\nRead memory:\n");
printf (" pic32prog -r file.bin address length\n");
printf ("\nArgs:\n");
printf (" file.srec Code file in SREC format\n");
printf (" file.hex Code file in Intel HEX format\n");
printf (" file.bin Code file in binary format\n");
printf (" -v Verify only\n");
printf (" -r Read mode\n");
printf (" -p Leave board powered on\n");
printf (" -D Debug mode\n");
printf (" -h, --help Print this help message\n");
printf (" -V, --version Print version\n");
printf (" -C, --copying Print copying information\n");
printf (" -W, --warranty Print warranty information\n");
printf (" -S, --skip-verify Skip the write verification step\n");
printf ("\n");
return 0;
}
printf ("%s\n", copyright);
argc -= optind;
argv += optind;
memset (boot_data, ~0, BOOT_BYTES);
memset (flash_data, ~0, FLASH_BYTES);
switch (argc) {
case 0:
do_probe ();
break;
case 1:
if (! read_srec (argv[0]) &&
! read_hex (argv[0])) {
fprintf (stderr, _("%s: bad file format\n"), argv[0]);
exit (1);
}
do_program (argv[0]);
break;
case 3:
if (! read_mode)
goto usage;
base = strtoul (argv[1], 0, 0);
nbytes = strtoul (argv[2], 0, 0);
do_read (argv[0], base, nbytes);
break;
default:
goto usage;
}
quit ();
return 0;
}