5#define _FREERTOS_DRIVERS_COMMON_EEPROM_HXX_
29 virtual void write(
unsigned int index,
const void *buf,
size_t len) = 0;
39 virtual void read(
unsigned int index,
void *buf,
size_t len) = 0;
53#define protected public
58static const char FILENAME[] =
"/tmp/eeprom";
94 memset(foo::__eeprom_start, 0xFF, EELEN);
108 ASSERT_LE(0u, sector);
110 void* address = &foo::__eeprom_start[sector *
SECTOR_SIZE];
115 ASSERT_LE(0u, sector);
117 ASSERT_LE(0u,
block);
121 memcpy(address, data, byte_count);
124 const uint32_t*
block(
unsigned sector,
unsigned index)
override {
128 return (uint32_t*) address;
132TEST(EepromStaticTest, assertions) {
134 volatile size_t p2 = (
volatile size_t)&foo::__eeprom_start[0];
139 ASSERT_EQ(p1 + EELEN, e1);
140 ASSERT_EQ(0u, p1 % 4);
159 ee()->
write(ofs, payload.data(), payload.size());
164 return static_cast<EEPROM*
>(
e.operator->());
170 uint32_t* address = (uint32_t*)&foo::__eeprom_start[block_number * EEBLOCKSIZE];
171 uint8_t data[EEBLOCKSIZE / 2];
172 for (
int i = 0; i < EEBLOCKSIZE / 4; ++i) {
173 data[(i * 2) + 0] = (address[i] >> 0) & 0xFF;
174 data[(i * 2) + 1] = (address[i] >> 8) & 0xFF;
176 return string((
char*)data, EEBLOCKSIZE / 2);
182 unsigned avail =
e->avail();
183 for (
int i = 0; i < 27000; ++i) {
184 char d[1] = {
static_cast<char>(i & 0xff)};
186 if (
e->avail() > avail)
return;
195 uint32_t* address = (uint32_t*)&foo::__eeprom_start[block_number * EEBLOCKSIZE];
196 return ((*address) >> 16) * (EEBLOCKSIZE / 2);
199#define EXPECT_AT(ofs, PAYLOAD) { string p(PAYLOAD); string ret(p.size(), 0); ee()->read(ofs, &ret[0], p.size()); EXPECT_EQ(p, ret); }
201#define EXPECT_SLOT(block_number, address, payload) { EXPECT_EQ((unsigned)address, block_address(block_number)); EXPECT_EQ(string(payload), block_data(block_number)); }
204 std::unique_ptr<MyEEPROM>
e;
210 EXPECT_EQ(0, e->activeSector_);
211 EXPECT_EQ(8u, e->sector_count());
213 EXPECT_EQ((uint32_t*)&foo::__eeprom_start[4*1024], e->block(1, 0));
219 write_to(13,
"abcd");
220 EXPECT_SLOT(3, 12,
"\xFF""a");
221 EXPECT_SLOT(4, 14,
"bc");
222 EXPECT_SLOT(5, 16,
"d\xFF");
223 EXPECT_SLOT(6, 2*0xFFFF,
"\xFF\xFF");
225 EXPECT_AT(13,
"abcd");
230 EXPECT_AT(12,
"upb");
231 EXPECT_AT(12,
"upbcd");
233 EXPECT_AT(12,
"kqbcd");
234 EXPECT_AT(12,
"kqbcd\xFF");
238 EXPECT_AT(12,
"kqbcd\xFF");
246 write_to(13,
"abcd");
247 EXPECT_AT(13,
"abcd");
251 EXPECT_AT(13,
"abcd");
256 write_to(13,
"abcd");
257 EXPECT_AT(13,
"abcd");
258 EXPECT_SLOT(3, 12,
"\xFF""a");
259 EXPECT_SLOT(4, 14,
"bc");
260 EXPECT_SLOT(5, 16,
"d\xFF");
262 EXPECT_EQ(1, e->activeSector_);
263 EXPECT_SLOT(3, 12,
"\xFF""a");
264 EXPECT_SLOT(4, 14,
"bc");
265 EXPECT_SLOT(5, 16,
"d\xFF");
266 EXPECT_SLOT(blocks_per_sector + 3, 12,
"\xFF""a");
267 EXPECT_SLOT(blocks_per_sector + 4, 14,
"bc");
268 EXPECT_SLOT(blocks_per_sector + 5, 16,
"d\xFF");
270 EXPECT_AT(13,
"abcd");
272 EXPECT_AT(13,
"abcd");
274 EXPECT_AT(13,
"abcd");
275 EXPECT_EQ(3, e->activeSector_);
277 EXPECT_AT(13,
"abcd");
278 EXPECT_EQ(3, e->activeSector_);
283 write_to(13,
"abcd");
284 EXPECT_AT(13,
"abcd");
287 for (
int i = 0; i < 20; ++i) {
290 EXPECT_AT(13,
"abcd");
291 unsigned s = e->activeSector_;
293 EXPECT_AT(13,
"abcd");
294 EXPECT_EQ(s, e->activeSector_);
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.
static const size_t BLOCK_SIZE
block size in bytes
static const size_t SECTOR_SIZE
Sector size in bytes.
size_t availableSlots_
Number of available (writable) slots for new data in the active sector.
void mount()
Mount the EEPROM file.
static const size_t BYTES_PER_BLOCK
useful data bytes size in bytes
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 fileSize
Maximum file size we can grow to.
virtual void read(unsigned int index, void *buf, size_t len)=0
Override this function to read data from the eeprom.
virtual void write(unsigned int index, const void *buf, size_t len)=0
Override this function to write data to the eeprom.
EEPROM(const char *name, size_t file_size)
Constructor.
size_t file_size()
Get the maximum file size of the EEPROM file.
Test fixture class for testing the EEPROM emulation.
string block_data(unsigned block_number)
void overflow_block()
Write enough much data to the eepromemu under test to overflow the current block.
void write_to(unsigned ofs, const string &payload)
Helper function to write to the test eeprom.
void create(bool clear=true)
Creates the eeprom under test.
std::unique_ptr< MyEEPROM > e
EEPROM under test.
static constexpr unsigned eeprom_size
test eeprom size
uint32_t block_address(unsigned block_number)
const char * name
device name
Test EEPROM emulation HAL implementation that writes to a block of (RAM) memory.
void flash_program(unsigned sector, unsigned block, uint32_t *data, uint32_t byte_count) override
Simple hardware abstraction for FLASH program API.
MyEEPROM(size_t file_size, bool clear=true)
Contructor.
const uint32_t * block(unsigned sector, unsigned index) override
Computes the pointer to load the data stored in a specific block from.
void flash_erase(unsigned sector) override
Simple hardware abstraction for FLASH erase API.
#define LOG(level, message...)
Conditionally write a message to the logging output.
static const int INFO
Loglevel that is printed by default, reporting some status information.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.