Functions

256 byte EEPROM emulator
[Libraries (LIB)]


Library emulating a 255 byte EEPROM in nRF24LE1 using the non-volatile data memory.

The library uses the two pages (total 512 bytes) of high endurance non-volatile data memory for emulating a 255-byte EEPROM.

Functions

void lib_eeprom255_byte_write (uint8_t adr, uint8_t dat)
void lib_eeprom255_bytes_write (uint8_t adr, uint8_t *src, uint8_t n)
uint8_t lib_eeprom255_byte_read (uint8_t adr)
void lib_eeprom255_bytes_read (uint8_t adr, uint8_t *dst, uint8_t n)

Function Documentation

void lib_eeprom255_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 30 of file lib_eeprom255_le1.c.

{
  uint8_t xdata i;
  uint16_t xdata flash_dst_pn, xdata_dst_adr, flash_old_pn, xdata_old_adr; 
  
  if(*((uint8_t xdata *)(PAGE_0_XDATA + BK_BYTE)) == 0xff)
  {
    flash_dst_pn = PAGE_0_FLASH_PN;
    xdata_dst_adr = PAGE_0_XDATA;  
    flash_old_pn = PAGE_1_FLASH_PN;
    xdata_old_adr = PAGE_1_XDATA;   
  }
  else
  {
    flash_dst_pn = PAGE_1_FLASH_PN;
    xdata_dst_adr = PAGE_1_XDATA;  
    flash_old_pn = PAGE_0_FLASH_PN;
    xdata_old_adr = PAGE_0_XDATA;   
  }
  
  if((*((uint8_t xdata *)(xdata_old_adr + adr))) != dat)
  {
    hal_flash_page_erase(flash_dst_pn);
    
    PCON &= ~(1 << 4);
    for(i = 0; i < 255; i++)
    {
      if(i == adr)
      {
        hal_flash_byte_write(xdata_dst_adr + adr, dat);
      }
      else
      {
        hal_flash_byte_write(xdata_dst_adr + i, *((uint8_t xdata *)(xdata_old_adr + i)));
      }
    }
  
    hal_flash_byte_write(xdata_dst_adr + BK_BYTE, 0);
    hal_flash_page_erase(flash_old_pn);
  }
}
void lib_eeprom255_bytes_write ( uint8_t  adr,
uint8_t *  src,
uint8_t  n 
)

Function to write n bytes to the EEPROM

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

Definition at line 84 of file lib_eeprom255_le1.c.

{
  uint8_t xdata i;
  uint16_t xdata flash_dst_pn, xdata_dst_adr, flash_old_pn, xdata_old_adr; 
  
  if(*((uint8_t xdata *)(PAGE_0_XDATA + BK_BYTE)) == 0xff)
  {
    flash_dst_pn = PAGE_0_FLASH_PN;
    xdata_dst_adr = PAGE_0_XDATA;  
    flash_old_pn = PAGE_1_FLASH_PN;
    xdata_old_adr = PAGE_1_XDATA;   
  }
  else
  {
    flash_dst_pn = PAGE_1_FLASH_PN;
    xdata_dst_adr = PAGE_1_XDATA;  
    flash_old_pn = PAGE_0_FLASH_PN;
    xdata_old_adr = PAGE_0_XDATA;   
  }

  for(i = 0; i < n; i++)
  {
    if((*((uint8_t xdata *)(xdata_old_adr + adr + i))) != src[i])
    {
      break;
    }
  }

  if(i < n)
  {
    hal_flash_page_erase(flash_dst_pn);
    for(i = 0; i < 255; i++)
    {
      if(i >= adr && i < (adr + n))
      {
        hal_flash_byte_write(xdata_dst_adr + i, src[i - adr]);
      }
      else
      {
        hal_flash_byte_write(xdata_dst_adr + i, *((uint8_t xdata *)(xdata_old_adr + i)));
      }
    }
  
    hal_flash_byte_write(xdata_dst_adr + BK_BYTE, 0);
    hal_flash_page_erase(flash_old_pn);
  }
}
uint8_t lib_eeprom255_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 72 of file lib_eeprom255_le1.c.

{
  if(*((uint8_t xdata *)(PAGE_0_XDATA + BK_BYTE)) == 0)
  {
    return (*((uint8_t xdata *)(PAGE_0_XDATA + adr)));
  }
  else
  {
    return (*((uint8_t xdata *)(PAGE_1_XDATA + adr)));
  }
}
void lib_eeprom255_bytes_read ( uint8_t  adr,
uint8_t *  dst,
uint8_t  n 
)

Function to read n bytes from the EEPROM

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

Definition at line 132 of file lib_eeprom255_le1.c.

{
  uint8_t xdata i;
  uint16_t xdata xdata_src_adr;

  if(*((uint8_t xdata *)(PAGE_0_XDATA + BK_BYTE)) == 0)
  {
    xdata_src_adr = PAGE_0_XDATA;
  }
  else
  {
    xdata_src_adr = PAGE_1_XDATA;
  }
  
  for(i = 0; i < n; i++)
  {
    dst[i] = *((uint8_t xdata *)(xdata_src_adr + adr + i));
  }  
}