2022-01-27 08:04 AM
Hello
I'm trying to read and write data to the flash on a STM32F413ZH using Keil MDK-ARM. I've been using the following code:
uint32_t Flash_Address1 = 0x08140066;
uint32_t Flash_Address2 = 0x08140067;
uint8_t Data[4] = {0x66, 0x77, 0x88, 0x99 };
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
HAL_StatusTypeDef status;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
HAL_FLASH_Unlock();
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Flash_Address1, Data[0]);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Flash_Address2, Data[1]);
HAL_FLASH_Lock();
while (1)
}
However, I am unable to change anything. Stepping through 'FLASH_SetErrorCode()' I see that the following three errors are begin set:
#define HAL_FLASH_ERROR_PGS 0x00000002U /*!< Programming Sequence error */
#define HAL_FLASH_ERROR_PGP 0x00000004U /*!< Programming Parallelism error */
#define HAL_FLASH_ERROR_WRP 0x00000010U /*!< Write protection error */
I've tried a few examples off the net and wondered if I've set something by mistake.
Can anyone suggest a way forward, please?
Thanks
Ron
2022-01-27 08:17 AM
Is it write protected? Check option bytes in STM32CubeProgrammer.
You may also want to check the flash error bits at the time you unlock the flash. Sometimes the debugger can do bad things.
2022-01-27 09:03 AM
Are you using an STM32F413H Discovery board ?
I noticed that on this board, immediately after boot the Flash IP error flags are always set.
If during boot a piece of code tries to write to Flash memory area , the error flags will be set.
The Flash HAL refuses to do a Flash operation if error flags are set in the Flash SR register.
A workaround is to reset the SR register error flags with call __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);
just after startup.
2022-01-27 09:05 AM
when I write "during boot" it may be the initialisation code before main() (provided by the compiler).
2022-02-01 08:15 AM
Hello @Guillaume K . Thanks for your suggestion.
That did clear the errors that were still existing at the start of 'main()'. I added it before HAL_Init().
I've still been unable to get the above example working, but have managed to get something sort of working on a NUCLEO-F413ZH board. I shall raise another question regarding that issue.