Add pico_unique_board_id_t struct and rename some functions/defines

This commit is contained in:
Luke Wren 2021-01-27 14:04:14 +00:00 committed by Graham Sanderson
parent 6cef7931f5
commit 21d447fa28
2 changed files with 22 additions and 11 deletions

View File

@ -31,7 +31,19 @@ extern "C" {
* flash-resident interrupt routines to be disabled when called into. * 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 /*! \brief Get unique ID
* \ingroup unique_id * \ingroup unique_id
@ -42,9 +54,9 @@ extern "C" {
* *
* On PICO_NO_FLASH builds the unique identifier is set to all 0xEE. * 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 #ifdef __cplusplus
} }

View File

@ -7,23 +7,22 @@
#include "hardware/flash.h" #include "hardware/flash.h"
#include "pico/unique_id.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() { static void __attribute__((constructor)) _retrieve_unique_id_on_boot() {
#if PICO_NO_FLASH #if PICO_NO_FLASH
// The hardware_flash call will panic() if called directly on a 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 // build. Since this constructor is pre-main it would be annoying to
// debug, so just produce something well-defined and obviously wrong. // debug, so just produce something well-defined and obviously wrong.
for (int i = 0; i < PICO_UNIQUE_ID_SIZE_BYTES; i++) for (int i = 0; i < PICO_UNIQUE_BOARD_ID_SIZE_BYTES; i++)
retrieved_id[i] = 0xee; retrieved_id.id[i] = 0xee;
#else #else
flash_get_unique_id(retrieved_id); flash_get_unique_id(retrieved_id.id);
#endif #endif
} }
void pico_get_unique_id(uint8_t *id_out) { void pico_get_unique_board_id(pico_unique_board_id_t *id_out) {
for (int i = 0; i < PICO_UNIQUE_ID_SIZE_BYTES; i++) *id_out = retrieved_id;
id_out[i] = retrieved_id[i];
} }