cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H503RBT6 Flash erase

PRAKKU12
Associate

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.

5 REPLIES 5
TDK
Super User

> I could not identify why erasing sectors beyond sector 7 is failing. Please help me identify the root cause and provide a proper solution.

The STM32H503RBT6 only has 8 sectors per bank. You can't erase sectors that don't exist. That is the root cause.

TDK_0-1767762747545.png

TDK_1-1767762756759.png

The proper solution would be to restrict yourself to sectors that exist, or move to a chip with more memory if you need it.

 

Maybe you meant to erase sector 0 of bank 2 rather than "sector 8".

If you feel a post has answered your question, please click "Accept as Solution".

in that case how can i erase bank 2 sector 0.

 

gbm
Principal

Well, set the bank number to 1 and sector number to 0 in FLASH->CR.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

I suggest using HAL_FLASHEx_Erase.

stm32h5xx-hal-driver/Src/stm32h5xx_hal_flash_ex.c at b1ef40f3a522d073cfa70d647172206707acd740 · STMicroelectronics/stm32h5xx-hal-driver

If you feel a post has answered your question, please click "Accept as Solution".
Andrew Neil
Super User

As @TDK said, look at the HAL_FLASHEx_Erase() function:

AndrewNeil_0-1767796926446.png

The FLASH_EraseInitTypeDef structure contains  fields to specify the Sector & Bank:

AndrewNeil_1-1767796983083.png

 

See: UM3132, Description of STM32H5 HAL and low-layer drivers

via: https://www.st.com/en/embedded-software/stm32cubeh5.html#documentation

via: https://www.st.com/en/microcontrollers-microprocessors/stm32h503rb.html#tools-software

 

 


@PRAKKU12 wrote:

how can i erase bank 2 sector 0..


As @gbm suggested, you select the Bank in the Flash configuration register:

AndrewNeil_2-1767797622248.png

https://www.st.com/resource/en/reference_manual/rm0492-stm32h503-line-armbased-32bit-mcus-stmicroelectronics.pdf#page=128

via: https://www.st.com/en/microcontrollers-microprocessors/stm32h503rb.html#documentation

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.