cancel
Showing results for 
Search instead for 
Did you mean: 

Implementing a sector erase using the HAL layer for flash manipulation--Updated

rsoc
Associate III

I have been attempting to implement the HAL layer for flash erasing, and decided the most effective erase is sequential sector erases for my application (creating a custom bootloader).

I have found that calling a single sector erase does not cause any issues for my MCU. However, if I do multiple sector erases sequentially, the MCU crashes. Am I missing something fundamental? Is this not possible?

// Erase current flash
uint8_t Bootloader_Erase(void)
{
  HAL_FLASH_Unlock();
  FLASH_Erase_Sector(FLASH_SECTOR_3);
  FLASH_Erase_Sector(FLASH_SECTOR_4);
  FLASH_Erase_Sector(FLASH_SECTOR_5);
  HAL_FLASH_Lock();
}

5 REPLIES 5
Piranha
Chief II
FLASH->CR &= CR_PSIZE_MASK;

This line doesn't do what it was intended to do.

P.S. Use "Code Snippet" for source code!

Wouldn't the premise of that line be to clear the bit before the next is set? And thank you for the tip on the code snippet, I will use that from now on!

Pavel A.
Evangelist III

If you've decided to use the HAL library for flash, just use it? Why do you write registers access stuff by hand?

Note that the voltage range parameter actually means the bit width of flash operations: 8/16/32/64 where available.

Selecting this correctly needs attention.

-- pa

This is the HAL library for erasing. It was not functioning for me, so I went down into the bowels to see if there is something fishy going on. And agreed with the voltage range parameter. Our system is running at low voltage in the 8 bit range, so thats why i manually set it to that. Would there be issues in that instance?

Pavel A.
Evangelist III

>  It was not functioning for me, so I went down into the bowels

This is strange. The HAL flash code is ugly, but usually works for me. Check carefully the parameters, and especially the "voltage" or the bit width.

-- pa