-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPic18.cpp
499 lines (446 loc) · 11.9 KB
/
Pic18.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
/*
* 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"
#define READBYTES 0x00
#define PROGRAMBYTES 0x01
#define PROGRAMCONFIGBYTE 0x02
#define ERASE 0x03
#define VDDON 0x04
#define VPPON 0x05
#define VPPVDDOFF 0x06
//===========================================================================
//
// Name : ReceiveOk18
//
// Desc : .
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ReceiveOk18 ()
{
unsigned char response[64];
int bytes_received = 64;
if (!Receive(response, &bytes_received))
{
printf("*** Waiting for an OK, but nothing came.\n");
return false;
}
if (bytes_received != 2)
{
printf("*** Waiting for an OK, but got %i bytes rather than 2.\n", bytes_received);
return false;
}
if (response[0] != 'O' || response[1] != 'K')
{
printf("*** Waiting for an OK, but got \"%c%c\".\n", response[0], response[1]);
return false;
}
return true;
}
//===========================================================================
//
// Name : VddOn
//
// Desc : Turns on Vdd (+5V) on the target PIC.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool VddOn ()
{
unsigned char command[] = {VDDON};
return Send(command, 1) && ReceiveOk18();
}
//===========================================================================
//
// Name : VppOn
//
// Desc : Turns on Vpp (+12V) on the target PIC.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool VppOn ()
{
unsigned char command[] = {VPPON};
return Send(command, 1) && ReceiveOk18();
}
//===========================================================================
//
// Name : VppVddOff
//
// Desc : Turns off Vpp and Vdd. Vdd must be turned off after Vpp, but no
// longer than 100ns after. In reality we have no choice but to turn
// them off at the same time.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool VppVddOff ()
{
unsigned char command[] = {VPPVDDOFF};
return Send(command, 1) && ReceiveOk18();
}
//===========================================================================
//
// Name : ReadBytes
//
// Desc : Reads "length" bytes from the target PIC into "buffer" starting
// at address "address".
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ReadBytes (unsigned int address, unsigned char *buffer, int length)
{
unsigned char command[] = {
READBYTES,
static_cast<unsigned char>((address & 0x00ff0000) >> 16),
static_cast<unsigned char>((address & 0x0000ff00) >> 8),
static_cast<unsigned char>((address & 0x000000ff) >> 0),
static_cast<unsigned char>(length)
};
if (!Send(command, 5))
{
return false;
}
int bytes_received = length;
if (!Receive(buffer, &bytes_received))
{
return false;
}
return true;
}
//===========================================================================
//
// Name : ProgramBytes
//
// Desc : Writes "length" bytes to the target PIC from "buffer" starting
// at address "address".
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ProgramBytes (unsigned int address, unsigned char *buffer, int length)
{
//
// Some PIC18F devices (such as the PIC18F1330) have a write buffer
// size of 8. For simplicity, we make all programming adhere to that
// limit.
//
bool ok = true;
for (int i = 0; i < length && ok; i += 8) {
unsigned int addr = address + i;
int len = (length - i < 8) ? length - i : 8;
unsigned char command[12] = { // Four byte header plus eight bytes of data.
PROGRAMBYTES,
static_cast<unsigned char>((addr & 0x00ff0000) >> 16),
static_cast<unsigned char>((addr & 0x0000ff00) >> 8),
static_cast<unsigned char>((addr & 0x000000ff) >> 0)
};
memcpy(&command[4], &buffer[i], len);
ok = Send(command, len + 4);
if (ok) {
ok = ReceiveOk18();
}
}
return ok;
}
//===========================================================================
//
// Name : ProgramConfigByte
//
// Desc : Write configuration byte "byte" into address "address". Address
// must be in the range 0x300000 to 0x30000d inclusive.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool ProgramConfigByte (unsigned int address, unsigned char byte)
{
unsigned char command[] = {
PROGRAMCONFIGBYTE,
static_cast<unsigned char>((address & 0x000000ff) >> 0),
byte
};
return Send(command, 3) && ReceiveOk18();
}
//===========================================================================
//===========================================================================
//===========================================================================
//
// Name : DumpDevice
//
// Desc : Write the contents of "length" bytes from address "address".
// "Length" should be a multiple of 16.
//
// Returns : True if successfull, false otherwise.
//
//===========================================================================
bool DumpDevice18(int address, int length)
{
unsigned char buffer[16];
for (int i = 0; i < length; i += 16)
{
if (!ReadBytes (address + i, buffer, 16))
{
return false;
}
printf ("%06x : %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
address + i,
buffer[0],
buffer[1],
buffer[2],
buffer[3],
buffer[4],
buffer[5],
buffer[6],
buffer[7],
buffer[8],
buffer[9],
buffer[10],
buffer[11],
buffer[12],
buffer[13],
buffer[14],
buffer[15]);
}
return true;
}
//===========================================================================
//
// Name : Erase18
//
// Desc :
//
// Returns : Nothing.
//
//===========================================================================
void Erase18()
{
VddOn ();
VppOn ();
//
// Wait for VPP to settle. The MAX680 takes a little while.
//
Sleep(100);
unsigned char command[] = {ERASE};
if (Send(command, 1) && ReceiveOk18())
{
printf ("Erased!\n");
}
VppVddOff ();
}
//===========================================================================
//
// Name : Program18
//
// Desc : Writes the contents of the "memory_segments" array to the device.
//
// Returns : Nothing.
//
//===========================================================================
void Program18()
{
VddOn ();
VppOn ();
//
// Wait for VPP to settle. The MAX680 takes a little while.
//
Sleep(100);
//
// Read the device ID. Different devices use different config bytes, so we need to know what to verify.
//
unsigned char buffer[2];
if (!ReadBytes (0x3ffffe, buffer, 2)) {
return;
}
unsigned short int device_id = buffer[0] | (buffer[1] << 8);
do
{
//
// Program...
//
for (int seg = 0; seg < g_number_of_segments; seg++)
{
if (g_memory_segment[seg].address < 0x300000)
{
//
// Make sure there are no single bytes as the programmer doesn't like them.
//
int length = g_memory_segment[seg].length;
if (length == 1)
{
g_memory_segment[seg].bytes[1] = 0xff;
length = 2;
}
if (!ProgramBytes (g_memory_segment[seg].address,
g_memory_segment[seg].bytes,
length))
{
break;
}
}
else if (g_memory_segment[seg].address < 0x3ffffe)
{
//
// CONFIG words will be programmed last...
//
}
else
{
printf ("ERROR: EEPROM data not supported!\n");
}
}
for (int seg = 0; seg < g_number_of_segments; seg++)
{
if (0x300000 <= g_memory_segment[seg].address
&& g_memory_segment[seg].address < 0x30000d)
{
for (int i = 0; i < g_memory_segment[seg].length; i++)
{
if (!ProgramConfigByte (g_memory_segment[seg].address + i,
g_memory_segment[seg].bytes[i]))
{
break;
}
}
}
}
//
// Verify...
//
bool error_found = false;
for (int seg = 0; seg < g_number_of_segments && !error_found; seg++)
{
unsigned char buffer[MAX_SEGMENT_LENGTH];
if (!ReadBytes (g_memory_segment[seg].address, buffer, g_memory_segment[seg].length))
{
break;
}
for (int i = 0; i < g_memory_segment[seg].length; i++)
{
//
// Not all config bytes are used on all devices. Unused ones
// are programmed 0xff, but read as 0x00, so they should not
// be verified.
//
boolean check_this_byte = true;
unsigned int addr = g_memory_segment[seg].address + i;
switch (device_id & 0xffe0) // Mask away the revision number.
{
case 0x1200:
case 0x1220:
case 0x1240:
case 0x1260:
case 0x5c00:
case 0x5c20:
case 0x5c60:
case 0x5d20:
case 0x5d60:
if (addr == 0x00300004) check_this_byte = false;
if (addr == 0x00300007) check_this_byte = false;
break;
case 0x1e00:
case 0x1e20:
case 0x1ee0:
if (addr == 0x00300000) check_this_byte = false;
if (addr == 0x00300007) check_this_byte = false;
break;
}
if (check_this_byte && buffer[i] != g_memory_segment[seg].bytes[i])
{
printf ("\n*** Verification Error: Byte %06x should be %02x, but reads as %02x.\n",
g_memory_segment[seg].address + i,
g_memory_segment[seg].bytes[i],
buffer[i]);
error_found = true;
break;
}
}
}
if (!error_found) {
printf ("Programmed!\n");
}
}
while(0);
VppVddOff ();
}
//===========================================================================
//
// Name : ReadDeviceId18
//
// Desc :
//
// Returns : Nothing.
//
//===========================================================================
void ReadDeviceId18()
{
VddOn ();
VppOn ();
//
// Wait for VPP to settle. The MAX680 takes a little while.
//
Sleep(100);
unsigned char buffer[2];
if (ReadBytes (0x3ffffe, buffer, 2))
{
unsigned short int word = buffer[0] | (buffer[1] << 8);
printf ("Dev_ID : 0x%02x%02x", buffer[1], buffer[0]);
switch (word & 0xffe0)
{
case 0x1200: printf(", a 18F4550 rev %i", word & 0x001f); break;
case 0x1220: printf(", a 18F4450 rev %i", word & 0x001f); break;
case 0x1240: printf(", a 18F2550 rev %i", word & 0x001f); break;
case 0x1260: printf(", a 18F2450 rev %i", word & 0x001f); break;
case 0x5c00: printf(", a 18F45K50 rev %i", word & 0x001f); break;
case 0x5c20: printf(", a 18F25K50 rev %i", word & 0x001f); break;
case 0x5c60: printf(", a 18F24K50 rev %i", word & 0x001f); break;
case 0x5d20: printf(", a 18F26K50 rev %i", word & 0x001f); break;
case 0x5d60: printf(", a 18F46K50 rev %i", word & 0x001f); break;
case 0x1e00: printf(", a 18F1230 rev %i", word & 0x001f); break;
case 0x1e20: printf(", a 18F1330 rev %i", word & 0x001f); break;
case 0x1ee0: printf(", a 18F1330-ICD rev %i", word & 0x001f); break;
default: printf(", an unknown part"); break;
}
printf(".\n");
}
VppVddOff ();
}
//===========================================================================
//
// Name : DumpDevice18
//
// Desc :
//
// Returns : Nothing.
//
//===========================================================================
void DumpDevice18()
{
VddOn ();
VppOn ();
//
// Wait for VPP to settle. The MAX680 takes a little while.
//
Sleep(100);
DumpDevice18(0x000000, 0x400);
//DumpDevice18(0x000800, 0x1000);
printf ("\nUser ID words:\n");
DumpDevice18(0x200000, 0x10); // User ID words.
printf ("\nConfiguration words:\n");
DumpDevice18(0x300000, 0x10); // Configuration words.
printf ("\nDevice ID words:\n");
DumpDevice18(0x3ffff0, 0x10); // Device ID words.
VppVddOff ();
}