Skip to content

Commit

Permalink
cpu/stm32_common/flashpage: force waiting for pending operations
Browse files Browse the repository at this point in the history
This commit fixes the flashpage not working on iotlab-m3.
  • Loading branch information
aabadie committed May 27, 2018
1 parent a93b4ab commit f120107
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions cpu/stm32_common/periph/flashpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ static void _unlock_flash(void)
#endif
}

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)
Expand All @@ -81,8 +90,8 @@ static void _erase_page(void *page_addr)
_unlock_flash();

/* 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;
Expand All @@ -97,8 +106,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);
Expand Down Expand Up @@ -151,7 +161,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 */
Expand Down

0 comments on commit f120107

Please sign in to comment.