stm32/serialno: Fixed a couple of mistakes made in the cleanup and attempted a different way to quiet the errant GCC warning

This commit is contained in:
dragonmux 2022-06-06 09:22:02 -04:00 committed by Piotr Esden-Tempski
parent 24ffef7175
commit 5a9131dcb2
1 changed files with 13 additions and 14 deletions

View File

@ -24,15 +24,13 @@ char *serial_no_read(char *s)
{ {
#if DFU_SERIAL_LENGTH == 9 #if DFU_SERIAL_LENGTH == 9
const volatile uint32_t *const unique_id_p = (volatile uint32_t *)DESIG_UNIQUE_ID_BASE; const volatile uint32_t *const unique_id_p = (volatile uint32_t *)DESIG_UNIQUE_ID_BASE;
const uint32_t unique_id = *unique_id_p + const uint32_t unique_id = unique_id_p[0] + unique_id_p[1] + unique_id_p[2];
*(unique_id_p + 1) +
*(unique_id_p + 2);
/* Fetch serial number from chip's unique ID */ /* Fetch serial number from chip's unique ID */
for(size_t i = 0; i < 8U; ++i) { for(size_t i = 0; i < 8U; ++i) {
s[7U - i] = ((unique_id >> (i * 4U)) & 0x0FU) + '0'; s[7U - i] = ((unique_id >> (i * 4U)) & 0x0FU) + '0';
/* If the character is something above 9, then add the offset to make it ASCII A-F */ /* If the character is something above 9, then add the offset to make it ASCII A-F */
if (s[i] > '9') if (s[7U - i] > '9')
s[i] += 16; /* 'A' - '9' = 17, less 1 gives 16. */ s[7U - i] += 16; /* 'A' - '9' = 17, less 1 gives 16. */
} }
#elif DFU_SERIAL_LENGTH == 13 #elif DFU_SERIAL_LENGTH == 13
/* Use the same serial number as the ST DFU Bootloader.*/ /* Use the same serial number as the ST DFU Bootloader.*/
@ -44,17 +42,18 @@ char *serial_no_read(char *s)
# endif # endif
sprintf(s, "%04X%04X%04X", uid[1] + uid[5], uid[0] + uid[4], uid[offset]); sprintf(s, "%04X%04X%04X", uid[1] + uid[5], uid[0] + uid[4], uid[offset]);
#elif DFU_SERIAL_LENGTH == 25 #elif DFU_SERIAL_LENGTH == 25
uint32_t unique_id[3]; volatile uint32_t *unique_id_p = (uint32_t *)DESIG_UNIQUE_ID_BASE;
memcpy(unique_id, (char *)DESIG_UNIQUE_ID_BASE, 12); uint32_t unique_id = 0;
for(size_t i = 0; i < 8U; ++i) {
s[ 7U - i] = ((unique_id[0] >> (i * 4)) & 0x0F) + '0';
s[15U - i] = ((unique_id[1] >> (i * 4)) & 0x0F) + '0';
s[23U - i] = ((unique_id[2] >> (i * 4)) & 0x0F) + '0';
}
for (size_t i = 0; i < 24U; ++i) { for (size_t i = 0; i < 24U; ++i) {
const size_t chunk = i >> 3U;
const size_t nibble = i & 7U;
if (nibble == 0)
unique_id = unique_id_p[chunk];
s[chunk + (7U - nibble)] = ((unique_id >> (i * 4)) & 0x0F) + '0';
/* If the character is something above 9, then add the offset to make it ASCII A-F */ /* If the character is something above 9, then add the offset to make it ASCII A-F */
if (s[i] > '9') if (s[chunk + (7U - nibble)] > '9')
s[i] += 16; /* 'A' - '9' = 17, less 1 gives 16. */ s[chunk + (7U - nibble)] += 16; /* 'A' - '9' = 17, less 1 gives 16. */
} }
#else #else
# WARNING "Unhandled DFU_SERIAL_LENGTH" # WARNING "Unhandled DFU_SERIAL_LENGTH"