summaryrefslogtreecommitdiffstats
path: root/src/progs/icd1/base/icd1.cpp
blob: 2ce3c804e82828b498dc6625d0eae15b332a4434 (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
/***************************************************************************
 *   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 "icd1.h"

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

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

bool Icd1::Hardware::internalConnect(const TQString &mode)
{
  if ( !port()->open() ) return false;
  if ( !port()->reset() ) return false;
  if ( hasError() ) return false;
  TQByteArray a = toAscii(mode);
  if ( !port()->send(a.data(), a.count()) ) return false;
  TQString s;
  if ( !port()->receive(1, s) ) return false;
  port()->setPinOn(Port::Serial::RTS, false, Port::PositiveLogic);
  Port::msleep(1);
  port()->setPinOn(Port::Serial::RTS, true, Port::PositiveLogic);
  if ( s.upper()!=mode ) {
    log(Log::LineType::Error, i18n("Failed to set port mode to '%1'.").tqarg(mode));
    return false;
  }
  return true;
}

bool Icd1::Hardware::sendCommand(uint cmd, BitValue *res, uint timeout)
{
  if ( !port()->sendCommand(cmd) ) return false;
  uchar byte;
  if ( !port()->receiveByte((char &)byte, false, timeout) ) return false;
  if (res) *res = byte << 8;
  if ( !port()->receiveByte((char &)byte, true, timeout) ) return false;
  if (res) *res += byte;
  return true;
}

bool Icd1::Hardware::readBlockCommand(uint nbBytes)
{
  Q_ASSERT( nbBytes<=0xFF );
  if ( !port()->sendCommand(0x7D00 + nbBytes) ) return false;
  TQByteArray a(nbBytes);
  for (uint i=0; i<nbBytes; i++)
    if ( !port()->receiveByte(*(a.data()+i), i!=0) ) return false;
  _rx = TQString(a);
  return true;
}

bool Icd1::Hardware::setTarget()
{
  return sendCommand(0x6300 + data(_base.device()->name()).part);
}

bool Icd1::Hardware::getFirmwareVersion(VersionData &version)
{
  BitValue v1, v2;
  if ( !sendCommand(0x7F00, &v1) || !sendCommand(0x7021, &v2) ) return false;
  version = VersionData(v1.byte(1), v1.byte(0), v2.toUInt());
  return true;
}

bool Icd1::Hardware::readVoltages(VoltagesData &voltages)
{
  if ( !sendCommand(0x7000) ) return false;
  BitValue res;
  if ( !sendCommand(0x701C, &res) ) return false;
  voltages[Pic::TargetVdd].value = (2.050 / 256) * res.toUInt();  // voltage at AN0 pin
  voltages[Pic::TargetVdd].value /= (4.7 / 14.7);        // voltage at Vcc
  log(Log::DebugLevel::Max, TQString("Vdd: %1 %2").tqarg(toHexLabel(res, 4)).tqarg(voltages[Pic::TargetVdd].value));
  voltages[Pic::TargetVdd].error = false;
  if ( !sendCommand(0x701D, &res) ) return false;
  voltages[Pic::TargetVpp].value = (2.050 / 256) * res.byte(0);  // voltage at AN1 pin
  voltages[Pic::TargetVpp].value /= (10.0 / 110.0);               // voltage at Vpp
  log(Log::DebugLevel::Max, TQString("Vpp: %1 %2").tqarg(toHexLabel(res, 4)).tqarg(voltages[Pic::TargetVpp].value));
  voltages[Pic::TargetVpp].error = false;
  return sendCommand(0x7001);
}

bool Icd1::Hardware::uploadFirmware(const Pic::Memory &memory)
{
  // #### TODO
  return false;
}

bool Icd1::Hardware::setTargetReset(Pic::ResetMode mode)
{
  // #### TODO
  return false;
}

bool Icd1::Hardware::selfTest()
{
  BitValue res;
  if ( !sendCommand(0x700B, &res, 5000) ) return false;
  if ( res!=0 ) {
    log(Log::LineType::Warning, i18n("Self-test failed (returned value is %1)").tqarg(toLabel(res)));
    return false;
  }
  return true;
}

bool Icd1::Hardware::gotoMemory(Pic::MemoryRangeType type, uint offset)
{
  if ( !sendCommand(0x7000) ) return false; // start sequence
  uint cmd = (type==Pic::MemoryRangeType::Eeprom ? 0x7006 : 0x7005);
  if ( !sendCommand(cmd) )  return false;
  if ( type==Pic::MemoryRangeType::Config || type==Pic::MemoryRangeType::UserId || type==Pic::MemoryRangeType::DeviceId ) {
    Q_ASSERT( offset==0 );
    offset = device().range(type).start - Address(0x2000);
    if ( !sendCommand(0xC000 + offset) )  return false;
  } else if ( !sendCommand(0x2000 + offset) )  return false;
  return true;
}

bool Icd1::Hardware::readMemory(Pic::MemoryRangeType type, uint wordOffset, Device::Array &data, const ::Programmer::VerifyData *vdata)
{
  if ( !gotoMemory(type, wordOffset) ) return false;
  for (uint i=0; i<data.count(); i++) if ( !sendCommand(0x7002, &data[i]) ) return false;
  if ( !sendCommand(0x7001) ) return false; // end sequence
  if (vdata) {
    for (uint i=0; i<data.count(); i++)
      if ( !verifyWord(wordOffset+i, data[i], type, *vdata) ) return false;
  }
  return true;
}

bool Icd1::Hardware::writeMemory(Pic::MemoryRangeType type, uint wordOffset, const Device::Array &data)
{
  if ( !gotoMemory(type, wordOffset) ) return false;
  for (uint i=0; i<data.count(); i++) if ( !sendCommand(0x8000 | data[i].toUInt()) ) return false;
  if ( !sendCommand(0x7001) ) return false; // end sequence
  return true;
}

bool Icd1::Hardware::eraseAll()
{
  BitValue res;
  if ( !sendCommand(0x7000) ) return false; // enable Vpp
  if ( !sendCommand(0x7007, &res) ) return false;
  if ( !sendCommand(0x7001) ) return false; // disable Vpp
  if ( res!=0x3FFF ) {
    log(Log::LineType::Error, i18n("Erase failed (returned value is %1)").tqarg(toHexLabel(res, 4)));
    return false;
  }
  return true;
}

bool Icd1::Hardware::haltRun()
{
  // #### TODO
  return false;
}

bool Icd1::Hardware::step()
{
  // #### TODO
  return false;
}

bool Icd1::Hardware::resumeRun()
{
  // #### TODO
  return false;
}

bool Icd1::Hardware::writeRegister(Address address, BitValue value, uint nbBytes)
{
  Q_ASSERT( nbBytes==1 || nbBytes==2 );
  // #### TODO
  return false;
}

bool Icd1::Hardware::readRegister(Address address, BitValue &value, uint nbBytes)
{
  Q_ASSERT( nbBytes==1 || nbBytes==2 );
  // #### TODO
  return false;
}

BitValue Icd1::Hardware::getProgramCounter()
{
  // #### TODO
  return 0;
}