Skip to main content
Explorer II
January 3, 2024
Solved

Flash Erase not Working (STM32G491RC)

  • January 3, 2024
  • 3 replies
  • 1931 views

I'm trying to write some data to a address in the flash memory of the above mentioned chip. However the Erase doesn't seem to work.

 

void writeToFlash(uint32_t address, uint64_t data, uint64_t data2){
	// Unlock the Flash Program controller
	HAL_FLASH_Unlock();
	
	// Setting Up the Erase
	FLASH_EraseInitTypeDef EraseInitStruct;
	EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
	EraseInitStruct.Banks = 2;
	EraseInitStruct.Page = 127;
	EraseInitStruct.NbPages = 1; // simple function only sending 8 Bytes at the time.
	uint32_t pageError;
	
	//Erasing specific part of memory
	HAL_FLASHEx_Erase(&EraseInitStruct, &pageError);
	//Programming the flash, last adress of the second Bank!
	HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data);
	HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address + 0x08, data2);
	//Locking the flash back up
	HAL_FLASH_Lock();
}

 

In the main I'm calling the following:

 

writeToFlash(0x0807FFF0, 0x00FFFFFF9A6410AC, 0x00000000016410AC);

 

The reason I'm specifying the bank and page number is because I (think I,) know where the data needs to be erased.

And when do a full chip erase via STM32Cube Programmer, the writing to the flash works. But when I try to do it with some data storage already in the memory. It doesn't. Please help, I've been struggling now for a couple of days.

Capture.PNG

 

 

    This topic has been closed for replies.
    Best answer by GwenoleB

    Hello @BAdmi.1,

    You are using a G491RC with 256KBytes of Flash size in single bank only.

    Dual bank is supported on devices STM32G4x3 Series.

    Best Regards,

    Gwénolé

     

    3 replies

    Super User
    January 3, 2024

    Try just erasing in the program, nothing else; and then read out the erased area using CubeProgrammer - was it erased?

    JW

    BAdmi.1Author
    Explorer II
    January 3, 2024

    Not when i try it in the second bank, when i run the program for the last page of the first bank. It works just fine:

    void writeToFlash(uint32_t address, uint64_t data, uint64_t data2){
    	// Unlock the Flash Program controller
    	HAL_FLASH_Unlock();
    
    	// Setting Up the Erase
    	FLASH_EraseInitTypeDef EraseInitStruct;
    	EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
    	EraseInitStruct.Banks = 0x01;
    	EraseInitStruct.Page = 126;
    	EraseInitStruct.NbPages = 2; // simple function only sending 8 Bytes at the time.
    	uint32_t pageError;
    
    	//Erasing specific part of memory
    	HAL_FLASHEx_Erase(&EraseInitStruct, &pageError);
    	//Programming the flash, last adress of the second Bank!
    	HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data);
    	HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address + 0x08, data2);
    	//Locking the flash back up
    	HAL_FLASH_Lock();
    }

    And calling it with:

    writeToFlash(0x0803FFF0, 0x00FFFFFF9A6410AC, 0x00000000016410AC);
    // and to test wheter i can change it
    writeToFlash(0x0803FFF0, 0x00FFFFFF906410AC, 0x00000000016410AC);
    // the data i write btw corresponds to IP adresses that have to be looked up after restarting

    BAdmi1_1-1704285146658.png

    BAdmi1_2-1704285204115.png

    BAdmi1_3-1704285232278.png

    So my code works when i try it in the first bank, when i try it in the second bank it fails.
    Changing banks value to 0x02 doesn't work. But why?

    Super User
    January 3, 2024

    I don't use Cube/HAL, but it's open source, so you can debug it as usually: observe what is written to the FLASH registers, and compare it against the description in Reference Manual.

    JW

    GwenoleBAnswer
    ST Employee
    January 3, 2024

    Hello @BAdmi.1,

    You are using a G491RC with 256KBytes of Flash size in single bank only.

    Dual bank is supported on devices STM32G4x3 Series.

    Best Regards,

    Gwénolé