Skip to main content
rsoc
Associate III
May 10, 2019
Question

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

  • May 10, 2019
  • 3 replies
  • 1047 views

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();
}

This topic has been closed for replies.

3 replies

Piranha
Principal III
May 10, 2019
FLASH->CR &= CR_PSIZE_MASK;

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

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

rsoc
rsocAuthor
Associate III
May 11, 2019

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.
Super User
May 11, 2019

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

rsoc
rsocAuthor
Associate III
May 12, 2019

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.
Super User
May 13, 2019

>  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