cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F413 flash erase doesn't work first time

Nix Wayne
Associate III

Hi,

I am using STM32F413 Nucleo board. I am using MDK5 IDE and HAL libraries. I made an example for reading/writing internal flash based upon a cube project.

The idea is to erase interal flash and then to write some data to it. I alreay did some similar porjects but with F0 and F3 series so I am not newbie in that field.

However what I am seeing here is that the first time I load application to MCU and run it it won't erase flash. Then I break it, reset and re-run it and it will erase and program everything perfectly. And from this point on it will always work fine.

To sumarize. Flash erase is not working only the first time right after I load application into flash. Flash unlock returns HAL_OK while flash erase function returns HAL_ERROR. After I break it, press reset and run it again it will work without a problem (no HAL_ERROR for flash function).

I am visualizing data in Memory 1 window where I can see how data is changing.

So my code excerpt is as follows:

HAL_FLASH_Unlock();
 
EraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInit.Sector = StartSector; //Specify start sector number
EraseInit.NbSectors = NumOfSectors; //Specify num of sectors
status = HAL_FLASHEx_Erase(&EraseInit, &SectorError) == HAL_OK;
 
...
 
// Do some data programing...
HAL_FLASH_Program(....)
 
... 
 
HAL_FLASH_Lock();

Again, it only happens first time right after I load application into MCU... after first reset/break it will always work ok.

Any idea? Could this be IDE problem?

Thank you in advance.

Nix

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Could be an error from the previous flash operation done by the debugger. Look at FLASH_SR prior to calling HAL_FLASHEx_Erase and see if the error is there.

If so, clear the error flag by writing 1 to the corresponding bit or with __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR).

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

View solution in original post

9 REPLIES 9
TDK
Guru

What is the error code reported in pFlash.ErrorCode? Gotta be one or more of these:

#define HAL_FLASH_ERROR_RD           0x00000001U    /*!< Read Protection error         */
#define HAL_FLASH_ERROR_PGS          0x00000002U    /*!< Programming Sequence error    */
#define HAL_FLASH_ERROR_PGP          0x00000004U    /*!< Programming Parallelism error */
#define HAL_FLASH_ERROR_PGA          0x00000008U    /*!< Programming Alignment error   */
#define HAL_FLASH_ERROR_WRP          0x00000010U    /*!< Write protection error        */
#define HAL_FLASH_ERROR_OPERATION    0x00000020U    /*!< Operation Error               */

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

Hi,

It fails in function Flash_WaitForLastOperation called by HAL_FLASHEx_Erase with error HAL_FLASH_ERROR_PGP.

TDK
Guru

Okay, so look in the reference manual to determine what that means.

The write access type (byte, half-word, word or double word) must correspond to the type of

parallelism chosen (x8, x16, x32 or x64). If not, the write operation is not performed and a

program parallelism error flag (PGPERR) is set in the FLASH_SR register.

Looks like you are not setting PSIZE explicitly in FLASH_CR but are instead assuming it has a given value.

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

Huh... The problem is that I don't even get to the point where PSIZE is set. See attached image. As I wrote in a post above it fails in function Flash_WaitForLastOperation (marked with red on the attached image)... therefore it never comes to the point where PSIZE is set in FLASH_Erase_Sector function. 0693W00000Dq99rQAB.png

TDK
Guru

Could be an error from the previous flash operation done by the debugger. Look at FLASH_SR prior to calling HAL_FLASHEx_Erase and see if the error is there.

If so, clear the error flag by writing 1 to the corresponding bit or with __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR).

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

I followed your idea and found out that PGSERR was always set therfore FLASH_WaitForLastOperation failed everytime... So what is now even more strange... I manually cleared that register and since now everything works just fine and PGSERR never gets set again. Even if I remove my code to manually clear it every time. I made the power off to the nucleo board and it still works... It so strange... it looks like PGSERR was somehow stuck and with my input I forced it to start working again... like in mechanical world - funny though.. Anyways now it's working and apparently it looks very much related to the IDE. Many thanks TDK for helping me out with this one. Much appreciated!!

Guillaume K
ST Employee

Hello

just curious: do you use a special linker file (.sct file) in your MDK-ARM project, or do you use default project config ? ("use memory layout from target dialog" in linker config)

if you have special memory regions the startup code running before main() could try to initialize Flash memory areas , thus causing flash programming errors.

Nix Wayne
Associate III

Hi. Sorry for my later reply. I use default project config - nothing special. As generated by CubeMX.

AWolf.8
Associate III

I'm having the same problem. I believe it is a debugger interaction. If I single step in the debugger or if I just run it on a board without the debugger, the error goes away.