Skip to main content
LPetr.1
Senior
February 6, 2023
Solved

STM32 Internal Flash erase but not 0xFF or 0x00

  • February 6, 2023
  • 1 reply
  • 2420 views

Hello. I am working with internal Flash memory on STM32F407, I have 4 functions as described below:

void FLASH_save_CP_serial(uint32_t serial_number) {
 
 
 
	taskENTER_CRITICAL();
 
 
 
	uint32_t write_test = 12345;
 
 
 
	FLASH_EraseInitTypeDef EraseInitStruct;
 
	uint32_t page_err;
 
 
 
	EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
 
	EraseInitStruct.Sector = FLASH_SECTOR_11;
 
	EraseInitStruct.NbSectors = 1;
 
	EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
 
 
 
	HAL_FLASH_Unlock();
 
	FLASH_WaitForLastOperation(2000);
 
	HAL_FLASHEx_Erase(&EraseInitStruct, &page_err);
 
	FLASH_WaitForLastOperation(2000);
 
 
 
 
 
 
 
	if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x80E0000, write_test) == HAL_OK){
 
 DEBUG_PRINT("write successfull \n");
 
	}
 
	else{
 
 DEBUG_PRINT("failed to write to EEPROM\n");
 
	}
 
	FLASH_WaitForLastOperation(2000);
 
	HAL_FLASH_Lock();
 
	FLASH_WaitForLastOperation(2000);
 
	taskEXIT_CRITICAL();
 
}
 
 
 
/*FLASH_read_CP_serial(uint32_t* RxBuf)
 
 * Reads back 4 byte value from the 0x80E0000 address and saves the value to RxBuf
 
 *
 
 *
 
 */
 
void FLASH_read_CP_serial(uint32_t* RxBuf){
 
	taskENTER_CRITICAL();
 
	RxBuf = (uint32_t *)0x80E0000;
 
	DEBUG_PRINT("%u\n", *RxBuf);
 
	taskEXIT_CRITICAL();
 
 
 
}
 
 
 
 
 
/*
 
 * FLASH_check_if_CP_serial_empty()
 
 * Check if there is any value saved to 0x80E0000 address
 
 * Returns 1 if no value is currently written
 
 * Returns 0 if some value has been written
 
 */
 
bool FLASH_check_if_CP_serial_empty(){
 
	const volatile uint32_t *EEP=(const volatile uint32_t *)((uint32_t)0x80E0000);
 
 if((EEP[0] == 0xFFFFFFFF) || (EEP[1] == 0xFFFFFFFF))
 
 {
 
 return true; // settings not written.
 
 }
 
 return false;
 
}
 
 
 
 
 
/*
 
 * FLASH_erase_cp()
 
 * Erase the whole sector (0x80E0000 - 0x80FFFFF)
 
 *
 
 *
 
 */
 
void FLASH_erase_cp(){
 
	FLASH_EraseInitTypeDef EraseInitStruct;
 
	uint32_t page_err;
 
 
 
	EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
 
	EraseInitStruct.Sector = FLASH_SECTOR_11;
 
	EraseInitStruct.NbSectors = 1;
 
	EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
 
 
 
	HAL_FLASH_Unlock();
 
	FLASH_WaitForLastOperation(2000);
 
	HAL_FLASHEx_Erase(&EraseInitStruct, &page_err);
 
	FLASH_WaitForLastOperation(2000);
 
}

In my program, I would like to detect if any value has been written to my desired location of the Flash memory (0x80E0000).

I expect to do that using function:

FLASH_check_if_CP_serial_empty()

When I erase the whole sector with

void FLASH_erase_cp()

and then read the current value in the Flash address using:

FLASH_read_CP_serial(uint32_t* RxBuf)

4294967295 is returned even though I have just erase the whole sector.

I am expecting to see 0xFFFFFFFF or 0x00000000. Is there any problem in my code? Why erasing does not write FF or 00 to the Flash memory?

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

4294967295 decimal is the same as hex 0xFFFFFFFF.

hth

KnarfB

1 reply

KnarfB
KnarfBBest answer
Super User
February 6, 2023

4294967295 decimal is the same as hex 0xFFFFFFFF.

hth

KnarfB

LPetr.1
LPetr.1Author
Senior
February 6, 2023

Oh my god that is such a silly mistake... Thank you so much for clarifying!