summaryrefslogtreecommitdiffstats
path: root/src/progs/pickit2/base/pickit.h
blob: 184a110b062e1de630f7559d708affb6f614e875 (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
/***************************************************************************
 * Copyright (C) 2006 Nicolas Hadacek <[email protected]>                    *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/
#ifndef PICKIT_H
#define PICKIT_H

#include "progs/icd2/base/microchip.h"
#include "common/port/usb_port.h"
#include "devices/pic/pic/pic_memory.h"
#include "devices/pic/prog/pic_prog.h"

namespace Pickit
{
enum PowerType { PowerOff = 0, PowerOn = 1, Osc2_5kHz = 2 };

//-----------------------------------------------------------------------------
class Array
{
public:
  uint length() const { return _data.count(); }
  TQString pretty() const;
  uchar &operator[](uint i) { return _data[i]; }
  uchar operator[](uint i) const { return _data[i]; }

protected:
  Array(uint length, uchar fillChar, PrintMode mode);

private:
  uchar _fillChar;
  PrintMode _mode;
  TQMemArray<uchar> _data;

  friend class USBPort;
};

//-----------------------------------------------------------------------------
class USBPort : public Port::USB
{
public:
  USBPort(uint deviceId, Log::Base &manager);
  virtual Array array() const = 0;
  bool command(const Array &cmd);
  bool command(uchar c);
  bool command(const char *s);
  bool receive(Array &data);
  bool getMode(VersionData &version, ::Programmer::Mode &mode);
  bool receiveWords(uint nbBytesWord, uint nbRead, TQValueVector<uint> &data, uint offset = 0);

protected:
  virtual uint writeEndPoint() const = 0;
  virtual uint readEndPoint() const = 0;

private:
  virtual uint timeout(uint) const { return 10000; } // 10s
  enum { CONFIG_HID = 1, // use HID for pickit configuration
         CONFIG_VENDOR = 2 // vendor specific configuration
  };
};

//-----------------------------------------------------------------------------
class Hardware : public ::Programmer::PicHardware
{
public:
  Hardware(::Programmer::Base &base, USBPort *port);
  bool setTargetPower(uint value);
  USBPort &port() { return static_cast<USBPort &>(*_port); }
  const USBPort &port() const { return static_cast<USBPort &>(*_port); }
  bool writeWords(uint max, char c, uint nbBytesWord,
                  uint &i, const Device::Array &data);
  bool regenerateOsccal(BitValue &newValue);

protected:
  virtual bool internalConnectHardware();
};

//-----------------------------------------------------------------------------
class DeviceSpecific : public ::Programmer::PicDeviceSpecific
{
public:
  DeviceSpecific(::Programmer::Base &base) : ::Programmer::PicDeviceSpecific(base) {}
  virtual bool canEraseAll() const { return true; }
  virtual bool canEraseRange(Pic::MemoryRangeType type) const { return ( type==Pic::MemoryRangeType::Code || type==Pic::MemoryRangeType::Eeprom ); }
  virtual bool canReadRange(Pic::MemoryRangeType) const { return true; }
  virtual bool canWriteRange(Pic::MemoryRangeType) const { return true; }
  Hardware &hardware() { return static_cast<Hardware &>(*_base.hardware()); }
  virtual bool init() = 0;
  virtual bool setPowerOn();
  virtual bool setPowerOff();
  virtual bool setTargetPowerOn(bool on);
  virtual char entryMode() const = 0;
};

//-----------------------------------------------------------------------------
class BMDeviceSpecific : public DeviceSpecific
{
public:
  BMDeviceSpecific(::Programmer::Base &base) : DeviceSpecific(base) {}
  virtual bool gotoMemory(Pic::MemoryRangeType type) = 0;
  virtual bool doRead(Pic::MemoryRangeType type, Device::Array &data, const ::Programmer::VerifyData *vdata);
  virtual bool incrementPC(uint nb);
  virtual uint nbWrites(Pic::MemoryRangeType type) const = 0;
  virtual char writeCode() const = 0;
  virtual char writeData() const = 0;
  virtual bool doWrite(Pic::MemoryRangeType type, const Device::Array &data, bool force);
  virtual bool doEraseRange(Pic::MemoryRangeType type);
};

//-----------------------------------------------------------------------------
class Baseline : public BMDeviceSpecific
{
public:
  Baseline(::Programmer::Base &base) : BMDeviceSpecific(base) {}
  virtual bool doErase(bool) { return doEraseRange(Pic::MemoryRangeType::Code); }
  virtual bool gotoMemory(Pic::MemoryRangeType type);
  virtual char writeCode() const { return 'w'; }
  virtual char writeData() const { return 'D'; }
};

class P16F : public BMDeviceSpecific
{
public:
  P16F(::Programmer::Base &base) : BMDeviceSpecific(base) {}
  virtual bool gotoMemory(Pic::MemoryRangeType type);
  virtual bool doEraseRange(Pic::MemoryRangeType type);
  virtual bool doErase(bool);
  virtual char writeCode() const { return 'W'; }
  virtual char writeData() const { return 'D'; }
};

} // namespace

#endif