How to write to STM32 Flash

b7031719

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?

Barış İşbiliroğlu

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to flash STM32 via Serial Port

How to boot STM32 from user flash?

DMA to Flash for STM32

STM32 Web server - How html file in "SPI Flash" read "image" in flash

How can I find the program STM32_Programmer_CLI.exe to write a bin file in the flash of STM32F429?

is stm32 erasing flash even it not writen?

mbed compiler: how to programatically read & write flash STM32F091RC on Nucleo eval board

STM32 HardFault when booted from Flash

Understanding the writing to flash process in the STM32 reference manual

Why are the FLASH pages not being cleared on the stm32?

Can´t erase data from flash memory (STM32)

How far is it safe to regularly write to the ESP32 flash, considering the flash MTBF?

(STM32) Erasing flash and writing to flash gives HAL_FLASH_ERROR_PGP error (using HAL)

How to get time intervals on STM32?

How to check if stm32 is working?

Getting CRC-32 over STM32 flash and consistency with other CRC-32 tools

How to set STM32 to generate standard CRC32

How to write DRY flash messages for ajax requests?

How to write FreeBSD image to USB flash drive

How to write the Flash actionscript to makes this mouse action?

How to remove write protection from a flash drive?

STM32 External flash reading device ID using HAL libraries

stm32 and external flash (w25q) connection problem

why Flash global interrupt handler not defined in stm32 startup code?

STM32: Code execution seems to depend on its location in flash memory

How to update with custom stm32 board with my owm software

How to use LL (low level) drivers in CubeMX STM32?

STM32 - How to trigger interrupt after a certain PWM ON time?

How to check size of SDRAM wizh STM32 and HAL

TOP Ranking

HotTag

Archive