File: tyt_interface.cc

package info (click to toggle)
qdmr 0.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,420 kB
  • sloc: cpp: 95,929; xml: 10,749; python: 1,108; makefile: 78; sh: 9
file content (253 lines) | stat: -rw-r--r-- 5,494 bytes parent folder | download | duplicates (2)
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
#include "tyt_interface.hh"
#include "logger.hh"
#include <unistd.h>
#include "utils.hh"
#include "errorstack.hh"

#define USB_VID 0x0483
#define USB_PID 0xdf11


TyTInterface::TyTInterface(const USBDeviceDescriptor &descr, const ErrorStack &err, QObject *parent)
  : DFUSEDevice(descr, err, 16, parent), RadioInterface()
{
  if (! DFUDevice::isOpen()) {
    errMsg(err) << "Cannot open TyTInterface.";
    return;
  }

  // Enter Programming Mode.
  if (wait_idle()) {
    errMsg(err) << "Device not ready. Close device.";
    close(); return;
  }

  if (md380_command(0x91, 0x01, err)) {
    errMsg(err) << "Cannot enter programming mode. Close device.";
    reboot(err);
    close(); return;
  }

  // Get device identifier in a static buffer.
  const char *idstr = identify(err);
  if (idstr && (0==strcmp("DR780", idstr))) {
    _ident = RadioInfo::byID(RadioInfo::MD380);
  } else if (idstr && (0==strcmp("MD390", idstr))) {
    _ident = RadioInfo::byID(RadioInfo::MD390);
  } else if (idstr && (0==strcmp("MD-UV380", idstr))) {
    _ident = RadioInfo::byID(RadioInfo::UV380);
  } else if (idstr && (0==strcmp("MD-UV390", idstr))) {
    _ident = RadioInfo::byID(RadioInfo::UV390);
  } else if (idstr && (0==strcmp("2017", idstr))) {
    _ident = RadioInfo::byID(RadioInfo::MD2017);
  } else if (idstr && (0==strcmp("DM-1701", idstr))) {
    _ident = RadioInfo::byID(RadioInfo::DM1701);
  } else if (idstr) {
    errMsg(err) << "Unknown TyT device '" << idstr << "'.";
    close(); return;
  }

  // Zero address.
  if(set_address(0x00000000, err)) {
    errMsg(err) << "Cannot set device address to 0x00000000.";
    close(); return;
  }

  logDebug() << "Found device " << _ident.manufacturer() << " "<< _ident.name()
             << " at " << descr.description() << ".";
}

TyTInterface::~TyTInterface() {
  if (isOpen())
    close();
}

USBDeviceInfo
TyTInterface::interfaceInfo() {
  return USBDeviceInfo(USBDeviceInfo::Class::DFU, USB_VID, USB_PID);
}

QList<USBDeviceDescriptor>
TyTInterface::detect(bool saveOnly) {
  Q_UNUSED(saveOnly);
  return DFUDevice::detect(USB_VID, USB_PID);
}

void
TyTInterface::close() {
  if (isOpen()) {
    _ident = RadioInfo();
  }
  DFUSEDevice::close();
}

bool
TyTInterface::isOpen() const {
  return DFUSEDevice::isOpen() && _ident.isValid();
}

RadioInfo
TyTInterface::identifier(const ErrorStack &err) {
  Q_UNUSED(err);
  return _ident;
}

int
TyTInterface::md380_command(uint8_t a, uint8_t b, const ErrorStack &err)
{
  unsigned char cmd[2] = { a, b };

  if (int error = download(0, cmd, 2, err))
    return error;

  usleep(100000);
  return wait_idle();
}


int
TyTInterface::set_address(uint32_t address, const ErrorStack &err)
{
  unsigned char cmd[5] =
  { 0x21,
    (uint8_t)address,
    (uint8_t)(address >> 8),
    (uint8_t)(address >> 16),
    (uint8_t)(address >> 24), };

  if (int error = download(0, cmd, 5, err))
    return error;

  return wait_idle();
}


int
TyTInterface::erase_block(uint32_t address, const ErrorStack &err)
{
  unsigned char cmd[5] =
  { 0x41,
    (uint8_t)address,
    (uint8_t)(address >> 8),
    (uint8_t)(address >> 16),
    (uint8_t)(address >> 24), };

  if (int error = download(0, cmd, 5, err))
    return error;

  wait_idle();

  return 0;
}

const char *
TyTInterface::identify(const ErrorStack &err)
{
  static uint8_t data[64];

  md380_command(0xa2, 0x01, err);

  if (upload(0, data, 64, err))
    return nullptr;

  return (const char*) data;
}


bool
TyTInterface::erase(unsigned start, unsigned size, void(*progress)(unsigned, void *), void *ctx, const ErrorStack &err) {
  int error;
  // Enter Programming Mode.
  if ((error = get_status(err)))
    return false;
  if ((error = wait_idle()))
    return false;
  if ((error = md380_command(0x91, 0x01, err)))
    return false;
  usleep(100000);

  unsigned end = start+size;
  start = align_addr(start, 0x10000);
  end = align_size(end, 0x10000);
  size = end-start;

  for (unsigned i=0; i<size; i+=0x10000) {
    erase_block(start+i, err);
    if (progress)
      progress((i*100)/size, ctx);
  }

  // Zero address.
  return (0 == set_address(0x00000000, err));
}

bool
TyTInterface::read_start(uint32_t bank, uint32_t addr, const ErrorStack &err) {
  Q_UNUSED(bank);
  Q_UNUSED(addr);
  Q_UNUSED(err);
  // pass...
  return true;
}

bool
TyTInterface::read(uint32_t bank, uint32_t addr, uint8_t *data, int nbytes, const ErrorStack &err) {
  Q_UNUSED(bank);

  if (nullptr == data) {
    errMsg(err) << "Cannot write data into nullptr!";
    return false;
  }

  uint32_t block = addr/1024;
  return 0 == upload(block+2, data, nbytes, err);
}

bool
TyTInterface::read_finish(const ErrorStack &err) {
  Q_UNUSED(err);
  return true;
}


bool
TyTInterface::write_start(uint32_t bank, uint32_t addr, const ErrorStack &err) {
  Q_UNUSED(bank); Q_UNUSED(addr); Q_UNUSED(err)
  return true;
}

bool
TyTInterface::write(uint32_t bank, uint32_t addr, uint8_t *data, int nbytes, const ErrorStack &err) {
  Q_UNUSED(bank);

  if (nullptr == data) {
    errMsg(err) << "Cannot read data from nullptr!";
    return false;
  }

  uint32_t block = addr/1024;
  if (download(block+2, data, nbytes, err))
    return false;

  return 0 == wait_idle();
}

bool
TyTInterface::write_finish(const ErrorStack &err) {
  Q_UNUSED(err);
  return true;
}


bool
TyTInterface::reboot(const ErrorStack &err) {
  if (! _ctx)
    return false;

  if (wait_idle())
    return false;

  unsigned char cmd[2] = { 0x91, 0x05 };
  return download(0, cmd, 2, err);
}