219 lines
6.9 KiB
C
Raw Normal View History

2020-11-05 13:05:19 +01:00
#include <main.h>
#include <spi.h>
#include <eeprom.h>
2020-11-04 23:46:55 +01:00
#include <string.h>
2020-11-05 14:38:10 +01:00
#include <logger.h>
2020-11-06 21:16:17 +01:00
#include <mbusComm.h>
2020-11-05 14:38:10 +01:00
#include <PontCoopScheduler.h>
2020-11-08 15:56:14 +01:00
#include <utils.h>
2020-11-04 23:46:55 +01:00
2020-11-27 15:54:16 +01:00
#include <assert.h>
2020-11-04 23:46:55 +01:00
#define HIGH GPIO_PIN_SET
#define LOW GPIO_PIN_RESET
2020-11-05 14:38:10 +01:00
static const uint8_t EEPROM_READ = 0x03;
static const uint8_t EEPROM_WRITE = 0x02;
// static const uint8_t EEPROM_WRDI = 0x04;
static const uint8_t EEPROM_WREN = 0x06;
// static const uint8_t EEPROM_RDSR = 0x05;
// static const uint8_t EEPROM_WRSR = 0x01;
2020-11-04 23:46:55 +01:00
2020-11-30 18:29:53 +01:00
static const uint32_t EEPROM_MAGIC = 0xaffe0009;
static const uint16_t EEPROM_HEADER_ADDR = EEPROM_BASE_ADDR;
2020-11-04 23:46:55 +01:00
2020-11-05 14:38:10 +01:00
typedef union {
2020-11-30 18:29:53 +01:00
t_eepromHeader s;
2020-11-05 14:38:10 +01:00
uint8_t b[sizeof(struct s_eepromHeader)];
2020-11-30 18:29:53 +01:00
} t_eepromHeaderBlock;
2020-11-05 14:38:10 +01:00
2020-11-30 18:29:53 +01:00
static t_eepromHeaderBlock eepromHeader;
2020-11-20 12:32:00 +01:00
2020-11-05 14:38:10 +01:00
typedef union {
2020-11-20 12:32:00 +01:00
t_deviceStats s;
uint8_t b[sizeof(t_deviceStats)];
} t_deviceStatsBlock;
2020-11-05 14:38:10 +01:00
2020-11-30 18:29:53 +01:00
static const uint16_t DEVICE_STATS_ADDR = EEPROM_DEVICE_STATS_ADDR;
2020-11-20 12:32:00 +01:00
static t_deviceStatsBlock deviceStats;
2020-11-05 14:38:10 +01:00
2020-11-30 18:29:53 +01:00
static const uint16_t CONFIG_BLOCK_ADDR = EEPROM_CONFIG_BLOCK_ADDR;
static const uint16_t DEVICE_BLOCK_ADDR = EEPROM_DEVICE_BLOCK_BASE_ADDR;
2020-11-04 23:46:55 +01:00
2020-11-05 13:05:19 +01:00
typedef union {
2020-11-05 20:26:28 +01:00
struct __attribute__((__packed__)) s_spiMsg {
2020-11-05 13:05:19 +01:00
uint8_t cmd;
uint16_t addr;
uint8_t data[32];
} s;
2020-11-05 14:38:10 +01:00
uint8_t b[sizeof(struct s_spiMsg)];
2020-11-05 13:05:19 +01:00
} t_spiMsg;
2020-11-04 23:46:55 +01:00
2020-11-20 12:32:00 +01:00
t_deviceStats* getGlobalDeviceStats() {
return &(deviceStats.s);
}
2020-11-05 14:38:10 +01:00
2020-11-05 13:05:19 +01:00
inline static void __EEPROM_CS(GPIO_PinState v) {
HAL_GPIO_WritePin(EEPROM_CS_GPIO_Port, EEPROM_CS_Pin, v);
2020-11-04 23:46:55 +01:00
}
2020-11-05 22:14:08 +01:00
static uint16_t swap(uint16_t i) {
return ((i & 0x00ff) << 8) | ((i & 0xff00) >> 8);
}
2020-11-27 16:19:33 +01:00
// active waiting, use only during initialization!
static void eepromActiveDelay(uint8_t delay_ms) {
activeDelay(delay_ms);
}
2020-11-05 13:05:19 +01:00
void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len) {
t_spiMsg msg = {
.s.cmd = EEPROM_WRITE,
2020-11-05 22:14:08 +01:00
.s.addr = swap(addr)
2020-11-04 23:46:55 +01:00
};
2020-11-05 13:05:19 +01:00
memcpy(msg.s.data, buf, len);
2020-11-04 23:46:55 +01:00
2020-11-05 22:14:08 +01:00
2020-11-05 13:05:19 +01:00
uint8_t writeEnable = EEPROM_WREN;
2020-11-04 23:46:55 +01:00
__EEPROM_CS(LOW);
2020-11-05 13:05:19 +01:00
HAL_SPI_Transmit(&eepromSpi, &writeEnable, 1, HAL_MAX_DELAY);
2020-11-04 23:46:55 +01:00
__EEPROM_CS(HIGH);
__EEPROM_CS(LOW);
2020-11-05 17:27:04 +01:00
HAL_SPI_Transmit(&eepromSpi, msg.b, ((uint16_t)(len+3)), HAL_MAX_DELAY);
2020-11-04 23:46:55 +01:00
__EEPROM_CS(HIGH);
}
void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len) {
2020-11-05 13:05:19 +01:00
t_spiMsg txMsg = {
.s.cmd = EEPROM_READ,
2020-11-05 22:14:08 +01:00
.s.addr = swap(addr)
2020-11-05 13:05:19 +01:00
};
2020-11-04 23:46:55 +01:00
2020-11-05 13:05:19 +01:00
t_spiMsg rxMsg;
2020-11-05 17:58:41 +01:00
2020-11-04 23:46:55 +01:00
__EEPROM_CS(LOW);
2020-11-05 17:27:04 +01:00
HAL_SPI_TransmitReceive(&eepromSpi, txMsg.b, rxMsg.b, ((uint16_t)(len+3)), HAL_MAX_DELAY);
2020-11-04 23:46:55 +01:00
__EEPROM_CS(HIGH);
2020-11-05 13:05:19 +01:00
memcpy(buf, rxMsg.s.data, len);
2020-11-04 23:46:55 +01:00
}
2020-11-05 14:38:10 +01:00
void eepromSpiTxCpltCallback(SPI_HandleTypeDef *hspi) {
}
static void eepromHourlyUpdateDeviceStats(void *handle) {
deviceStats.s.totalRunningHours += 1;
2020-11-06 21:16:17 +01:00
t_mbusCommStats *stats = mbusCommGetStats();
2020-11-25 10:02:38 +01:00
deviceStats.s.totalRequests += stats->mbusRequestCnt;
deviceStats.s.totalFailures += stats->mbusErrorCnt;
2020-11-06 21:16:17 +01:00
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);
2020-11-05 14:38:10 +01:00
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
}
2020-11-30 18:29:53 +01:00
void eepromReadConfigBlock(t_configBlock *destConfigBlock) {
// static_assert((sizeof(*destConfigBlock) % EEPROM_WRITE_BLOCK_SIZE == 0), "config block has illegal size, must be dividable by 32");
2020-11-27 15:54:16 +01:00
for (uint8_t i = 0; i < (sizeof(*destConfigBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) {
2020-11-30 18:29:53 +01:00
eepromRead(CONFIG_BLOCK_ADDR + (i * EEPROM_WRITE_BLOCK_SIZE), ((uint8_t*)destConfigBlock) + (i * EEPROM_WRITE_BLOCK_SIZE), EEPROM_WRITE_BLOCK_SIZE);
2020-11-27 15:54:16 +01:00
}
}
2020-11-30 18:29:53 +01:00
void eepromWriteConfigBlock(t_configBlock *srcConfigBlock) {
2020-11-27 16:17:28 +01:00
for (uint8_t i = 0; i < (sizeof(*srcConfigBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) {
2020-11-30 18:29:53 +01:00
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);
2020-11-27 16:19:01 +01:00
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
2020-11-27 15:25:19 +01:00
}
}
2020-11-27 15:54:16 +01:00
2020-11-06 21:16:17 +01:00
2020-11-05 14:38:10 +01:00
void eepromInit() {
2020-11-06 21:16:17 +01:00
__EEPROM_CS(HIGH);
2020-11-05 22:14:08 +01:00
logMsg("eeI, read header");
2020-11-05 14:38:10 +01:00
eepromRead(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));
2020-11-17 12:01:15 +01:00
logMsg("eeI, magic: %08x", eepromHeader.s.magic);
2020-11-05 14:38:10 +01:00
if (eepromHeader.s.magic != EEPROM_MAGIC) {
2020-11-17 12:01:15 +01:00
logMsg("eeI, eeprom is uninitialized");
2020-11-05 14:38:10 +01:00
deviceStats.s.totalPowercycles = 0;
deviceStats.s.totalRunningHours = 0;
deviceStats.s.totalRequests = 0;
deviceStats.s.totalFailures = 0;
2020-11-17 12:01:15 +01:00
logMsg("eeI, about to write device stats for the first time");
2020-11-05 14:38:10 +01:00
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
2020-11-27 16:19:01 +01:00
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
2020-11-05 14:38:10 +01:00
2020-11-30 18:29:53 +01:00
uint8_t emptyBlock[EEPROM_WRITE_BLOCK_SIZE];
2020-11-05 14:38:10 +01:00
memset(emptyBlock, 0, sizeof(emptyBlock));
2020-11-30 18:29:53 +01:00
eepromWrite(CONFIG_BLOCK_ADDR, emptyBlock, EEPROM_WRITE_BLOCK_SIZE);
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
logMsg("eeI, config block initialized");
2020-11-05 14:38:10 +01:00
eepromHeader.s.magic = EEPROM_MAGIC;
eepromHeader.s.writeCounter = 1;
2020-11-17 12:01:15 +01:00
logMsg("eeI, about to write header for the first time");
2020-11-05 14:38:10 +01:00
eepromWrite(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader));
2020-11-27 16:19:01 +01:00
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
2020-11-17 12:01:15 +01:00
logMsg("eeI, eeprom has been initialized");
2020-11-05 14:38:10 +01:00
} else {
2020-11-17 12:01:15 +01:00
logMsg("eeI, eeprom is initialized");
2020-11-05 14:38:10 +01:00
}
2020-11-17 12:01:15 +01:00
logMsg("eeI, about to read device stats");
2020-11-05 14:38:10 +01:00
eepromRead(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
2020-11-17 12:01:15 +01:00
logMsg("eeI, total powercycles so far: %d", deviceStats.s.totalPowercycles);
logMsg("eeI, total running hours so far: %d", deviceStats.s.totalRunningHours);
logMsg("eeI, total requests so far: %d", deviceStats.s.totalRequests);
logMsg("eeI, total failures so far: %d", deviceStats.s.totalFailures);
2020-11-06 21:16:17 +01:00
2020-11-05 14:38:10 +01:00
deviceStats.s.totalPowercycles += 1;
2020-11-17 12:01:15 +01:00
logMsg("eeI, about to write device stats with updated power cycles counter");
2020-11-05 14:38:10 +01:00
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
2020-11-27 16:19:01 +01:00
eepromActiveDelay(EEPROM_AFTER_WRITE_DELAY);
2020-11-05 14:38:10 +01:00
2020-11-06 21:16:17 +01:00
schAdd(eepromHourlyUpdateDeviceStats, NULL, 0, 60 * 60 * 1000);
2020-11-17 12:01:15 +01:00
logMsg("eeI, hourly device stats update scheduled");
2020-11-05 14:50:55 +01:00
}