cancel
Showing results for 
Search instead for 
Did you mean: 

Flash memory data access

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;

}

1 REPLY 1
KDJEM.1
ST Employee

Hello @YS4 and welcome to Community :);

 

I apologize for my late reply.

Did you have a look at FLASH_EraseProgram example? 

This example explains How to configure and use the FLASH HAL API to erase and program the internal Flash memory.

 

Thank you.

Kaouthar

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.