diff --git a/drivers/flash/flash_stm32.h b/drivers/flash/flash_stm32.h index dd8628f81d7..e0fba89a550 100644 --- a/drivers/flash/flash_stm32.h +++ b/drivers/flash/flash_stm32.h @@ -257,6 +257,12 @@ static inline bool flash_stm32_range_exists(const struct device *dev, } #endif /* CONFIG_FLASH_PAGE_LAYOUT */ +static inline bool flash_stm32_valid_write(off_t offset, uint32_t len) +{ + return ((offset % FLASH_STM32_WRITE_BLOCK_SIZE == 0) && + (len % FLASH_STM32_WRITE_BLOCK_SIZE == 0U)); +} + bool flash_stm32_valid_range(const struct device *dev, off_t offset, uint32_t len, bool write); diff --git a/drivers/flash/flash_stm32l5x.c b/drivers/flash/flash_stm32l5x.c index a0b692b45c8..b4381289ad1 100644 --- a/drivers/flash/flash_stm32l5x.c +++ b/drivers/flash/flash_stm32l5x.c @@ -128,7 +128,7 @@ static int icache_wait_for_invalidate_complete(void) #endif /* CONFIG_SOC_SERIES_STM32H5X */ /* - * offset and len must be aligned on 8 for write, + * offset and len must be aligned on write-block-size for write, * positive and not beyond end of flash */ bool flash_stm32_valid_range(const struct device *dev, off_t offset, @@ -149,8 +149,10 @@ bool flash_stm32_valid_range(const struct device *dev, off_t offset, } } - return (!write || (offset % 8 == 0 && len % 8 == 0U)) && - flash_stm32_range_exists(dev, offset, len); + if (write && !flash_stm32_valid_write(offset, len)) { + return false; + } + return flash_stm32_range_exists(dev, offset, len); } static int write_dword(const struct device *dev, off_t offset, uint64_t val)