1 /*
2  * Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _PICO_UNIQUE_ID_H
8 #define _PICO_UNIQUE_ID_H
9 
10 #include "pico.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /** \file pico/unique_id.h
17  *  \defgroup pico_unique_id pico_unique_id
18  *
19  * Unique device ID access API
20  *
21  * RP2040 does not have an on-board unique identifier (all instances of RP2040
22  * silicon are identical and have no persistent state). However, RP2040 boots
23  * from serial NOR flash devices which have a 64-bit unique ID as a standard
24  * feature, and there is a 1:1 association between RP2040 and flash, so this
25  * is suitable for use as a unique identifier for an RP2040-based board.
26  *
27  * This library injects a call to the flash_get_unique_id function from the
28  * hardware_flash library, to run before main, and stores the result in a
29  * static location which can safely be accessed at any time via
30  * pico_get_unique_id().
31  *
32  * This avoids some pitfalls of the hardware_flash API, which requires any
33  * flash-resident interrupt routines to be disabled when called into.
34  */
35 
36 #define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8
37 
38 /**
39  * \brief Unique board identifier
40  * \ingroup pico_unique_id
41  *
42  * This struct is suitable for holding the unique identifier of a NOR flash
43  * device on an RP2040-based board. It contains an array of
44  * PICO_UNIQUE_BOARD_ID_SIZE_BYTES identifier bytes.
45  */
46 typedef struct {
47 	uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES];
48 } pico_unique_board_id_t;
49 
50 /*! \brief Get unique ID
51  *  \ingroup pico_unique_id
52  *
53  * Get the unique 64-bit device identifier which was retrieved from the
54  * external NOR flash device at boot.
55  *
56  * On PICO_NO_FLASH builds the unique identifier is set to all 0xEE.
57  *
58  * \param id_out a pointer to a pico_unique_board_id_t struct, to which the identifier will be written
59  */
60 void pico_get_unique_board_id(pico_unique_board_id_t *id_out);
61 
62 /*! \brief Get unique ID in string format
63  *  \ingroup pico_unique_id
64  *
65  * Get the unique 64-bit device identifier which was retrieved from the
66  * external NOR flash device at boot, formatted as an ASCII hex string.
67  * Will always 0-terminate.
68  *
69  * On PICO_NO_FLASH builds the unique identifier is set to all 0xEE.
70  *
71  * \param id_out a pointer to a char buffer of size len, to which the identifier will be written
72  * \param len the size of id_out. For full serial, len >= 2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1
73  */
74 void pico_get_unique_board_id_string(char *id_out, uint len);
75 
76 
77 #ifdef __cplusplus
78 }
79 #endif
80 
81 #endif
82