Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
EEPROMEmulation.hxx
Go to the documentation of this file.
1
34#ifndef _FREERTOS_DRIVERS_COMMON_EEPROMEMULATION_HXX_
35#define _FREERTOS_DRIVERS_COMMON_EEPROMEMULATION_HXX_
36
37#include "EEPROM.hxx"
38
41extern const char __eeprom_start;
44extern const char __eeprom_end;
45
131{
132protected:
137 EEPROMEmulation(const char *name, size_t file_size);
138
142 {
143 }
144
148 void mount();
149
151 static const size_t SECTOR_SIZE;
152
154 static const size_t BLOCK_SIZE;
155
157 static constexpr unsigned MAX_BLOCK_SIZE = 16;
158
159private:
165
173 void write(unsigned int offset, const void *buf, size_t len) OVERRIDE;
174
180 void read(unsigned int offset, void *buf, size_t len) OVERRIDE;
181
187 #define EEPROMEMU_FLASH_SIZE ((uintptr_t)(&__eeprom_end - &__eeprom_start))
188
192 static const size_t BYTES_PER_BLOCK;
193
195 static const size_t HEADER_BLOCK_COUNT;
196
200 static const bool SHADOW_IN_RAM;
201
202protected:
204 static const uint32_t MAGIC_INTACT;
205
207 static const uint32_t MAGIC_DIRTY;
208
210 static const uint32_t MAGIC_USED;
211
213 static const uint32_t MAGIC_ERASED;
214
227
228private:
233 void write_fblock(unsigned int index, const uint8_t data[]);
234
241 bool read_fblock(unsigned int index, uint8_t data[]);
242
246 unsigned next_active()
247 {
248 unsigned next = activeSector_ + 1;
249 if (next >= sectorCount_) next = 0;
250 return next;
251 }
252
256 unsigned sector_count()
257 {
258 return sectorCount_;
259 }
260
265 unsigned slot_count()
266 {
268 }
269
273 unsigned slot_last()
274 {
275 return rawBlockCount_ - 1;
276 }
277
281 unsigned slot_first()
282 {
283 return MAGIC_COUNT;
284 }
285
292 virtual const uint32_t* block(unsigned sector, unsigned offset) = 0;
293
297 virtual void flash_erase(unsigned sector) = 0;
298
309 virtual void flash_program(unsigned sector, unsigned start_block, uint32_t *data, uint32_t byte_count) = 0;
310
311protected:
313 const uint8_t sectorCount_{(uint8_t)(EEPROMEMU_FLASH_SIZE / SECTOR_SIZE)};
314
316 uint8_t activeSector_{0};
317
319 const uint16_t rawBlockCount_{(uint16_t)(SECTOR_SIZE / BLOCK_SIZE)};
320
323
325 bool shadowInRam_{false};
326
328 uint8_t *shadow_{nullptr};
329
330
334
336};
337
338#endif /* _FREERTOS_DRIVERS_COMMON_EEPROMEMULATION_HXX_ */
#define EEPROMEMU_FLASH_SIZE
Total FLASH memory size to use for EEPROM Emulation.
const char __eeprom_end
Linker-defined symbol where in the memory space (flash) the eeprom emulation data ends.
const char __eeprom_start
Linker-defined symbol where in the memory space (flash) the eeprom emulation data starts.
Emulates EEPROM in FLASH for the Tiva, LPC17xx and LPC40xx platforms.
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
MagicBlockIndex
Raw block indexes within a sector to mark the state of the sector.
@ 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.
@ MAGIC_FIRST_INDEX
first metadata block index
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.
const uint8_t sectorCount_
Total number of sectors available.
unsigned sector_count()
Total number of FLASH sectors being used for emulation.
void updated_notification()
This function will be called after every write.
~EEPROMEmulation()
Destructor.
Common base class for all EEPROM access.
Definition EEPROM.hxx:44
size_t file_size()
Get the maximum file size of the EEPROM file.
Definition EEPROM.hxx:81
const char * name
device name
Definition Devtab.hxx:266
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
Device * next
next device in linked list
Definition Devtab.hxx:538