Detailed explanation of the use of STM8 internal EEPROM

1 memory map

STM8S105It integrates up 1Kto EEPROM(the data will not be lost after power-off), which can support up to 300,000 erasing and writing times, and the user can save some data in EEPROMit, memory mapas shown in the figure below;

The size of a page of memory here is 64 bytes( 1 block), and DATA EEPROMthe memory address mapping is shown in the figure below;

As you can see, EEPROMthe start address is 0x004000, and the end address is 0x00427F. This will be used in the programming below. Of course, the specific size will vary depending on the model;

2 program implementation

The following program is based on stm8the standard library, and the corresponding standard library can be stdownloaded on the official website;

eeprom_inner.ch

#ifndef EEPROM_INNER_H
#define EEPROM_INNER_H

#include "stm8s.h"

#define EEPROM_BASE_ADDR    0x4000
#define EEPROM_SIZE         0x07FF

union data32_unit_mod {
    
    

  uint32_t data32;
  uint8_t  buf[4];
};

typedef union data32_unit_mod data32_unit_mod_t;

union data16_unit_mod {
    
    

  uint16_t data16;
  uint8_t  buf[2];
};
typedef union data16_unit_mod data16_unit_mod_t;

/**
    外部函数声明
*/
void eeprom_init(void);
uint8_t eeprom_read_byte(uint16_t addr);
void eeprom_write_byte(uint16_t addr,uint8_t data);

void eeprom_write_data32(uint16_t offset, uint32_t data);
void eeprom_write_data16(uint16_t offset, uint16_t data);
void eeprom_write_data8(uint16_t offset, uint8_t data);

uint32_t eeprom_read_data32(uint16_t offset);
uint16_t eeprom_read_data16(uint16_t offset);
uint8_t eeprom_read_data8(uint16_t offset);

void eeprom_area_clear(void);

#endif

eeprom_inner.c

#include "eeprom_inner.h"
#include "stm8s_clk.h"
#include "stm8s_flash.h"

void eeprom_init(void)
{
    
    
    FLASH_DeInit();
    FLASH_Unlock(FLASH_MEMTYPE_DATA);//EEPROM
    FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_TPROG); 
}

inline uint8_t eeprom_read_byte(uint16_t addr)
{
    
        
    return FLASH_ReadByte(addr);
}

inline void eeprom_write_byte(uint16_t addr,uint8_t data)
{
    
    
    FLASH_Unlock(FLASH_MEMTYPE_DATA);
    FLASH_ProgramByte(addr, data);
    FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);
    FLASH_Lock(FLASH_MEMTYPE_DATA);
}

void eeprom_write_data32(uint16_t offset, uint32_t data){
    
    
    
    data32_unit_mod_t tmp;
    tmp.data32 = data;
    if(offset+4 > EEPROM_SIZE){
    
    
        //error
        return;
    }
    eeprom_write_byte(EEPROM_BASE_ADDR+offset,  tmp.buf[0]);
    eeprom_write_byte(EEPROM_BASE_ADDR+offset+1,tmp.buf[1]);
    eeprom_write_byte(EEPROM_BASE_ADDR+offset+2,tmp.buf[2]);
    eeprom_write_byte(EEPROM_BASE_ADDR+offset+3,tmp.buf[3]);
}

void eeprom_write_data16(uint16_t offset, uint16_t data){
    
    
    
    data16_unit_mod_t tmp;
    tmp.data16 = data;
    if(offset+2 > EEPROM_SIZE){
    
    
        //error
        return;
    }
    eeprom_write_byte(EEPROM_BASE_ADDR+offset,  tmp.buf[0]);
    eeprom_write_byte(EEPROM_BASE_ADDR+offset+1,tmp.buf[1]);
}

void eeprom_write_data8(uint16_t offset, uint8_t data){
    
    
    
    if(offset+1 > EEPROM_SIZE){
    
    
        //error
        return;
    }
    eeprom_write_byte(EEPROM_BASE_ADDR+offset,  data);
}

uint32_t eeprom_read_data32(uint16_t offset){
    
    
    
    data32_unit_mod_t tmp;
    if(offset+4 > EEPROM_SIZE){
    
    
        //error
        return 0xEEEEEEEE;
    }

    tmp.buf[0] = eeprom_read_byte(EEPROM_BASE_ADDR+offset);
    tmp.buf[1] = eeprom_read_byte(EEPROM_BASE_ADDR+offset+1);
    tmp.buf[2] = eeprom_read_byte(EEPROM_BASE_ADDR+offset+2);
    tmp.buf[3] = eeprom_read_byte(EEPROM_BASE_ADDR+offset+3);
    
    return tmp.data32;
}

uint16_t eeprom_read_data16(uint16_t offset){
    
    
    
    data16_unit_mod_t tmp;
    if(offset+2 > EEPROM_SIZE){
    
    
        //error
        return 0xEEEE;
    }
    tmp.buf[0] = eeprom_read_byte(EEPROM_BASE_ADDR+offset);
    tmp.buf[1] = eeprom_read_byte(EEPROM_BASE_ADDR+offset+1);
    
    return tmp.data16;
}

uint8_t eeprom_read_data8(uint16_t offset){
    
    

    if(offset+1 > EEPROM_SIZE){
    
    
        //error
        return 0xEE;
    }
    
    return eeprom_read_byte(EEPROM_BASE_ADDR+offset);
}

void eeprom_area_clear(void){
    
    

    uint16_t index = 0;
    for( ; index < EEPROM_SIZE; index+=2 ){
    
    
        eeprom_write_data16(index,0xFFFF);
    }
}

Guess you like

Origin blog.csdn.net/u010632165/article/details/107846877