cancel
Showing results for 
Search instead for 
Did you mean: 

fail to write flash into STM32C0, HadFault_Handler()

Hollis_K
Associate III

Hi, may have some reference on writing flash into STM32C0 using STM32CubeIDE? I tried to write program just to eraser flash and it enter the function HardFault_Handler(). Here are my codes: 

 

void myFlash_init(void)
{
FLASH_EraseInitTypeDef flash_erase_struct = {0};
HAL_FLASH_Unlock();
flash_erase_struct.TypeErase = FLASH_TYPEERASE_PAGES;
flash_erase_struct.Page = 3;
flash_erase_struct.NbPages = 3;
 
uint32_t  error_status = 0;
HAL_FLASHEx_Erase(&flash_erase_struct, &error_status);
}
 
void myFlash(void)
{
uint64_t data64 = 0x1234567812345678;
 
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, FLASH_ADDRESS_START, data64);
HAL_FLASH_Lock();
}
 
Does any peripheral have to be include if using flash? 
3 REPLIES 3
Hollis_K
Associate III

I noticed that STM32C0x will enter HardFault_Handler after the myFlash_init() function. May I know how to it cause and how to avoid? 

Simon.T
ST Employee

Hello @Hollis_K ,

 

Are you executing your code from Flash or Ram ? 

 

Also, there is an example available on the STM32Cube_FW_C0_V1.1.0 library called "FLASH_EraseProgram" (https://github.com/STMicroelectronics/STM32CubeC0/tree/main/Projects/NUCLEO-C031C6/Examples/FLASH/FLASH_EraseProgram). You can try it and see if it is working (it should).

 

Best regards,

 

Simon 

 

Hollis_K
Associate III

Hello @Simon.T ,

 

I am trying to execute my code in flash. 

 

Thanks for the information, i will try the example and understand the algorithm. If there are any issue, will inform here again, Thank you.