-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pic16F.c
338 lines (285 loc) · 8.28 KB
/
Pic16F.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
/*
* 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 a
* 16f6x8A. It communicates with a PC over USB.
*
* The programming specification is Microchip document DS41196E titled
* "PIC16F6xx/A EEPROM 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 (it is the same as for the PIC18). 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 the six bit commands sent to a device when
// programming. See page 5 of Microchip document DS41196E.
//
#define LOAD_CONFIGURATION 0x00
#define LOAD_DATA_FOR_PROGRAM_MEMORY 0x02
#define LOAD_DATA_FOR_DATA_MEMORY 0x03
#define INCREMENT_ADDRESS 0x06
#define READ_DATA_FROM_PROGRAM_MEMORY 0x04
#define READ_DATA_FROM_DATA_MEMORY 0x05
#define BEGIN_PROGRAMMING_CYCLE 0x08
#define BULK_ERASE_PROGRAM_MEMORY 0x09
#define BULK_ERASE_DATA_MEMORY 0x0b
unsigned short int g_current_address = 0x0000;
//---------------------------------------------------------------------------
// WriteCommand (Send6Bits)
//
// Pulses the PGC six 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 6 lowest bits in "commamd" lsb first.
//---------------------------------------------------------------------------
void WriteCommand16(uint8_t command)
{
unsigned char number_of_bits = 6;
Delayus(1);
do
{
PGC = 1;
PGD_OUT = command & 0x01;
command = command >> 1;
Delay100ns();
PGC = 0;
Delay100ns();
}
while (--number_of_bits != 0);
}
//---------------------------------------------------------------------------
// WriteWord
//---------------------------------------------------------------------------
void WriteWord16(uint16_t word)
{
unsigned char number_of_bits = 16;
word <<= 1;
Delayus(1);
do
{
PGC = 1;
PGD_OUT = word & 0x01;
word = word >> 1;
Delay100ns();
PGC = 0;
Delay100ns();
}
while (--number_of_bits != 0);
}
//---------------------------------------------------------------------------
// ReadWord
//---------------------------------------------------------------------------
uint16_t ReadWord16(void)
{
uint16_t word = 0;
unsigned char i;
Delayus(1);
//
// The 16F will go OUTPUT after the second raising clock edge.
//
PGC = 1;
Delay100ns();
PGC = 0;
TRIS_PGD = 1;
Delay100ns();
PGC = 1;
Delay100ns();
for (i = 0; i < 14; i++)
{
word >>= 1;
if (PGD_IN)
{
word |= 0x8000;
}
PGC = 0;
Delay100ns();
PGC = 1;
Delay100ns();
}
PGC = 0;
Delay100ns();
TRIS_PGD = 0;
return word >> 2;
}
//---------------------------------------------------------------------------
// ResetDevice
//
// Resets the device by dropping and then re-asserting Vpp and Vdd. This has
// the effect of resetting the PC to 0x0000.
//---------------------------------------------------------------------------
void ResetDevice16(void)
{
VDD = 0;
VPP = 0;
Delayms(2);
VPP = 1;
Delayms(2);
VDD = 1;
Delayms(2);
g_current_address = 0x0000;
}
//---------------------------------------------------------------------------
// SetPC
//
// Sets the PC to the desired address. If the address is lower than the PC,
// the device is reset. If the addrses id 0x2000 or above a LOAD_CONFIGURATION
// command is issued. Then a number of INCREMENT_ADDRESS commands are issued
// until the address is right.
//---------------------------------------------------------------------------
void SetPC16(uint16_t address)
{
if (address < g_current_address)
{
ResetDevice16();
}
if (address >= 0x2000)
{
WriteCommand16(LOAD_CONFIGURATION);
WriteWord16(0x3fff);
g_current_address = 0x2000;
}
while (g_current_address < address)
{
WriteCommand16(INCREMENT_ADDRESS);
g_current_address++;
}
}
//===========================================================================
//---------------------------------------------------------------------------
// Erase
//---------------------------------------------------------------------------
void Erase16(void)
{
WriteCommand16(LOAD_CONFIGURATION);
WriteWord16(0xffff);
g_current_address = 0x2000;
WriteCommand16(LOAD_DATA_FOR_PROGRAM_MEMORY);
WriteWord16(0x3fff);
WriteCommand16(BULK_ERASE_PROGRAM_MEMORY);
Delayms(10);
WriteCommand16(BULK_ERASE_DATA_MEMORY);
Delayms(10);
SendOk();
}
//---------------------------------------------------------------------------
// ProgramBytes
//---------------------------------------------------------------------------
void ProgramBytes16(uint16_t address,
uint8_t *bytes,
uint8_t length)
{
unsigned char i;
SetPC16(address);
for (i = 0; i < length; i += 2)
{
uint16_t word = (((uint16_t)(bytes[i + 1])) << 8) | (uint16_t)(bytes[i]);
WriteCommand16(LOAD_DATA_FOR_PROGRAM_MEMORY);
WriteWord16(word);
WriteCommand16(BEGIN_PROGRAMMING_CYCLE);
Delayms(10);
WriteCommand16(INCREMENT_ADDRESS);
g_current_address++;
}
SendOk();
}
//---------------------------------------------------------------------------
// ProgramConfigWord16
//---------------------------------------------------------------------------
void ProgramConfigWord16(uint16_t word)
{
WriteCommand16(LOAD_CONFIGURATION);
WriteWord16(0x0000);
WriteCommand16(INCREMENT_ADDRESS);
WriteCommand16(INCREMENT_ADDRESS);
WriteCommand16(INCREMENT_ADDRESS);
WriteCommand16(INCREMENT_ADDRESS);
WriteCommand16(INCREMENT_ADDRESS);
WriteCommand16(INCREMENT_ADDRESS);
WriteCommand16(INCREMENT_ADDRESS);
g_current_address += 0x07;
WriteCommand16(LOAD_DATA_FOR_PROGRAM_MEMORY);
WriteWord16(word);
WriteCommand16(BEGIN_PROGRAMMING_CYCLE);
Delayms(10);
SendOk();
}
//---------------------------------------------------------------------------
// ReadBytes
//---------------------------------------------------------------------------
void ReadBytes16(uint16_t address,
uint8_t length)
{
unsigned char bytes[64];
unsigned char i = 0;
SetPC16(address);
//
// Read "length" bytes.
//
for (i = 0; i < length; i = i + 2)
{
uint16_t word;
WriteCommand16(READ_DATA_FROM_PROGRAM_MEMORY);
word = ReadWord16();
bytes[i + 0] = word & 0x00ff;
bytes[i + 1] = word >> 8;
WriteCommand16(INCREMENT_ADDRESS);
g_current_address++;
}
SendInPacket(bytes, length);
}
//---------------------------------------------------------------------------
// ProcessMessage
//---------------------------------------------------------------------------
void ProcessMessage16(unsigned char *message, unsigned char length)
{
uint16_t address, word;
switch (message[0])
{
case COMMAND_VDD_ON_16:
case COMMAND_VPP_ON_16:
case COMMAND_VPP_VDD_OFF_16:
PGC = 0;
PGD_OUT = 0;
g_current_address = 0x0000;
switch (message[0])
{
case COMMAND_VDD_ON_16 : VDD = 1; break;
case COMMAND_VPP_ON_16 : VPP = 1; break;
case COMMAND_VPP_VDD_OFF_16: VPP = 0; VDD = 0; break;
}
SendOk();
break;
case COMMAND_ERASE_16:
Erase16();
break;
case COMMAND_PROGRAM_BYTES_16:
address = ((uint16_t)(message[1]) << 8) | (uint16_t)(message[2]);
ProgramBytes16(address, &message[3], length - 3);
break;
case COMMAND_PROGRAM_CONFIG_16:
word = ((uint16_t)(message[1])) | ((uint16_t)(message[2]) << 8);
ProgramConfigWord16(word);
break;
case COMMAND_READ_BYTES_16:
address = ((uint16_t)(message[1]) << 8) | (uint16_t)(message[2]);
ReadBytes16(address, message[3]);
break;
}
}