Skip to content

Instantly share code, notes, and snippets.

@aynka
Created July 8, 2023 10:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aynka/c512be08a3ca3613855f88cc985addc1 to your computer and use it in GitHub Desktop.
Save aynka/c512be08a3ca3613855f88cc985addc1 to your computer and use it in GitHub Desktop.
Decoding and making a Serial Number from STM32 96bit UID
uint16_t snprint_stm32_serialno(char* p, uint16_t maxlen)
{
uint32_t uid0 = *(uint32_t*) (UID_BASE);
uint32_t uid1 = *(uint32_t*) (UID_BASE + 0x4);
uint32_t uid2 = *(uint32_t*) (UID_BASE + 0x8);
uint16_t n = 0;
if (maxlen > 16)
{
p[0]= 'P';
p[1]= 'G';
p[2] = uid1 >> 8;
p[3] = uid1 >> 16;
p[4] = uid1 >> 24;
p[5] = uid2;
p[6] = uid2 >> 8;
p[7] = uid2 >> 16;
p[8] = uid2 >> 24;
if (0 == isalnum((int) p[8]))
p[8] = '-';
n = 9;
uint8_t wafer = uid1;
uint8_t x_coord = uid0;
uint8_t y_coord = uid0 >> 16;
n += snprintf( &p[n], (maxlen - n), "%X%02X%02X", wafer, x_coord, y_coord);
if (n > maxlen-1)
n = maxlen-1;
p[n]='\0';
}
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment