From fa83c1c2f96d83e9d33880100275d915365937b6 Mon Sep 17 00:00:00 2001 From: Max Holliday Date: Wed, 8 Sep 2021 19:56:23 -0600 Subject: [PATCH] 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. --- main.c | 19 +++++++++++++++++++ .../atmel-samd/boards/pycubed/mpconfigboard.h | 1 + .../boards/pycubed_mram/mpconfigboard.h | 1 + py/circuitpy_mpconfig.h | 4 ++++ 4 files changed, 25 insertions(+) diff --git a/main.c b/main.c index d024717beb..8f2d9e806e 100755 --- a/main.c +++ b/main.c @@ -107,6 +107,10 @@ #include "shared-bindings/wifi/__init__.h" #endif +#ifdef CIRCUITPY_BOOT_COUNTER +#include "shared-bindings/nvm/ByteArray.h" +#endif + #if MICROPY_ENABLE_PYSTACK static size_t PLACE_IN_DTCM_BSS(_pystack[CIRCUITPY_PYSTACK_SIZE / sizeof(size_t)]); #endif @@ -304,6 +308,15 @@ STATIC void print_code_py_status_message(safe_mode_t safe_mode) { } } +#ifdef CIRCUITPY_BOOT_COUNTER +nvm_bytearray_obj_t bootcnt = { + .base = {.type = &nvm_bytearray_type}, + .len = ( uint32_t) 1, + .start_address = (uint8_t*) (0x00080000 - CIRCUITPY_INTERNAL_NVM_SIZE) + }; +uint8_t value_out = 0; +#endif + STATIC bool run_code_py(safe_mode_t safe_mode) { bool serial_connected_at_start = serial_connected(); bool printed_safe_mode_message = false; @@ -316,6 +329,12 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { } #endif + #ifdef CIRCUITPY_BOOT_COUNTER + common_hal_nvm_bytearray_get_bytes(&bootcnt,0,1,&value_out); + ++value_out; + common_hal_nvm_bytearray_set_bytes(&bootcnt,0,&value_out,1); + #endif + pyexec_result_t result; result.return_code = 0; diff --git a/ports/atmel-samd/boards/pycubed/mpconfigboard.h b/ports/atmel-samd/boards/pycubed/mpconfigboard.h index 07ecb01ffa..eed590fda4 100644 --- a/ports/atmel-samd/boards/pycubed/mpconfigboard.h +++ b/ports/atmel-samd/boards/pycubed/mpconfigboard.h @@ -15,6 +15,7 @@ #define EXTERNAL_FLASH_QSPI_DUAL #define CIRCUITPY_DRIVE_LABEL "PYCUBED" +#define CIRCUITPY_BOOT_COUNTER 1 #define BOARD_HAS_CRYSTAL 1 diff --git a/ports/atmel-samd/boards/pycubed_mram/mpconfigboard.h b/ports/atmel-samd/boards/pycubed_mram/mpconfigboard.h index 9380d149d2..9332966dd4 100644 --- a/ports/atmel-samd/boards/pycubed_mram/mpconfigboard.h +++ b/ports/atmel-samd/boards/pycubed_mram/mpconfigboard.h @@ -16,6 +16,7 @@ #define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE) #define CIRCUITPY_DRIVE_LABEL "PYCUBED" +#define CIRCUITPY_BOOT_COUNTER 1 #define BOARD_HAS_CRYSTAL 1 diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 3f998b7795..803fc1dbdf 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -554,6 +554,10 @@ void supervisor_run_background_tasks_if_tick(void); #define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt" +#if !defined(CIRCUITPY_INTERNAL_NVM_SIZE) && defined(CIRCUITPY_BOOT_COUNTER) +#error "boot counter requires CIRCUITPY_NVM enabled" +#endif + #define CIRCUITPY_VERBOSE_BLE 0 // This trades ~1k flash space (1) for that much in RAM plus the cost to compute