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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* PanelDueUpdater.cpp
*
* Created on: 25 Nov 2020
* Author: manuel
*/
#include "PanelDueUpdater.h"
#if HAS_AUX_DEVICES
#include <Platform/Platform.h>
#include <Platform/RepRap.h>
class AuxSerialPort : public SerialPort
{
public:
AuxSerialPort(UARTClass& uartClass) noexcept : uart(uartClass), _timeout(0) {}
~AuxSerialPort() {}
bool open(int baud = 115200,
int data = 8,
SerialPort::Parity parity = SerialPort::ParityNone,
SerialPort::StopBit stop = SerialPort::StopBitOne) noexcept override { return true; }
void close() noexcept override {}
bool isUsb() noexcept override { return false; }
int read(uint8_t* data, int size) noexcept override;
int write(const uint8_t* data, int size) noexcept override { return this->uart.write(data, size); }
int get() noexcept override;
int put(int c) noexcept override { return this->uart.write(c); }
bool timeout(int millisecs) noexcept override { _timeout = millisecs; return true; }
void flush() noexcept override { this->uart.flush(); }
void setDTR(bool dtr) noexcept override {}
void setRTS(bool rts) noexcept override {}
private:
UARTClass& uart;
int _timeout;
};
int AuxSerialPort::get() noexcept
{
uint8_t byte;
if (read(&byte, 1) != 1)
return -1;
return byte;
}
int AuxSerialPort::read(uint8_t* data, int size) noexcept
{
const uint32_t start = millis();
int read = 0;
do
{
const int readNow = (int) this->uart.readBytes((uint8_t*)data+read, size-read);
if (readNow >= 0)
{
read += readNow;
}
} while (read < size && (int) (millis() - start) < _timeout);
return read;
}
class DebugObserver : public FlasherObserver
{
public:
DebugObserver() noexcept : lastPercentage(0) {}
virtual ~DebugObserver() {}
void onStatus(const char *message, ...) noexcept override;
void onProgress(int num, int div) noexcept override;
void Reset() noexcept override { lastPercentage = 0; };
private:
uint8_t lastPercentage;
};
void DebugObserver::onStatus(const char *message, ...) noexcept
{
va_list ap;
va_start(ap, message);
reprap.GetPlatform().MessageF(GenericMessage, message, ap);
va_end(ap);
}
void DebugObserver::onProgress(int num, int div) noexcept
{
uint8_t percentage = (uint8_t)(num * 100 / div);
if (percentage == lastPercentage + 20) {
lastPercentage = percentage;
reprap.GetPlatform().MessageF(GenericMessage, "Progress: %d%%\n", percentage);
}
}
PanelDueUpdater::PanelDueUpdater() noexcept
:
serialChannel(NumSerialChannels+1)
, currentBaudRate(0)
, samba(nullptr)
, serialPort(nullptr)
, device(nullptr)
, flasherObserver(nullptr)
, flasher(nullptr)
, offset(0)
, erasedAndResetAt(0)
, state(FlashState::idle)
, firmwareFile(nullptr)
{
}
PanelDueUpdater::~PanelDueUpdater() noexcept
{
DeleteObject(samba);
DeleteObject(serialPort);
DeleteObject(device);
DeleteObject(flasherObserver);
DeleteObject(flasher);
}
void PanelDueUpdater::Start(const StringRef& filenameRef, const uint32_t serialChan) noexcept
{
if (state != FlashState::idle)
{
return;
}
serialChannel = serialChan;
firmwareFile = reprap.GetPlatform().OpenFile(FIRMWARE_DIRECTORY, filenameRef.IsEmpty() ? PANEL_DUE_FIRMWARE_FILE : filenameRef.c_str(), OpenMode::read);
state = FlashState::eraseAndReset;
}
void PanelDueUpdater::Spin() noexcept
{
try
{
switch (state.RawValue())
{
case FlashState::eraseAndReset:
{
reprap.GetPlatform().Message(GenericMessage, "Sending Erase-and-Reset command to PanelDue\n");
// Since writing messages via AppendAuxReply is disabled while flashing we need to send it directly
auto auxPort = GetAuxPort();
auxPort->write('\n'); // Make sure the previous message is regarded as terminated by PanelDue
auxPort->write(panelDueCommandEraseAndReset);
auxPort->flush();
state = FlashState::waitAfterEraseAndReset;
erasedAndResetAt = millis();
}
break;
case FlashState::waitAfterEraseAndReset:
if (millis() - erasedAndResetAt > WaitMsAfterEraseAndReset)
{
state = FlashState::setup;
}
break;
case FlashState::setup:
{
reprap.GetPlatform().Message(GenericMessage, "Establishing connection to PanelDue bootloader\n");
// Verifying creates the Emergency Stop sequence so disable it here
auto auxPort = GetAuxPort();
currentInterruptCallbackFn = auxPort->SetInterruptCallback(nullptr);
Platform& platform = reprap.GetPlatform();
uint32_t baudRate = platform.GetBaudRate(serialChannel);
// Make sure baud rate is set correctly
if (baudRate != RequiredBaudRate)
{
currentBaudRate = baudRate;
platform.SetBaudRate(serialChannel, RequiredBaudRate);
platform.ResetChannel(serialChannel);
}
samba = new Samba();
serialPort = new AuxSerialPort(*auxPort);
samba->connect(serialPort);
device = new Device(*samba);
device->create(); // If this throws the bootloader did not answer
flasherObserver = new DebugObserver();
flasher = new Flasher(*samba, *device, *flasherObserver);
state = FlashState::bossaUnlock;
}
break;
case FlashState::bossaUnlock:
reprap.GetPlatform().Message(GenericMessage, "Unlocking PanelDue flash memory\n");
flasher->lock(false);
state = FlashState::bossaErase;
break;
case FlashState::bossaErase:
reprap.GetPlatform().Message(GenericMessage, "Erasing PanelDue flash memory\n");
flasher->erase(0);
state = FlashState::bossaWrite;
break;
case FlashState::bossaWrite:
{
bool done = flasher->write(firmwareFile, offset);
if (done)
{
offset = 0; // Reset it for verification
firmwareFile->Seek(offset);
state = FlashState::bossaVerify;
flasherObserver->Reset();
}
}
break;
case FlashState::bossaVerify:
{
uint32_t pageErrors;
uint32_t totalErrors;
bool done = flasher->verify(firmwareFile, pageErrors, totalErrors, offset);
if (done && pageErrors == 0)
{
state = FlashState::bossaWriteOptions;
}
else if (pageErrors > 0)
{
reprap.GetPlatform().MessageF(ErrorMessage, "Verify failed: Page errors: %" PRIu32 " - Byte errors: %" PRIu32 "\n", pageErrors, totalErrors);
state = FlashState::done;
}
}
break;
case FlashState::bossaWriteOptions:
{
reprap.GetPlatform().Message(GenericMessage, "Writing PanelDue flash options\n");
BossaFlash* flash = device->getFlash();
flash->setBootFlash(true);
flash->writeOptions();
state = FlashState::bossaReset;
}
break;
case FlashState::bossaReset:
reprap.GetPlatform().Message(GenericMessage, "Restarting PanelDue\n");
device->reset();
GetAuxPort()->flush();
state = FlashState::done;
break;
case FlashState::done:
{
// Restore previous baud rate
if (currentBaudRate != 0)
{
Platform& platform = reprap.GetPlatform();
platform.SetBaudRate(serialChannel, currentBaudRate);
platform.ResetChannel(serialChannel);
}
// Restore the callback for the Emergency Stop sequence
auto auxPort = GetAuxPort();
auxPort->SetInterruptCallback(currentInterruptCallbackFn);
currentInterruptCallbackFn = nullptr;
serialChannel = NumSerialChannels+1;
currentBaudRate = 0;
// Delete all objects we new'd
DeleteObject(samba);
DeleteObject(serialPort);
DeleteObject(device);
DeleteObject(flasherObserver);
DeleteObject(flasher);
offset = 0;
erasedAndResetAt = 0;
if (firmwareFile != nullptr)
{
firmwareFile->Close();
firmwareFile = nullptr;
}
state = FlashState::idle;
}
break;
default:
break;
}
}
catch (GCodeException& ex)
{
#if DEBUG_BOSSA
String<StringLength100> errorMessage;
ex.GetMessage(errorMessage.GetRef(), nullptr);
reprap.GetPlatform().MessageF(ErrorMessage, "%s", errorMessage.c_str());
#endif
if (state == FlashState::setup)
{
reprap.GetPlatform().Message(ErrorMessage, "Failed to communicate with PanelDue bootloader (no START signal received). Please try again or press the Erase and Reset switches on PanelDue.");
}
else
{
reprap.GetPlatform().MessageF(ErrorMessage, "Flashing PanelDue failed in step %s. Please try again.", state.ToString());
}
// Delete all objects we new'd
DeleteObject(samba);
DeleteObject(serialPort);
DeleteObject(device);
DeleteObject(flasherObserver);
DeleteObject(flasher);
state = FlashState::done;
}
}
UARTClass* PanelDueUpdater::GetAuxPort() noexcept
{
return
serialChannel == 0 || serialChannel > NumSerialChannels ? nullptr :
#ifdef SERIAL_AUX2_DEVICE
serialChannel == 2 ? &SERIAL_AUX2_DEVICE :
#endif
&SERIAL_AUX_DEVICE; // Channel 1
}
#endif
// End
|