Showing results for 
Search instead for 
Did you mean: 

STM32L series first write to EEPROM fails

Associate II

I have an STM32L073CZ that I'm having an occasional error with. I'm using the HAL libraries to program EEPROM and a number of our MCUs are seeing errors where they fail to write the first word. After that it'll start working correctly and everything is fine, but the first time always fails.

Stepping through the code I can see that I'm successfully unlocking flash and that there are no pending operations. It's in the flag check that's I'm getting errors:


From FLASH_WaitForLastOperation that is called before the write operation in HAL_FLASHEx_DATAEEPROM_Program.

Examining the SFR's I can see that:

  1. WRPERR == 1
  2. PGAERR == 0
  3. SIZE_ERR == 0
  4. OPTV_ERR == 0
  5. RDERR == 0
  6. FWWERR == 0
  7. NO_ZERO_ERROR == 0

So the write protection isn't being cleared.

According to the documentation (RM0367, page 85), when writing to data eeprom you can get this error if:

WRPERR is set to 1 (and the write operation is not executed) if PELOCK = 1 or if the

memory is read-out protected.

Since the memory isn't read out protected it must be that the PELOCK is 1. However before calling the write I am making sure to clear the PELOCK using:


On some of my devices I do have external read out protection enabled via the firewall, but not all including the devices I'm testing with. So this shouldn't be the issue. Also if the read protection was enabled and causing problems the device should hard fault.

More details are given later:

Write protection error flag (WRPERR)

If an erase/program operation to a write-protected page of the Flash program memory and

data EEPROM is launched, the Write Protection Error flag (WRPERR) is set in the

FLASH_SR register. Consequently, the WRPERR flag is set when the software tries to:

• Write to a WRP page.

• Write to a System memory page or to factory option bytes.

• Write to the Flash program memory, data EEPROM or Option bytes if they are not

unlocked by PEKEY, PRGKEY or OPTKEY.

• Write to the Flash program memory, data EEPROM or Option bytes when the RDP

Option byte is set and t

What ends up being the reason is that, or at least when the write protection error is set, is when doing a custom de-init on the CRC module:

	WRITE_REG(hcrc.Instance->DR, 0xFFFFFFFF);
	WRITE_REG(hcrc.Instance->IDR, 0U);
	WRITE_REG(hcrc.Instance->CR, 0U);
	WRITE_REG(hcrc.Instance->INIT, 0xFFFFFFFF);
	WRITE_REG(hcrc.Instance->POL, 0x4c11db7);

This triggers the WRPERR flag.

Does anyone know why that is? This isn't flash related so it shouldn't have the same problems...