cancel
Showing results for 
Search instead for 
Did you mean: 

Flash program and flash erase on micro STM32F746ZGT7

SGasp.1
Senior

Hi everyone... I received a source code from another microcontroller STM32F401RBT6 where another developer was flashing and erasing the flash in this way ..

int16_t FlashEraseApp(void)
{
  HAL_StatusTypeDef res;
  FLASH_EraseInitTypeDef erase_init;
  uint32_t page_error;
  
  erase_init.Banks = FLASH_BANK_1;
  erase_init.TypeErase = FLASH_TYPEERASE_SECTORS;
  erase_init.Sector = 3;
  erase_init.NbSectors = 3;
  erase_init.VoltageRange = FLASH_VOLTAGE_RANGE_3;
 
  HAL_FLASH_Unlock();
  HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)&_app_validity, 0);
  HAL_FLASH_Lock();
 
 
  HAL_FLASH_Unlock();
  res = HAL_FLASHEx_Erase(&erase_init, &page_error);
  HAL_FLASH_Lock();
  
  if (res == HAL_OK)
	  return 0;
 
  return -1;
}

I would like to know if I can keep the same code considering that i have to start to erase from sector 3... Moreover i would like to know If also these routines are still valid for the new micro

int16_t FlashWriteData(uint32_t flash_addr, uint8_t *data, uint32_t size)
{
  short ret;
  uint32_t len, addr;
  
  len = 0;
  addr = flash_addr;
  
  ret = 0;
  HAL_FLASH_Unlock();
  while (size != len) {
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, data[len]) != HAL_OK) {
      ret = -1;
      break;
    }
    len += 1;
    addr += 1;
  }
  HAL_FLASH_Lock();
          
  return ret;
}

Thanks a lot for the kind support

1 ACCEPTED SOLUTION

Accepted Solutions

The debugger shows what the CPU sees. The FLASH memory is actually deleted, but at the address 0x08019000 you seeing a 32 bytes (a single cache line) of data from D-cache.

For a typical data usage the solution is to call SCB_InvalidateDCache_by_Addr() on the erased memory after erase operation. If you are flashing and executing a code without doing a system reset, you will have to also use SCB_InvalidateICache_by_Addr(). But for bootloaders the performance is not critical and the safest and simplest option is to not turn on cache memories at all!

View solution in original post

5 REPLIES 5
SofLit
ST Employee

Dear @SGasp.1​ ,

Normally yes since HAL FLASH APIs are the same except you have to remove the line erase_init.Banks = FLASH_BANK_1; as STM32F746 device is a single bank Flash device.

You can also refer to the example provided in STM32CubeF7 under the following path for your reference: Projects\STM32F746ZG-Nucleo\Examples\FLASH\FLASH_EraseProgram

If you find your question answered by this answer, please mark this topic as answered by selecting Select as best. This will help other users to find that answer faster.

STM32

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Thanks ST Employee.. so

let's supposing i have a bootloader starting at 0x08000000 with size 96k.. so finishing at 0x08018000...

And from this address is starting my app to be updated...

If I use the code above Can I keep my bootloader and I just erase the app flash part..

Is it right?

  erase_init.TypeErase = FLASH_TYPEERASE_SECTORS;
  erase_init.Sector = 3;
  erase_init.NbSectors = 3;
  erase_init.VoltageRange = FLASH_VOLTAGE_RANGE_3;

SofLit
ST Employee

Dear @SGasp.1​ ,

Please refer to the reference manual RM0385 / Table 3. STM32F756xx and STM32F74xxx Flash memory organization of how the sectors are organized.

So in your case you want to keep the first 3 sectors for bootloader and the rest of the Flash will contain your application which is feasible.

For  erase_init.Sector and erase_init.NbSectors parameters, I propose to follow the example I pointed out above. These two parameters are provided by GetSector().

PS: you can also to refer to AN4657 "STM32 in-application programming (IAP) using the USART" for a customized bootloader via UART.

STM32

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
SGasp.1
Senior

Thank STM32 user .. I am running the following code... It should erase everything starting from sector 3

int16_t FlashEraseApp(void)
{
  HAL_StatusTypeDef res;
  FLASH_EraseInitTypeDef erase_init;
  uint32_t page_error;
  /*data has to be in hex format*/
  uint64_t data_validity =0x00;
  char* demo="ciao";
  
  uint32_t address = *(__IO uint32_t*) (ADDRESS_TEST);
 
 
  erase_init.TypeErase = FLASH_TYPEERASE_SECTORS;
  erase_init.Sector = 3;
  erase_init.NbSectors = 4;
  erase_init.VoltageRange = FLASH_VOLTAGE_RANGE_3;
 
  HAL_FLASH_Unlock();
  HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)&_app_validity, data_validity);
  //HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, ADDRESS_TEST, *(uint32_t*)demo);
 
  // cancellazione di tutti i settori dell'app
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR );
  res = HAL_FLASHEx_Erase(&erase_init, &page_error);
  //FLASH_Erase_Sector(FLASH_SECTOR_3, VOLTAGE_RANGE_3);
 
  HAL_FLASH_Lock();
  
  if (res == HAL_OK) {
	  return 0;
  }
 
 
  return -1;
}

But when I read the memory flash I discover that some flash addresses are not deleted... can you help me to understand this ? Thanks a lot... Regards0693W00000CzntoQAB.jpg 

The debugger shows what the CPU sees. The FLASH memory is actually deleted, but at the address 0x08019000 you seeing a 32 bytes (a single cache line) of data from D-cache.

For a typical data usage the solution is to call SCB_InvalidateDCache_by_Addr() on the erased memory after erase operation. If you are flashing and executing a code without doing a system reset, you will have to also use SCB_InvalidateICache_by_Addr(). But for bootloaders the performance is not critical and the safest and simplest option is to not turn on cache memories at all!