From b80691fe276c898bd655b91057b121601c88796c Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Wed, 16 May 2018 22:10:32 +0200 Subject: [PATCH] cpu/stm32_common/flashpage: force waiting for pending operations This commit fixes the flashpage not working on iotlab-m3. --- cpu/stm32_common/periph/flashpage.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cpu/stm32_common/periph/flashpage.c b/cpu/stm32_common/periph/flashpage.c index bb6324049477..fb314cd3f0ad 100644 --- a/cpu/stm32_common/periph/flashpage.c +++ b/cpu/stm32_common/periph/flashpage.c @@ -79,6 +79,15 @@ static void _lock(void) } } +static void _wait_for_pending_operations(void) +{ + DEBUG("[flashpage] waiting for any pending operation to finish\n"); + while (FLASH->SR & FLASH_SR_BSY) {} + if (FLASH->SR & FLASH_SR_EOP) { + FLASH->SR &= ~(FLASH_SR_EOP); + } +} + static void _erase_page(void *page_addr) { #if defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1) @@ -95,8 +104,8 @@ static void _erase_page(void *page_addr) _unlock(); /* make sure no flash operation is ongoing */ - DEBUG("[flashpage] erase: waiting for any operation to finish\n"); - while (FLASH->SR & FLASH_SR_BSY) {} + _wait_for_pending_operations(); + /* set page erase bit and program page address */ DEBUG("[flashpage] erase: setting the erase bit\n"); CNTRL_REG |= FLASH_CR_PER; @@ -111,8 +120,9 @@ static void _erase_page(void *page_addr) DEBUG("[flashpage] erase: trigger the page erase\n"); CNTRL_REG |= FLASH_CR_STRT; #endif - DEBUG("[flashpage] erase: wait as long as device is busy\n"); - while (FLASH->SR & FLASH_SR_BSY) {} + /* wait as long as device is busy */ + _wait_for_pending_operations(); + /* reset PER bit */ DEBUG("[flashpage] erase: resetting the page erase bit\n"); CNTRL_REG &= ~(FLASH_CR_PER); @@ -165,7 +175,8 @@ void flashpage_write_raw(void *target_addr, const void *data, size_t len) for (size_t i = 0; i < (len / FLASHPAGE_DIV); i++) { DEBUG("[flashpage_raw] writing %c to %p\n", (char)data_addr[i], dst); *dst++ = data_addr[i]; - while (FLASH->SR & FLASH_SR_BSY) {} + /* wait as long as device is busy */ + _wait_for_pending_operations(); } /* clear program bit again */