cancel
Showing results for 
Search instead for 
Did you mean: 

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

Ppk.1
Associate II
/* 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 */

8 REPLIES 8
Andrew Neil
Evangelist III

"the FLASH example code from the GitHub source files"

Which example, from which GitHub repository?

Have you got that example to work on its own?

"it is showing an error in the WRITE operation"

What, exactly, is showing an error, and what error, exactly, is that thing showing?

Ppk.1
Associate II

Hi,

Currently, I'm working on STM32WLE5JC with the LoRaWAN end node example,

0693W00000KbHTYQA3.pngI have referred to the following GitHub source. First of all, I'm new to this board & a beginner level in communication protocol so called LoRaWAN

Referred link,

https://github.com/STMicroelectronics/STM32CubeWL

Referred flash program from examples folder,

0693W00000KbHWIQA3.pngIn this, I'm able to write/read the values in the flash memory using cubemx platform(which stores two uint32_t type variables)!

But when I'm trying the same implementation in LoRaWAN end node example code, facing an issue like "Error in write operation"

I just need to store the values in the memory using LoRaWAN end node example code! (I have attached the functions that i used in the above chain!)

Can you please help with this?

"facing an issue like (sic) "Error in write operation"

how do you mean, "like" ?

Again, where exactly are you getting that, and what is the exact message?

As you've got one case which does work, and one which doesn't, have you tried comparing the two to see where they differ? eg, stepping through the code?

MCasa.5
Associate II

Have you resolve that question? Thanks in advance.

Pretty sure the Flash can be written if coded properly.

It will need aligned addresses, to have been erased and written once.

Fail at a hardware level perhaps from power supply issues or repetitive cycling.

People need to learn how debug, diagnose, and address issues​. Others having solved the issue is not a prerequisite to solving the issue yourself.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
MCasa.5
Associate II

I can't found a free page to store my info into the flash memory with lorawan example sample code. I use this code adapted for my own code, but i don't know how address will must use to do it. https://github.com/STMicroelectronics/STM32CubeWL/blob/main/Projects/NUCLEO-WL55JC/Examples/FLASH/FLASH_FastProgram/Src/main.c

I have the same problem that Ppk1

I do it!

I found a pair of pages free to store my info.

:beaming_face_with_smiling_eyes: