-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPic32.cpp
745 lines (682 loc) · 18.2 KB
/
Pic32.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
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
* Copyright (C) 2017 Johan Bergkvist
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include "stdio.h"
#include "time.h"
#include "windows.h"
#include "Usb.h"
#include "HexFile.h"
#include "pic32mx220f032b.h"
//
// These are the commands sent from the PC to the PIC. Commands in the range
// 0x00 - 0x0f are used in Prog18FUsb and not reused here to enable a merge
// of the two at some future date.
//
#define COMMAND_CHECK_DEVICE 0x10
#define COMMAND_ERASE 0x11
#define COMMAND_ENTER_SERIAL_EXECUTION_MODE 0x12
#define COMMAND_EXIT_PROGRAMMING_MODE 0x13
#define COMMAND_READ_WORDS 0x15
#define COMMAND_SEND_WORDS 0x16
#define COMMAND_PROGRAM_WORDS 0x17
unsigned char bfm[BFM_SIZE];
unsigned char pfm[PFM_SIZE];
bool g_verbose = true;
//===========================================================================
//
// Name : ReceiveAll
//
// Desc : This function receives a number of bytes from the USB pipe. If
// received data consist of more than four bytes and start with the
// four byte word "DEBU" or "TEXT" the bytes are printed and the
// function reads again.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ReceiveAll(unsigned char *response, int *bytes_received)
{
do
{
//
// Since we're doing USB bulk transfers at 64 bytes maximum packet
// length.
//
*bytes_received = 64;
if (!Receive(response, bytes_received))
{
if (g_verbose)
{
printf("*** Waiting for a result, but the read failed.\n");
}
return false;
}
if (*bytes_received == 0)
{
if (g_verbose)
{
printf("*** Got 0 bytes, reading again.\n");
}
continue;
}
if (*bytes_received > 4
&& response[0] == 'D'
&& response[1] == 'E'
&& response[2] == 'B'
&& response[3] == 'U')
{
printf("DEBU: ");
if ((*bytes_received % 4) == 0)
{
//
// Treat the bytes as an array of words (32 bit).
//
unsigned int *long_response = (unsigned int *)&response[4];
int words = (*bytes_received - 4) / 4;
for (int i = 0; i < words; i++)
{
printf("%08x ", long_response[i]);
}
}
else
{
for (int i = 0; i < *bytes_received - 4; i++)
{
printf("%02x ", response[i + 4]);
}
}
printf("\n");
continue;
}
if (*bytes_received > 4
&& response[0] == 'T'
&& response[1] == 'E'
&& response[2] == 'X'
&& response[3] == 'T')
{
char string[1024];
int i;
for (i = 0; i < *bytes_received - 4; i++)
{
string[i] = response[4 + i];
}
string[i] = 0;
printf("TEXT: %s\n", string);
continue;
}
if (*bytes_received > 0)
{
return true;
}
}
while(1);
}
//===========================================================================
//
// Name : ReceiveResult
//
// Desc : This function reads the USB inpipe until a single bytes has
// been received. This is the result byte.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ReceiveResult(unsigned char *result)
{
unsigned char response[64];
do
{
int bytes_received;
if (!ReceiveAll(response, &bytes_received))
{
return false;
}
if (bytes_received == 1)
{
*result = response[0];
return true;
}
if (g_verbose)
{
printf("*** Waiting for an OK, but got %i bytes rather than 2.\n", bytes_received);
for (int i = 0; i < bytes_received; i++)
{
printf("%02x ", response[i]);
}
printf("\n");
}
}
while(1);
}
//===========================================================================
//
// Name : ReceiveOk
//
// Desc : This function reads the USB in pipe until the two bytes "OK" or
// the four bytes "FAIL" has been received.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ReceiveOk()
{
unsigned char response[64];
do
{
int bytes_received = 64;
if (!ReceiveAll(response, &bytes_received))
{
return false;
}
if (bytes_received == 2
&& response[0] == 'O'
&& response[1] == 'K')
{
return true;
}
if (bytes_received == 4
&& response[0] == 'F'
&& response[1] == 'A'
&& response[2] == 'I'
&& response[3] == 'L')
{
if (g_verbose)
{
printf("*** Waiting for an OK, but got %c%c%c%c.\n", response[0], response[1], response[2], response[3]);
}
return false;
}
if (bytes_received != 2)
{
if (g_verbose)
{
printf("*** Waiting for an OK, but got %i bytes rather than 2.\n", bytes_received);
for (int i = 0; i < bytes_received; i++)
{
printf("%02x ", response[i]);
}
printf("\n");
}
}
}
while(1);
}
//===========================================================================
//
// Name : ReadWords
//
// Desc : Reads "length" words from the target PIC into "buffer" starting
// at address "address".
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ReadWords (unsigned int address, unsigned int *buffer, int number_of_words)
{
unsigned char data[64];
unsigned char command[] = {
COMMAND_READ_WORDS,
static_cast<unsigned char>((address & 0x000000ff) >> 0),
static_cast<unsigned char>((address & 0x0000ff00) >> 8),
static_cast<unsigned char>((address & 0x00ff0000) >> 16),
static_cast<unsigned char>((address & 0xff000000) >> 24),
static_cast<unsigned char>(number_of_words)
};
if (!Send(command, 6))
{
if (g_verbose)
{
printf("*** Failed to send the COMMAND_READ_WORDS message.\n");
}
return false;
}
do
{
int bytes_received;
if (!ReceiveAll(data, &bytes_received))
{
return false;
}
if (number_of_words * 4 != bytes_received)
{
printf("+++ Expected %i bytes but got %i: (", number_of_words * 4, bytes_received);
for (int i = 0; i < bytes_received; i++)
{
printf("%02x ", data[i]);
}
printf(")\n");
continue;
}
memcpy(buffer, data, number_of_words * 4);
break;
}
while(1);
return true;
}
//===========================================================================
//
// Name : SendWords
//
// Desc : Sends 32 bytes to be programmed. Doesn't actually program them,
// that is done by the ProgramWords function.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool SendWords (unsigned char offset, unsigned char *bytes)
{
unsigned char command[1024] = {
COMMAND_SEND_WORDS,
offset
};
memcpy(&command[2], bytes, 32);
return Send(command, 2 + 32) && ReceiveOk();
}
//===========================================================================
//
// Name : ProgramWords
//
// Desc : Program bytes previously sent using the SendBytes function.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ProgramWords (unsigned int address)
{
unsigned char command[1024] = {
COMMAND_PROGRAM_WORDS,
static_cast<unsigned char>((address & 0x000000ff) >> 0),
static_cast<unsigned char>((address & 0x0000ff00) >> 8),
static_cast<unsigned char>((address & 0x00ff0000) >> 16),
static_cast<unsigned char>((address & 0xff000000) >> 24)
};
return Send(command, 5) && ReceiveOk();
}
//===========================================================================
//===========================================================================
//===========================================================================
//
// Name : CheckDevice
//
// Desc : .
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool CheckDevice ()
{
unsigned char mchp_status;
if (!Send(COMMAND_CHECK_DEVICE))
{
return false;
}
if (!ReceiveResult(&mchp_status))
{
return false;
}
if (!(mchp_status & MCHP_STATUS_FCBUSY)
&& (mchp_status & MCHP_STATUS_CFGRDY))
{
return true;
}
if (mchp_status == 0xff)
{
printf("*** (CheckDevice) MCHP_STATUS was 0xff. That usually means the PIC32MX is not powered.\n");
}
else
{
if (mchp_status & MCHP_STATUS_NVMERR)
{
printf("*** (CheckDevice): NVMERR was asserted in MCHP_STATUS (%02x).\n", mchp_status);
}
if (mchp_status & MCHP_STATUS_FCBUSY)
{
printf("*** (CheckDevice): FCBUSY was asserted in MCHP_STATUS (%02x).\n", mchp_status);
}
if (!(mchp_status & MCHP_STATUS_CFGRDY))
{
printf("*** (CheckDevice): CFGRDY was NOT asserted in MCHP_STATUS (%02x).\n", mchp_status);
}
}
return false;
}
//===========================================================================
//
// Name : Erase
//
// Desc : .
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool Erase ()
{
unsigned char mchp_status;
if (!Send(COMMAND_ERASE))
{
return false;
}
if (!ReceiveResult(&mchp_status))
{
return false;
}
if (!(mchp_status & MCHP_STATUS_NVMERR)
&& !(mchp_status & MCHP_STATUS_FCBUSY)
&& (mchp_status & MCHP_STATUS_CFGRDY))
{
return true;
}
if (mchp_status & MCHP_STATUS_NVMERR)
{
printf("*** The erase failed. NVMERR was asserted in MCHP_STATUS (%02x).\n", mchp_status);
}
if (mchp_status & MCHP_STATUS_FCBUSY)
{
printf("*** The erase failed. FCBUSY was asserted in MCHP_STATUS (%02x).\n", mchp_status);
}
if (!(mchp_status & MCHP_STATUS_CFGRDY))
{
printf("*** The erase failed. CFGRDY was NOT asserted in MCHP_STATUS (%02x).\n", mchp_status);
}
return false;
}
//===========================================================================
//
// Name : EnterProgrammingMode
//
// Desc : .
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool EnterProgrammingMode ()
{
return Send(COMMAND_ENTER_SERIAL_EXECUTION_MODE) && ReceiveOk();
}
//===========================================================================
//
// Name : ExitProgrammingMode
//
// Desc : .
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ExitProgrammingMode ()
{
return Send(COMMAND_EXIT_PROGRAMMING_MODE) && ReceiveOk();
}
//===========================================================================
//
// Name : ProgramFlashMemory
//
// Desc : This function will program the content of a memory area "fm"
// (short for flash memory) consisting of "size" bytes into the
// target PIC starting at physical address "address".
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ProgramFlashMemory(unsigned char *fm, int size, unsigned int address)
{
//
// Loop over each "row", as specified by the PIC32 flash programming
// specification. A row is the number of bytes programmed into the
// target PIC in one go. The size of the row depends on the target PIC;
// for the PIC32MX220F032B a row is 128 bytes, or 32 words.
//
for (int row_index = 0; row_index < size / ROW_SIZE; row_index++)
{
//
// Does this row need programming? It does if at least one byte is not
// 0xff.
bool needs_programming = false;
for (int i = 0; i < ROW_SIZE && !needs_programming; i++)
{
needs_programming = fm[row_index * ROW_SIZE + i] != 0xff;
}
if (!needs_programming)
{
//
// This row is all 0xff, so does not need to be programmed. On to
// the next row.
//
continue;
}
//
// This row does need to be programmed. We do so by sending the bytes to
// the target PIC using the SendWords function, which makes sure the words
// are stored in target PIC SRAM, then issue a ProgramWords call to have
// them programmed to the flash memory.
//
// For the PIC32MX220F032B, we send 32 bytes in each call to SendWords. With
// a row equal to 128 bytes, we will call SendWords four times per row.
//
for (int offset = 0; offset < ROW_SIZE; offset += 32)
{
if (!SendWords(offset, &fm[row_index * ROW_SIZE + offset]))
{
return false;
}
}
if (!ProgramWords(address + row_index * ROW_SIZE))
{
return false;
}
}
return true;
}
//===========================================================================
//
// Name : Program
//
// Desc : This function will program the content of the bfm and pgm areas
// into the target PIC.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool Program ()
{
return ProgramFlashMemory(pfm, PFM_SIZE, PFM_START)
&& ProgramFlashMemory(bfm, BFM_SIZE, BFM_START);
}
//===========================================================================
//
// Name : Verify
//
// Desc : Verifies that the contents of the "memory_segments" array matches
// the contents of the target PIC.
//
// Returns : True if successfull, false otherwise.
//
//===========================================================================
bool Verify ()
{
//
// Loop over each segment as read from the hex file and verify than the
// words read from the PIC match.
//
for (int seg = 0; seg < g_number_of_segments; seg++)
{
unsigned char buffer[MAX_SEGMENT_LENGTH];
//
// Read the bytes from the PIC.
//
if (!ReadWords (g_memory_segment[seg].address, (unsigned int *)buffer, g_memory_segment[seg].length / 4))
{
return false;
}
//
// Loop over each bytes and verify.
//
for (int i = 0; i < g_memory_segment[seg].length; i++)
{
//
// SPECIAL CASE: The DEVCFG0 word is always read with it's most signficant
// bit set to zero (this is according to documentation and not unexpected)
// and for some reason the JTAGEN bit cannot be programmed to 0, which is
// not expected.
//
// In any event we ignore a mismatch in the the DEVCFG0 word.
//
if (buffer[i] != g_memory_segment[seg].bytes[i]
&& (((g_memory_segment[seg].address + i) & 0xfffffffc) != DEVICE_CONFIG_ADDRESS_0))
{
printf ("\n\n*** Verification Error: Byte %08x should be %02x, but reads as %02x.\n\n",
g_memory_segment[seg].address + i,
g_memory_segment[seg].bytes[i],
buffer[i]);
return false;
}
}
}
return true;
}
//===========================================================================
//
// Name : DumpDevice32
//
// Desc : Write the contents of "length" bytes from address "address".
// "Length" should be a multiple of 16.
//
// Returns : True if successfull, false otherwise.
//
//===========================================================================
bool DumpDevice32(int address, int length)
{
unsigned int buffer[1024];
for (int i = 0; i < length; i += 16)
{
if (!ReadWords (address + i, buffer, 4))
{
return false;
}
printf ("%08x : %08x %08x %08x %08x\n",
address + i,
buffer[0],
buffer[1],
buffer[2],
buffer[3]);
}
return true;
}
//===========================================================================
//
// Name : Erase32
//
// Desc :
//
// Returns : Nothing.
//
//===========================================================================
void Erase32()
{
if (CheckDevice()
&& Erase()
&& EnterProgrammingMode()
&& ExitProgrammingMode())
{
printf ("Erased!\n");
}
}
//===========================================================================
//
// Name : Program32
//
// Desc :
//
// Returns : Nothing.
//
//===========================================================================
void Program32()
{
//
// The bfm (boot flash memory) and pgm (program flash memory) area areas
// that will hold the bytes to be programmed. Bytes that do not need to be
// programmed should be 0xff.
//
memset(bfm, 0xff, BFM_SIZE);
memset(pfm, 0xff, PFM_SIZE);
//
// Now loop over each of the segments loaded and store them in the BFM or
// PGM as appropriate.
//
for (int seg = 0; seg < g_number_of_segments; seg++)
{
unsigned char *fm = (g_memory_segment[seg].address < BFM_START) ? pfm : bfm;
int offset = g_memory_segment[seg].address & 0x000fffff;
memcpy(&fm[offset], g_memory_segment[seg].bytes, g_memory_segment[seg].length);
}
if (CheckDevice()
&& Erase()
&& EnterProgrammingMode()
&& Program()
&& Verify()
&& ExitProgrammingMode())
{
printf ("Programmed!\n");
}
}
//===========================================================================
//
// Name : ReadDeviceId32
//
// Desc :
//
// Returns : Nothing.
//
//===========================================================================
void ReadDeviceId32()
{
unsigned int dev_id;
if (CheckDevice() && EnterProgrammingMode() && ReadWords(DEVICE_ID_ADDRESS, &dev_id, 1) && ExitProgrammingMode())
{
printf ("Dev_ID : 0x%08x", dev_id);
switch (dev_id & 0x0fffffff)
{
case 0x04A07053: printf(", a PIC32MX110F016B rev %i", dev_id >> 28); break;
case 0x04A06053: printf(", a PIC32MX120F032B rev %i", dev_id >> 28); break;
case 0x04D07053: printf(", a PIC32MX130F064B rev %i", dev_id >> 28); break;
case 0x04D06053: printf(", a PIC32MX150F128B rev %i", dev_id >> 28); break;
case 0x04A01053: printf(", a PIC32MX210F016B rev %i", dev_id >> 28); break;
case 0x04A00053: printf(", a PIC32MX220F032B rev %i", dev_id >> 28); break;
case 0x04D00053: printf(", a PIC32MX250F128B rev %i", dev_id >> 28); break;
case 0x04D01053: printf(", a PIC32MX230F063B rev %i", dev_id >> 28); break;
default: printf(", an unknown part"); break;
}
printf(".\n");
}
else
{
printf ("Failed to read the device ID.\n");
}
}
//===========================================================================
//
// Name : DumpDevice32
//
// Desc :
//
// Returns : Nothing.
//
//===========================================================================
void DumpDevice32()
{
if (!CheckDevice() || !EnterProgrammingMode())
{
return;
}
printf ("\nProgram Flash Memory:\n");
DumpDevice32(0x1d000000, 256);
printf ("\nBoot Flash Memory:\n");
DumpDevice32(0x1fc00000, 256);
printf ("\nConfiguration words:\n");
DumpDevice32(DEVICE_CONFIG_ADDRESS_3, 0x10); // Configuration words.
printf ("\nDevice ID bytes:\n");
DumpDevice32(DEVICE_ID_ADDRESS, 0x01); // Device ID bytes.
ExitProgrammingMode();
}