stm32g0b1cetx flash erase example code is not working
Hello everyone I try erase and write the flash of stm32g0b1cetx series. every time I try I got this error code from flash " pFlash.ErrorCode = 0x000000A0".
is anybody have idea what problem is here i try example code too.
I am totally beginner to stm32g0 series
this is my code
HAL_FLASH_Unlock();
HAL_GPIO_WritePin(STATUS_LED_GPIO_Port,STATUS_LED_Pin,1);
//__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR| FLASH_CR_PER | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR | FLASH_FLAG_BSY);
/* Program the user Flash area word by word
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
FirstPage = GetPage(FLASH_USER_START_ADDR);
/* Get the number of pages to erase from 1st page */
NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;
/* Get the bank */
BankNumber = GetBank(FLASH_USER_START_ADDR);
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks = BankNumber;
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 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()'
*/
/* Infinite loop */
HAL_FLASH_GetError();
while (1)
{
/* Make LED4 blink (100ms on, 2s off) to indicate error in Erase operation */
HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,1);
HAL_Delay(100);
HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,0);
HAL_Delay(2000);
}
}
Address = FLASH_USER_START_ADDR;
while (Address < FLASH_USER_END_ADDR)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, DATA_64) == HAL_OK)
{
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 */
while (1)
{
/* Make LED4 blink (100ms on, 2s off) to indicate error in Write operation */
HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,1);
HAL_Delay(100);
HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,0);
HAL_Delay(2000);
}
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
/* Check if the programmed data is OK
MemoryProgramStatus = 0: data programmed correctly
MemoryProgramStatus != 0: number of words not programmed correctly ******/
Address = FLASH_USER_START_ADDR;
MemoryProgramStatus = 0x0;
while (Address < FLASH_USER_END_ADDR)
{
data32 = *(__IO uint32_t *)Address;
if (data32 != DATA_32)
{
MemoryProgramStatus++;
}
Address = Address + 4;
}
/*Check if there is an issue to program data*/
if (MemoryProgramStatus == 0)
{
/* No error detected. Switch on LED4*/
HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,1);;
}
else
{
/* Error detected. LED4 will blink with 1s period */
while (1)
{
HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,1);
HAL_Delay(500);
HAL_GPIO_WritePin(SERVER_LED_GPIO_Port,SERVER_LED_Pin,0);
HAL_Delay(500);
}
}
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 */
}
//get page and get bank function
/**
* @brief Gets the page of a given address
*
* @retval The page of a given address
*/
static 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;
}
/**
* @brief Gets the bank of a given address
*
* @retval The bank of a given address
*/
static uint32_t GetBank(uint32_t Addr)
{
return FLASH_BANK_1;
}
