Question
LoRaWAN end node example in STM32WL | How to store a value in flash memory? Integrated the FLASH example code from the GitHub source files, not working & it is showing an error in the WRITE operation
/* Flash Init */
uint32_t GetPage(uint32_t Addr)
{
uint32_t page = 0;
if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
{
/* Bank 1 */
page = (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;
}
else
{
/* Bank 2 */
page = (Addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
}
return page;
}
void FLASH_init(void)
{
/* Initialise test status */
MemoryProgramStatus = PASSED;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Clear OPTVERR bit set on virgin samples */
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
/* Get the number of the start and end pages */
StartPage = GetPage(FLASH_USER_START_ADDR);
EndPage = GetPage(FLASH_USER_END_ADDR);
}
/* Erase operation */
void FLASH_erase(uint32_t page_address)
{
/* The desired pages are not write protected */
/* Fill EraseInit structure************************************************/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; // Mass erase or Page erase
EraseInitStruct.Page = page_address;
EraseInitStruct.NbPages = EndPage - StartPage + 1; // Number of pages to be erased
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
{
/*
Error occurred while page erase.
User can add here some code to deal with this error.
PageError will contain the faulty page and then to know the code error on this page,
user can call function 'HAL_FLASH_GetError()'
*/
while (1)
{
printf("Error in erase operation\n");
}
}
}
/* Write Operation */
void FLASH_program(uint32_t add, uint64_t *data, uint8_t count)
{
/* FLASH Word program of DATA_32 at addresses defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR */
uint32_t Address=0;
int i=0;
Address = add;
while (i < count)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, data[i]) == HAL_OK)
{
Address = Address + 8;
i++;
}
else
{
/* Error occurred while writing data in Flash memory.
User can add here some code to deal with this error */
while (1)
{
printf("Error in write operation\n");
}
}
}
}
/* Read operation */
uint64_t FLASH_read(uint32_t Address)
{
/* Check the correctness of written data */
data64 = *(__IO uint32_t *)Address;
return data64;
}
####### Storing values #######
/* WRITE OPERATION */
void Store_Config(void)
{
s_config[config_count++] = data; // uint32_t data
s_config[config_count++] = data1; // uint32_t data1
FLASH_erase(StartPage);
FLASH_program(FLASH_USER_START_ADDR,s_config,config_count); //store config
config_count=0;
}
/* READ OPERATION */
void Read_Config(void)
{
uint32_t star_address=0;
uint64_t r_config[10];
star_address=FLASH_USER_START_ADDR;
for(int i=0;i<10;i++)
{
r_config[i]=FLASH_read(star_address);
star_address+=8;
}
data = r_config[0];
data1 = r_config[1];
printf("Read Data1: %lu\n", data);
printf("Read Data2: %lu\n", data1);
}
####### Address details #######
#define FLASH_USER_START_ADDR ADDR_FLASH_PAGE_24 /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR ADDR_FLASH_PAGE_25 /* End @ of user Flash area */