device configuration prepared

This commit is contained in:
2020-11-30 18:29:53 +01:00
parent 76ff0d8e24
commit f22c821ca3
4 changed files with 117 additions and 46 deletions

View File

@ -22,21 +22,18 @@ static const uint8_t EEPROM_WREN = 0x06;
// static const uint8_t EEPROM_WRSR = 0x01;
static const uint32_t EEPROM_MAGIC = 0xaffe0008;
static const uint32_t EEPROM_MAGIC = 0xaffe0009;
static const uint16_t EEPROM_HEADER_ADDR = EEPROM_BASE_ADDR;
typedef union {
struct __attribute__((__packed__)) s_eepromHeader {
uint32_t magic;
uint32_t writeCounter;
uint8_t activeBlock;
} s;
t_eepromHeader s;
uint8_t b[sizeof(struct s_eepromHeader)];
} t_eepromHeader;
static const uint16_t EEPROM_HEADER_ADDR = 0;
static t_eepromHeader eepromHeader;
} t_eepromHeaderBlock;
static t_eepromHeaderBlock eepromHeader;
typedef union {
@ -44,14 +41,14 @@ typedef union {
uint8_t b[sizeof(t_deviceStats)];
} t_deviceStatsBlock;
static const uint16_t DEVICE_STATS_ADDR = 32;
static const uint16_t DEVICE_STATS_ADDR = EEPROM_DEVICE_STATS_ADDR;
static t_deviceStatsBlock deviceStats;
static const uint8_t NUM_OF_BLOCKS = 2;
static const uint16_t BLOCK_ADDR[] = { 64, 4128 };
static const uint16_t CONFIG_BLOCK_ADDR = EEPROM_CONFIG_BLOCK_ADDR;
static const uint16_t DEVICE_BLOCK_ADDR = EEPROM_DEVICE_BLOCK_BASE_ADDR;
#define EEPROM_WRITE_BLOCK_SIZE 32
#define EEPROM_AFTER_WRITE_DELAY 7
typedef union {
struct __attribute__((__packed__)) s_spiMsg {
@ -134,17 +131,36 @@ static void eepromHourlyUpdateDeviceStats(void *handle) {
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
}
void eepromReadConfigBlock(uint8_t blockNum, t_configBlock *destConfigBlock) {
static_assert((sizeof(*destConfigBlock) % EEPROM_WRITE_BLOCK_SIZE == 0), "config block has illegal size, must be dividable by 32");
void eepromReadConfigBlock(t_configBlock *destConfigBlock) {
// static_assert((sizeof(*destConfigBlock) % EEPROM_WRITE_BLOCK_SIZE == 0), "config block has illegal size, must be dividable by 32");
for (uint8_t i = 0; i < (sizeof(*destConfigBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) {
eepromRead(BLOCK_ADDR[blockNum] + (i * EEPROM_WRITE_BLOCK_SIZE), ((uint8_t*)destConfigBlock) + (i * EEPROM_WRITE_BLOCK_SIZE), EEPROM_WRITE_BLOCK_SIZE);
eepromRead(CONFIG_BLOCK_ADDR + (i * EEPROM_WRITE_BLOCK_SIZE), ((uint8_t*)destConfigBlock) + (i * EEPROM_WRITE_BLOCK_SIZE), EEPROM_WRITE_BLOCK_SIZE);
}
}
void eepromWriteConfigBlock(uint8_t blockNum, t_configBlock *srcConfigBlock) {
void eepromWriteConfigBlock(t_configBlock *srcConfigBlock) {
for (uint8_t i = 0; i < (sizeof(*srcConfigBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) {
eepromWrite(BLOCK_ADDR[blockNum] + (i * EEPROM_WRITE_BLOCK_SIZE), ((uint8_t*)srcConfigBlock) + (i * EEPROM_WRITE_BLOCK_SIZE), EEPROM_WRITE_BLOCK_SIZE);
eepromWrite(CONFIG_BLOCK_ADDR + (i * EEPROM_WRITE_BLOCK_SIZE), ((uint8_t*)srcConfigBlock) + (i * EEPROM_WRITE_BLOCK_SIZE), EEPROM_WRITE_BLOCK_SIZE);
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
}
}
void eepromReadDeviceBlock(uint8_t blockNum, t_deviceBlock *destDeviceBlock) {
static_assert((sizeof(*destDeviceBlock) % EEPROM_WRITE_BLOCK_SIZE == 0), "device block has illegal size, must be dividable by 32");
for (uint8_t i = 0; i < (sizeof(*destDeviceBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) {
eepromRead(DEVICE_BLOCK_ADDR + (blockNum * sizeof(*destDeviceBlock)) + (i * EEPROM_WRITE_BLOCK_SIZE),
((uint8_t*)destDeviceBlock) + (i * EEPROM_WRITE_BLOCK_SIZE),
EEPROM_WRITE_BLOCK_SIZE);
}
}
void eepromWriteDeviceBlock(uint8_t blockNum, t_deviceBlock *srcDeviceBlock) {
for (uint8_t i = 0; i < (sizeof(*srcDeviceBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) {
eepromWrite(DEVICE_BLOCK_ADDR + (blockNum * sizeof(*srcDeviceBlock)) + (i * EEPROM_WRITE_BLOCK_SIZE),
((uint8_t*)srcDeviceBlock) + (i * EEPROM_WRITE_BLOCK_SIZE),
EEPROM_WRITE_BLOCK_SIZE);
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
}
}
@ -169,20 +185,13 @@ void eepromInit() {
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
uint8_t emptyBlock[32];
uint8_t emptyBlock[EEPROM_WRITE_BLOCK_SIZE];
memset(emptyBlock, 0, sizeof(emptyBlock));
for (uint8_t i = 0; i < NUM_OF_BLOCKS; i++) {
for (uint8_t j = 0; j <= 127; j++) {
uint16_t addr = BLOCK_ADDR[i] + sizeof(emptyBlock) * j;
eepromWrite(addr, emptyBlock, sizeof(emptyBlock));
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
}
}
logMsg("eeI, storage blocks initialized");
eepromWrite(CONFIG_BLOCK_ADDR, emptyBlock, EEPROM_WRITE_BLOCK_SIZE);
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
logMsg("eeI, config block initialized");
eepromHeader.s.magic = EEPROM_MAGIC;
eepromHeader.s.activeBlock = 0;
eepromHeader.s.writeCounter = 1;
logMsg("eeI, about to write header for the first time");
eepromWrite(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));