Skip to main content
Glee.11
Associate III
July 7, 2020
Question

stm32f767igt doesn't erase the flash

  • July 7, 2020
  • 17 replies
  • 6605 views

Hi All.

I work with stm32f767igt for using flash.

The code below is erasing and writing flash.

I succeed writing the flash.

0693W000001sH6wQAE.png

But when i try to delete the flash with

HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) ;

and

FLASH_Erase_Sector(FLASH_SECTOR_10 ,FLASH_VOLTAGE_RANGE_3);

it didn't work properly.

So i can't update data in flash because flash doesn't deleted.

What am i doing wrong?

------------------------------------code ---------------------------------------------------------------------------------------

#define FLASH_BASE_ADDR  ADDR_FLASH_SECTOR_10  /* Start @ of user Flash area */

#define FLASH_END_ADDR   (ADDR_FLASH_SECTOR_11-1)  /* End @ of user Flash area */

void flash_erase_write(void)

{

HAL_FLASH_Unlock();

// HAL_FLASH_OB_Unlock();

// HAL_FLASHEx_OBGetConfig(&OBInit);

// #if defined(DUAL_BANK)

// /* Turn on LED3 if FLASH is configured in Dual Bank mode */

// if((OBInit.USERConfig & OB_NDBANK_SINGLE_BANK) == OB_NDBANK_SINGLE_BANK)

// #else

// /* Turn on LED3 if FLASH is configured in Single Bank mode */

// if((OBInit.USERConfig & OB_NDBANK_SINGLE_BANK) == OB_NDBANK_DUAL_BANK)

// #endif

// {

// while(1);

// }

/* Get the 1st sector to erase */

FirstSector = GetSector(FLASH_BASE_ADDR);

/* Get the number of sector to erase from 1st sector*/

NbOfSectors = GetSector(FLASH_END_ADDR) - FirstSector + 1;

/* Fill EraseInit structure*/

EraseInitStruct.TypeErase   = FLASH_TYPEERASE_SECTORS;

EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;

EraseInitStruct.Sector    = FirstSector;

EraseInitStruct.NbSectors   = NbOfSectors;//NbOfSectors;

//FLASH_Erase_Sector(FLASH_SECTOR_10 ,FLASH_VOLTAGE_RANGE_3);

 if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)           

 {

printf("flash erase Error!!!\r\n");

  while (1)

  {

  }

 }

  

Address = FLASH_BASE_ADDR;

#define DATA_32 ((uint32_t)0x12345678)

if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, DATA_32) == HAL_OK)

{

 Address = Address + 4;

}

}

This topic has been closed for replies.

17 replies

TDK
July 7, 2020

Does HAL_FLASHEx_Erase return HAL_OK or some other value?

Double check your arguments in EraseInitStruct. You haven't included enough code to check them here.

Ensure your DUAL_BANK define matches up with what you have configured in the option bytes.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Glee.11
Glee.11Author
Associate III
July 7, 2020

thank you for your quick reply.

Yes. the HAL_FLASHEx_Erase returns HAL_OK otherwise it stuck in while(1).

i didn't set any option realted with DUAL_BANK.

how do i check the option bytes?

Piranha
Principal III
July 7, 2020

Say hello to high-performance Cortex-M7 with cache and idiot made HAL (non-)high-level library. By default FLASH region is configured as write-through. Because of that after write operations no cache management is necessary, but after erase cache invalidation must be executed on the respective region. Typically you need to use SCB_InvalidateDCache_by_Addr().

Glee.11
Glee.11Author
Associate III
July 7, 2020

SCB_InvalidateDCache_by_Addr((uint32_t*)0x080C0000, 131072);

/* flash delete */

 if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)

 {

  /*

   Error occurred while sector erase.

   User can add here some code to deal with this error.

   SECTORError will contain the faulty sector and then to know the code error on this sector,

   user can call function 'HAL_FLASH_GetError()'

  */

  /* Infinite loop */

printf("flash erase Error!!!\r\n");

  while (1)

  {

  }

 }

I program like this, but it didn't work.

How do i use this function correctly?

Piranha
Principal III
July 7, 2020

What do you think the word "after" means?

Glee.11
Glee.11Author
Associate III
July 7, 2020

Yes. . . I added after erase flash()​ ,,too

But , not working

Also​ i have tested on nucleo-144 stm32f767ZIboard, it works.​

Piranha
Principal III
July 7, 2020

"Not working" is not a description of the problem and with that noone can help. Have you looked at flash contents after power cycle? Have you debugged HAL bloatware?

Oh, that bloatware even has multiple copies of the same function... Because of sector number 12, this could also be your problem.

https://github.com/STMicroelectronics/STM32CubeF7/blob/08376dce1b404687e4a86b73077f396bccfc9cb5/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_flash_ex.c#L488

https://github.com/STMicroelectronics/STM32CubeF7/blob/08376dce1b404687e4a86b73077f396bccfc9cb5/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_flash_ex.c#L672

The first one seems to be the latest one with additional code for sector numbers. @Imen DAHMEN​​, how can such a crap get through all ST's year long "tests"?

Glee.11
Glee.11Author
Associate III
July 7, 2020

i'm not familiar with english =)

anyway, thank you for the link.

very helpful!

regard

Piranha
Principal III
July 7, 2020

That's not related to the language but the fact that you have not done your homework of debugging and describing the situation.

Piranha
Principal III
July 7, 2020

Post code in code snippet!

// here should be erased flash. but it says "HAL_OK". but flash is not erased

Why there still isn't cache invalidation at this point? Why are you still keeping it before erase operation and commented out? So what is the memory content immediately after erase and cache invalidation? What is the memory content if you comment out further data programming and look at memory with ST-LINK Utility after disconnecting and reconnecting the power supply?

Glee.11
Glee.11Author
Associate III
July 7, 2020

 if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)

 {

  /*

   Error occurred while sector erase.

   User can add here some code to deal with this error.

   SECTORError will contain the faulty sector and then to know the code error on this sector,

   user can call function 'HAL_FLASH_GetError()'

  */

  /* Infinite loop */

printf("flash erase Error!!!\r\n");

  while (1)

  {

  }

 }

 SCB_InvalidateDCache_by_Addr((uint32_t*)0x080C0000, 131072);

Sorry, i changed.

  1. I erased flash using "STM32 ST-LINK Utility"(because, if don't erase, truestudio show me the errer i said above). and downloaded the program using debugger in truestudio. some reason on first executing, truestudio does not show me the memory

0693W000001sJUqQAM.png

2. So i execute rest of my code and restart the debugger, then i could see the memory.

0693W000001sJVtQAM.png

3. I disconnected power supply, and i reconnecting power supply. and connect with ST-LINK_UTILITY.

0693W000001sJX1QAM.png

Piranha
Principal III
July 7, 2020

You still have HAL_FLASH_Program() called after erase. Do the erase without it (comment it out) and then look at memory with ST-LINK Utility!

Glee.11
Glee.11Author
Associate III
July 8, 2020

  1. downloaded the firmware which include HAL_FLASH_Program();
  2. look memory with stm-link-utility

memory 0x080C0000 : 78563412 FFFFFFFF FFFFFFFF FFFFFFFF

3. downloaded the firmware below which is removed HAL_FLASH_Program();

. and truestudio show me the error but i ignore that

message. and close debug and see the memory with STM32-LINK-Utility

memory 0x080C0000 : 78563412 FFFFFFFF FFFFFFFF FFFFFFFF

#define FLASH_BASE_ADDR  ADDR_FLASH_SECTOR_10  /* Start @ of user Flash area */

#define FLASH_END_ADDR   (ADDR_FLASH_SECTOR_11-1)  /* End @ of user Flash area */

void flash_erase_write(void)

{

HAL_FLASH_Unlock();

// HAL_FLASH_OB_Unlock();

// HAL_FLASHEx_OBGetConfig(&OBInit);

// #if defined(DUAL_BANK)

// /* Turn on LED3 if FLASH is configured in Dual Bank mode */

// if((OBInit.USERConfig & OB_NDBANK_SINGLE_BANK) == OB_NDBANK_SINGLE_BANK)

// #else

// /* Turn on LED3 if FLASH is configured in Single Bank mode */

// if((OBInit.USERConfig & OB_NDBANK_SINGLE_BANK) == OB_NDBANK_DUAL_BANK)

// #endif

// {

// while(1);

// }

/* Get the 1st sector to erase */

FirstSector = GetSector(FLASH_BASE_ADDR);

/* Get the number of sector to erase from 1st sector*/

NbOfSectors = GetSector(FLASH_END_ADDR) - FirstSector + 1;

/* Fill EraseInit structure*/

EraseInitStruct.TypeErase   = FLASH_TYPEERASE_SECTORS;

EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;

EraseInitStruct.Sector    = FirstSector;

EraseInitStruct.NbSectors   = NbOfSectors;//NbOfSectors;

//FLASH_Erase_Sector(FLASH_SECTOR_10 ,FLASH_VOLTAGE_RANGE_3);

 if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)           

 {

printf("flash erase Error!!!\r\n");

  while (1)

  {

  }

 }

 SCB_InvalidateDCache_by_Addr((uint32_t*)0x080C0000, 131072);

//Address = FLASH_BASE_ADDR;

//#define DATA_32 ((uint32_t)0x12345678)

//if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, DATA_32) == HAL_OK)

//{

// Address = Address + 4;

//}

}

Piranha
Principal III
July 8, 2020

Well, then start debugging and fixing that HAL/CubeMX bloatware...

Glee.11
Glee.11Author
Associate III
July 8, 2020

thank you very much for all this

when i solve this problem, i comment again.

thank you!