How to write to STM32 Flash
Asked Answered
A

1

6

I want to write to flash Sector 11 of STM32F407VGT from my user code to store some data. I have used the stm32f4xx_hal_flash.c library. I first erase the sector using this code:

void Flash_Init(void)
{        
    FLASH_EraseInitTypeDef pEraseInit;

    pEraseInit.Banks = FLASH_BANK_1;
    pEraseInit.NbSectors = 1;
    pEraseInit.Sector = FLASH_SECTOR_10;
    pEraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;
    pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;

    if(HAL_FLASH_Unlock() == HAL_OK)
    {
        __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
        HAL_FLASHEx_Erase(&pEraseInit,0);
        HAL_FLASH_Lock();
    }
}

The program hangs when it reaches the HAL_FLASHEx_Erase(&pEraseInit,0); function. My scatter file looks like this:

LR_IROM1 0x08000000 0x01000000  {    ; load region size_region
  ER_IROM1 0x08000000 0x01000000  {  ; load address = execution address
   *.o (RESET, +First)
   *(InRoot$$Sections)
   .ANY (+RO)
  }
  RW_IRAM1 0x20000000 0x00020000  {  ; RW data
   .ANY (+RW +ZI)
  }
  RW_IRAM2 0x10000000 0x00010000  {
   .ANY (+RW +ZI)
  }
}

Is there something I must do first to allow this function to work?

Afrit answered 12/1, 2019 at 12:58 Comment(5)
Define hangs. Where is your code executing from (are you erasing flash that stores your current code)?Chitkara
Hi thanks for the reply. I'm not sure what you mean when you ask where is it executing from. The function is called from main. In this example I am trying to erase flash sector 10 (0x080C0000 to 0x080DFFFF) which is a 12kbyte block in main flash memory. The program size according to the build output is "Program Size: Code=21432 RO-data=11152 RW-data=152 ZI-data=2152" so I find it hard to believe my sector erase function is overwriting part of the program. Sectors 0 to 3 are 16 kbyte each, sector 4 is 64kbyte and sector 5 to 11 are 128kbyte.Afrit
Ah, yes, that adds up. Probably not erasing your code then. Still, what does hangs mean? Does the datasheet mention any precautions when erasing flash?Chitkara
By hangs I mean the function HAL_FLASH_Lock(); never executes. From the debugger I can see that this area of memory is being erased (all F's) when it is called but the next function isn't called. I can't see anything in the data sheet or .c file that would prevent this function from executing. The flash control register access has to be unlocked before any operation but the program manages that sucessfully. I was thinking that my scatter file isn't properly configured but other than that I am not sure?Afrit
you can use following answer for your issue. #44444119Bronze
S
9

You want to write sector 11 but your pEraseInit.Sector variable is FLASH_SECTOR_10 in your init function. So you should change FLASH_SECTOR_10 to FLASH_SECTOR_11. Also if you are use CubeMX you can try following write and read function without init function.

uint32_t flash_read(uint32_t address){
    return *(uint32_t*)address;
}

void flash_write(uint32_t address, uint32_t data){
    HAL_FLASH_Unlock();
    FLASH_Erase_Sector(FLASH_SECTOR_11,VOLTAGE_RANGE_1);
    HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,address,data);
    HAL_FLASH_Lock();
}

You can see flash memoty map from here

Superstitious answered 22/1, 2019 at 12:53 Comment(1)
Thanks, after reviewing your code it turns out that the HAL_FLASHEx_Erase(); function does not work but FLASH_Erase_Sector(); does. Having looked at the library I'm not sure why but at least it solves my problem thank you.Afrit

© 2022 - 2024 — McMap. All rights reserved.