diff --git a/firmware/hw_layer/ports/stm32/flash_int.c b/firmware/hw_layer/ports/stm32/flash_int.c index 0a4de81967..625f1fbc3b 100644 --- a/firmware/hw_layer/ports/stm32/flash_int.c +++ b/firmware/hw_layer/ports/stm32/flash_int.c @@ -29,14 +29,14 @@ #define FLASH_BASE 0x08100000 // QW bit supercedes the older BSY bit - #define intFlashWaitWhileBusy() { while (FLASH_SR & FLASH_SR_QW) {} } + #define intFlashWaitWhileBusy() do { __DSB(); } while (FLASH_SR & FLASH_SR_QW); #else #define FLASH_CR FLASH->CR #define FLASH_SR FLASH->SR #define FLASH_KEYR FLASH->KEYR // Wait for the flash operation to finish - #define intFlashWaitWhileBusy() { while (FLASH->SR & FLASH_SR_BSY) {} } + #define intFlashWaitWhileBusy() do { __DSB(); } while (FLASH->SR & FLASH_SR_BSY); #endif flashaddr_t intFlashSectorBegin(flashsector_t sector) { @@ -84,7 +84,25 @@ static bool intFlashUnlock(void) { */ #define intFlashLock() { FLASH_CR |= FLASH_CR_LOCK; } +#ifdef STM32F7XX +static bool isDualBank(void) { + // cleared bit indicates dual bank + return (FLASH->OPTCR & FLASH_OPTCR_nDBANK) == 0; +} +#endif + int intFlashSectorErase(flashsector_t sector) { +#ifdef STM32F7XX + // On dual bank STM32F7, sector index doesn't match register value. + // High bit indicates bank, low 4 bits indicate sector within bank. + // Since each bank has 12 sectors, increment second-bank sector idx + // by 4 so that the first sector of the second bank (12) ends up with + // index 16 (0b10000) + if (isDualBank() && sector >= 12) { + sector += 4; + } +#endif + /* Unlock flash for write access */ if (intFlashUnlock() == HAL_FAILED) return FLASH_RETURN_NO_PERMISSION; diff --git a/firmware/hw_layer/ports/stm32/stm32f7/mpu_util.cpp b/firmware/hw_layer/ports/stm32/stm32f7/mpu_util.cpp index 7f1f99f117..f9580e58ac 100644 --- a/firmware/hw_layer/ports/stm32/stm32f7/mpu_util.cpp +++ b/firmware/hw_layer/ports/stm32/stm32f7/mpu_util.cpp @@ -60,6 +60,13 @@ size_t flashSectorSize(flashsector_t sector) { return flashSectorSize(sector - 12); } + // On 1MB devices, sectors 8-11 don't exist, therefore have zero size. + if (flashSize() == 1024) { + if (sector > 7 && sector < 12) { + return 0; + } + } + // Pages are twice the size when in single bank mode size_t dbMul = isDualBank() ? 1 : 2;