Skip to content

Commit fa83c1c

Browse files
authored
adding boot counter to main.c
The boot counter is a uint8_t single-byte counter stored in the first NVM byte position (`micrcontroller.nvm[0]`). The counter increments by 1 each time the board boots, regardless if it's a hard or soft reset. Enable the boot counter by adding `#define CIRCUITPY_BOOT_COUNTER 1` to your board's mpconfigboard.h file. Note that an error will be thrown during the build if `CIRCUITPY_INTERNAL_NVM_SIZE` is not also set within mpconfigboard.h.
1 parent 5d29caf commit fa83c1c

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@
107107
#include "shared-bindings/wifi/__init__.h"
108108
#endif
109109

110+
#ifdef CIRCUITPY_BOOT_COUNTER
111+
#include "shared-bindings/nvm/ByteArray.h"
112+
#endif
113+
110114
#if MICROPY_ENABLE_PYSTACK
111115
static size_t PLACE_IN_DTCM_BSS(_pystack[CIRCUITPY_PYSTACK_SIZE / sizeof(size_t)]);
112116
#endif
@@ -304,6 +308,15 @@ STATIC void print_code_py_status_message(safe_mode_t safe_mode) {
304308
}
305309
}
306310

311+
#ifdef CIRCUITPY_BOOT_COUNTER
312+
nvm_bytearray_obj_t bootcnt = {
313+
.base = {.type = &nvm_bytearray_type},
314+
.len = ( uint32_t) 1,
315+
.start_address = (uint8_t*) (0x00080000 - CIRCUITPY_INTERNAL_NVM_SIZE)
316+
};
317+
uint8_t value_out = 0;
318+
#endif
319+
307320
STATIC bool run_code_py(safe_mode_t safe_mode) {
308321
bool serial_connected_at_start = serial_connected();
309322
bool printed_safe_mode_message = false;
@@ -316,6 +329,12 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
316329
}
317330
#endif
318331

332+
#ifdef CIRCUITPY_BOOT_COUNTER
333+
common_hal_nvm_bytearray_get_bytes(&bootcnt,0,1,&value_out);
334+
++value_out;
335+
common_hal_nvm_bytearray_set_bytes(&bootcnt,0,&value_out,1);
336+
#endif
337+
319338
pyexec_result_t result;
320339

321340
result.return_code = 0;

ports/atmel-samd/boards/pycubed/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define EXTERNAL_FLASH_QSPI_DUAL
1616

1717
#define CIRCUITPY_DRIVE_LABEL "PYCUBED"
18+
#define CIRCUITPY_BOOT_COUNTER 1
1819

1920
#define BOARD_HAS_CRYSTAL 1
2021

ports/atmel-samd/boards/pycubed_mram/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)
1717

1818
#define CIRCUITPY_DRIVE_LABEL "PYCUBED"
19+
#define CIRCUITPY_BOOT_COUNTER 1
1920

2021
#define BOARD_HAS_CRYSTAL 1
2122

py/circuitpy_mpconfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,10 @@ void supervisor_run_background_tasks_if_tick(void);
554554

555555
#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
556556

557+
#if !defined(CIRCUITPY_INTERNAL_NVM_SIZE) && defined(CIRCUITPY_BOOT_COUNTER)
558+
#error "boot counter requires CIRCUITPY_NVM enabled"
559+
#endif
560+
557561
#define CIRCUITPY_VERBOSE_BLE 0
558562

559563
// This trades ~1k flash space (1) for that much in RAM plus the cost to compute

0 commit comments

Comments
 (0)