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
|
/***************************************************************************
* Copyright (C) 2006 Nicolas Hadacek <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "direct_30.h"
const uint BULK_ERASE_SEQUENCE[] = { // for all dsPICs
0x000000, 0x000000, 0x040100, 0x000000, // step 1
Direct::Pic30::LoopStart + 2,
0x24008A, 0x883B0A, // step 2
0x200F80, 0x880190, 0x200067, // step 3
0xBB0300, // step 4
0xBB1B86, // step 5
0x200558, 0x200AA9, 0x883B38, 0x883B39, // step 6
0xA8E761, 0x000000, 0x000000, // step 7
Direct::Pic30::Pause + 2000000,
0xA9E761, 0x000000, 0x000000,
Direct::Pic30::LoopEnd,
0x2407FA, 0x883B0A, // step 9
0x200558, 0x883B38, 0x200AA9, 0x883B39, // step 10
0xA8E761, 0x000000, 0x000000, // step 11
Direct::Pic30::Pause + 2000000,
0xA9E761, 0x000000, 0x000000,
Direct::Pic30::End
};
const uint READ_APPLICATION_ID_SEQUENCE[] = {
0x000000, 0x000000, 0x040100, 0x000000, // step 1
0x200800, 0x880190, 0x205FE0, 0x207841, // step 2
0xBA0890, 0x000000, 0x000000,
Direct::Pic30::RegisterOut, 0x000000, // step 3
Direct::Pic30::End
};
const uint READ_CONFIG_SEQUENCE[] = {
0x000000, 0x000000, 0x040100, 0x000000, // step 1
0x200F80, 0x880190, 0xEB0300, 0xEB0380, // step 2
Direct::Pic30::LoopStart + 7,
0xBA0BB6, 0x000000, 0x000000, 0x883C20, 0x000000, // step 3
Direct::Pic30::RegisterOut, 0x000000, // step 4
0x040100, 0x000000, // step 5
Direct::Pic30::LoopEnd,
Direct::Pic30::End
};
const uint WRITE_CONFIG_SEQUENCE[] = {
0x000000, 0x000000, 0x040100, 0x000000, // step 1
Direct::Pic30::FillAddressLow + 0x200007, // step 2
0x24008A, 0x883B0A, // step 3
0x200F80, 0x880190, // step 4
Direct::Pic30::FillData + 0x200000, // step 5
//0xEB0300, ???
0xBB1B96, 0x000000, 0x000000, // step 8
0x200558, 0x883B38, 0x200AA9, 0x883B39, // step 7
0xA8E761, 0x000000, 0x000000, // step 8
Direct::Pic30::Pause + 2000000,
0xA9E761, 0x000000, 0x000000,
Direct::Pic30::End
};
const uint READ_DATA_SEQUENCE[] = {
0x000000, 0x000000, 0x040100, 0x000000, // step 1
0x2007F0, 0x880190, Direct::Pic30::FillAddressLow + 0x200006, // step 2
0xEB0380, // step 3
0xBA1BB6, 0x000000, 0x000000,
0xBA1BB6, 0x000000, 0x000000,
0xBA1BB6, 0x000000, 0x000000,
0xBA1BB6, 0x000000, 0x000000,
0x883C20, 0x000000, Direct::Pic30::RegisterOut, 0x000000, // step 4
0x883C21, 0x000000, Direct::Pic30::RegisterOut, 0x000000,
0x883C22, 0x000000, Direct::Pic30::RegisterOut, 0x000000,
0x883C23, 0x000000, Direct::Pic30::RegisterOut, 0x000000,
Direct::Pic30::End
};
const uint WRITE_DATA_SEQUENCE[] = {
0x000000, 0x000000, 0x040100, 0x000000, // step 1
0x24005A, 0x883B0A, // step 2
0x2007F0, 0x880190, Direct::Pic30::FillAddressLow + 0x200007, // step 3
Direct::Pic30::LoopStart + 4,
Direct::Pic30::FillData + 0x200000, // step 4
Direct::Pic30::FillData + 0x200001,
Direct::Pic30::FillData + 0x200002,
Direct::Pic30::FillData + 0x200003,
0xEB0300, 0x000000, // step 5
0xBB1BB6, 0x000000, 0x000000,
0xBB1BB6, 0x000000, 0x000000,
0xBB1BB6, 0x000000, 0x000000,
0xBB1BB6, 0x000000, 0x000000,
Direct::Pic30::LoopEnd,
0x200558, 0x883B38, 0x200AA9, 0x883B39, // step 7
0xA8E761, 0x000000, 0x000000, // step 8
Direct::Pic30::Pause + 2000000,
0xA9E761, 0x000000, 0x000000,
Direct::Pic30::End
};
const uint READ_CODE_SEQUENCE[] = {
0x000000, 0x000000, 0x040100, 0x000000, // step 1
Direct::Pic30::FillAddressHigh + 0x200000, // step 2
0x880190, Direct::Pic30::FillAddressLow + 0x200006,
0xEB0380, // step 3
0xBA1B96, 0x000000, 0x000000, 0xBADBB6, 0x000000, 0x000000,
0xBADBD6, 0x000000, 0x000000, 0xBA1BB6, 0x000000, 0x000000,
0xBA1B96, 0x000000, 0x000000, 0xBADBB6, 0x000000, 0x000000,
0xBADBD6, 0x000000, 0x000000, 0xBA0BB6, 0x000000, 0x000000,
0x883C20, 0x000000, Direct::Pic30::RegisterOut, 0x000000, // step 4
0x883C21, 0x000000, Direct::Pic30::RegisterOut, 0x000000,
0x883C22, 0x000000, Direct::Pic30::RegisterOut, 0x000000,
0x883C23, 0x000000, Direct::Pic30::RegisterOut, 0x000000,
0x883C24, 0x000000, Direct::Pic30::RegisterOut, 0x000000,
0x883C25, 0x000000, Direct::Pic30::RegisterOut, 0x000000,
Direct::Pic30::End
};
const uint WRITE_CODE_SEQUENCE[] = {
0x000000, 0x000000, 0x040100, 0x000000, // step 1
0x24001A, 0x883B0A, // step 2
Direct::Pic30::FillAddressHigh + 0x200000, // step 3
0x880190, Direct::Pic30::FillAddressLow + 0x200007,
Direct::Pic30::LoopStart + 8,
Direct::Pic30::FillData + 0x200000, // step 4
Direct::Pic30::FillData + 0x200001,
Direct::Pic30::FillData + 0x200002,
Direct::Pic30::FillData + 0x200003,
Direct::Pic30::FillData + 0x200004,
Direct::Pic30::FillData + 0x200005,
0xEB0300, 0x000000, // step 5
0xBB0BB6, 0x000000, 0x000000, 0xBBDBB6, 0x000000, 0x000000,
0xBBEBB6, 0x000000, 0x000000, 0xBB1BB6, 0x000000, 0x000000,
0xBB0BB6, 0x000000, 0x000000, 0xBBDBB6, 0x000000, 0x000000,
0xBBEBB6, 0x000000, 0x000000, 0xBB1BB6, 0x000000, 0x000000,
Direct::Pic30::LoopEnd,
0x200558, 0x883B38, 0x200AA9, 0x883B39, // step 7
0xA8E761, 0x000000, 0x000000, 0x000000, 0x000000, // step 8
Direct::Pic30::Pause + 2000000,
0xA9E761, 0x000000, 0x000000, 0x000000, 0x000000,
Direct::Pic30::End
};
bool Direct::Pic30::sendCommand(Command cmd)
{
for (uint i=0; i<4; i++) {
hardware().setPin(DataOut, ((cmd>>i) & 1 ? High : Low));
hardware().setPin(Clock, High);
Port::nsleep(100);
hardware().setPin(Clock, Low);
Port::nsleep(100);
}
return true;
}
bool Direct::Pic30::executeSIX(uint opcode)
{
if ( !sendCommand(SIX) ) return false;
for (uint i=0; i<24; i++) {
hardware().setPin(DataOut, ((opcode>>i) & 1 ? High : Low));
hardware().setPin(Clock, High);
Port::nsleep(100);
hardware().setPin(Clock, Low);
// seem to need this longer delay here -- maybe
// specific to my programmer hardware? #### from dspicprg
Port::nsleep(5000);
}
return true;
}
bool Direct::Pic30::executeREGOUT(uint &v)
{
v = 0;
if ( !sendCommand(REGOUT) ) return false;
hardware().setPin(DataOut, High);
for (uint i=0; i<24; i++) {
hardware().setPin(Clock, High);
Port::nsleep(100);
hardware().setPin(Clock, Low);
// as above, need extra delay here, but even
// longer in this case -- maybe data line takes
// longer to settle when it is being driven by
// the PIC than by the programmer? #### from dspicprg
Port::nsleep(10000);
if ( i>=8 ) {
uint r = hardware().readBit();
v |= (r << (i-8));
}
}
return true;
}
bool Direct::Pic30::doStdpSequence(const uint *sequence, uint address, const char *outData, char *inData)
{
uint loopStart = 0, loopCound = 0;
for (uint i=0; sequence[i]!=End; i++) {
uint opcode = (sequence[i] & OpcodeMask);
switch (Operation(sequence[i] & OperationMask)) {
case SendSix:
if ( !executeSIX(opcode) ) return false;
break;
case RegisterOut: {
uint v;
if ( !executeREGOUT(v) ) return false;
(*inData) = v & 0xFF;
inData++;
(*inData) = (v >> 8) & 0xFF;
inData++;
break;
}
case Pause:
Port::nsleep(opcode);
break;
case LoopStart:
loopCound = opcode;
loopStart = i;
break;
case LoopEnd:
loopCound--;
if (loopCound) i = loopStart;
break;
case FillData: {
uint v = *outData;
v <<= 4;
outData++;
v |= *outData;
outData++;
v <<= 4;
executeSIX(v | opcode);
break;
}
case FillAddressLow: {
uint v = address & 0xFFFF;
v <<= 4;
executeSIX(v | opcode);
break;
}
case FillAddressHigh: {
uint v = (address & 0xFF0000) >> 16;
v <<= 4;
executeSIX(v | opcode);
break;
}
case End: Q_ASSERT(false); break;
}
}
return true;
}
bool Direct::Pic30::doErase(bool)
{
if ( !enterStdpMode() ) return false;
if ( !doStdpSequence(BULK_ERASE_SEQUENCE, 0, 0, 0) ) return false;
return exitStdpMode();
}
bool Direct::Pic30::doRead(Pic::MemoryRangeType type, Device::Array &data, const ::Programmer::VerifyData *data)
{
// #### TODO
return false;
}
bool Direct::Pic30::doWrite(Pic::MemoryRangeType type, const Device::Array &data, bool force)
{
// #### TODO
return false;
}
|