Question
HAL_FLASH_Program Error!!!!!!!
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