eeprom working now

This commit is contained in:
2020-11-06 21:16:17 +01:00
parent 7c7e1c4725
commit 371c22768a
8 changed files with 70 additions and 33 deletions

View File

@ -3,6 +3,7 @@
#include <eeprom.h>
#include <string.h>
#include <logger.h>
#include <mbusComm.h>
#include <PontCoopScheduler.h>
#define HIGH GPIO_PIN_SET
@ -18,11 +19,9 @@ static const uint8_t EEPROM_WREN = 0x06;
// static const uint8_t EEPROM_WRSR = 0x01;
static const uint32_t EEPROM_MAGIC = 0xaffe0000;
static const uint32_t EEPROM_MAGIC = 0xaffe0005;
// static const uint8_t NUM_OF_BLOCKS;
typedef union {
struct __attribute__((__packed__)) s_eepromHeader {
uint32_t magic;
@ -48,8 +47,8 @@ typedef union {
static const uint16_t DEVICE_STATS_ADDR = 32;
static t_deviceStats deviceStats;
// static const uint8_t NUM_OF_BLOCKS;
// static const uint16_t BLOCK_ADDR[] = { 64, 4128 };
static const uint8_t NUM_OF_BLOCKS = 2;
static const uint16_t BLOCK_ADDR[] = { 64, 4128 };
typedef union {
@ -79,8 +78,6 @@ void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len) {
};
memcpy(msg.s.data, buf, len);
coloredMsg(LOG_HIGH, "w: %02x %02x %02x", msg.b[0], msg.b[1], msg.b[2]);
coloredMsg(LOG_HIGH, "b: %02x %02x %02x %02x %02x %02x %02x %02x", msg.s.data[0], msg.s.data[1], msg.s.data[2], msg.s.data[3], msg.s.data[4], msg.s.data[5], msg.s.data[6], msg.s.data[7]);
uint8_t writeEnable = EEPROM_WREN;
__EEPROM_CS(LOW);
@ -97,14 +94,12 @@ void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len) {
.s.cmd = EEPROM_READ,
.s.addr = swap(addr)
};
coloredMsg(LOG_HIGH, "r: %02x %02x %02x", txMsg.b[0], txMsg.b[1], txMsg.b[2]);
t_spiMsg rxMsg;
__EEPROM_CS(LOW);
HAL_SPI_TransmitReceive(&eepromSpi, txMsg.b, rxMsg.b, ((uint16_t)(len+3)), HAL_MAX_DELAY);
__EEPROM_CS(HIGH);
coloredMsg(LOG_HIGH, "b: %02x %02x %02x %02x %02x %02x %02x %02x", rxMsg.s.data[0], rxMsg.s.data[1], rxMsg.s.data[2], rxMsg.s.data[3], rxMsg.s.data[4], rxMsg.s.data[5], rxMsg.s.data[6], rxMsg.s.data[7]);
memcpy(buf, rxMsg.s.data, len);
}
@ -115,11 +110,30 @@ void eepromSpiTxCpltCallback(SPI_HandleTypeDef *hspi) {
static void eepromHourlyUpdateDeviceStats(void *handle) {
deviceStats.s.totalRunningHours += 1;
logMsg("eeI, about to write updated device stats");
t_mbusCommStats *stats = mbusCommGetStats();
deviceStats.s.totalRequests = stats->requestCnt;
deviceStats.s.totalFailures = stats->errorCnt;
logMsg("eeHUDS, about to write updated device stats");
logMsg("eeHUDS, total powercycles so far: %d", deviceStats.s.totalPowercycles);
logMsg("eeHUDS, total running hours so far: %d", deviceStats.s.totalRunningHours);
logMsg("eeHUDS, total requests so far: %d", deviceStats.s.totalRequests);
logMsg("eeHUDS, total failures so far: %d", deviceStats.s.totalFailures);
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
}
// active waiting, use only during initialization!
static void eepromActiveDelay(uint8_t delay_ms) {
uint32_t startTime = HAL_GetTick();
while (startTime + delay_ms > HAL_GetTick());
}
void eepromInit() {
__EEPROM_CS(HIGH);
logMsg("eeI, read header");
eepromRead(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));
coloredMsg(LOG_RED, "eeI, magic: %08x", eepromHeader.s.magic);
@ -133,24 +147,26 @@ void eepromInit() {
deviceStats.s.totalFailures = 0;
coloredMsg(LOG_RED, "eeI, about to write device stats for the first time");
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
eepromActiveDelay(7);
/*
uint8_t emptyBlock[32];
memset(emptyBlock, 0, sizeof(emptyBlock));
for (uint8_t i = 0; i < 2; i++) {
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;
coloredMsg(LOG_RED, "eeI, about to write empty block at %d", addr);
eepromWrite(addr, emptyBlock, sizeof(emptyBlock));
eepromActiveDelay(7);
}
}
*/
coloredMsg(LOG_RED, "eeI, storage blocks initialized");
eepromHeader.s.magic = EEPROM_MAGIC;
eepromHeader.s.activeBlock = 0;
eepromHeader.s.writeCounter = 1;
coloredMsg(LOG_RED, "eeI, about to write header for the first time");
eepromWrite(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));
eepromActiveDelay(7);
coloredMsg(LOG_GREEN, "eeI, eeprom has been initialized");
} else {
coloredMsg(LOG_GREEN, "eeI, eeprom is initialized");
@ -163,10 +179,14 @@ void eepromInit() {
coloredMsg(LOG_GREEN, "eeI, total requests so far: %d", deviceStats.s.totalRequests);
coloredMsg(LOG_GREEN, "eeI, total failures so far: %d", deviceStats.s.totalFailures);
t_mbusCommStats stats = { .requestCnt = deviceStats.s.totalRequests, .errorCnt = deviceStats.s.totalFailures};
mbusCommSetStats(stats);
deviceStats.s.totalPowercycles += 1;
coloredMsg(LOG_GREEN, "eeI, about to write device stats with updated power cycles counter");
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
eepromActiveDelay(7);
schAdd(eepromHourlyUpdateDeviceStats, NULL, 0, 10 * 1000);
schAdd(eepromHourlyUpdateDeviceStats, NULL, 0, 60 * 60 * 1000);
coloredMsg(LOG_GREEN, "eeI, hourly device stats update scheduled");
}