Move unique_id (configurably) earlier in the static init process (#2379)

This enables using the unique_id in C++ static initializers by default
This commit is contained in:
Patrick Plenefisch 2025-03-29 13:02:03 -04:00 committed by GitHub
parent a5ba689cb5
commit bb5b5f96e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View file

@ -41,6 +41,28 @@ extern "C" {
#define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8
/**
* \brief Static initialization order
* \ingroup pico_unique_id
*
* This defines the init_priority of the pico_unique_id. By default, it is 1000. The valid range is
* from 101-65535. Set it to -1 to set the priority to none, thus putting it after 65535. Changing
* this value will initialize the unique_id earlier or later in the static initialization order.
* This is most useful for C++ consumers of the pico-sdk.
*
* See https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-constructor-function-attribute
* and https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html#index-init_005fpriority-variable-attribute
*
* Here is an example of C++ static initializers that will run before, and then after, pico_unique_id is loaded:
*
* [[gnu::init_priority(500)]] my_class before_instance;
* [[gnu::init_priority(2000)]] my_class after_instance;
*
*/
#ifndef PICO_UNIQUE_BOARD_ID_INIT_PRIORITY
#define PICO_UNIQUE_BOARD_ID_INIT_PRIORITY 1000
#endif
/**
* \brief Unique board identifier
* \ingroup pico_unique_id

View file

@ -12,7 +12,13 @@ static_assert(PICO_UNIQUE_BOARD_ID_SIZE_BYTES <= FLASH_UNIQUE_ID_SIZE_BYTES, "Bo
static pico_unique_board_id_t retrieved_id;
static void __attribute__((constructor)) _retrieve_unique_id_on_boot(void) {
#if PICO_UNIQUE_BOARD_ID_INIT_PRIORITY == -1
#define PICO_UNIQUE_BOARD_ID_INIT_ATTRIBUTES constructor
#else
#define PICO_UNIQUE_BOARD_ID_INIT_ATTRIBUTES constructor(PICO_UNIQUE_BOARD_ID_INIT_PRIORITY)
#endif
static void __attribute__((PICO_UNIQUE_BOARD_ID_INIT_ATTRIBUTES)) _retrieve_unique_id_on_boot(void) {
#if PICO_RP2040
#if PICO_NO_FLASH
// The hardware_flash call will panic() if called directly on a NO_FLASH