Add pico_get_unique_board_id_string API (#281)

* Add pico_get_unique_board_id_string API

Add a new API to pico_unique which will turn the unique ID into a
canonical text string.

Use this API to update the USB device serial number in stdio_usb.

Supercedes #280

* Clean up -Wconversion=error issues

* Address review comments, fix api typing

Use cleaner binary-to-hex conversion.  Update the length parameter to
use uint per the SDK standard .
This commit is contained in:
Earle F. Philhower, III
2021-03-30 07:04:58 -07:00
committed by GitHub
parent 98574564b8
commit 3c0309c10e
4 changed files with 38 additions and 3 deletions

View File

@ -57,6 +57,21 @@ typedef struct {
*/
void pico_get_unique_board_id(pico_unique_board_id_t *id_out);
/*! \brief Get unique ID in string format
* \ingroup pico_unique_id
*
* Get the unique 64-bit device identifier which was retrieved from the
* external NOR flash device at boot, formatted as an ASCII hex string.
* Will always 0-terminate.
*
* On PICO_NO_FLASH builds the unique identifier is set to all 0xEE.
*
* \param id_out a pointer to a char buffer of size len, to which the identifier will be written
* \param len the size of id_out. For full serial, len >= 2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1
*/
void pico_get_unique_board_id_string(char *id_out, uint len);
#ifdef __cplusplus
}
#endif

View File

@ -26,3 +26,14 @@ static void __attribute__((constructor)) _retrieve_unique_id_on_boot(void) {
void pico_get_unique_board_id(pico_unique_board_id_t *id_out) {
*id_out = retrieved_id;
}
void pico_get_unique_board_id_string(char *id_out, uint len) {
assert(len > 0);
size_t i;
// Generate hex one nibble at a time
for (i = 0; (i < len - 1) && (i < PICO_UNIQUE_BOARD_ID_SIZE_BYTES * 2); i++) {
int nibble = (retrieved_id.id[i/2] >> (4 - 4 * (i&1))) & 0xf;
id_out[i] = (char)(nibble < 10 ? nibble + '0' : nibble + 'A' - 10);
}
id_out[i] = 0;
}