From a33e620424a8c0a9f62b4fe5c01df5af8e922247 Mon Sep 17 00:00:00 2001 From: Luke Wren Date: Wed, 27 Jan 2021 14:04:14 +0000 Subject: [PATCH] Add pico_unique_board_id_t struct and rename some functions/defines --- .../pico_unique_id/include/pico/unique_id.h | 18 +++++++++++++++--- src/rp2_common/pico_unique_id/unique_id.c | 15 +++++++-------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/rp2_common/pico_unique_id/include/pico/unique_id.h b/src/rp2_common/pico_unique_id/include/pico/unique_id.h index 9c9e38a..a0b4758 100644 --- a/src/rp2_common/pico_unique_id/include/pico/unique_id.h +++ b/src/rp2_common/pico_unique_id/include/pico/unique_id.h @@ -31,7 +31,19 @@ extern "C" { * flash-resident interrupt routines to be disabled when called into. */ -#define PICO_UNIQUE_ID_SIZE_BYTES 8 +#define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8 + +/** + * \brief Unique board identifier + * \ingroup unique_id + * + * This struct is suitable for holding the unique identifier of a NOR flash + * device on an RP2040-based board. It contains an array of + * PICO_UNIQUE_BOARD_ID_SIZE_BYTES identifier bytes. + */ +typedef struct { + uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; +} pico_unique_board_id_t; /*! \brief Get unique ID * \ingroup unique_id @@ -42,9 +54,9 @@ extern "C" { * * On PICO_NO_FLASH builds the unique identifier is set to all 0xEE. * - * \param id_out an 8-byte buffer to which the identifer will be written. + * \param id_out a pointer to a pico_unique_board_id_t struct, to which the identifier will be written */ -void pico_get_unique_id(uint8_t *id_out); +void pico_get_unique_board_id(pico_unique_board_id_t *id_out); #ifdef __cplusplus } diff --git a/src/rp2_common/pico_unique_id/unique_id.c b/src/rp2_common/pico_unique_id/unique_id.c index d79ff77..6d5aef1 100644 --- a/src/rp2_common/pico_unique_id/unique_id.c +++ b/src/rp2_common/pico_unique_id/unique_id.c @@ -7,23 +7,22 @@ #include "hardware/flash.h" #include "pico/unique_id.h" -static_assert(PICO_UNIQUE_ID_SIZE_BYTES == FLASH_UNIQUE_ID_SIZE_BYTES); +static_assert(PICO_UNIQUE_BOARD_ID_SIZE_BYTES == FLASH_UNIQUE_ID_SIZE_BYTES); -static uint8_t retrieved_id[PICO_UNIQUE_ID_SIZE_BYTES]; +static pico_unique_board_id_t retrieved_id; static void __attribute__((constructor)) _retrieve_unique_id_on_boot() { #if PICO_NO_FLASH // The hardware_flash call will panic() if called directly on a NO_FLASH // build. Since this constructor is pre-main it would be annoying to // debug, so just produce something well-defined and obviously wrong. - for (int i = 0; i < PICO_UNIQUE_ID_SIZE_BYTES; i++) - retrieved_id[i] = 0xee; + for (int i = 0; i < PICO_UNIQUE_BOARD_ID_SIZE_BYTES; i++) + retrieved_id.id[i] = 0xee; #else - flash_get_unique_id(retrieved_id); + flash_get_unique_id(retrieved_id.id); #endif } -void pico_get_unique_id(uint8_t *id_out) { - for (int i = 0; i < PICO_UNIQUE_ID_SIZE_BYTES; i++) - id_out[i] = retrieved_id[i]; +void pico_get_unique_board_id(pico_unique_board_id_t *id_out) { + *id_out = retrieved_id; }