132 uint8_t* byte_data = (uint8_t*)buf;
133 uint8_t* shadow_data = (uint8_t*)buf;
134 unsigned shadow_index = index;
135 size_t shadow_len = len;
149 if (memcmp(data + lsa, byte_data, write_size) != 0)
152 memcpy(data + lsa, byte_data, write_size);
158 byte_data += write_size;
166 if (memcmp(data, byte_data, len) != 0)
169 memcpy(data, byte_data, len);
196 memcpy(
shadow_ + shadow_index, shadow_data, shadow_len);
212 for (
unsigned int i = 0; i <
BLOCK_SIZE /
sizeof(uint32_t); ++i)
214 slot_data[i] = (index << 16) |
215 (data[(i * 2) + 1] << 8) |
216 (data[(i * 2) + 0] << 0);
240 for (
unsigned int i = 0; i <
BLOCK_SIZE /
sizeof(uint32_t); ++i)
242 slot_data[i] = (index << 16) |
243 (data[(i * 2) + 1] << 8) |
244 (data[(i * 2) + 0] << 0);
256 for (
unsigned int i = 0; i <
BLOCK_SIZE /
sizeof(uint32_t); ++i)
258 slot_data[i] = (fblock << 16) |
259 (read_data[(i * 2) + 1] << 8) |
260 (read_data[(i * 2) + 0] << 0);
288 memcpy(buf,
shadow_ + offset, len);
292 uint8_t *byte_data = (uint8_t *)buf;
293 memset(byte_data, 0xff, len);
302 if (offset + len <= slot_offset)
312 for (
unsigned int i = 0; i <
BLOCK_SIZE /
sizeof(uint32_t); ++i)
314 data[(i * 2) + 0] = (address[i] >> 0) & 0xFF;
315 data[(i * 2) + 1] = (address[i] >> 8) & 0xFF;
318 unsigned slotofs, bufofs;
319 if (slot_offset < offset)
321 slotofs = offset - slot_offset;
327 bufofs = slot_offset - offset;
335 memcpy(byte_data + bufofs, data + slotofs, copylen);
369 if (index == (*address >> 16))
372 for (
unsigned int i = 0; i <
BLOCK_SIZE /
sizeof(uint32_t); ++i)
374 data[(i * 2) + 0] = (address[i] >> 0) & 0xFF;
375 data[(i * 2) + 1] = (address[i] >> 8) & 0xFF;
#define EEPROMEMU_FLASH_SIZE
Total FLASH memory size to use for EEPROM Emulation.
uint8_t activeSector_
Index of the active sector.
bool read_fblock(unsigned int index, uint8_t data[])
Read from the EEPROM on a native block boundary.
static const size_t HEADER_BLOCK_COUNT
number of reserved header blocks
virtual const uint32_t * block(unsigned sector, unsigned offset)=0
Computes the pointer to load the data stored in a specific block from.
static const uint32_t MAGIC_ERASED
magic marker for an erased block
virtual void flash_program(unsigned sector, unsigned start_block, uint32_t *data, uint32_t byte_count)=0
Simple hardware abstraction for FLASH program API.
void write(unsigned int offset, const void *buf, size_t len) OVERRIDE
Write to the EEPROM.
static const uint32_t MAGIC_USED
magic marker for a used block
unsigned next_active()
Get the next active sector pointer.
static const bool SHADOW_IN_RAM
Shadow the EEPROM data in RAM.
void write_fblock(unsigned int index, const uint8_t data[])
Write to the EEPROM on a native block boundary.
static const size_t BLOCK_SIZE
block size in bytes
@ MAGIC_DIRTY_INDEX
dirty metadata block index: programmed when we start writing to the sector (i.e.
@ MAGIC_USED_INDEX
used metadata block index: programmed when the data has been copied to a new sector.
@ MAGIC_COUNT
total metadata block count
@ MAGIC_INTACT_INDEX
intact metadata block index: programmed when the data in this sector is authoritative.
static constexpr unsigned MAX_BLOCK_SIZE
Maximum byte size of a single block.
const uint16_t rawBlockCount_
How many blocks are there in a sector.
static const uint32_t MAGIC_DIRTY
magic marker for a block that we are transitioning to intact
EEPROMEmulation()
Default constructor.
bool shadowInRam_
local copy of SHADOW_IN_RAM which we can manipulate at run time.
static const size_t SECTOR_SIZE
Sector size in bytes.
void read(unsigned int offset, void *buf, size_t len) OVERRIDE
Read from the EEPROM.
size_t availableSlots_
Number of available (writable) slots for new data in the active sector.
virtual void flash_erase(unsigned sector)=0
Simple hardware abstraction for FLASH erase API.
void mount()
Mount the EEPROM file.
static const size_t BYTES_PER_BLOCK
useful data bytes size in bytes
static const uint32_t MAGIC_INTACT
magic marker for an intact block
uint8_t * shadow_
pointer to RAM for shadowing EEPROM.
unsigned slot_count()
Total number of EEPROM slots in a FLASH sector.
unsigned sector_count()
Total number of FLASH sectors being used for emulation.
void updated_notification()
This function will be called after every write.
Common base class for all EEPROM access.
size_t file_size()
Get the maximum file size of the EEPROM file.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.