summaryrefslogtreecommitdiffstats
path: root/src/progs/direct/base/direct_16F.cpp
blob: feeb185511185dfb3f5c02fe7a9021e9439f5842 (plain)
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
/***************************************************************************
 *   Copyright (C) 2003-2005 Alain Gibaud <alain.gibaud@free.fr>           *
 *   Copyright (C) 2006 Nicolas Hadacek <hadacek@kde.org>                  *
 *   Copyright (C) 2004 Keith Baker <syzygy@pubnix.org>  [16F7X]           *
 *                                                                         *
 *   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_16F.h"

#include "common/global/global.h"

//-----------------------------------------------------------------------------
bool Direct::P16F::gotoTestMemory()
{
  pulseEngine("k0,S,", 0x3FFF); // PC set at 0x2000
  return true;
}

bool Direct::P16F::gotoMemory(Pic::MemoryRangeType type)
{
  switch (type.type()) {
    case Pic::MemoryRangeType::Code: return true;
    case Pic::MemoryRangeType::Eeprom: return true;
    case Pic::MemoryRangeType::UserId: return gotoTestMemory();
    case Pic::MemoryRangeType::DeviceId: return (gotoTestMemory() && incrementPC(6));
    case Pic::MemoryRangeType::Config: return (gotoTestMemory() && incrementPC(7));
    case Pic::MemoryRangeType::Cal:
      if ( device().range(type).start==device().range(Pic::MemoryRangeType::Code).end+1 )
        return incrementPC(device().range(type).start.toUInt());
      return (gotoTestMemory() && incrementPC(8));
    case Pic::MemoryRangeType::CalBackup:
    case Pic::MemoryRangeType::DebugVector:
    case Pic::MemoryRangeType::HardwareStack:
    case Pic::MemoryRangeType::ProgramExecutive:
    case Pic::MemoryRangeType::Nb_Types: break;
  }
  Q_ASSERT(false);
  return false;
}

//-----------------------------------------------------------------------------
bool Direct::P16F8X::doEraseRange(Pic::MemoryRangeType type)
{
  if ( type==Pic::MemoryRangeType::Code ) {
    pulseEngine("k2,S,k1,k7,k8,w10000,k1,k7", 0x3FFF); // bulk erase code
    return true;
  }
  if ( type==Pic::MemoryRangeType::Eeprom ) {
    pulseEngine("k3,S,k1,k7,k8,w10000,k1,k7", 0x3FFF); // bulk erase data
    return true;
  }
  return false;
}

bool Direct::P16F8X::doErase(bool isProtected)
{
  if (isProtected) { // disable code protection + erase code and data
    gotoMemory(Pic::MemoryRangeType::Config);
    pulseEngine("k1,k7,k8,w10000,k1,k7");
  } else {
    doEraseRange(Pic::MemoryRangeType::Code);
    doEraseRange(Pic::MemoryRangeType::Eeprom);
  }
  doEmulatedEraseRange(Pic::MemoryRangeType::UserId);
  doEmulatedEraseRange(Pic::MemoryRangeType::Config);
  return true;
}

//-----------------------------------------------------------------------------
uint Direct::P16F84A::waitProgTime(Pic::MemoryRangeType type) const
{
  if ( type==Pic::MemoryRangeType::Config || type==Pic::MemoryRangeType::UserId ) return 4000 + 4000; // prog +erase
  return 4000;
}

void Direct::P16F84A::startProg(Pic::MemoryRangeType type)
{
  if ( type==Pic::MemoryRangeType::Code || type==Pic::MemoryRangeType::Eeprom ) pulseEngine("k24,");
  else pulseEngine("k8,");
}

bool Direct::P16F84A::doEraseRange(Pic::MemoryRangeType type)
{
  if ( type==Pic::MemoryRangeType::Code ) {
    pulseEngine("k2,S,k9,k8,w10000", 0x3FFF);  // bulk erase code
    return true;
  }
  if ( type==Pic::MemoryRangeType::Eeprom ) {
    pulseEngine("k3,S,k11,k8,w10000", 0x3FFF); // bulk erase data
    return true;
  }
  return false;
}

bool Direct::P16F84A::doErase(bool isProtected)
{
  if (isProtected) { // disable code protection and erase
    gotoMemory(Pic::MemoryRangeType::Config);
    pulseEngine("k1,k7,k8,w10000,k1,k7");
  } else {
    doEraseRange(Pic::MemoryRangeType::Code);
    doEraseRange(Pic::MemoryRangeType::Eeprom);
  }
  doEmulatedEraseRange(Pic::MemoryRangeType::UserId);
  doEmulatedEraseRange(Pic::MemoryRangeType::Config);
  return true;
}

//-----------------------------------------------------------------------------
uint Direct::P16F7X::waitProgTime(Pic::MemoryRangeType) const
{
  if ( device().name()=="16F72" ) return 3000;
  return 1000;
}

bool Direct::P16F7X::doEraseRange(Pic::MemoryRangeType type)
{
  if ( type!=Pic::MemoryRangeType::Code ) return false;
  Device::Array array;
  if ( !doRead(Pic::MemoryRangeType::Config, array, 0) ) return false;
  pulseEngine("k9w400000", 0x3FFF); // chip erase (should only need 30ms according to prog sheet)
  return doWrite(Pic::MemoryRangeType::Config, array, true);
}

bool Direct::P16F7X::doErase(bool)
{
  pulseEngine("k9w400000", 0x3FFF); // chip erase (should only need 30ms according to prog sheet)
  return doEmulatedEraseRange(Pic::MemoryRangeType::UserId);
}

//-----------------------------------------------------------------------------
// 16F628 seems to have problems with the standard 16F84 bulk
// erase when disabling code protection : the data memory is not set to 0xFF.
// This code adds a erase/programming pass on the data memory
bool Direct::P16F62X::doErase(bool isProtected)
{
  P16F84A::doErase(isProtected);
  if (isProtected) return doEmulatedEraseRange(Pic::MemoryRangeType::Eeprom);
  return true;
}

//-----------------------------------------------------------------------------
bool Direct::P16F81X::doEraseRange(Pic::MemoryRangeType type)
{
  if ( type==Pic::MemoryRangeType::Code ) {
    pulseEngine("k9w2000"); // bulk erase code: 2ms
    return true;
  }
  if ( type==Pic::MemoryRangeType::Eeprom ) {
    pulseEngine("k11w2000"); // bulk erase data: 2ms
    return true;
  }
  return false;
}

bool Direct::P16F81X::doErase(bool)
{
  pulseEngine("k31w8000"); // chip erase: 8ms
  return true;
}

//-----------------------------------------------------------------------------
uint Direct::P12F675::waitProgTime(Pic::MemoryRangeType type) const
{
  if ( type==Pic::MemoryRangeType::Eeprom ) return 6000;
  return 2500;
}

bool Direct::P12F675::doEraseRange(Pic::MemoryRangeType type)
{
  if ( type==Pic::MemoryRangeType::Code ) {
    pulseEngine("k9,w9000");  // bulk erase code
    return true;
  }
  if ( type==Pic::MemoryRangeType::Eeprom ) {
    pulseEngine("k11,w9000"); // bulk erase data
    return true;
  }
  return false;
}

bool Direct::P12F675::doErase(bool)
{
  pulseEngine("k0,S,", 0x3FFF);
  doEraseRange(Pic::MemoryRangeType::Code);
  doEraseRange(Pic::MemoryRangeType::Eeprom);
  return true;
}

//-----------------------------------------------------------------------------
uint Direct::P16F62XA::waitProgTime(Pic::MemoryRangeType type) const
{
  if ( type==Pic::MemoryRangeType::Eeprom ) return 6000;
  return 2500;
}

bool Direct::P16F62XA::doEraseRange(Pic::MemoryRangeType type)
{
  if ( type==Pic::MemoryRangeType::Code ) {
    pulseEngine("k9,w12000");  // bulk erase code
    return true;
  }
  if ( type==Pic::MemoryRangeType::Eeprom ) {
    pulseEngine("k11,w12000"); // bulk erase data
    return true;
  }
  return false;
}

bool Direct::P16F62XA::doErase(bool)
{
  pulseEngine("k0,S,w100", 0x3FFF);
  doEraseRange(Pic::MemoryRangeType::Code);
  doEraseRange(Pic::MemoryRangeType::Eeprom);
  return true;
}

//-----------------------------------------------------------------------------
bool Direct::P16F87XA::doEraseRange(Pic::MemoryRangeType type)
{
  if ( type==Pic::MemoryRangeType::Code ) {
    pulseEngine("k9,w8000");  // bulk erase code
    return true;
  }
  if ( type==Pic::MemoryRangeType::Eeprom ) {
    pulseEngine("k11,w8000"); // bulk erase data
    return true;
  }
  return false;
}

bool Direct::P16F87XA::doErase(bool)
{
  // I use the command 31 twice because, sometimes, the prog
  // memory is not totally erased. Not very elegant, but it works.
  pulseEngine("k0,S,k31w8000,k31w8000", 0x3FFF);
  return doEmulatedEraseRange(Pic::MemoryRangeType::UserId);
}

//-----------------------------------------------------------------------------
bool Direct::P16F913::doEraseRange(Pic::MemoryRangeType type)
{
  if ( type==Pic::MemoryRangeType::Code ) {
    pulseEngine("k9,w6000");  // bulk erase code
    return true;
  }
  if ( type==Pic::MemoryRangeType::Eeprom ) {
    pulseEngine("k11,w6000"); // bulk erase data
    return true;
  }
  return false;
}

bool Direct::P16F913::doErase(bool)
{
  // flow chart of figure 3.21 of prog sheet
  doEraseRange(Pic::MemoryRangeType::Code);
  pulseEngine("k0,S,", 0x3FFF);
  doEraseRange(Pic::MemoryRangeType::Code);
  doEraseRange(Pic::MemoryRangeType::Eeprom);
  return true;
}

//-----------------------------------------------------------------------------
bool Direct::P16F785::doErase(bool)
{
  // flow chart of figure 3.20 of prog sheet
  pulseEngine("k0,S,", 0x3FFF);
  doEraseRange(Pic::MemoryRangeType::Code);
  doEraseRange(Pic::MemoryRangeType::Eeprom);
  return true;
}