Repository navigation
Expand file tree
/
Copy pathBootloader.cpp
More file actions
297 lines (252 loc) · 8.53 KB
/
Copy pathBootloader.cpp
File metadata and controls
297 lines (252 loc) · 8.53 KB
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
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: (c) 2018-2023 DH electronics GmbH
#include "Bootloader.h"
#include "hal/System.h"
#include "hal/GPIO.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
extern "C"
{
#include "cybtldr_api.h"
#include "cybtldr_api2.h"
}
using namespace dhcom;
Bootloader * Bootloader::self_ = NULL;
static CyBtldr_CommunicationsData commData;
static const char* getBlErrorString(int err)
{
if(CYRET_ERR_COMM_MASK & err)
{ // DHCOM_HAL errors
switch(STATUS(err & ~CYRET_ERR_COMM_MASK))
{
case STATUS_DEVICE_DOESNT_EXIST:
return "The requested I/O device does not exist for this target hardware";
case STATUS_DEVICE_OPEN_FAILED:
return "Opening I/O device failed";
case STATUS_DEVICE_CLOSE_FAILED:
return "Closing I/O failed";
case STATUS_DEVICE_NOT_OPEN:
return "device must be open to perform the requested operation";
case STATUS_DEVICE_ALREADY_OPEN:
return "trying to open the already open device";
case STATUS_DEVICE_READ_FAILED:
return "reading from the device failed";
case STATUS_DEVICE_WRITE_FAILED:
return "writing to the device failed";
case STATUS_DEVICE_CONFIG_FAILED:
return "attempt to configure the device failed (parameters invalid)";
case STATUS_I2C_SLAVE_SELECT_FAILED:
return "I2C Slave select failed";
default:
return "Communication Interface unknown error";
}
}
if(CYRET_ERR_BTLDR_MASK & err)
{ // Bootloader errors
switch(err & ~CYRET_ERR_BTLDR_MASK)
{
case CYBTLDR_STAT_ERR_KEY:
return "The provided key does not match the expected value";
case CYBTLDR_STAT_ERR_VERIFY:
return "The verification of flash failed";
case CYBTLDR_STAT_ERR_LENGTH:
return "The amount of data available is outside the expected range ";
case CYBTLDR_STAT_ERR_DATA:
return "The data is not of the proper form";
case CYBTLDR_STAT_ERR_CMD:
return "The command is not recognized";
case CYBTLDR_STAT_ERR_DEVICE:
return "The expected device does not match the detected device";
case CYBTLDR_STAT_ERR_VERSION:
return "The bootloader version detected is not supported";
case CYBTLDR_STAT_ERR_CHECKSUM:
return "The checksum does not match the expected value";
case CYBTLDR_STAT_ERR_ARRAY:
return "The flash array is not valid";
case CYBTLDR_STAT_ERR_ROW:
return "The flash row is not valid";
case CYBTLDR_STAT_ERR_PROTECT:
return "The flash row is protected and can not be programmed";
case CYBTLDR_STAT_ERR_APP:
return "The application is not valid and cannot be set as active";
case CYBTLDR_STAT_ERR_ACTIVE:
return "The application is currently marked as active";
default:
case CYBTLDR_STAT_ERR_UNK:
return "An unknown error occurred";
}
}
switch(err)
{ // Bootloader host errors
case CYRET_ERR_FILE:
return "File is not accessible";
case CYRET_ERR_EOF:
return "Reached the end of the file";
case CYRET_ERR_LENGTH:
return "The amount of data available is outside the expected range";
case CYRET_ERR_DATA:
return "The data is not of the proper form";
case CYRET_ERR_CMD:
return "The command is not recognized";
case CYRET_ERR_DEVICE:
return "The expected device does not match the detected device";
case CYRET_ERR_VERSION:
return "The bootloader version detected is not supported";
case CYRET_ERR_CHECKSUM:
return "The checksum does not match the expected value";
case CYRET_ERR_ARRAY:
return "The flash array is not valid";
case CYRET_ERR_ROW:
return "The flash row is not valid";
case CYRET_ERR_BTLDR:
return "The bootloader is not ready to process data";
case CYRET_ERR_ACTIVE:
return "The application is currently marked as active";
default:
case CYRET_ERR_UNK:
return "An unknown error occurred";
case CYRET_ABORT:
return "The operation was aborted";
}
}
static const char * getGpioErrorString(STATUS status)
{
switch(status)
{
case STATUS_DEVICE_OPEN_FAILED: return "Opening I/O device failed";
case STATUS_DEVICE_CLOSE_FAILED: return "Closing I/O failed";
case STATUS_DEVICE_NOT_OPEN: return "Device must be open to perform the requested operation";
case STATUS_DEVICE_ALREADY_OPEN: return "Trying to open the already open device";
case STATUS_DEVICE_READ_FAILED: return "Reading from the device failed";
case STATUS_DEVICE_WRITE_FAILED: return "Writing to the device failed";
case STATUS_DEVICE_CONFIG_FAILED: return "Attempt to configure the device failed (parameters invalid)";
default: return "Unknown error";
}
}
static void progressUpdate(uint8_t arrayId, uint16_t rowNum)
{
printf("Array: %hhd\tRow: %hd \r", arrayId, rowNum); fflush(stdout);
}
Bootloader::Bootloader(CommDevice *dev, uint16_t xresPin, const char *xres_gpio_label, uint32_t expSiId, uint32_t expSiRev)
: device_(dev)
, expSiId_(expSiId)
, expSiRev_(expSiRev)
, blVer_(0)
, xresPin_(xresPin)
, xres_gpio_label_(xres_gpio_label)
{
assert(!self_);
commData.CloseConnection = closeConnection;
commData.OpenConnection = openConnection;
commData.ReadData = readData;
commData.WriteData = writeData;
commData.MaxTransferSize = dev->getMaxTransferSize();
self_ = this;
}
Bootloader::~Bootloader()
{
self_ = NULL;
}
static bool resetDevice(GPIO *xres)
{
STATUS st = xres->set(false); // pulling low
if(st != STATUS_SUCCESS)
{
fprintf(stderr, "\n...failed with: %s.\n", getGpioErrorString(st));
return false;
}
usleep(50000); // 50 mS be in reset
st = xres->set(true); // pulling high
if(st != STATUS_SUCCESS)
{
fprintf(stderr, "\n...failed with: %s.\n", getGpioErrorString(st));
return false;
}
printf("...done\n");
return true;
}
int Bootloader::load(const char *file)
{
GPIO *xres = NULL;
if(xresPin_ != XRES_GPIO_SUPRESS)
{
if (xres_gpio_label_ != NULL and xresPin_ == XRES_GPIO_LABEL)
{
System system = System();
system.setHardware(System::detectSoC());
std::string s(xres_gpio_label_);
for (auto & c: s) c = toupper(c);
int gpio_index = int(s.at(0) - 'A');
xres = new GPIO(system, GPIO::PORT(gpio_index));
}
else
xres = new GPIO(xresPin_);
if(!xres)
{
fprintf(stderr, "\nCannot create XRES GPIO\n");
return EXIT_FAILURE;
}
}
int retcode = EXIT_FAILURE;
do
{
if(xres)
{
printf("\nResetting device...\n");
STATUS st = xres->open();
if(st != STATUS_SUCCESS)
{
fprintf(stderr, "\n...failed with: %s.\n", getGpioErrorString(st));
break;
}
st = xres->setDirection(GPIO::DIRECTION_OUTPUT);
if(st != STATUS_SUCCESS)
{
fprintf(stderr, "\n...failed with: %s.\n", getGpioErrorString(st));
break;
}
resetDevice(xres);
usleep(100000); // wait 100mS for PSOC to boot
}
printf("\nProgramming/Verifying file %s...\n", file);
int err = CyBtldr_Program(file, NULL, 0, &commData, progressUpdate);
if(err != CYRET_SUCCESS)
{
fprintf(stderr, "...failed with: %s.\n", getBlErrorString(err));
break;
}
printf("...done\n");
retcode = EXIT_SUCCESS;
}
while(false);
if(xres)
{
xres->close();
delete xres;
}
return retcode;
}
int Bootloader::openConnection()
{
assert(self_);
return self_->device_->open();
}
int Bootloader::closeConnection()
{
assert(self_);
return self_->device_->close();
}
int Bootloader::readData(uint8_t *buf, int bytes)
{
assert(self_);
usleep(50000); // 20 mS wait for Flash to be written
return self_->device_->read(buf, bytes);
}
int Bootloader::writeData(uint8_t *buf, int bytes)
{
assert(self_);
usleep(1000); // 20 mS wait for Flash to be written
return self_->device_->write(buf, bytes);
}