From 30d88452e6e360ef5f321646cafdbcb98c0e8f4e Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Tue, 10 Jul 2018 14:16:24 +0000 Subject: [PATCH] stm32f4: flash: support discontinuous sectors on banked flash The sector list is numerically contiguous, but the bits to write are not contiguous. Good job ST. Fixes: https://github.com/libopencm3/libopencm3/pull/934 Don't see any reason to make public definitions of these sorts of magic numbers, they're completely internal to sector addressing. --- lib/stm32/common/flash_common_f24.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/stm32/common/flash_common_f24.c b/lib/stm32/common/flash_common_f24.c index 000abff9..cd176cbe 100644 --- a/lib/stm32/common/flash_common_f24.c +++ b/lib/stm32/common/flash_common_f24.c @@ -359,6 +359,11 @@ void flash_erase_sector(uint8_t sector, uint32_t program_size) flash_wait_for_last_operation(); flash_set_program_size(program_size); + /* Sector numbering is not contiguous internally! */ + if (sector >= 12) { + sector += 4; + } + FLASH_CR &= ~(FLASH_CR_SNB_MASK << FLASH_CR_SNB_SHIFT); FLASH_CR |= (sector & FLASH_CR_SNB_MASK) << FLASH_CR_SNB_SHIFT; FLASH_CR |= FLASH_CR_SER;