cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_FLASH_Program Error!!!!!!!

ptagh.1
Associate II

Hello every body,

I got stuck in Flash programming of STM32WB55CEU MCU!!! and I can not find the problem.

First. I implemented the stm32wb55 example FLASH_EreaseProgram on Nucleo board successfully and then I tried to port this simple program on my PCB.

Ereasing working well, but it is not able to pass HAL_FLASH_Program. I mean it returns HAL_ERROR

My code:

static uint32_t GetPage(uint32_t Addr)
{
  return (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;;
}
 
 
 
 
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
	/* USER CODE BEGIN 1 */
 
	/* USER CODE END 1 */
 
	/* MCU Configuration--------------------------------------------------------*/
 
	/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
	HAL_Init();
 
	/* USER CODE BEGIN Init */
 
	/* USER CODE END Init */
 
	/* Configure the system clock */
	SystemClock_Config();
 
	/* USER CODE BEGIN SysInit */
 
	/* USER CODE END SysInit */
 
	/* Initialize all configured peripherals */
	MX_GPIO_Init();
	MX_LPUART1_UART_Init();
 
 
	/* Clear OPTVERR bit set on virgin samples */
	__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
 
 
	uint32_t FirstPage = 0, NbOfPages = 0;
	uint32_t Address = 0, PageError = 0;
	__IO uint32_t MemoryProgramStatus = 0;
	__IO uint32_t data32 = 0;
	static FLASH_EraseInitTypeDef EraseInitStruct;
 
 
 
  /* Get the 1st page to erase */
   FirstPage = GetPage(FLASH_USER_START_ADDR);
 
   /* Get the number of pages to erase from 1st page */
	 NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;
 
	 /* Fill EraseInit structure*/
	 EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
	 EraseInitStruct.Page        = FirstPage;
	 EraseInitStruct.NbPages     = NbOfPages;
 
	 /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
		you have to make sure that these data are rewritten before they are accessed during code
		execution. If this cannot be done safely, it is recommended to flush the caches by setting the
		DCRST and ICRST bits in the FLASH_CR register. */
	 if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
	 {
	   /*
		 Error occurred while  erase.
		 User can add here some code to deal with this error.
		 PageError will contain the faulty  and then to know the code error on this ,
		 user can call function 'HAL_FLASH_GetError()'
	   */
	   /* Infinite loop */
 
 
		 My_Printf("Flash>> HAL_FLASHEx_Erase ==>> Error! \n\r");
 
		 while(1)
		 {
 
		 }
	 }
	 else
	 {
		 My_Printf("Flash>> HAL_FLASHEx_Erase ==>> Done! \n\r");
	 }
 
	 /* Program the user Flash area word by word
	   (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
 
	 Address = FLASH_USER_START_ADDR;
 
#define DATA_64                 ((uint64_t)0x1234567812345678)
 
	 while (Address < FLASH_USER_END_ADDR)
	 {
	   if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, DATA_64) == HAL_OK)
	   {
		   My_Printf("Flash>> Program ==>> ok ++++! \n\r");
		 Address = Address + 8;  /* increment to next double word*/
	   }
	  else
	   {
		 /* Error occurred while writing data in Flash memory.
			User can add here some code to deal with this error */
 
		  My_Printf("Flash>> Program ==>> Error! \n\r");
		 while (1)
		 {
 
		 }
	   }
	 }
 
	 /* Lock the Flash to disable the flash control register access (recommended
		to protect the FLASH memory against possible unwanted operation) *********/
	 HAL_FLASH_Lock();
 
 
 
} // End of main

4 REPLIES 4
Petr DAVID
ST Employee

Hello ptagh.1,

I just have went through your code and I have one note and a questions. First of all you are missing HAL_FLASH_Unlock(); in your code. And further I would like to ask you what address are you trying to write at? What is your definition of FLASH_USER_START_ADDR? Also are you checking the memory address with the debugger?

Best regards,

Petr

ptagh.1
Associate II

Hi Petr,

Thank you for your respone.

Actually I used different address. For Example:

#define ADDR_FLASH_PAGE_85  ((uint32_t)0x08055000)

#define FLASH_USER_START_ADDR  ADDR_FLASH_PAGE_85  /* Start @ of user Flash area */

#define FLASH_USER_END_ADDR   (ADDR_FLASH_PAGE_20 + FLASH_PAGE_SIZE - 1)  /* End @ of user Flash area */

But doesn't work still. I will try again with debugger

Remy ISSALYS
ST Employee

Hello,

If I understand well, the same code works well on P-NUCLEO-WB55 but it not running on your custom board, is that correct ?

I recommend you to use flash_driver provided by ST, you can look BLE_RfWithFlash example available in STM32CubeWB package.

Best Regards

Hi,

in this moment I am not really sure what your code does as you have the FLASH_USER_END_ADDR defined lower than FLASH_USER_START_ADDR. It should definitely be the other way around. And for start maybe start with just clearing 1 page and write to the first double word in the page.

Best regards,