2025-06-21 4:05 AM - edited 2025-06-21 6:04 AM
Hi,
I'm currently using an STM32G0B0CET6 and encountering an issue with writing a uint32_t variable to EEPROM using Write_U32_Variable(). The function works correctly during task initialization and in normal bare metal while loop too, but it fails when I try to save the variable at runtime (EE_WRITE_ERROR).
This code was previously working without issues on an STM32G0B0KET6. I have configured the EEPROM emulation with START_PAGE_ADDRESS set to 0x08040000U and NB_OF_VARIABLES defined as 781.
Could you help review or suggest changes to ensure compatibility with STM32G0B0CET6?
void DaQ(void *argument)
{
/* USER CODE BEGIN DaQ */
Write_U32_Variable(__OPERATING_MODE, 12); // Working
/* Infinite loop */
for(;;)
{
Write_U32_Variable(__OPERATING_MODE, 14); // Non-working
osDelay(30000);
}
/* USER CODE END DaQ */
}
bool Write_U32_Variable(uint32_t variable_id, uint32_t value){
/* Declaring 8-bit unsigned integer to store the EEPROM read/Write Status */
bool return_variable = true;
uint8_t EE_Status = 0x00;
/* Computing the Half Word to be put into */
uint16_t half_word_position = PERSISTENCE_FLASH_MEMORY_U32_VARIABLES_START_INDEX + (uint16_t)(variable_id*2);
/* Declaring a variable to store the computed half word */
uint16_t original_half_word = 0x0000;
/* Declaring a variable to store the read half word */
uint16_t modified_half_word = 0x0000;
/* ----Adding LSB of Data into Slot 1---- */
original_half_word = (uint16_t)(value&0x0000FFFF);
if(__is_hardware_initialized == 1){
__disable_irq();
taskENTER_CRITICAL();
}
HAL_FLASH_Unlock();
/* Writing the half word into the memory */
EE_Status = EE_WriteVariable16bits(half_word_position, original_half_word); // STOPPING HERE
/* Locking the Flash memory */
HAL_FLASH_Lock();
if(__is_hardware_initialized == 1){
taskEXIT_CRITICAL();
__enable_irq();
}
/* Reading the memory contents to confirm the data written */
EE_Status = EE_ReadVariable16bits(half_word_position, &modified_half_word);
/* Checking for Successful Write operation */
if(original_half_word!=modified_half_word){
/* Setting Write Operation Error */
__ERROR_SET(__FLASH_EEPROM_WRITE_ERROR);
}
/* ----Adding MSB of Data into Slot 2---- */
original_half_word = (uint16_t)((value&0xFFFF0000)>>16);
if(__is_hardware_initialized == 1){
__disable_irq();
taskENTER_CRITICAL();
}
/* Unlocking the Flash Memory */
HAL_FLASH_Unlock();
/* Writing the half word into the memory */
EE_Status = EE_WriteVariable16bits(half_word_position+1, original_half_word);
/* Locking the Flash memory */
HAL_FLASH_Lock();
if(__is_hardware_initialized == 1){
taskEXIT_CRITICAL();
__enable_irq();
}
/* Reading the memory contents to confirm the data written */
EE_Status = EE_ReadVariable16bits(half_word_position+1, &modified_half_word);
/* Checking for Successful Write operation */
if(original_half_word!=modified_half_word){
/* Setting Write Operation Error */
__ERROR_SET(__FLASH_EEPROM_WRITE_ERROR);
}
/* Returning values to the calling function */
return return_variable;
}
2025-06-21 8:31 AM - edited 2025-06-21 9:17 AM
It should be basically the same IC, one in a 48-pin package, the other in 32-pin
Would check the Option Bytes to see if the FLASH is configured / banked differently, but otherwise assume the problem is elsewhere.
Does __is_hardware_initialized change at any point? I don't expect the EE code to be multi-threaded or re-entrant, so you're like going to need to serialize access to the resource.