FLASH_WaitForLastOperation() returning HAL_ERROR
Hi.. i'm stuck with this problem for days... i'm not able to store basic data in the G030 flash memory...
When i tried to erase a flash page using HAL_FLASHEx_Erase(), the FLASH_WaitForLastOperation() returns a HAL_ERROR... I did not found the issue...
page to user data = 0x08007800 to 0x08007FFF (it's the last page for my STM32G030K6)
Here are the main.c code (i have created a blank project to test it):
int main(void)
{
/* USER CODE BEGIN 1 */
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;
/* 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();
/* USER CODE BEGIN 2 */
HAL_FLASH_Unlock();
FirstPage = GetPage(0x08007800);
NbOfPages = GetPage(0x08007FFF) - FirstPage + 1;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Page = FirstPage;
EraseInitStruct.NbPages = NbOfPages;
if(HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
{
Error_Handler();
}
Address = 0x08007800;
uint64_t data = 0x1234567812345678;
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, data) != HAL_OK)
{
Error_Handler();
}
HAL_FLASH_Lock();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Here is the error that i found when debugging (line 33):
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
{
uint32_t error;
uint32_t tickstart = HAL_GetTick();
/* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
Even if the FLASH operation fails, the BUSY flag will be reset and an error
flag will be set */
#if defined(FLASH_DBANK_SUPPORT)
error = (FLASH_SR_BSY1 | FLASH_SR_BSY2);
#else
error = FLASH_SR_BSY1;
#endif /* FLASH_DBANK_SUPPORT */
while ((FLASH->SR & error) != 0x00U)
{
if(Timeout != HAL_MAX_DELAY)
{
if ((HAL_GetTick() - tickstart) >= Timeout)
{
return HAL_TIMEOUT;
}
}
}
/* check flash errors */
error = (FLASH->SR & FLASH_SR_ERRORS);
/* Clear SR register */
FLASH->SR = FLASH_SR_CLEAR;
if (error != 0x00U) <<<<<<<<<<<<<< HERE THE error = 0xe0 <<<<<<<<<<<<<<<
{
/*Save the error code*/
pFlash.ErrorCode = error;
return HAL_ERROR;
}
/* Wait for control register to be written */
while ((FLASH->SR & FLASH_SR_CFGBSY) != 0x00U)
{
if(Timeout != HAL_MAX_DELAY)
{
if ((HAL_GetTick() - tickstart) >= Timeout)
{
return HAL_TIMEOUT;
}
}
}
return HAL_OK;
}
