From a5aa60f3114aed3c3b4a085a56ef8b49a3565c57 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 7 Nov 2025 14:00:42 -0800 Subject: [PATCH] Fix for the STM32 internal flash erase page selection mask (some variants have larger sizes) --- hal/stm32c0.c | 4 ++-- hal/stm32g0.c | 4 ++-- hal/stm32h5.h | 4 ++++ hal/stm32l5.h | 2 +- hal/stm32u5.h | 7 ++++++- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/hal/stm32c0.c b/hal/stm32c0.c index 7a0a9f53fa..ceb8231fc2 100644 --- a/hal/stm32c0.c +++ b/hal/stm32c0.c @@ -95,8 +95,8 @@ #define FLASH_CR_PG (1 << 0) /* RM0490 - 3.7.5 - FLASH_CR */ #define FLASH_CR_SEC_PROT (1 << 28) /* RM0490 - 3.7.5 - FLASH_CR */ -#define FLASH_CR_PNB_SHIFT 3 /* RM0490 - 3.7.5 - FLASH_CR - PNB bits 8:3 */ -#define FLASH_CR_PNB_MASK 0x3f /* RM0490 - 3.7.5 - FLASH_CR - PNB bits 8:3 - 6 bits */ +#define FLASH_CR_PNB_SHIFT 3 /* RM0490 - 4.7.5 - FLASH_CR - PNB bits 9:3 */ +#define FLASH_CR_PNB_MASK 0x7f /* RM0490 - 4.7.5 - FLASH_CR - PNB bits 9:3 - 7 bits */ #define FLASH_SECR_SEC_SIZE_POS (0U) #define FLASH_SECR_SEC_SIZE_MASK (0xFF) diff --git a/hal/stm32g0.c b/hal/stm32g0.c index d26436cc63..d4e7b31668 100644 --- a/hal/stm32g0.c +++ b/hal/stm32g0.c @@ -87,8 +87,8 @@ #define FLASH_CR_PG (1 << 0) /* RM0444 - 3.7.5 - FLASH_CR */ #define FLASH_CR_SEC_PROT (1 << 28) /* RM0444 - 3.7.5 - FLASH_CR */ -#define FLASH_CR_PNB_SHIFT 3 /* RM0444 - 3.7.5 - FLASH_CR - PNB bits 8:3 */ -#define FLASH_CR_PNB_MASK 0x3f /* RM0444 - 3.7.5 - FLASH_CR - PNB bits 8:3 - 6 bits */ +#define FLASH_CR_PNB_SHIFT 3 /* RM0444 - 3.7.5 - FLASH_CR - PNB bits 9:3 */ +#define FLASH_CR_PNB_MASK 0x7f /* RM0444 - 3.7.5 - FLASH_CR - PNB bits 9:3 - 7 bits */ #define FLASH_SECR_SEC_SIZE_POS (0U) #define FLASH_SECR_SEC_SIZE_MASK (0xFF) diff --git a/hal/stm32h5.h b/hal/stm32h5.h index 95e203ebf0..57282f2a23 100644 --- a/hal/stm32h5.h +++ b/hal/stm32h5.h @@ -293,6 +293,10 @@ #define FLASH_CR_BER (1 << 3) #define FLASH_CR_FW (1 << 4) #define FLASH_CR_STRT (1 << 5) +/* Page number selection: + * Up to 31 pages: H523/33xx + * Up to 127 pages: All others + */ #define FLASH_CR_PNB_SHIFT 6 #define FLASH_CR_PNB_MASK 0x7F #define FLASH_CR_MER (1 << 15) diff --git a/hal/stm32l5.h b/hal/stm32l5.h index b080d89b12..0981ef29ab 100644 --- a/hal/stm32l5.h +++ b/hal/stm32l5.h @@ -203,7 +203,7 @@ #define FLASH_CR_PER (1 << 1) #define FLASH_CR_MER1 (1 << 2) #define FLASH_CR_PNB_SHIFT 3 -#define FLASH_CR_PNB_MASK 0x7F +#define FLASH_CR_PNB_MASK 0x7F /* up to 127 pages */ #define FLASH_CR_BKER (1 << 11) #define FLASH_CR_MER2 (1 << 15) #define FLASH_CR_STRT (1 << 16) diff --git a/hal/stm32u5.h b/hal/stm32u5.h index 8578938bf1..b26975dd25 100644 --- a/hal/stm32u5.h +++ b/hal/stm32u5.h @@ -209,8 +209,13 @@ #define FLASH_CR_PG (1 << 0) #define FLASH_CR_PER (1 << 1) #define FLASH_CR_MER1 (1 << 2) +/* Page number selection: + * Up to 31 pages: U535/U545 + * Up to 127 pages: U575/U585 + * Up to 255 pages: U59x/5Ax/5Fx/5Gx + */ #define FLASH_CR_PNB_SHIFT 3 -#define FLASH_CR_PNB_MASK 0x7F +#define FLASH_CR_PNB_MASK 0xFF /* support up to 255 pages */ #define FLASH_CR_BKER (1 << 11) #define FLASH_CR_MER2 (1 << 15) #define FLASH_CR_STRT (1 << 16)