Browse Source

Added EAT - EEPROM Allocation Table - storage management

master
Adam Pippin 4 years ago
parent
commit
ad41fbf703
  1. 142
      EAT.cpp
  2. 8
      EAT.h

142
EAT.cpp

@ -0,0 +1,142 @@
#include <M5StickC.h>
#include "Kernel.h"
#include "EAT.h"
#include "EEPROM.h"
#define VERSION 1
#define MAX_ENTRIES 16
#define EEPROM_SIZE 4096
byte version = 0;
byte options = 0;
byte modules[MAX_ENTRIES];
byte lengths[MAX_ENTRIES];
int EAT_get_offset(byte module);
void EAT_load()
{
EEPROM.begin(EEPROM_SIZE);
version = EEPROM.read(0);
options = EEPROM.read(1);
if (version != VERSION)
{
char msg[255];
sprintf(msg, "Unsupported version (%d) -- please reinitialize EEPROM.", version);
Kernel_panic(msg);
//Kernel_panic("Unsupported EAT version -- please reinitialize EEPROM.");
}
for (int i=0; i<MAX_ENTRIES; i++)
{
modules[i] = EEPROM.read((i*2)+2);
lengths[i] = EEPROM.read((i*2)+3);
}
}
void EAT_commit()
{
EEPROM.write(0, version);
EEPROM.write(1, options);
for (int i=0; i<MAX_ENTRIES; i++)
{
EEPROM.write((i*2)+2, modules[i]);
EEPROM.write((i*2)+3, lengths[i]);
}
EEPROM.commit();
}
void EAT_initialize()
{
EEPROM.begin(EEPROM_SIZE);
version = VERSION;
options = 0;
for (int i=0; i<MAX_ENTRIES; i++)
{
modules[i] = 0;
lengths[i] = 0;
}
EAT_commit();
}
int EAT_get_offset(byte module)
{
int offset = 0;
for (int i=0; i<MAX_ENTRIES; i++)
{
if (modules[i] == module)
{
return offset + 2 + (MAX_ENTRIES * 2); // header + EAT
}
else
{
offset += lengths[i];
}
}
return -1;
}
int EAT_get_length(byte module)
{
for (int i=0; i<MAX_ENTRIES; i++)
{
if (modules[i] == module)
{
return lengths[i];
}
}
return -1;
}
bool EAT_allocate(byte module, byte length)
{
for (int i=0; i<MAX_ENTRIES; i++)
{
if (modules[i] == module)
{
return false;
}
if (modules[i] == 0)
{
modules[i] = module;
lengths[i] = length;
EAT_commit();
return true;
}
}
Kernel_panic("Attempt to allocate EAT module but out of entries");
}
void EAT_write(byte module, byte offset, byte value)
{
int module_offset = EAT_get_offset(module);
if (offset == -1)
{
Kernel_panic("Attempt to write to uninitialized EAT module");
}
if (offset > EAT_get_length(module) - 1)
{
Kernel_panic("Attempt to write outside EAT module bounds");
}
offset += module_offset;
EEPROM.write(offset, value);
EEPROM.commit();
}
byte EAT_read(byte module, byte offset)
{
int module_offset = EAT_get_offset(module);
if (offset == -1)
{
Kernel_panic("Attempt to write to uninitialized EAT module");
}
if (offset > EAT_get_length(module) - 1)
{
Kernel_panic("Attempt to write outside EAT module bounds");
}
offset += module_offset;
return EEPROM.read(offset
);
}

8
EAT.h

@ -0,0 +1,8 @@
void EAT_load();
void EAT_commit();
void EAT_initialize();
int EAT_get_length(byte module);
bool EAT_allocate(byte module, byte length);
void EAT_write(byte module, byte offset, byte value);
byte EAT_read(byte module, byte offset);
Loading…
Cancel
Save