Search code examples
cstm32keilflash-memory

How to delete multiple flash addresses in C?


I try to erase flash address in stm32l011k4. My code like that;

#define SLAVE_ID_ADDR_I                             0x08080001
#define SLAVE_ID_ADDR_II                            0x08080002
#define SLAVE_ID_ADDR_III                           0x08080003
#define MASTERID                                    0x08080000

void software_erase(void){  

        HAL_FLASH_Unlock();

    /* Fill EraseInit structure*/
    EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
    EraseInitStruct.PageAddress = SlaveID_III;              
    EraseInitStruct.NbPages     = 4;                                    

    if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
    {
        playTone=3;
    }
    else{           
        MasterID = 0;
        SlaveID_I = 0;
        SlaveID_II = 0;
        SlaveID_III = 0;
        MasterID_loaded = 0;
        SlaveID_loaded_I = 0;
        SlaveID_loaded_II = 0;
        SlaveID_loaded_III = 0;
        clear_keyfobs = 1;
        playTone=2;
    }
}

Edit: But I want to erase bytes between 0x08080001 - 0x08080003. Not all sections. It's means "0x08080001, 0x08080002, 0x08080003" must be deleted but "0x08080000" must be remain.

Any suggestion?


Solution

  • The addresses are pointing to EEPROM, not flash.

    enter image description here

    You don't have to erase anything in EEPROM, just unlock it and write the new values.

    However, in order to write a byte, you'd need a properly dereferenced pointer, an integer constant won't work.

    #define SLAVE_ID_ADDR_I   (*(volatile unsigned char *)0x08080001)
    #define SLAVE_ID_ADDR_II  (*(volatile unsigned char *)0x08080002)
    #define SLAVE_ID_ADDR_III (*(volatile unsigned char *)0x08080003)
    #define MASTERID          (*(volatile unsigned char *)0x08080000)
    
    void software_erase(void)
    {
        if(FLASH->PECR & FLASH_PECR_PELOCK)
            HAL_FLASH_Unlock();
        SLAVE_ID_ADDR_I = 0;
        ...