-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pic18F.c
362 lines (320 loc) · 8.75 KB
/
Pic18F.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
/*
* 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.
*
* This is the firmware for the 18f2550 chip allowing it to program another
* 18f2550 (and others). It communicates with a PC over USB.
*
* The programming specification is Microchip document DS39622F titled
* "Flash Microcontroller Programming Specification". This firmware
* implements the high-voltage In-Circuit Serial Programming (ICSP)
* specification.
*
* The following table illustrate how pins on the 18f2550 should be
* connected to the target (another 18f2550). Note that the MCLR signal is
* routed through a MAX680 to boost the voltage.
*
* 18f2550 | target
* ================
* RB3 | MCLR
* RB2 | VDD
* RB1 | PGD
* RB0 | PGC
*/
#include <xc.h>
#include "Pins.h"
#include "Usb.h"
#include "Commands.h"
#include "Delays.h"
//
// These are (some of) the four bit commands sent to a device when
// programming. See page 11 of Microchip document DS39622F.
//
#define CORE 0x00
#define TABLE_READ_POSTINC 0x09
#define TABLE_WRITE 0x0c
#define TABLE_WRITE_POSTINC 0x0d
#define TABLE_WRITE_START_PROGRAM 0x0f
//---------------------------------------------------------------------------
// WriteCommand (Send4Bits)
//
// Pulses the PGC four times, each time holding PGD according to the lsb of
// the "command", then shifting "command" to the right. This has the effect of
// sending the 4 lowest bits in "commamd" lsb first.
//---------------------------------------------------------------------------
void WriteCommand(unsigned char command)
{
unsigned char number_of_bits = 4;
do
{
PGD_OUT = command & 0x01;
command = command >> 1;
PGC = 1;
PGC = 0;
}
while (--number_of_bits != 0);
}
//---------------------------------------------------------------------------
// WriteByte (Send8Bits)
//
// Pulses the PGC eight times, each time holding PGD according to the lsb of
// the "byte", then shifting "byte" to the right. This has the effect of
// sending the 8 bits in "byte" lsb first.
//---------------------------------------------------------------------------
void WriteByte(unsigned char byte)
{
unsigned char number_of_bits = 8;
do
{
PGD_OUT = byte & 0x01;
byte = byte >> 1;
PGC = 1;
PGC = 0;
}
while (--number_of_bits != 0);
}
//---------------------------------------------------------------------------
// ReadByte
//
// Sets PGD to input, then pulsing PGC 8 times. The state of PGD sets the
// msb before it is shifted right once for each PGC cycle. This has the
// effect of reading 8 bits lsb first and that byte is returned from this
// function.
//---------------------------------------------------------------------------
unsigned char ReadByte(void)
{
unsigned char byte = 0;
unsigned char i;
TRIS_PGD = 1;
for (i = 0; i < 8; i++)
{
PGC = 1;
byte = ((byte >> 1) & 0x7f);
if (PGD_IN)
{
byte |= 0x80;
}
PGC = 0;
}
TRIS_PGD = 0;
return byte;
}
//---------------------------------------------------------------------------
// WriteCore
//---------------------------------------------------------------------------
void WriteCore(unsigned char arg1, unsigned char arg2)
{
WriteCommand(CORE);
WriteByte(arg1);
WriteByte(arg2);
}
//---------------------------------------------------------------------------
// WriteProgrammingCore
//---------------------------------------------------------------------------
void WriteProgrammingCore(void)
{
unsigned char i;
PGD_OUT = 0;
for (i = 0; i < 3; i++)
{
PGC = 1;
PGC = 0;
}
PGC = 1;
Delayms(2);
PGC = 0;
Delayms(2);
}
//---------------------------------------------------------------------------
// TableWrite
//---------------------------------------------------------------------------
void TableWrite(unsigned char arg1, unsigned char arg2)
{
WriteCommand(TABLE_WRITE);
WriteByte(arg1);
WriteByte(arg2);
}
//---------------------------------------------------------------------------
// SetTBLPTR
//---------------------------------------------------------------------------
void SetTBLPTR(unsigned char address_0, unsigned char address_1, unsigned char address_2)
{
//
// Load the TBLPT registers with the provided address.
//
WriteCore(address_0, 0x0e); // MOVLW <arg>
WriteCore(0xf8, 0x6e); // MOVWF TBLTRU
WriteCore(address_1, 0x0e); // MOVLW <arg>
WriteCore(0xf7, 0x6e); // MOVWF TBLTRH
WriteCore(address_2, 0x0e); // MOVLW <arg>
WriteCore(0xf6, 0x6e); // MOVWF TBLTRL
}
//===========================================================================
//---------------------------------------------------------------------------
// ProgramBytes
//---------------------------------------------------------------------------
void ProgramBytes(unsigned char address_0,
unsigned char address_1,
unsigned char address_2,
unsigned char *bytes,
unsigned char length)
{
unsigned char i;
//
// "BSF EECON1, EEPGD"
//
WriteCore(0xa6, 0x8e);
//
// "BSF EECON1, CFGS"
//
WriteCore(0xa6, 0x9c);
//
// Start writing at address "address_x".
//
SetTBLPTR(address_0, address_1, address_2);
//
// Write two bytes at a time and save the last two for later; they shall
// not use POSTINC.
//
for (i = 0; i < length - 2; i += 2)
{
WriteCommand(TABLE_WRITE_POSTINC);
WriteByte(bytes[i]);
WriteByte(bytes[i + 1]);
}
//
// Write the last two bytes.
//
WriteCommand(TABLE_WRITE_START_PROGRAM);
WriteByte(bytes[i]);
WriteByte(bytes[i + 1]);
//
// Finish with a special NOP where the fourth command PGC is held high
// while the actual programming takes place.
//
WriteProgrammingCore();
WriteByte(0);
WriteByte(0);
SendOk();
}
//---------------------------------------------------------------------------
// ProgramConfigByte
//---------------------------------------------------------------------------
void ProgramConfigByte(unsigned char offset, unsigned char byte)
{
//
// "BSF EECON1, EEPGD"
//
WriteCore(0xa6, 0x8e);
//
// "BSF EECON1, CFGS"
//
WriteCore(0xa6, 0x8c);
//
// Write "byte" to address 0x300000+offset. If the address in an even
// address one of the bytes will be written and the other ignored. If the
// address is odd the other bytes is used instead.
//
SetTBLPTR(0x30, 0x00, offset);
WriteCommand(TABLE_WRITE_START_PROGRAM);
WriteByte(byte);
WriteByte(byte);
//
// Finish with a special NOP where the fourth command PGC is held high
// while the actual programming takes place.
//
WriteProgrammingCore();
WriteByte(0);
WriteByte(0);
SendOk();
}
//---------------------------------------------------------------------------
// ReadBytes
//---------------------------------------------------------------------------
void ReadBytes(unsigned char address_0,
unsigned char address_1,
unsigned char address_2,
unsigned char length)
{
unsigned char bytes[64];
unsigned char i;
//
// Start reading at address "address_x".
//
SetTBLPTR(address_0, address_1, address_2);
//
// Read "length" bytes.
//
for (i = 0; i < length; i++)
{
WriteCommand(TABLE_READ_POSTINC);
WriteByte(0);
bytes[i] = ReadByte();
}
SendInPacket(bytes, length);
}
//---------------------------------------------------------------------------
// Erase
//---------------------------------------------------------------------------
void Erase(void)
{
//
// Write 0x0f to 0x3c0005.
//
SetTBLPTR(0x3c, 0x00, 0x05);
TableWrite(0x0f, 0x0f);
//
// Write 0x87 to 0x3c0004.
//
SetTBLPTR(0x3c, 0x00, 0x04);
TableWrite(0x87, 0x87);
//
// NOP.
//
WriteCore(0x00, 0x00);
//
// Special NOP with a long gap between the end of the four bit command
// and the 16 bit argument.
//
WriteCommand(CORE);
Delayms(6);
WriteByte(0);
WriteByte(0);
SendOk();
}
//---------------------------------------------------------------------------
// ProcessMessage
//---------------------------------------------------------------------------
void ProcessMessage18(unsigned char *message, unsigned char length)
{
switch (message[0])
{
case COMMAND_VDD_ON:
case COMMAND_VPP_ON:
case COMMAND_VPP_VDD_OFF:
PGC = 0;
PGD_OUT = 0;
switch (message[0])
{
case COMMAND_VDD_ON : VDD = 1; break;
case COMMAND_VPP_ON : VPP = 1; break;
case COMMAND_VPP_VDD_OFF: VPP = 0; VDD = 0; break;
}
SendOk();
break;
case COMMAND_ERASE:
Erase();
break;
case COMMAND_PROGRAM_BYTES:
ProgramBytes(message[1], message[2], message[3], &message[4], length - 4);
break;
case COMMAND_PROGRAM_CONFIG:
ProgramConfigByte(message[1], message[2]);
break;
case COMMAND_READ_BYTES:
ReadBytes(message[1], message[2], message[3], message[4]);
break;
}
}