Skip to content

Commit bb5b5f9

Browse files
authored
Move unique_id (configurably) earlier in the static init process (#2379)
This enables using the unique_id in C++ static initializers by default
1 parent a5ba689 commit bb5b5f9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/rp2_common/pico_unique_id/include/pico/unique_id.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ extern "C" {
4141

4242
#define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8
4343

44+
/**
45+
* \brief Static initialization order
46+
* \ingroup pico_unique_id
47+
*
48+
* This defines the init_priority of the pico_unique_id. By default, it is 1000. The valid range is
49+
* from 101-65535. Set it to -1 to set the priority to none, thus putting it after 65535. Changing
50+
* this value will initialize the unique_id earlier or later in the static initialization order.
51+
* This is most useful for C++ consumers of the pico-sdk.
52+
*
53+
* See https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-constructor-function-attribute
54+
* and https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html#index-init_005fpriority-variable-attribute
55+
*
56+
* Here is an example of C++ static initializers that will run before, and then after, pico_unique_id is loaded:
57+
*
58+
* [[gnu::init_priority(500)]] my_class before_instance;
59+
* [[gnu::init_priority(2000)]] my_class after_instance;
60+
*
61+
*/
62+
#ifndef PICO_UNIQUE_BOARD_ID_INIT_PRIORITY
63+
#define PICO_UNIQUE_BOARD_ID_INIT_PRIORITY 1000
64+
#endif
65+
4466
/**
4567
* \brief Unique board identifier
4668
* \ingroup pico_unique_id

src/rp2_common/pico_unique_id/unique_id.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ static_assert(PICO_UNIQUE_BOARD_ID_SIZE_BYTES <= FLASH_UNIQUE_ID_SIZE_BYTES, "Bo
1212

1313
static pico_unique_board_id_t retrieved_id;
1414

15-
static void __attribute__((constructor)) _retrieve_unique_id_on_boot(void) {
15+
#if PICO_UNIQUE_BOARD_ID_INIT_PRIORITY == -1
16+
#define PICO_UNIQUE_BOARD_ID_INIT_ATTRIBUTES constructor
17+
#else
18+
#define PICO_UNIQUE_BOARD_ID_INIT_ATTRIBUTES constructor(PICO_UNIQUE_BOARD_ID_INIT_PRIORITY)
19+
#endif
20+
21+
static void __attribute__((PICO_UNIQUE_BOARD_ID_INIT_ATTRIBUTES)) _retrieve_unique_id_on_boot(void) {
1622
#if PICO_RP2040
1723
#if PICO_NO_FLASH
1824
// The hardware_flash call will panic() if called directly on a NO_FLASH

0 commit comments

Comments
 (0)