diff --git a/src/rp2_common/CMakeLists.txt b/src/rp2_common/CMakeLists.txt index b864568..ae0561f 100644 --- a/src/rp2_common/CMakeLists.txt +++ b/src/rp2_common/CMakeLists.txt @@ -37,6 +37,7 @@ if (NOT PICO_BARE_METAL) pico_add_subdirectory(boot_stage2) pico_add_subdirectory(pico_multicore) + pico_add_subdirectory(pico_unique_id) pico_add_subdirectory(pico_bit_ops) pico_add_subdirectory(pico_divider) diff --git a/src/rp2_common/pico_unique_id/CMakeLists.txt b/src/rp2_common/pico_unique_id/CMakeLists.txt new file mode 100644 index 0000000..4c367d7 --- /dev/null +++ b/src/rp2_common/pico_unique_id/CMakeLists.txt @@ -0,0 +1,9 @@ +add_library(pico_unique_id INTERFACE) + +target_sources(pico_unique_id INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/unique_id.c +) + +target_include_directories(pico_unique_id INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) + +target_link_libraries(pico_unique_id INTERFACE hardware_flash) 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 new file mode 100644 index 0000000..9c9e38a --- /dev/null +++ b/src/rp2_common/pico_unique_id/include/pico/unique_id.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef _PICO_UNIQUE_ID_H_ +#define _PICO_UNIQUE_ID_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/** \file pico/unique_id.h + * \defgroup unique_id unique_id + * + * Unique device ID access API + * + * RP2040 does not have an on-board unique identifier (all instances of RP2040 + * silicon are identical and have no persistent state). However, RP2040 boots + * from serial NOR flash devices which have a 64-bit unique ID as a standard + * feature, and there is a 1:1 association between RP2040 and flash, so this + * is suitable for use as a unique identifier for an RP2040-based board. + * + * This library injects a call to the flash_get_unique_id function from the + * hardware_flash library, to run before main, and stores the result in a + * static location which can safely be accessed at any time via + * pico_get_unique_id(). + * + * This avoids some pitfalls of the hardware_flash API, which requires any + * flash-resident interrupt routines to be disabled when called into. + */ + +#define PICO_UNIQUE_ID_SIZE_BYTES 8 + +/*! \brief Get unique ID + * \ingroup unique_id + * + + * Get the unique 64-bit device identifier which was retrieved from the + * external NOR flash device at boot. + * + * 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. + */ +void pico_get_unique_id(uint8_t *id_out); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/rp2_common/pico_unique_id/unique_id.c b/src/rp2_common/pico_unique_id/unique_id.c new file mode 100644 index 0000000..b88c312 --- /dev/null +++ b/src/rp2_common/pico_unique_id/unique_id.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "hardware/flash.h" +#include "pico/unique_id.h" + +static_assert(PICO_UNIQUE_ID_SIZE_BYTES == FLASH_UNIQUE_ID_SIZE_BYTES); + +static uint8_t retrieved_id[PICO_UNIQUE_ID_SIZE_BYTES]; + +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; +#else + flash_get_unique_id(retrieved_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]; +}