cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f767igt doesn't erase the flash

Glee.11
Associate II

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;

}

}

17 REPLIES 17

ye... you're right. i have to solve this.

I use truestudio for this.

And.. I tested this(flash_erase, flash write) successfully on nucleo-144 stm32f767ZI board Using the file i added below.

Now, i'm testing this on my custom board which cpu is stm32f767IGT6

Maybe there are differences between two cpu. i think

And i use st link v2 for debug.

And I have looked flash with "STM32 ST-LINK Utility" & trustudio

Some reason, after write the flash true studio shows me

"Failure at line:6 in 'Target Software Startup Scripts'. Please edit the debug configuration settings. Error finishing flash operation"

and this is same in nucleo-114 which i tested this successfully.

So i write the flash, and then close the debug situation, and open the "STM32 ST-LINK Utility" and look the flash.

problem is why the flash_write() works properly. and why only the erase does not work.

I excute the program step by step using debbugger.

In nucleo-144, when the program execute

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

the flash is set 0x12345678 to 0xFFFFFFFF (I've seen this in truestudio in 0x080C0000)

but the my board which have stm32f767IGT6

when the program execute if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK) the flash doesn't set 0x12345678 to 0xFFFFFFFF

it only show me 0x12345678. even it said "HAL_OK".

below is my code i've testing.

void flash_erase_write(void)

{

HAL_FLASH_Unlock();

/* 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   = 1;//NbOfSectors;

//FLASH_Erase_Sector(FLASH_SECTOR_10 ,FLASH_VOLTAGE_RANGE_3);

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

/* flash delete */

 if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK) // here should be erased flash. but it says "HAL_OK". but flash is not erased

 {

  /* Infinite loop */

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; // writing is ok. i've looked flash with debugger

}

}

HAL_FLASH_Lock();

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* Enable I-Cache */

// SCB_EnableICache();

 /* Enable D-Cache */

// SCB_EnableDCache();

 /* USER CODE END 1 */

  

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_I2C1_Init();

 MX_I2C2_Init();

 MX_SDMMC1_SD_Init();

 MX_SPI1_Init();

 MX_SPI2_Init();

 MX_SPI3_Init();

 MX_USART1_UART_Init();

 MX_USART2_UART_Init();

 MX_USART3_UART_Init();

 MX_FATFS_Init();

 MX_ADC1_Init();

 MX_TIM10_Init();

 MX_TIM3_Init();

 MX_TIM11_Init();

 MX_TIM5_Init();

 MX_TIM7_Init();

 MX_RTC_Init();

 MX_DMA_Init();

 //MX_IWDG_Init();

 MX_FMC_Init();

 /* USER CODE BEGIN 2 */

 flash_erase_write();

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

Piranha
Chief II

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?

 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
Chief II

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!

  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
Chief II

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

thank you very much for all this

when i solve this problem, i comment again.

thank you!

Thank you @Piranha​  for bringing this to my attention.

I raised this internally for review.

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen