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.
This commit is contained in:
Max Holliday 2021-09-08 19:56:23 -06:00 committed by GitHub
parent 5d29caf499
commit fa83c1c2f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 0 deletions

19
main.c
View File

@ -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;

View File

@ -15,6 +15,7 @@
#define EXTERNAL_FLASH_QSPI_DUAL
#define CIRCUITPY_DRIVE_LABEL "PYCUBED"
#define CIRCUITPY_BOOT_COUNTER 1
#define BOARD_HAS_CRYSTAL 1

View File

@ -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

View File

@ -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