Skip to main content
Visitor II
August 30, 2019
Question

Data not being saved to EEPROM

  • August 30, 2019
  • 2 replies
  • 1227 views

I'm trying to use the emulated EEPROM on the b-l475e-iot board... I'm following the example from X-Cube-EEPROM, and everything looks ok... when it goes through storing values and then reading them back, everything matches... but if I try to read the data at startup, I get an EE_NO_DATA error.

#define PWR_FLAG_WUF PWR_FLAG_WUF1
/* Virtual address Tab defined by the user: 0x0000 and 0xFFFF values are prohibited */
uint16_t VirtAddVarTab[NB_OF_VARIABLES];
uint32_t Index = 0;
__IO uint32_t ErasingOnGoing = 0;
uint32_t VarDataTab[NB_OF_VARIABLES] = {0};
uint32_t VarValue = 0;
 
void InitEEPROM(void)
{
	 EE_Status ee_status = EE_OK;
 
	/* Enable and set FLASH Interrupt priority */
	 /* FLASH interrupt is used for the purpose of pages clean up under interrupt */
	 HAL_NVIC_SetPriority(FLASH_IRQn, 0, 0);
	 HAL_NVIC_EnableIRQ(FLASH_IRQn);
 
	 /* Unlock the Flash Program Erase controller */
	 HAL_FLASH_Unlock();
 
	 /* Configure Programmable Voltage Detector (PVD) (optional) */
	 /* PVD interrupt is used to suspend the current application flow in case
	 a power-down is detected, allowing the flash interface to finish any
	 ongoing operation before a reset is triggered. */
	 PVD_Config();
 
	 /* Set user List of Virtual Address variables: 0x0000 and 0xFFFF values are prohibited */
	 for (int i = 0; i < NB_OF_VARIABLES; i++)
	 {
	 VirtAddVarTab[i] = (uint16_t)(10*i + 1);
	 }
	 EE_Format(EE_FORCED_ERASE);
	 /* Set EEPROM emulation firmware to erase all potentially incompletely erased
	 pages if the system came from an asynchronous reset. Conditional erase is
	 safe to use if all Flash operations where completed before the system reset */
	 if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) == RESET)
	 {
	 /* Blink LED_OK (Green) twice at startup */
	 BSP_LED_On(LED2);
	 HAL_Delay(100);
	 BSP_LED_Off(LED2);
	 HAL_Delay(100);
	 BSP_LED_On(LED2);
	 HAL_Delay(100);
	 BSP_LED_Off(LED2);
 
 
	 /* System reset comes from a power-on reset: Forced Erase */
	 /* Initialize EEPROM emulation driver (mandatory) */
	 ee_status = EE_Init(VirtAddVarTab, EE_FORCED_ERASE);
	 if(ee_status != EE_OK) {Error_Handler(__LINE__);}
	 }
	 else
	 {
	 /* Clear the Standby flag */
	 __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
 
	 /* Check and Clear the Wakeup flag */
	 if (__HAL_PWR_GET_FLAG(PWR_FLAG_WUF) != RESET)
	 {
	 __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF);
	 }
 
	 /* Blink LED_OK (Green) upon wakeup */
	 BSP_LED_On(LED2);
	 HAL_Delay(100);
	 BSP_LED_Off(LED2);
 
	 /* System reset comes from a STANDBY wakeup: Conditional Erase*/
	 /* Initialize EEPROM emulation driver (mandatory) */
	 ee_status = EE_Init(VirtAddVarTab, EE_CONDITIONAL_ERASE);
	 if(ee_status != EE_OK) {Error_Handler(__LINE__);}
	 }
 
	 //saveEEPROMValues();
	 for (Index = 0; Index < NB_OF_VARIABLES; Index++)
	 {
		ee_status = EE_ReadVariable32bits(VirtAddVarTab[Index], &VarValue);
		AZURE_PRINTF("Index: %u...Value: %u\r\n", Index , VarValue);
 
		if (ee_status != EE_OK) {
			AZURE_PRINTF("Error reading data: %u\r\n", ee_status);
		}
	 }
 
	 AZURE_PRINTF("------- End initial read-------------\r\n");
	 /* Store 10 values of all variables in EEPROM, ascending order */
	 for (VarValue = 1; VarValue <= 10; VarValue++)
	 {
	 for (Index = 0; Index < NB_OF_VARIABLES; Index++)
	 {
	 /* Wait any cleanup is completed before accessing flash again */
	 while (ErasingOnGoing == 1) { }
 
	 ee_status = EE_WriteVariable32bits(VirtAddVarTab[Index], Index*VarValue);
	 AZURE_PRINTF("Index: %u...Value: %u\r\n", Index , Index*VarValue);
	 ee_status|= EE_ReadVariable32bits(VirtAddVarTab[Index], &VarDataTab[Index]);
	 if (Index*VarValue != VarDataTab[Index]) {
 
	 	AZURE_PRINTF("%u:%u:%u::%u\r\n",Index,VarValue, Index*VarValue, VarDataTab[Index]);
	 	Error_Handler(__LINE__);
	 }
 
	 /* Start cleanup IT mode, if cleanup is needed */
	 if ((ee_status & EE_STATUSMASK_CLEANUP) == EE_STATUSMASK_CLEANUP) {ErasingOnGoing = 1;ee_status|= EE_CleanUp_IT();}
	 if ((ee_status & EE_STATUSMASK_ERROR) == EE_STATUSMASK_ERROR) {Error_Handler(__LINE__);}
	 }
	 }
 
	 AZURE_PRINTF("------- End storing data -------------\r\n");
	 /* Read all the variables */
	 for (Index = 0; Index < NB_OF_VARIABLES; Index++)
	 {
	 ee_status = EE_ReadVariable32bits(VirtAddVarTab[Index], &VarValue);
	 if (VarValue != VarDataTab[Index])
	 {
	 	AZURE_PRINTF("eeprom not OK!!! %u::%u \r\n", VarValue, VarDataTab[Index]);
	 	//Error_Handler(__LINE__);
	 }
	 else
	 {
	 	AZURE_PRINTF("Index: %u...Value: %u\r\n", Index , VarValue);
	 }
	 if (ee_status != EE_OK) {
	 	AZURE_PRINTF("%u\r\n", ee_status);
	 	Error_Handler(__LINE__);
	 }
	 }
	 AZURE_PRINTF("------- End final read-------------\r\n");
	 /* Lock the Flash Program Erase controller */
	 HAL_FLASH_Lock();
}

Index: 0...Value: 0
Error reading data: 7
Index: 1...Value: 0
Error reading data: 7
Index: 2...Value: 0
Error reading data: 7
Index: 3...Value: 0
Error reading data: 7
Index: 4...Value: 0
Error reading data: 7
Index: 5...Value: 0
Error reading data: 7
Index: 6...Value: 0
Error reading data: 7
Index: 7...Value: 0
Error reading data: 7
------- End initial read-------------
Index: 0...Value: 0
Index: 1...Value: 1
Index: 2...Value: 2
Index: 3...Value: 3
Index: 4...Value: 4
Index: 5...Value: 5
Index: 6...Value: 6
Index: 7...Value: 7
Index: 0...Value: 0
Index: 1...Value: 2
Index: 2...Value: 4
Index: 3...Value: 6
Index: 4...Value: 8
Index: 5...Value: 10
Index: 6...Value: 12
Index: 7...Value: 14
Index: 0...Value: 0
Index: 1...Value: 3
Index: 2...Value: 6
Index: 3...Value: 9
Index: 4...Value: 12
Index: 5...Value: 15
Index: 6...Value: 18
Index: 7...Value: 21
Index: 0...Value: 0
Index: 1...Value: 4
Index: 2...Value: 8
Index: 3...Value: 12
Index: 4...Value: 16
Index: 5...Value: 20
Index: 6...Value: 24
Index: 7...Value: 28
Index: 0...Value: 0
Index: 1...Value: 5
Index: 2...Value: 10
Index: 3...Value: 15
Index: 4...Value: 20
Index: 5...Value: 25
Index: 6...Value: 30
Index: 7...Value: 35
Index: 0...Value: 0
Index: 1...Value: 6
Index: 2...Value: 12
Index: 3...Value: 18
Index: 4...Value: 24
Index: 5...Value: 30
Index: 6...Value: 36
Index: 7...Value: 42
Index: 0...Value: 0
Index: 1...Value: 7
Index: 2...Value: 14
Index: 3...Value: 21
Index: 4...Value: 28
Index: 5...Value: 35
Index: 6...Value: 42
Index: 7...Value: 49
Index: 0...Value: 0
Index: 1...Value: 8
Index: 2...Value: 16
Index: 3...Value: 24
Index: 4...Value: 32
Index: 5...Value: 40
Index: 6...Value: 48
Index: 7...Value: 56
Index: 0...Value: 0
Index: 1...Value: 9
Index: 2...Value: 18
Index: 3...Value: 27
Index: 4...Value: 36
Index: 5...Value: 45
Index: 6...Value: 54
Index: 7...Value: 63
Index: 0...Value: 0
Index: 1...Value: 10
Index: 2...Value: 20
Index: 3...Value: 30
Index: 4...Value: 40
Index: 5...Value: 50
Index: 6...Value: 60
Index: 7...Value: 70
------- End storing data -------------
Index: 0...Value: 0
Index: 1...Value: 10
Index: 2...Value: 20
Index: 3...Value: 30
Index: 4...Value: 40
Index: 5...Value: 50
Index: 6...Value: 60
Index: 7...Value: 70
------- End final read-------------
 
 

    This topic has been closed for replies.

    2 replies

    Graduate II
    August 30, 2019

    Perhaps don't EE_Format(EE_FORCED_ERASE); every time through?

    DSaboAuthor
    Visitor II
    August 30, 2019

    You're right, that shouldn't be there, but I'm seeing the same result. I had been trying to save values as floats instead of uint32 but it was failing, and I thought formatting might help and I forgot to take it out.

    Graduate II
    August 30, 2019

    You could perhaps inspect the memory with the STM32 Cube Programmer to confirm there is content in the sectors used by the EEPROM emulation.

    DSaboAuthor
    Visitor II
    September 9, 2019

    Not sure why it wasn't working before, but everything looks like it is saving ok now.