cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4R5 || Flash internal read/erase

POval.1
Associate II

Hello everyone,

Dev Board based on STM32L4R5

I am just trying to erase and then write something on the internal FLASH.

I am trying to follow the example:

https://github.com/STMicroelectronics/STM32CubeL4/blob/master/Projects/NUCLEO-L4R5ZI/Examples/FLASH/FLASH_FastProgram/Src/main.c

I have created a small test function to just erase the bank 2

/* !!! Be careful the user area should be in another bank than the code !!! */
#define FLASH_USER_START_ADDR   ADDR_FLASH_PAGE_256   /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR     ADDR_FLASH_PAGE_511 + FLASH_PAGE_SIZE - 1   /* End @ of user Flash area */
 
 
void testFlash( void )
{
	/* 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 bank */
	BankNumber = GetBank(FLASH_USER_START_ADDR);
 
 
	/* Fill EraseInit structure*/
	EraseInitStruct.TypeErase = FLASH_TYPEERASE_MASSERASE;
	EraseInitStruct.Banks     = BankNumber;
 
	if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
	{
	 // Error occurred while mass erase.
	 // To know the code error, user can call function 'HAL_FLASH_GetError()'
 
		//Infinite loop
		while (1)
		{
		  //BSP_LED_On(LED3);
		}
	}
 
	/* Lock the Flash to disable the flash control register access (recommended
	to protect the FLASH memory against possible unwanted operation) *********/
	HAL_FLASH_Lock();
}

Issue:

Having testFlash() function in main(), cases the HAL_Init() not return at all.

If I remove the testFlash() the application runs as expected.

Hope someone can direct me to some debug information.

Thank you.

5 REPLIES 5
POval.1
Associate II

Further investigation reveled that in function HAL_InitTick() : HAL_TIM_Base_Start_IT(&htim6); does not return

The HAL is using the TIM6 as the FreeRTOS is using sys timer.

TDK
Guru

What is the returned value of BankNumber? Are you erasing part of the FLASH that is being used by your program?

> Having testFlash() function in main(), cases the HAL_Init() not return at all.

When you debug it, where does the cpu end up?

If you feel a post has answered your question, please click "Accept as Solution".
/**
  * @brief  Gets the bank of a given address
  * @param  Addr: Address of the FLASH Memory
  * @retval The bank of a given address
  */
static uint32_t GetBank(uint32_t Addr)
{
	uint32_t bank = 0;
 
	if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0)
	{
		/* No Bank swap */
		if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
		{
		  bank = FLASH_BANK_1;
		}
		else
		{
		  bank = FLASH_BANK_2;
		}
	}
	else
	{
		/* Bank swap */
		if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
		{
		  bank = FLASH_BANK_2;
		}
		else
		{
		  bank = FLASH_BANK_1;
		}
	}
 
	return bank;
}

When I try to debug: the first time the code ends up into the reset handler.

After running/debugging it again in get stuck at HAL_TIM_Base_Start_IT(&htim6) int function HAL_InitTick()

That is the code of the function you call, but what is the value it returns?

Doesn't make sense that the code would do different things each time you debug it.

If you feel a post has answered your question, please click "Accept as Solution".
POval.1
Associate II

The return value is always FLASH_BANK_2.

Yes even I am not understanding why the debug would do that.

 function HAL_InitTick() : HAL_TIM_Base_Start_IT(&htim6); does not return : Does this indicate any issue?