summaryrefslogtreecommitdiffstats
path: root/src/progs/pickit2/base/pickit.cpp
blob: 3344ef8d85b66d15b0bb04e9cd4af36d3e72df92 (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
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
/***************************************************************************
 * Copyright (C) 2006 Nicolas Hadacek <hadacek@kde.org>                    *
 *                                                                         *
 *   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 "pickit.h"

#include "devices/base/device_group.h"
#include "progs/base/prog_group.h"

//-----------------------------------------------------------------------------
Pickit::Array::Array(uint length, uchar fillChar, PrintMode mode)
  : _fillChar(fillChar), _mode(mode), _data(length)
{
  _data.fill(fillChar);
}

TQString Pickit::Array::pretty() const
{
  int end = _data.count() - 1;
  for (; end>=0; end--)
    if ( _data[end]!=_fillChar ) break;
  TQString s;
  for (int i=0; i<=end; i++) s += toPrintable(_data[i], _mode);
  return s;
}

//-----------------------------------------------------------------------------
Pickit::USBPort::USBPort(uint deviceId, Log::Base &log)
  : Port::USB(log, Microchip::VENDOR_ID, deviceId, CONFIG_VENDOR, 0)
{}

bool Pickit::USBPort::command(uchar c)
{
  Array a = array();
  a._data[0] = c;
  return command(a);
}

bool Pickit::USBPort::command(const char *s)
{
  Array a = array();
  if (s) {
    Q_ASSERT( strlen(s)<=a.length() );
    for (uint i=0; i<strlen(s); i++) a._data[i] = s[i];
  }
  return command(a);
}

bool Pickit::USBPort::command(const Array &cmd)
{
  log(Log::DebugLevel::Extra, TQString("send command: \"%1\"").tqarg(cmd.pretty()));
  return write(writeEndPoint(), (const char *)cmd._data.data(), cmd.length());
}

bool Pickit::USBPort::receive(Pickit::Array &array)
{
  if ( !read(readEndPoint(), (char *)array._data.data(), array.length()) ) return false;
  log(Log::DebugLevel::Max, TQString("received: \"%1\"").tqarg(array.pretty()));
  return true;
}

bool Pickit::USBPort::getMode(VersionData &version, ::Programmer::Mode &mode)
{
  if ( !command('v') ) return false;
  Array a = array();
  if ( !receive(a) ) return false;
  if ( a[5]=='B' ) {
    version = VersionData(a[6], a[7], 0);
    mode = ::Programmer::BootloadMode;
  } else {
    version = VersionData(a[0], a[1], a[2]);
    mode = ::Programmer::NormalMode;
  }
  return true;
}

bool Pickit::USBPort::receiveWords(uint nbBytesWord, uint nbRead, TQValueVector<uint> &words, uint offset)
{
  log(Log::DebugLevel::Max, TQString("receive words nbBytesWord=%1 nbRead=%2 offset=%3").tqarg(nbBytesWord).tqarg(nbRead).tqarg(offset));
  Array a = array();
  TQMemArray<uchar> data(nbRead*a.length());
  uint l = 0;
  for (uint i=0; i<nbRead; i++) {
    if ( !receive(a) ) return false;
    for (uint k=offset; k<a.length(); k++) {
      data[l] = a[k];
      l++;
    }
  }
  words.resize(data.count()/nbBytesWord);
  for (uint i=0; i<uint(words.count()); i++) {
    words[i] = 0;
    for (uint k=0; k<nbBytesWord; k++) words[i] |= data[nbBytesWord*i + k] << (8*k);
  }
  return true;
}

//-----------------------------------------------------------------------------
Pickit::Hardware::Hardware(::Programmer::Base &base, USBPort *port)
  : ::Programmer::PicHardware(base, port, TQString())
{}

bool Pickit::Hardware::internalConnectHardware()
{
  return port().open();
}

bool Pickit::Hardware::setTargetPower(uint v)
{
  Array cmd = port().array();
  cmd[0] = 'V';
  cmd[1] = v;
  return port().command(cmd);
}

bool Pickit::Hardware::writeWords(uint max, char c, uint nbBytesWord,
                                  uint &i, const Device::Array &data)
{
  Q_ASSERT( i<data.count() );
  Q_ASSERT( nbBytesWord==1 || nbBytesWord==2 );
  Array cmd = port().array();
  uint n = (nbBytesWord==1 ? 2 : 3);
  Q_ASSERT( n*max<=cmd.length() );
  for (uint k=0; k<max; k++) {
    cmd[n*k] = c;
    cmd[n*k+1] = data[i].byte(0);
    if ( nbBytesWord==2 ) cmd[n*k+2] = data[i].byte(1);
    i++;
    if ( i>=data.count() ) break;
  }
  return port().command(cmd);
}

bool Pickit::Hardware::regenerateOsccal(BitValue &newValue)
{
  if ( !setTargetPower(Osc2_5kHz) ) return false;
  if ( !setTargetPower(PowerOn + Osc2_5kHz) ) return false;
  Port::usleep(400000); // 400 ms
  if ( !setTargetPower(PowerOff) ) return false;
  Pickit::Array cmd = port().array();
  cmd[0] = 'P';
  cmd[1] = 'I';
  cmd[2] = 120;
  cmd[3] = 0;
  cmd[4] = 'r';
  cmd[5] = 'p';
  if ( !port().command(cmd) ) return false;
  TQValueVector<uint> words;
  if ( !port().receiveWords(1, 1, words) ) return false;
  newValue = words[7] | 0x3400;
  return true;
}

//----------------------------------------------------------------------------
bool Pickit::DeviceSpecific::setPowerOn()
{
  return hardware().port().command(entryMode());
}

bool Pickit::DeviceSpecific::setPowerOff()
{
  return hardware().port().command('p');
}

bool Pickit::DeviceSpecific::setTargetPowerOn(bool on)
{
  return hardware().setTargetPower(on ? PowerOn : PowerOff);
}

//----------------------------------------------------------------------------
bool Pickit::BMDeviceSpecific::doRead(Pic::MemoryRangeType type, Device::Array &data, const ::Programmer::VerifyData *vdata)
{
  data.resize(device().nbWords(type));
  gotoMemory(type);
  TQValueVector<uint> words;
  switch (type.type()) {
    case Pic::MemoryRangeType::Config:
    case Pic::MemoryRangeType::Code:
    case Pic::MemoryRangeType::Cal:
    case Pic::MemoryRangeType::CalBackup:
    case Pic::MemoryRangeType::UserId:
    case Pic::MemoryRangeType::DeviceId:
      for (uint i=0; i<data.count();) {
        if ( !hardware().port().command('R') ) return false;
        if ( !hardware().port().receiveWords(2, 1, words) ) return false;
        for (uint k=0; k<uint(words.count()); k++) {
          data[i] = words[k];
          if ( vdata && !hardware().verifyWord(i, data[i], type, *vdata) ) return false;
          i++;
          if ( i>=data.count() ) break;
        }
      }
      break;
    case Pic::MemoryRangeType::Eeprom:
      for (uint i=0; i<data.count();) {
        if ( !hardware().port().command('r') ) return false; // #### not sure this is correct for Pickit1: "rrrrrrrr" used by usb_pickit
        if ( !hardware().port().receiveWords(1, 1, words) ) return false;
        for (uint k=0; k<uint(words.count()); k++) {
          data[i] = words[k];
          if ( vdata && !hardware().verifyWord(i, data[i], type, *vdata) ) return false;
          i++;
          if ( i>=data.count() ) break;
        }
      }
      break;
    case Pic::MemoryRangeType::DebugVector:
    case Pic::MemoryRangeType::HardwareStack:
    case Pic::MemoryRangeType::ProgramExecutive:
    case Pic::MemoryRangeType::Nb_Types: Q_ASSERT(false); return false;
  }
  if ( type==Pic::MemoryRangeType::Code || type==Pic::MemoryRangeType::Eeprom )
    _base.progressMonitor().addTaskProgress(data.count());
  return true;
}

bool Pickit::BMDeviceSpecific::incrementPC(uint nb)
{
  Pickit::Array cmd = hardware().port().array();
  cmd[0] = 'I';
  cmd[1] = nb & 0xFF;
  cmd[2] = (nb >> 8) & 0xFF;
  return hardware().port().command(cmd);
}

bool Pickit::BMDeviceSpecific::doEraseRange(Pic::MemoryRangeType type)
{
  Q_ASSERT( type==Pic::MemoryRangeType::Code );
  return hardware().port().command('E');
}

bool Pickit::BMDeviceSpecific::doWrite(Pic::MemoryRangeType type, const Device::Array &data, bool force)
{
  // #### TODO: speed optimize...
  Q_UNUSED(force);
  gotoMemory(type);
  uint nb = nbWrites(type);
  switch (type.type()) {
    case Pic::MemoryRangeType::Config:
    case Pic::MemoryRangeType::Code:
    case Pic::MemoryRangeType::Cal:
    case Pic::MemoryRangeType::UserId:
      for (uint i=0; i<data.count(); )
        hardware().writeWords(nb, writeCode(), 2, i, data);
      break;
    case Pic::MemoryRangeType::Eeprom:
      for (uint i=0; i<data.count(); )
        hardware().writeWords(nb, writeData(), 1, i, data);
      break;
    case Pic::MemoryRangeType::CalBackup:
    case Pic::MemoryRangeType::DeviceId:
    case Pic::MemoryRangeType::DebugVector:
    case Pic::MemoryRangeType::HardwareStack:
    case Pic::MemoryRangeType::ProgramExecutive:
    case Pic::MemoryRangeType::Nb_Types: Q_ASSERT(false); return false;
  }
  if ( type==Pic::MemoryRangeType::Code || type==Pic::MemoryRangeType::Eeprom )
    _base.progressMonitor().addTaskProgress(data.count());
  return true;
}

//----------------------------------------------------------------------------
bool Pickit::Baseline::gotoMemory(Pic::MemoryRangeType type)
{
  switch (type.type()) {
    case Pic::MemoryRangeType::Config:
    case Pic::MemoryRangeType::Eeprom: return true;
    case Pic::MemoryRangeType::Code:
    case Pic::MemoryRangeType::Cal:
    case Pic::MemoryRangeType::UserId:
    case Pic::MemoryRangeType::CalBackup: return incrementPC(1+device().range(type).start.toUInt());
    case Pic::MemoryRangeType::DeviceId:
    case Pic::MemoryRangeType::DebugVector:
    case Pic::MemoryRangeType::HardwareStack:
    case Pic::MemoryRangeType::ProgramExecutive:
    case Pic::MemoryRangeType::Nb_Types: break;
  }
  Q_ASSERT(false);
  return false;
}

//----------------------------------------------------------------------------
bool Pickit::P16F::gotoMemory(Pic::MemoryRangeType type)
{
  Pickit::Array cmd = hardware().port().array();
  cmd[0] = 'C';
  switch (type.type()) {
    case Pic::MemoryRangeType::Code: return true;
    case Pic::MemoryRangeType::Eeprom: return true;
    case Pic::MemoryRangeType::UserId: return hardware().port().command(cmd);
    case Pic::MemoryRangeType::DeviceId:
      cmd[1] = 'I';
      cmd[2] = 0x06;
      cmd[3] = 0x00;
      return hardware().port().command(cmd);
    case Pic::MemoryRangeType::Config:
      cmd[1] = 'I';
      cmd[2] = 0x07;
      cmd[3] = 0x00;
      return hardware().port().command(cmd);
    case Pic::MemoryRangeType::Cal:
      if ( device().range(type).start==device().range(Pic::MemoryRangeType::Code).end+1 )
        return incrementPC(device().range(type).start.toUInt());
      cmd[1] = 'I';
      cmd[2] = 0x08;
      cmd[3] = 0x00;
      return hardware().port().command(cmd);
    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 Pickit::P16F::doErase(bool)
{
  Pickit::Array cmd = hardware().port().array();
  cmd[0] = 'C';
  cmd[1] = writeCode();
  cmd[2] = 0xFF;
  cmd[3] = 0x3F;
  cmd[4] = 'E';
  cmd[5] = 'e';
  return hardware().port().command(cmd);
}

bool Pickit::P16F::doEraseRange(Pic::MemoryRangeType type)
{
  if ( type==Pic::MemoryRangeType::Eeprom ) return hardware().port().command('e');
  return BMDeviceSpecific::doEraseRange(type);
}