Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpu/stm32-common: slightly rework flashpage driver and fix iotlab-m3 #9068

Merged
merged 4 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cpu/stm32_common/periph/flash_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@

void _unlock(void)
{
DEBUG("[flash-common] unlocking the flash module\n");
if (CNTRL_REG & CNTRL_REG_LOCK) {
DEBUG("[flash-common] unlocking the flash module\n");
KEY_REG = FLASH_KEY1;
KEY_REG = FLASH_KEY2;
}
}

void _lock(void)
{
DEBUG("[flash-common] locking the flash module\n");
CNTRL_REG |= CNTRL_REG_LOCK;
if (!(CNTRL_REG & CNTRL_REG_LOCK)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there still a case where this is necessary ?
But maybe this would also be needed to ask it for _unlock.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least 'test 255' and 'write_raw 0x807f808 abcdefgh' work on M3 nodes without both protections.

But there may be good reasons for it just asking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is the same check in the _unlock function, I wanted it to be consistent. The idea is "if already locked, do nothing".

DEBUG("[flash-common] locking the flash module\n");
CNTRL_REG |= CNTRL_REG_LOCK;
}
}
33 changes: 23 additions & 10 deletions cpu/stm32_common/periph/flashpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ 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) {}

/* Clear 'end of operation' bit in status register */
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 @@ -77,12 +88,12 @@ static void _erase_page(void *page_addr)
stmclk_enable_hsi();
#endif

/* unlock the flash module */
/* unlock the flash module */
_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 +108,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 @@ -140,25 +152,26 @@ void flashpage_write_raw(void *target_addr, const void *data, size_t len)
stmclk_enable_hsi();
#endif

DEBUG("[flashpage_raw] unlocking the flash module\n");
/* unlock the flash module */
_unlock_flash();

DEBUG("[flashpage] write: now writing the data\n");
DEBUG("[flashpage_raw] write: now writing the data\n");
#if !(defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1))
/* set PG bit and program page to flash */
CNTRL_REG |= FLASH_CR_PG;
#endif
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 */
CNTRL_REG &= ~(FLASH_CR_PG);
DEBUG("[flashpage] write: done writing data\n");
DEBUG("[flashpage_raw] write: done writing data\n");

DEBUG("flashpage_raw] now locking the flash module again\n");
/* lock the flash module again */
_lock();

#if !(defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1))
Expand Down