cancel
Showing results for 
Search instead for 
Did you mean: 

[STM32F373RC] Question about using flash memory like EEPROM.

YS4
Associate

Hi.

I want to save and load data to flash memory. (STM32F373RC)

And initialization code was generated through cubemx.

All functions including SPI, I2C, and ADC were successfully tested.

However, saving and loading data into flash memory failed.

All functions used are functions provided in the HAL library.

What I don't understand is that it doesn't work properly when you program it after erasing it,

but it works normally when you just erase it or program it in an erased state.

The code written is the four lines below.

 

#define FLASH_SECTOR_91 ((uint32_t)0x0802D800)

 

void save_flash(void)

 

{

HAL_FLASH_Unlock();

FLASH_PageErase(FLASH_SECTOR_91);

HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_SECTOR_91, 0x12345678);

HAL_FLASH_Lock();

}

 

 

 

 

 

 

 

 

 

 

 

----------------------------------------------------------------------

#define FLASH_TYPEPROGRAM_WORD (0x02U) /*!<Program a word (32-bit) at a specified address.*/

 

void FLASH_PageErase(uint32_t PageAddress)

{

/* Clean the error context */

pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;

 

/* Proceed to erase the page */

SET_BIT(FLASH->CR, FLASH_CR_PER);

WRITE_REG(FLASH->AR, PageAddress);

SET_BIT(FLASH->CR, FLASH_CR_STRT);

}

 

 

 

 

HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)

{

HAL_StatusTypeDef status = HAL_ERROR;

uint8_t index = 0U;

uint8_t nbiterations = 0U;

 

/* Process Locked */

__HAL_LOCK(&pFlash);

 

/* Check the parameters */

assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));

assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));

 

/* Wait for last operation to be completed */

status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);

 

if(status == HAL_OK)

{

if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)

{

/* Program halfword (16-bit) at a specified address. */

nbiterations = 1U;

}

else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)

{

/* Program word (32-bit = 2*16-bit) at a specified address. */

nbiterations = 2U;

}

else

{

/* Program double word (64-bit = 4*16-bit) at a specified address. */

nbiterations = 4U;

}

 

for (index = 0U; index < nbiterations; index++)

{

FLASH_Program_HalfWord((Address + (2U*index)), (uint16_t)(Data >> (16U*index)));

 

/* Wait for last operation to be completed */

status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);

 

/* If the program operation is completed, disable the PG Bit */

CLEAR_BIT(FLASH->CR, FLASH_CR_PG);

/* In case of error, stop programming procedure */

if (status != HAL_OK)

{

break;

}

}

}

 

/* Process Unlocked */

__HAL_UNLOCK(&pFlash);

 

return status;

}

2 REPLIES 2
Uwe Bonnes
Principal III

Did you have a look at the EEPROM emulation examples?

YS4
Associate

Yes, I've checked.