Functions

16 byte EEPROM emulator
[Libraries (LIB)]


Library emulating a 16 byte high endurance EEPROM using one page of on-chip Flash memory.

Functions

void lib_eeprom_byte_write (uint8_t adr, uint8_t dat)
void lib_eeprom_bytes_write (uint8_t adr, uint8_t *p, uint8_t n)
uint8_t lib_eeprom_byte_read (uint8_t adr)
void lib_eeprom_bytes_read (uint8_t adr, uint8_t *p, uint8_t n)

Function Documentation

void lib_eeprom_byte_write ( uint8_t  adr,
uint8_t  dat 
)

Function to write a byte to the EEPROM

Parameters:
adr8 bit address in EEPROM
datbyte to write

Definition at line 50 of file lib_eeprom.c.

{
    uint8_t i;
    uint8_t xdata *ea = get_eeprom_address(adr);
    if (dat == *ea)
        return;
    if (*ea != 0xff)
    {
        ea -= adr;
        for(i=0;i<EEPROM_SIZE;i++)
            eeprom_buf[i] = *ea++;
        eeprom_buf[adr] = dat;
        if (ea == (HAL_DATA_NV_BASE_ADDRESS + HAL_FLASH_PAGE_SIZE))
        {
          hal_flash_page_erase(HAL_DATA_NV_FLASH_PN0);
         
          if(HAL_DATA_NV_FLASH_PAGES == 2)
          {
            hal_flash_page_erase(HAL_DATA_NV_FLASH_PN1);
          }
             
          ea = (uint8_t xdata *)(HAL_DATA_NV_BASE_ADDRESS + EEPROM_SIZE);
        }
        else
        {
            if (*fa == 0x00)
                fa++;
            if (*fa == 0xff)
                hal_flash_byte_write((uint16_t)fa, 0xf0);
            else
                hal_flash_byte_write((uint16_t)fa, 0x00);
        }
        for(i=0;i<EEPROM_SIZE;i++)
        {
            hal_flash_byte_write((uint16_t)ea, eeprom_buf[i]);
            ea++;
        }
    }
    else
    {
        // When the byte at current location is 0xff we write the
        // byte directly to the flash:
        hal_flash_byte_write((uint16_t)ea, dat);
    }
}
void lib_eeprom_bytes_write ( uint8_t  adr,
uint8_t *  p,
uint8_t  n 
)

Function to write n bytes to the EEPROM

Parameters:
adr8 bit address in EEPROM
*ppointer to bytes to write
nnumber of bytes to write

Definition at line 96 of file lib_eeprom.c.

{
    while(n--)
        lib_eeprom_byte_write(adr++, *p++);
}
uint8_t lib_eeprom_byte_read ( uint8_t  adr )

Function to read a byte from the EEPROM

Parameters:
adr8 bit address in EEPROM
Returns:
the byte read

Definition at line 102 of file lib_eeprom.c.

{
    uint8_t xdata *fa = get_eeprom_address(adr);
    return *fa;
}
void lib_eeprom_bytes_read ( uint8_t  adr,
uint8_t *  p,
uint8_t  n 
)

Function to read n bytes from the EEPROM

Parameters:
adr8 bit address in EEPROM
*ppointer to bytes to write
nnumber of bytes to read

Definition at line 108 of file lib_eeprom.c.

{
    uint8_t xdata *fa = get_eeprom_address(adr);
    while(n--)
    {
        *p++ = *fa++;
    }
}