2023-12-10 02:31 AM
Hi,
I try to write and read from flash on stm32wb10cc but when i try to write by HAL_FLASH_Program function i received 3 errors in FLASH Status register:
1. Programming alignment error
2. size error
3. Programming sequence error
my code is:
on the main:
uint64_t data2[] = {0x44444444,0x55555555};
uint64_t Rx_Data[30] = {0};
Flash_Write_Data((FLASH_END_ADDR - (FLASH_PAGE_SIZE * 150)) - 1 , (uint64_t *)data2, 2);
the Flash_Write_Data function is:
uint32_t Flash_Write_Data (uint32_t StartPageAddress, uint64_t *Data, uint16_t numberofwords)
{
static FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t PAGEError;
int sofar=0;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Erase the user Flash area*/
uint32_t StartPage = GetPageIndex(StartPageAddress);
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Page = StartPage;
EraseInitStruct.NbPages = 1;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
{
/*Error occurred while page erase.*/
return HAL_FLASH_GetError ();
}
/* Program the user Flash area word by word*/
while (sofar<numberofwords)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, StartPageAddress, Data[sofar]) == HAL_OK)
{
StartPageAddress += 2; // use StartPageAddress += 2 for half word and 8 for double word
sofar++;
}
else
{
/* Error occurred while writing data in Flash memory*/
return HAL_FLASH_GetError ();
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();
return 0;
}
The GetPageIndex function is:
static uint32_t GetPageIndex(uint32_t Address)
{
for (int indx=0; indx<160; indx++)
{
uint32_t addressOne = 0x08000000 + (FLASH_PAGE_SIZE *(indx+1));
uint32_t addresstwo = (0x08000000 + FLASH_PAGE_SIZE*indx);
if((Address < (0x08000000 + (FLASH_PAGE_SIZE *(indx+1))) ) && (Address >= (0x08000000 + FLASH_PAGE_SIZE*indx)))
{
return (indx);
}
}
return 0;
}
the erase page its ok but the write fail :(
Can anyone explain to me what the problem is?
2023-12-10 06:41 AM
> StartPageAddress += 2; // use StartPageAddress += 2 for half word and 8 for double word
You're writing double words, so this should be 8, surely. Would explain the alignment error. Could be other issues as well.