Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/rp2_common/pico_unique_id/include/pico/unique_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/rp2_common/pico_unique_id/unique_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down