STM32H503RBT6 Flash erase
- January 7, 2026
- 3 replies
- 410 views
I have a requirement to implement CAN based bootloader in my STM32h503rbt6 controller. Right now iam working on the testing of Flash functions which i have written on my own.My flash write, read and erase all are working fine.but when iam erasing flash sector wise,here comes the problem.after sector 7, from sector 8 onwards with y function i cant abe to erase.but i want full memory controll over my st controller flash.then only i can decide which part of flash i can use for my bootloader and which part of flash i can use for my application.
HAL_StatusTypeDef Flash_Erase_Sector(uint32_t sector)
{
while (FLASH->NSSR & FLASH_SR_BSY);
if (FLASH->NSCR & FLASH_CR_LOCK)
{
FLASH->NSKEYR = FLASH_KEY1;
FLASH->NSKEYR = FLASH_KEY2;
}
/* Clear valid flags only */
FLASH->NSCCR =
FLASH_CCR_CLR_EOP
| FLASH_CCR_CLR_PGSERR
| FLASH_CCR_CLR_WRPERR;
FLASH->NSCR &= ~FLASH_CR_SNB_Msk;
FLASH->NSCR |= FLASH_CR_SER;
FLASH->NSCR |= (sector << FLASH_CR_SNB_Pos);
FLASH->NSCR |= FLASH_CR_START;
while (FLASH->NSSR & FLASH_SR_BSY);
FLASH->NSCR &= ~FLASH_CR_SER;
FLASH->NSCR |= FLASH_CR_LOCK;
return HAL_OK;
}Even after checking the datasheet, I could not identify why erasing sectors beyond sector 7 is failing.
Please help me identify the root cause and provide a proper solution.i have attached my project file
Edited to apply source code formatting - please see How to insert source code for future reference.
