summaryrefslogtreecommitdiffstats
path: root/src/progs/psp/base/psp.cpp
blob: 188fbcb9f84a70f2f3b17ac17e0cce5d19585a10 (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
338
339
340
341
342
343
344
345
346
/***************************************************************************
 *   Copyright (C) 2006 Nicolas Hadacek <hadacek@kde.org>                  *
 *   Copyright (C) 2002-2003 Stephen Landamore <stephen@landamore.com>     *
 *                                                                         *
 *   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 "psp.h"

#include "common/global/global.h"
#include "common/common/misc.h"

//-----------------------------------------------------------------------------
TQMemArray<uchar> Psp::createConfigInfo(const Pic::Data &data)
{
  TQMemArray<uchar> array(33);
  array.fill(0x00);

  const Pic::Config &config = data.config();
  for (uint i=0; i<uint(config._words.count()); i++) {
    BitValue v = config._words[i].usedMask();
    if ( data.nbBytesWord(Pic::MemoryRangeType::UserId)==1 ) array[i ^ 1] = v.byte(0);
    else {
      array[2*i] = v.byte(1);
      array[2*i + 1] = v.byte(0);
    }
  }
  for (uint i=0; i<16; i++) array[i + 16] = array[i];

  // checksum
  for (uint i=0; i<32; i++) array[32] += array[i];

  return array;
}

TQMemArray<uchar> Psp::createDeviceInfo(const Pic::Data &data)
{
  TQMemArray<uchar> config = createConfigInfo(data);
  TQMemArray<uchar> array(45);
  array.fill(0x00);

  // memory code length
  BitValue v = data.nbWords(Pic::MemoryRangeType::Code);
  array[0] = v.byte(1);
  array[1] = v.byte(0);
  // address word width
  v = data.tqmask(Pic::MemoryRangeType::Code);
  array[2] = v.byte(1);
  array[3] = v.byte(0);
  // data word width
  array[4] = v.byte(1);
  array[5] = v.byte(0);

  if ( data.isReadable(Pic::MemoryRangeType::UserId) ) {
    // user id width
    v = data.userIdRecommendedMask();
    if ( data.nbBytesWord(Pic::MemoryRangeType::UserId)==1 ) v += v << 8;
    array[6] = v.byte(1);
    array[7] = v.byte(0);
    // user id tqmask
    array[8] = v.byte(1);
    array[9] = v.byte(0);
  }

  // memory config tqmask
  array[10] = config[0];
  array[11] = config[1];
  array[12] = config[16];
  array[13] = config[17];

  if ( data.isReadable(Pic::MemoryRangeType::Eeprom) ) {
    // memory eeprom width
    v = data.tqmask(Pic::MemoryRangeType::Eeprom);
    array[14] = v.byte(1);
    array[15] = v.byte(0);
    // memory eeprom tqmask
    array[16] = v.byte(1);
    array[17] = v.byte(0);
  }

  if ( data.isReadable(Pic::MemoryRangeType::Cal) ) {
    // memory calibration width
    v = data.tqmask(Pic::MemoryRangeType::Cal);
    array[18] = v.byte(1);
    array[19] = v.byte(0);
    // memory calibration tqmask
    array[20] = v.byte(1);
    array[21] = v.byte(0);
  }

  // memory code start
  array[22] = 0x00;
  array[23] = 0x00;
  // memory code length
  array[24] = 0x00; // array[0]; in lplab and for some devices in picp...
  array[25] = 0x01; // array[1]; in lplab and for some devices in picp...

  if ( data.isReadable(Pic::MemoryRangeType::UserId) ) {
    // user id start
    v = data.range(Pic::MemoryRangeType::UserId).start.toUInt();
    array[26] = v.byte(1);
    array[27] = v.byte(0);
    // user id length
    v = data.nbWords(Pic::MemoryRangeType::UserId);
    if ( data.nbBytesWord(Pic::MemoryRangeType::UserId)==1 ) v >>= 1;
    array[28] = v.byte(0);
  }

  if ( data.isReadable(Pic::MemoryRangeType::Config) ) {
    // config start
    v = data.range(Pic::MemoryRangeType::Config).start.toUInt();
    array[29] = v.byte(1);
    array[30] = v.byte(0);
    // config length
    v = data.nbWords(Pic::MemoryRangeType::Config);
    if ( data.nbBytesWord(Pic::MemoryRangeType::Config)==1 ) v >>= 1;
    array[31] = v.byte(0);
  }

  if ( data.isReadable(Pic::MemoryRangeType::Config) ) {
    // eeprom start
    array[32] = 0x00;
    array[33] = 0x00;
    // eeprom length
    v = data.nbWords(Pic::MemoryRangeType::Eeprom);
    array[34] = v.byte(1);
    array[35] = v.byte(0);
  }

  if ( data.isReadable(Pic::MemoryRangeType::Cal) ) {
    // calibration start
    v = data.range(Pic::MemoryRangeType::Cal).start.toUInt();
    array[36] = v.byte(1);
    array[37] = v.byte(0);
    // calibration length
    v = data.nbWords(Pic::MemoryRangeType::Cal);
    array[38] = v.byte(1);
    array[39] = v.byte(0);
  }

  // programming
  const Psp::Data &pdata = Psp::data(data.name());
  array[40] = pdata.overprogram;
  array[41] = pdata.tries;
  array[42] = pdata.algorithm;
  if ( data.memoryTechnology()==Device::MemoryTechnology::Flash ) array[43] |= 0x01;
  if ( data.isReadable(Pic::MemoryRangeType::UserId) ) array[43] |= 0x02;
  if ( data.isReadable(Pic::MemoryRangeType::Config) ) array[43] |= 0x04;
  if ( data.isReadable(Pic::MemoryRangeType::Eeprom) ) array[43] |= 0x08;
  if ( data.isReadable(Pic::MemoryRangeType::Cal) )    array[43] |= 0x10;
  if ( data.config().findMask("PARITY") )  array[43] |= 0x20;

  // checksum
  for (uint i=0; i<44; i++) array[44] += array[i];

  return array;
}

//-----------------------------------------------------------------------------
Psp::Hardware::Hardware(::Programmer::Base &base, const TQString &portDevice)
  : Programmer::PicHardware(base, new SerialPort(portDevice, base), TQString())
{}

bool Psp::Hardware::internalConnectHardware()
{
  if ( !port()->open() ) return false;
  if ( !port()->reset() ) return false;
  // #### TODO: detect Warp13 or JuPic
  TQMemArray<uchar> a;
  if ( !port()->command(0x88, 1, a) ) return false;
  if ( a[0]!=0xAB ) {
    log(Log::LineType::Error, i18n("Wrong programmer connected"));
    return false;
  }
  return true;
}

bool Psp::Hardware::getFirmwareVersion(VersionData &version)
{
  TQMemArray<uchar> a1;
  if ( !port()->commandAck(0x8D, 2, &a1) ) return false;
  if ( a1[1]==0xFF ) {
    log(Log::LineType::Warning, i18n("Invalid firmware version"));
    version = VersionData(0xFF, 0, 0);
  } else {
    TQMemArray<uchar> a2;
    if ( !port()->receive(2, a2) ) return false;
    version = VersionData(a1[1], a2[0], a2[1]);
  }
  return true;
}

bool Psp::Hardware::setTarget()
{
  log(Log::DebugLevel::Max, "set target");
  // load device info
  if ( !port()->commandAck(0x81) ) return false;
  TQMemArray<uchar> a = createDeviceInfo(device());
  if ( !port()->send(a) ) return false;
  if ( !port()->receiveEnd() ) return false;

  // load config info
  if ( !port()->commandAck(0x82) ) return false;
  a = createConfigInfo(device());
  if ( !port()->send(a) ) return false;
  if ( !port()->receiveEnd() ) return false;

  return true;
}

bool Psp::Hardware::setRange(uint start, uint size)
{
  TQMemArray<uchar> a(6);
  a[0] = 0x8E;
  a[1] = start >> 16;
  a[2] = (start >> 8) & 0xFF;
  a[3] = start & 0xFF;
  a[4] = size >> 8;
  a[5] = size & 0xFF;
  if ( !port()->send(a) ) return false;
  TQMemArray<uchar> r;
  if ( !port()->receive(6, r) ) return false;
  for (uint i=0; i<6; i++) {
    if ( r[i]!=a[i] ) {
      log(Log::LineType::Error, i18n("Failed to set range"));
      return false;
    }
  }
  return true;
}

char Psp::Hardware::readCommand(Pic::MemoryRangeType type)
{
  switch (type.type()) {
    case Pic::MemoryRangeType::Code:    return 'T';
    case Pic::MemoryRangeType::Eeprom:  return 'd';
    case Pic::MemoryRangeType::Config:  return 'f';
    case Pic::MemoryRangeType::UserId:  return 'e';
    case Pic::MemoryRangeType::Cal:     return 'c';
    case Pic::MemoryRangeType::DeviceId:
    case Pic::MemoryRangeType::CalBackup:
    case Pic::MemoryRangeType::DebugVector:
    case Pic::MemoryRangeType::ProgramExecutive:
    case Pic::MemoryRangeType::HardwareStack:
    case Pic::MemoryRangeType::Nb_Types: break;
  }
  return 0;
}

char Psp::Hardware::writeCommand(Pic::MemoryRangeType type)
{
  switch (type.type()) {
    case Pic::MemoryRangeType::Code:    return 'Q';
    case Pic::MemoryRangeType::Eeprom:  return 'i';
    case Pic::MemoryRangeType::Config:  return 'g';
    case Pic::MemoryRangeType::UserId:  return 'h';
    case Pic::MemoryRangeType::Cal:     return 'q';
    case Pic::MemoryRangeType::DeviceId:
    case Pic::MemoryRangeType::CalBackup:
    case Pic::MemoryRangeType::DebugVector:
    case Pic::MemoryRangeType::ProgramExecutive:
    case Pic::MemoryRangeType::HardwareStack:
    case Pic::MemoryRangeType::Nb_Types: break;
  }
  return 0;
}

bool Psp::Hardware::readMemory(Pic::MemoryRangeType type, Device::Array &data, const ::Programmer::VerifyData *vdata)
{
  TQMemArray<uchar> a;
  uint nbWords = device().nbWords(type);
  data.resize(nbWords);
  uint nbBytes = nbWords * device().nbBytesWord(type);
  char c = readCommand(type);
  switch (type.type()) {
    case Pic::MemoryRangeType::Code:
      if ( !setRange(0, nbWords) ) return false;
      if ( !port()->commandAck(c) ) return false;
      for (uint i=0; i<data.count(); i++) {
        if ( !port()->receive(2, a) ) return false;
        data[i] = (a[0] << 8) + a[1];
//        log(Log::DebugLevel::Max, TQString("code data %1: %2 (%3, %4)").tqarg(i).tqarg(toHexLabel(data[i], 4))
//                           .tqarg(toHexLabel(a[0], 2)).tqarg(toHexLabel(a[1], 2)));
      }
      if ( !port()->receiveEnd() ) return false;
      break;
    case Pic::MemoryRangeType::Eeprom:
      if ( !port()->commandAck(c) ) return false;
      for (uint i=0; i<data.count(); i++) {
        if ( !port()->receive(1, a) ) return false;
        data[i] = a[0];
      }
      if ( !port()->receiveEnd() ) return false;
      break;
    case Pic::MemoryRangeType::UserId:
      if ( !port()->commandAckEnd(c, nbBytes+2, a) ) return false;
      for (uint i=0; i<data.count(); i++) data[i] = (a[1+2*i] << 8) + a[1+2*i+1];
      break;
    case Pic::MemoryRangeType::Config:
      if ( !port()->commandAckEnd(c, nbBytes+2, a) ) return false;
      for (uint i=0; i<data.count(); i++) data[i] = (a[1+2*i] << 8) + a[1+2*i+1];
      break;
    case Pic::MemoryRangeType::Cal:
      if ( !setRange(0, nbWords) ) return false;
      if ( !port()->commandAckEnd(c, nbBytes+2, a) ) return false;
      for (uint i=0; i<data.count(); i++) data[i] = (a[1+2*i] << 8) + a[1+2*i+1];
      break;
    case Pic::MemoryRangeType::DeviceId:
    case Pic::MemoryRangeType::CalBackup:
      data.resize(0);
      return true;
    case Pic::MemoryRangeType::DebugVector:
    case Pic::MemoryRangeType::HardwareStack:
    case Pic::MemoryRangeType::ProgramExecutive:
    case Pic::MemoryRangeType::Nb_Types: Q_ASSERT(false); return false;
  }
  if (vdata) {
    for (uint i=0; i<data.count(); i++)
      if ( !verifyWord(i, data[i], type, *vdata) ) return false;
  }
  return true;
}

bool Psp::Hardware::writeMemory(Pic::MemoryRangeType type, const Device::Array &data, bool force)
{
  Q_UNUSED(force); // #### TODO: optimize for program memory
  if ( type==Pic::MemoryRangeType::Code && !setRange(0, data.count()) ) return false;
  uint nbBytes = device().nbBytesWord(type);
  if ( !port()->commandAck(writeCommand(type)) ) return false;
  for (uint i=0; i<data.count(); i++)
    if ( !port()->sendData(data[i].toUInt(), nbBytes) ) return false;
  return port()->receiveEnd();
}

bool Psp::Hardware::eraseAll()
{
  TQMemArray<uchar> a;
  if ( !port()->commandAck(0x8F, 2, &a) ) return false;
  if ( a[1]!=0x00 ) {
    log(Log::LineType::Error, i18n("Erase failed"));
    return false;
  }
  return true;
}