Skip to main content
Visitor II
July 23, 2024
Question

HardFault when erasing flash STM32U535

  • July 23, 2024
  • 2 replies
  • 992 views

Hi,

I'm having trouble with flash erasing on STM32U535.

 

 

	FLASH_EraseInitTypeDef EraseInitStruct;
	volatile uint32_t pageError;
	uint32_t FirstPage = 0, NbOfPages = 0, BankNumber = 0;
	
uint32_t Address = 0, PageError = 0;
	if (HAL_ICACHE_Disable() != HAL_OK)
 {
 Error_Handler();
 }
 HAL_FLASH_Unlock();
	
 FirstPage = GetPage(FLASH_USER_START_ADDR);
 NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;
 BankNumber = GetBank(FLASH_USER_START_ADDR);
 EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
 EraseInitStruct.Banks = BankNumber;
 EraseInitStruct.Page = FirstPage;
 EraseInitStruct.NbPages = NbOfPages;


 if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError)!= HAL_OK)
 {
	while (1)
 {
 Error_Handler();
 }
 }	

 

 

 

 

I used the code from the STM32545 example.

Erasing step brings to hardfault.

 

the definitions are

 

 

#define ADDR_FLASH_PAGE_0 ((uint32_t)0x08000000) /* Base @ of Page 0, 8 Kbytes */
#define ADDR_FLASH_PAGE_1 ((uint32_t)0x08002000) /* Base @ of Page 1, 8 Kbytes */
#define ADDR_FLASH_PAGE_2 ((uint32_t)0x08004000) /* Base @ of Page 2, 8 Kbytes */
#define ADDR_FLASH_PAGE_3 ((uint32_t)0x08006000) /* Base @ of Page 3, 8 Kbytes */
#define ADDR_FLASH_PAGE_4 ((uint32_t)0x08008000) /* Base @ of Page 4, 8 Kbytes */
#define ADDR_FLASH_PAGE_5 ((uint32_t)0x0800A000) /* Base @ of Page 5, 8 Kbytes */
#define ADDR_FLASH_PAGE_6 ((uint32_t)0x0800C000) /* Base @ of Page 6, 8 Kbytes */
#define ADDR_FLASH_PAGE_7 ((uint32_t)0x0800E000) /* Base @ of Page 7, 8 Kbytes */
#define ADDR_FLASH_PAGE_8 ((uint32_t)0x08010000) /* Base @ of Page 8, 8 Kbytes */
#define ADDR_FLASH_PAGE_9 ((uint32_t)0x08012000) /* Base @ of Page 9, 8 Kbytes */
#define ADDR_FLASH_PAGE_10 ((uint32_t)0x08014000) /* Base @ of Page 10, 8 Kbytes */
#define ADDR_FLASH_PAGE_11 ((uint32_t)0x08016000) /* Base @ of Page 11, 8 Kbytes */
#define ADDR_FLASH_PAGE_12 ((uint32_t)0x08018000) /* Base @ of Page 12, 8 Kbytes */
#define ADDR_FLASH_PAGE_13 ((uint32_t)0x0801A000) /* Base @ of Page 13, 8 Kbytes */
#define ADDR_FLASH_PAGE_14 ((uint32_t)0x0801C000) /* Base @ of Page 14, 8 Kbytes */
#define ADDR_FLASH_PAGE_15 ((uint32_t)0x0801E000) /* Base @ of Page 15, 8 Kbytes */
#define ADDR_FLASH_PAGE_16 ((uint32_t)0x08020000) /* Base @ of Page 16, 8 Kbytes */
#define ADDR_FLASH_PAGE_17 ((uint32_t)0x08022000) /* Base @ of Page 17, 8 Kbytes */
#define ADDR_FLASH_PAGE_18 ((uint32_t)0x08024000) /* Base @ of Page 18, 8 Kbytes */
#define ADDR_FLASH_PAGE_19 ((uint32_t)0x08026000) /* Base @ of Page 19, 8 Kbytes */
#define ADDR_FLASH_PAGE_20 ((uint32_t)0x08028000) /* Base @ of Page 20, 8 Kbytes */
#define ADDR_FLASH_PAGE_21 ((uint32_t)0x0802A000) /* Base @ of Page 21, 8 Kbytes */
#define ADDR_FLASH_PAGE_22 ((uint32_t)0x0802C000) /* Base @ of Page 22, 8 Kbytes */
#define ADDR_FLASH_PAGE_23 ((uint32_t)0x0802E000) /* Base @ of Page 23, 8 Kbytes */
#define ADDR_FLASH_PAGE_24 ((uint32_t)0x08030000) /* Base @ of Page 24, 8 Kbytes */
#define ADDR_FLASH_PAGE_25 ((uint32_t)0x08032000) /* Base @ of Page 25, 8 Kbytes */
#define ADDR_FLASH_PAGE_26 ((uint32_t)0x08034000) /* Base @ of Page 26, 8 Kbytes */
#define ADDR_FLASH_PAGE_27 ((uint32_t)0x08036000) /* Base @ of Page 27, 8 Kbytes */
#define ADDR_FLASH_PAGE_28 ((uint32_t)0x08038000) /* Base @ of Page 28, 8 Kbytes */
#define ADDR_FLASH_PAGE_29 ((uint32_t)0x0803A000) /* Base @ of Page 29, 8 Kbytes */
#define ADDR_FLASH_PAGE_30 ((uint32_t)0x0803C000) /* Base @ of Page 30, 8 Kbytes */
#define ADDR_FLASH_PAGE_31 ((uint32_t)0x0803E000) /* Base @ of Page 31, 8 Kbytes */
#define FLASH_USER_START_ADDR ADDR_FLASH_PAGE_4 /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR (ADDR_FLASH_PAGE_31 + FLASH_PAGE_SIZE - 1) /* End @ of user Flash area */

 

 

 

Thanks.

    This topic has been closed for replies.

    2 replies

    Super User
    July 23, 2024

    Erasing the code that is currently running is expected to cause a hard fault. Are you sure you're not using page 4? Show your linker file.

    What is the nature of the hard fault?

    lFurm.1Author
    Visitor II
    July 24, 2024

    Hi,

    When debugging line after line, the error occurs at FLASH_WaitForLastOperation, error code 80.

    When I just run and pause, its in hardfault.

     

    1.png2.png

     

    Thanks

    Super User
    July 24, 2024

    It looks like page 4 might be in use by your user program code. It can not be erased then.

    Change the size of IROM1 section not to include the sectors you want to write to.

    lFurm.1Author
    Visitor II
    July 29, 2024

    Thanks for your answer, I've tried it and it doesn't enter HardFault anymore.

    Now, Flash erase returns HAL_ERROR, When debugging it line by line, seems the error comes from FLASH_WaitForLastOperation with error code 0x00000080 which seems is FLASH_SR_OPTVERR  (option byte) error.

    Any idea why it happens and how to handle it?

    Thanks