Purgoufr
Purgoufr

Reputation: 972

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?

Upvotes: 0

Views: 913

Answers (2)

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;
    ...

Upvotes: 2

unwind
unwind

Reputation: 399871

You seem to say "page" when you mean "byte", and that doesn't make a lot of sense.

The smallest erasable unit of the STM32's flash is often called a "sector", and is much larger than a single byte.

It's possible to program (i.e. write, i.e. turn 1s into 0s) single words, but you cannot erase (i.e. turn 0s into 1s) less than a certain limit at a time. Usually the sector size is multiple kilobytes, and can also vary over the address space.

Upvotes: 3

Related Questions