Skip to content

Commit 6e3e210

Browse files
Let FINALIZE_FLASH return the number of pages erased
1 parent d7f6bdf commit 6e3e210

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

PROTOCOL.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ should be sent, except with a zero address to start over.
482482
|-------|-------------------------------
483483
| 1 | Status: `COMMAND_OK` (0x00)
484484
| 1 | Length
485+
| 1 | Erasecount
485486
| 1 | CRC
486487

487488
| Bytes | Reply format
@@ -491,6 +492,10 @@ should be sent, except with a zero address to start over.
491492
| 1 | Reason
492493
| 1 | CRC
493494

495+
The erasecount is the number of pages erased since the last reset, or
496+
the last succesful `FINALIZE_FLASH` command. This is returned to
497+
facilitate verification of the "erase only when needed" mechanism.
498+
494499
When flashing fails for any reason, an additional reason byte is
495500
returned. The meaning of this byte is purely informative and not defined
496501
by this protocol, it should be looked up in the bootloader.

SelfProgram.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// The actual value is set by main(), to avoid the overhead gcc
3030
// generates for running a "constructor" to set this value
3131
uint16_t SelfProgram::trampolineStart = 0;
32+
uint8_t SelfProgram::eraseCount = 0;
3233

3334
void SelfProgram::readFlash(uint16_t address, uint8_t *data, uint8_t len) {
3435
for (uint8_t i=0; i < len; i++) {
@@ -81,8 +82,11 @@ uint8_t SelfProgram::writePage(uint16_t address, uint8_t *data, uint8_t len) {
8182
if (address % SPM_ERASESIZE == 0) {
8283
// If this is the page containing the trampoline, it
8384
// will already be erased when writing the first page
84-
if (address / SPM_ERASESIZE != trampolineStart / SPM_ERASESIZE)
85+
if (address / SPM_ERASESIZE != trampolineStart / SPM_ERASESIZE) {
86+
if (eraseCount < 0xff)
87+
++eraseCount;
8588
boot_page_erase_safe(address);
89+
}
8690
}
8791

8892
for (int i=0; i < len; i += 2) {
@@ -118,6 +122,8 @@ void SelfProgram::writeTrampoline(uint16_t instruction) {
118122
// other application code. This makes no attempt to preserve the
119123
// other code to keep since simple. This should work since we're
120124
// writing a new application anyway.
125+
if (eraseCount < 0xff)
126+
++eraseCount;
121127
boot_page_erase_safe(address);
122128

123129
// Take the reset instruction from the page at offset 0 and 1

SelfProgram.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class SelfProgram {
3939
// Use a reference to make this an alias to trampolineStart for
4040
// readability
4141
static constexpr const uint16_t& applicationSize = trampolineStart;
42+
43+
static uint8_t eraseCount;
4244
};
4345

4446
#endif /* SELFPROGRAM_H_ */

bootloader.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ cmd_result processCommand(uint8_t cmd, uint8_t *datain, uint8_t len, uint8_t *da
222222
dataout[0] = err;
223223
return cmd_result(Status::COMMAND_FAILED, 1);
224224
} else {
225-
return cmd_ok();
225+
dataout[0] = SelfProgram::eraseCount;
226+
SelfProgram::eraseCount = 0;
227+
return cmd_ok(1);
226228
}
227229
}
228230
case Commands::READ_FLASH:

0 commit comments

Comments
 (0)