cancel
Showing results for 
Search instead for 
Did you mean: 

Why error = FLASH->SR is set and block erasing flash memory?

Fabsch
Associate II

In my code i try to delete a flash mem page using

  if(HAL_FLASHEx_Erase(&FlashErase,&PageError) != HAL_OK)

but always got an HAL_ERROR because in this function (which is called in HAL_FLASHEx_Erase...)

HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
{
  uint32_t error;
  uint32_t tickstart = HAL_GetTick();
 
  /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
     Even if the FLASH operation fails, the BUSY flag will be reset and an error
     flag will be set */
  while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  {
    if ((HAL_GetTick() - tickstart) >= Timeout)
    {
      return HAL_TIMEOUT;
    }
  }
 
  /* check flash errors. Only ECC correction can be checeked here as ECCD
      generates NMI */
  error = FLASH->SR;
 
  /* Check FLASH End of Operation flag */
  if ((error & FLASH_FLAG_EOP) != 0U)
  {
    /* Clear FLASH End of Operation pending bit */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  }
 
  /* Now update error variable to only error value */
  error &= FLASH_FLAG_SR_ERROR;
 
  /* Update error with ECC error value */
  error |= (FLASH->ECCR & FLASH_FLAG_ECCC);
 
  /* clear error flags */
  __HAL_FLASH_CLEAR_FLAG(error);
 
  if (error != 0U)
  {
    /*Save the error code*/
    pFlash.ErrorCode = error;
 
    return HAL_ERROR;
  }
 
  /* Wait for control register to be written */
  while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_CFGBSY))
  {
    if ((HAL_GetTick() - tickstart) >= Timeout)
    {
      return HAL_TIMEOUT;
    }
  }

error = FLASH->SR sets error to 0x8000.

In the datasheet this part is marked as reserved so why FLASH-SR sets this error bit and blocks the erase process?

Thank you all for your comments!

11 REPLIES 11

Hello @Le Corre Pierre​ ,

I did not buy the STM32WBs myself, it's my manufacturer that did it. If it's important I can ask them where did they get them, but the answer should be Farnell / Avnet / Arrow or something like that. To give you an idea, the cards were manufactured about a month ago.

The exact marking is:

STM32WB55

CGU6

GQ21K149R

CHN GQ 003

The STM32 Cube Firmware is the latest one, STM32Cube_FW_WB_V1.7.0.

I use Keil µVision as IDE and programmer, However I did use the STM32CubeProgrammer to flash a wireless stack on the M0 coprocessor ("stm32wb5x_BLE_Stack_full_fw.bin" from "STM32Cube_FW_WB_V1.7.0\Projects\STM32WB_Copro_Wireless_Binaries\STM32WB5x\", flashed at address 0x080CB000). Cube Programmer is version 2.4.0 (latest version is 2.5 if I recall correctly)

Regarding the OB: I do not recall changing them, but anyway, I copied them at the end of my message, as marked in cubeProgrammer.

As suggested, I flashed an example program provided in the Firmware Package ("STM32Cube_FW_WB_V1.7.0\Projects\P-NUCLEO-WB55.Nucleo\Examples\FLASH\FLASH_EraseProgram\") compiled with Keil. However this program has this line n°123:

 /* Clear OPTVERR bit set on virgin samples */
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

So of course the program does work. Before executing this line, the SR->OPTVERR bit is set to 1. If I remove this line, the behavior is the same as in my software.

Thank you for your time,

Cheers,

Xavier

Option bytes:

RDP = AA

BOR_LEV = 0

nBOOT0 = 1

nBoot1 = 1

nSWBOOT0 = 0

SRAM2RST = 0

SRAM2PE = 1

nRST_STOP = 1

nRST_STDBY = 1

nRSTSHDW = 1

WWDGSW = 1

IWGDSTDBY = 1

IWDGSTOP = 1

IWDGSW = 1

ESE = 1

SFSA = 0xcb

FSD = 0

DDS = 1

C2OPT = 1

NBRSD = 0

SNBRSA = 0xf

BRSD = 0

SBRSA = 0xa

SBRV = 0x32c00

PCROP1A_STRT = 0x1ff @ = 0x8000ff8

PCROP1A_END = 0x0 @ = 0x8000008

PCROP_RDP = 0

PCROP1A_STRT = 0x1ff @ = 0x8000ff8

PCROP1B_END = 0x0 @ = 0x8000008

WRP1A_STRT = 0xff @ = 0x80ff000

WRP1A_END = 0x0 @ = 0x8000000

WRP1B_STRT = 0xff @ = 0x80ff000

WRP1B_END = 0x0 @ = 0x8000000

juergen23
Associate II

Hi,

i found this old thread and i have a question.

I'm using a STM32L151 now for several months for measurement of some data to calculate a flow value.

I use the EEPROM to save some settings and load them when i restart the system.

All works fine, but then the OPTVERRUSR-Flag was set and i couldn't accesss the eeprom any more.

After resetting the flag all works fine.

When is this flag set by the hardware?

The reference manual says "If option have not loaded properly...", but which options are meant?

So i can reset the flag every time at startup when it is set, but i wan't to understand when and why this flag is set.

Thanks in advance

Juergen