summaryrefslogtreecommitdiffstats
path: root/src/progs/pickit2v2/base/pickit2v2_prog.cpp
blob: 3fab7cab16a1533cec2d990e361e2960ea6d04f1 (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
/***************************************************************************
 * Copyright (C) 2007 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 "pickit2v2_prog.h"

#include "devices/list/device_list.h"
#include "progs/base/prog_config.h"
#include "pickit2v2.h"

//----------------------------------------------------------------------------
Pickit2V2::Base::Base(const Programmer::Group &group, const Pic::Data *data)
  : Pickit2::Base(group, data)
{}

VersionData Pickit2V2::Base::firmwareVersion(Programmer::FirmwareVersionType type) const
{
  switch (type.type()) {
    case Programmer::FirmwareVersionType::Min: return VersionData(2, 10, 0);
    case Programmer::FirmwareVersionType::Recommended: return VersionData(2, 10, 0);
    case Programmer::FirmwareVersionType::Max: return VersionData(2, 10, 0);
    case Programmer::FirmwareVersionType::Nb_Types: break;
  }
  Q_ASSERT(false);
  return VersionData();
}

Pickit2V2::Hardware &Pickit2V2::Base::hardware()
{
  return static_cast<Hardware &>(*_hardware);
}

bool Pickit2V2::Base::identifyDevice()
{
  double vdd = 3.3; // set 3.3V to check for fragile devices
  if ( !_targetSelfPowered ) {
    if ( !hardware().setVddVoltage(vdd, 0.85)) return false;
  }
  TQString message = i18n("Unknown device");
  for (uint i=0; FAMILY_DATA[i].architecture!=Pic::Architecture::Nb_Types; i++) {
    const FamilyData &fdata = FAMILY_DATA[i];
    if ( fdata.readDevIdScript==0 ) {
      if ( device()->architecture()==fdata.architecture ) return true; // not autodetectable
      continue;
    }
    hardware().setVddOn(false);
    if ( fdata.vpp==0.0 ) hardware().setVppVoltage(vdd, 0.7);
    else hardware().setVppVoltage(fdata.vpp, 0.7);
    hardware().setVddOn(true);
    if ( !hardware().executeScript(fdata.progEntryScript) ) return false;
    if ( !hardware().executeScript(fdata.readDevIdScript) ) return false;
    if ( !hardware().port().command(UploadData) ) return false;
    Array data;
    if ( !hardware().port().receive(data) ) return false;
    if ( !hardware().executeScript(fdata.progExitScript) ) return false;
    uint rawId = (data[2]<<8) + data[1];
    if (fdata.progMemShift) rawId >>= 1;
    log(Log::DebugLevel::Normal, TQString("Read id for family %1: %2").arg(fdata.architecture.key()).arg(toHexLabelAbs(rawId)));
    TQMap<TQString, Device::IdData> ids;
    ::Group::Base::ConstIterator it;
    for (it=group().begin(); it!=group().end(); ++it) {
      const Pic::Data *data = static_cast<const Pic::Data *>(it.data().data);
      if ( data->architecture()!=fdata.architecture ) continue;
      Device::IdData idata;
      if ( data->matchId(rawId, idata) ) ids[it.key()] = idata;
    }
    if ( ids.count()!=0 ) {
      log(Log::LineType::Information, i18n("Read id: %1").arg(device()->idNames(ids).join("; ")));
      if ( ids.contains(device()->name()) ) return true;
      message = i18n("Read id does not match the specified device name \"%1\".").arg(device()->name());
      break;
    }
  }
  if ( !askContinue(message) ) {
    logUserAbort();
    return false;
  }
  log(Log::LineType::Information, i18n("Continue with the specified device name: \"%1\"...").arg(device()->name()));
  return true;
}

bool Pickit2V2::Base::setTarget()
{
  if ( !identifyDevice() ) return false;
  return hardware().setTarget();
}

bool Pickit2V2::Base::selfTest(bool ask)
{
  ushort status;
  if ( !hardware().readStatus(status) ) return false;
  TQString error;
  if ( status & VppError ) error += i18n("Vpp voltage level error; ");
  if ( status & VddError ) error += i18n("Vdd voltage level error; ");
  if ( error.isEmpty() ) return true;
  log(Log::LineType::Warning, i18n("Self-test failed: %1").arg(error));
  if ( ask && !askContinue(i18n("Self-test failed (%1). Do you want to continue anyway?").arg(error)) ) {
      logUserAbort();
      return false;
    }
  return true;
}

//----------------------------------------------------------------------------
Programmer::Hardware *Pickit2V2::Group::createHardware(Programmer::Base &base, const Programmer::HardwareDescription &) const
{
  return new Hardware(base);
}

Programmer::DeviceSpecific *Pickit2V2::Group::createDeviceSpecific(Programmer::Base &base) const
{
  return new DeviceSpecific(base);
}

#include "pickit2v2_prog.moc"