From 209b4e6cf100b4ed970a80db362c6aad0f8c10ac Mon Sep 17 00:00:00 2001 From: dragonmux Date: Mon, 6 Jun 2022 07:10:45 -0400 Subject: [PATCH 1/3] stm32/serialno: Cleaned up the serial number handling code which is tripping GCC 11 up due to a compiler regression --- src/platforms/stm32/serialno.c | 42 ++++++++++++++++------------------ 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/platforms/stm32/serialno.c b/src/platforms/stm32/serialno.c index a8b2898ba3a..a5c03efa578 100644 --- a/src/platforms/stm32/serialno.c +++ b/src/platforms/stm32/serialno.c @@ -23,44 +23,42 @@ char *serial_no_read(char *s) { #if DFU_SERIAL_LENGTH == 9 - int i; - volatile uint32_t *unique_id_p = (volatile uint32_t *)DESIG_UNIQUE_ID_BASE; - uint32_t unique_id = *unique_id_p + + const volatile uint32_t *const unique_id_p = (volatile uint32_t *)DESIG_UNIQUE_ID_BASE; + const uint32_t unique_id = *unique_id_p + *(unique_id_p + 1) + *(unique_id_p + 2); /* Fetch serial number from chip's unique ID */ - for(i = 0; i < 8; i++) { - s[7-i] = ((unique_id >> (4*i)) & 0xF) + '0'; + for(size_t i = 0; i < 8U; ++i) { + 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 (s[i] > '9') + s[i] += 16; /* 'A' - '9' = 17, less 1 gives 16. */ } - for(i = 0; i < 8; i++) - if(s[i] > '9') - s[i] += 'A' - '9' - 1; #elif DFU_SERIAL_LENGTH == 13 /* Use the same serial number as the ST DFU Bootloader.*/ - uint16_t *uid = (uint16_t *)DESIG_UNIQUE_ID_BASE; + const uint16_t *const uid = (uint16_t *)DESIG_UNIQUE_ID_BASE; # if defined(STM32F4) || defined(STM32F7) int offset = 3; # elif defined(STM32L0) || defined(STM32F0) || defined(STM32F3) int offset = 5; # 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 - int i; uint32_t unique_id[3]; - memcpy(unique_id, (uint32_t *)DESIG_UNIQUE_ID_BASE, 12); - for(i = 0; i < 8; i++) { - s[ 7-i] = ((unique_id[0] >> (4*i)) & 0xF) + '0'; - s[15-i] = ((unique_id[1] >> (4*i)) & 0xF) + '0'; - s[23-i] = ((unique_id[2] >> (4*i)) & 0xF) + '0'; + memcpy(unique_id, (char *)DESIG_UNIQUE_ID_BASE, 12); + 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) { + /* If the character is something above 9, then add the offset to make it ASCII A-F */ + if (s[i] > '9') + s[i] += 16; /* 'A' - '9' = 17, less 1 gives 16. */ } - for (i = 0; i < 24; i++) - if(s[i] > '9') - s[i] += 'A' - '9' - 1; #else # WARNING "Unhandled DFU_SERIAL_LENGTH" #endif - s[DFU_SERIAL_LENGTH - 1] = 0; + s[DFU_SERIAL_LENGTH - 1] = '\0'; return s; } - From a5af5f24da45acb00c99f51a32cce055277da6e5 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Mon, 6 Jun 2022 09:22:02 -0400 Subject: [PATCH 2/3] stm32/serialno: Fixed a couple of mistakes made in the cleanup and attempted a different way to quiet the errant GCC warning --- src/platforms/stm32/serialno.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/platforms/stm32/serialno.c b/src/platforms/stm32/serialno.c index a5c03efa578..574fce0d9ca 100644 --- a/src/platforms/stm32/serialno.c +++ b/src/platforms/stm32/serialno.c @@ -24,15 +24,13 @@ char *serial_no_read(char *s) { #if DFU_SERIAL_LENGTH == 9 const volatile uint32_t *const unique_id_p = (volatile uint32_t *)DESIG_UNIQUE_ID_BASE; - const uint32_t unique_id = *unique_id_p + - *(unique_id_p + 1) + - *(unique_id_p + 2); + const uint32_t unique_id = unique_id_p[0] + unique_id_p[1] + unique_id_p[2]; /* Fetch serial number from chip's unique ID */ for(size_t i = 0; i < 8U; ++i) { 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 (s[i] > '9') - s[i] += 16; /* 'A' - '9' = 17, less 1 gives 16. */ + if (s[7U - i] > '9') + s[7U - i] += 16; /* 'A' - '9' = 17, less 1 gives 16. */ } #elif DFU_SERIAL_LENGTH == 13 /* Use the same serial number as the ST DFU Bootloader.*/ @@ -44,17 +42,18 @@ char *serial_no_read(char *s) # endif sprintf(s, "%04X%04X%04X", uid[1] + uid[5], uid[0] + uid[4], uid[offset]); #elif DFU_SERIAL_LENGTH == 25 - uint32_t unique_id[3]; - memcpy(unique_id, (char *)DESIG_UNIQUE_ID_BASE, 12); - 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'; - } + volatile uint32_t *unique_id_p = (uint32_t *)DESIG_UNIQUE_ID_BASE; + uint32_t unique_id = 0; 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 (s[i] > '9') - s[i] += 16; /* 'A' - '9' = 17, less 1 gives 16. */ + if (s[chunk + (7U - nibble)] > '9') + s[chunk + (7U - nibble)] += 16; /* 'A' - '9' = 17, less 1 gives 16. */ } #else # WARNING "Unhandled DFU_SERIAL_LENGTH" From 029942d5132c24cf862801798611b312aa49362d Mon Sep 17 00:00:00 2001 From: dragonmux Date: Mon, 6 Jun 2022 10:26:33 -0400 Subject: [PATCH 3/3] stm32/serialno: Cleaned up the inconsistencies in how DESIG_UNIQUE_ID_BASE is used in the serialno code --- src/platforms/stm32/serialno.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/platforms/stm32/serialno.c b/src/platforms/stm32/serialno.c index 574fce0d9ca..67776cfca62 100644 --- a/src/platforms/stm32/serialno.c +++ b/src/platforms/stm32/serialno.c @@ -23,7 +23,7 @@ char *serial_no_read(char *s) { #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 = (uint32_t *)DESIG_UNIQUE_ID_BASE; const uint32_t unique_id = unique_id_p[0] + unique_id_p[1] + unique_id_p[2]; /* Fetch serial number from chip's unique ID */ for(size_t i = 0; i < 8U; ++i) { @@ -34,7 +34,7 @@ char *serial_no_read(char *s) } #elif DFU_SERIAL_LENGTH == 13 /* Use the same serial number as the ST DFU Bootloader.*/ - const uint16_t *const uid = (uint16_t *)DESIG_UNIQUE_ID_BASE; + const volatile uint16_t *const uid = (uint16_t *)DESIG_UNIQUE_ID_BASE; # if defined(STM32F4) || defined(STM32F7) int offset = 3; # elif defined(STM32L0) || defined(STM32F0) || defined(STM32F3) @@ -42,7 +42,7 @@ char *serial_no_read(char *s) # endif sprintf(s, "%04X%04X%04X", uid[1] + uid[5], uid[0] + uid[4], uid[offset]); #elif DFU_SERIAL_LENGTH == 25 - volatile uint32_t *unique_id_p = (uint32_t *)DESIG_UNIQUE_ID_BASE; + const volatile uint32_t *const unique_id_p = (uint32_t *)DESIG_UNIQUE_ID_BASE; uint32_t unique_id = 0; for (size_t i = 0; i < 24U; ++i) { const size_t chunk = i >> 3U;