cancel
Showing results for 
Search instead for 
Did you mean: 

When 'FLASH_ERROR_PROGRAM' error is fired? and how to leave from 'FLASH_ERROR_PROGRAM' status?

Jjeon.21
Associate II

Hello.

I discovered a weird phenomenon that some flash memory of the board that I have is always the last status is 'FLASH_ERROR_PROGRAM'.

The MCU of the board is STM32L152VD.

I download my code that checking the last status of flash memory. (It used ST Periph library.)

The code is as below. Check the last status of flash memory and if the last status is a FLASH_ERROR_PROGRAM then on LED.

void Test()
{
    FLASH_Status status = FLASH_GetStatus();
    if(status == FLASH_ERROR_PROGRAM) LED::AllOn();
}

Many boards are normal (no FLASH_ERROR_PROGRAM status) but some boards are always FLASH_ERROR_PROGRAM status.

I want to know why 'FLASH_ERROR_PROGRAM' error is fired and how to leave from 'FLASH_ERROR_PROGRAM' status.

Any advice would be much appreciated.

Thanks for reading.

5 REPLIES 5
TDK
Guru

It will return FLASH_ERROR_PROGRAM if a variety of different bits are set.

https://github.com/TPYBoard/MAX31865/blob/0b00cd7e78b25b223907bde77a4012485b08372e/project/Libraries/STM32L1xx_StdPeriph_Driver/src/stm32l1xx_flash.c#L1276

You can read FLASH_SR yourself to determine the exact reason.

It's probably better to figure out why you've gotten into the error state and fix that.

If you feel a post has answered your question, please click "Accept as Solution".
Jjeon.21
Associate II

Thank you for your answer.

I checked FLASH_SR value. The value was 0x180C.

The problem is that the init status of the flash memory is 0x180C.

in other words, I checked the flash memory status value as soon as starting code as below and the value was 0x180C.

void main()
{
    uint32_t status = FLASH->SR; // 0x180C
}

After rebooting the value is always 0x180C.

I tried to initialize FLASH->SR value to 0x00 but still, the value is not changed.

Is it possible that a circuit problem is causing this problem?​

Any advice would be much appreciated.

Thank you.​

TDK
Guru

Okay, so the OPTVERR (option validity error flag) is set. That means one or more of your option byte settings are invalid. Check those and correct as necessary.

If you feel a post has answered your question, please click "Accept as Solution".
Jjeon.21
Associate II

I solved this problem thank you.

My solution is as below.

  1. It unlocks the flash register
  2. It set 0x1f0f value to FLASH->SR to clear all bits.
  3. It waits slightly.
  4. ​It erases the flash memory.
  5. It does the writing work if need.
  6. It locks the flash register.

The main point is to set 0x1f0f value to FLASH->SR before erase the flash memory to clear all bits.

The example code is as below.

Thanks.

void Test()
{
	FLASH_Unlock();
	
	FLASH_ClearFlag(0x1f0f);
	FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
 
	if(FLASH_ErasePage(startAddr+(this->pageSize*i)) == FLASH_COMPLETE)
            LED::AllOn();
 
	FLASH_Lock();
}

TDK
Guru

This is the equivalent to putting a piece of tape over your "check engine" light instead of taking it to a mechanic. But at least you won't see the check engine light anymore.

If you feel a post has answered your question, please click "Accept as Solution".