Skip to content

Commit

Permalink
Reset flash MER bit after mass erase
Browse files Browse the repository at this point in the history
Otherwise all subsequent erases before turning off the power will be mass erases (#489)
  • Loading branch information
night-ghost authored and xor-gate committed Dec 30, 2016
1 parent c441824 commit e761a47
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ static void __attribute__((unused)) clear_flash_cr_per(stlink_t *sl) {
stlink_write_debug32(sl, FLASH_CR, n);
}

static void set_flash_cr_mer(stlink_t *sl) {
static void set_flash_cr_mer(stlink_t *sl, bool v) {
uint32_t val, cr_reg, cr_mer, cr_pg;

if (sl->flash_type == STLINK_FLASH_TYPE_F4) {
Expand All @@ -322,7 +322,10 @@ static void set_flash_cr_mer(stlink_t *sl) {
stlink_write_debug32(sl, cr_reg, val);
}

val |= cr_mer;
if(v)
val |= cr_mer;
else
val &= ~cr_mer;
stlink_write_debug32(sl, cr_reg, val);
}

Expand Down Expand Up @@ -1582,7 +1585,7 @@ int stlink_erase_flash_mass(stlink_t *sl) {
unlock_flash_if(sl);

/* set the mass erase bit */
set_flash_cr_mer(sl);
set_flash_cr_mer(sl,1);

/* start erase operation, reset by hw with bsy bit */
set_flash_cr_strt(sl);
Expand All @@ -1593,6 +1596,9 @@ int stlink_erase_flash_mass(stlink_t *sl) {
/* relock the flash */
lock_flash(sl);

/* reset the mass erase bit */
set_flash_cr_mer(sl,0);

/* todo: verify the erased memory */
}
return 0;
Expand Down

0 comments on commit e761a47

Please sign in to comment.