193 lines
5.4 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
#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-15 23:10:03 +01:00
static const uint32_t EEPROM_MAGIC = 0xaffe0006;
2020-11-04 23:46:55 +01:00
2020-11-05 14:38:10 +01:00
typedef union {
2020-11-05 20:26:28 +01:00
struct __attribute__((__packed__)) s_eepromHeader {
2020-11-05 14:38:10 +01:00
uint32_t magic;
uint32_t writeCounter;
uint8_t activeBlock;
} s;
uint8_t b[sizeof(struct s_eepromHeader)];
} t_eepromHeader;
static const uint16_t EEPROM_HEADER_ADDR = 0;
static t_eepromHeader eepromHeader;
typedef union {
2020-11-05 20:26:28 +01:00
struct __attribute__((__packed__)) s_deviceStats {
2020-11-05 14:38:10 +01:00
uint32_t totalRunningHours;
uint32_t totalPowercycles;
uint32_t totalRequests;
uint32_t totalFailures;
} s;
uint8_t b[sizeof(struct s_deviceStats)];
} t_deviceStats;
static const uint16_t DEVICE_STATS_ADDR = 32;
static t_deviceStats deviceStats;
2020-11-06 21:16:17 +01:00
static const uint8_t NUM_OF_BLOCKS = 2;
static const uint16_t BLOCK_ADDR[] = { 64, 4128 };
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-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-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();
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);
2020-11-05 14:38:10 +01:00
eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats));
}
2020-11-06 21:16:17 +01:00
// active waiting, use only during initialization!
static void eepromActiveDelay(uint8_t delay_ms) {
2020-11-08 15:56:14 +01:00
activeDelay(delay_ms);
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-06 21:16:17 +01:00
eepromActiveDelay(7);
2020-11-05 14:38:10 +01:00
uint8_t emptyBlock[32];
memset(emptyBlock, 0, sizeof(emptyBlock));
2020-11-06 21:16:17 +01:00
for (uint8_t i = 0; i < NUM_OF_BLOCKS; i++) {
2020-11-05 14:38:10 +01:00
for (uint8_t j = 0; j <= 127; j++) {
uint16_t addr = BLOCK_ADDR[i] + sizeof(emptyBlock) * j;
eepromWrite(addr, emptyBlock, sizeof(emptyBlock));
2020-11-06 21:16:17 +01:00
eepromActiveDelay(7);
2020-11-05 14:38:10 +01:00
}
}
2020-11-17 12:01:15 +01:00
logMsg("eeI, storage blocks initialized");
2020-11-06 21:16:17 +01:00
2020-11-05 14:38:10 +01:00
eepromHeader.s.magic = EEPROM_MAGIC;
eepromHeader.s.activeBlock = 0;
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-06 21:16:17 +01:00
eepromActiveDelay(7);
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-05 14:38:10 +01:00
2020-11-06 21:16:17 +01:00
t_mbusCommStats stats = { .requestCnt = deviceStats.s.totalRequests, .errorCnt = deviceStats.s.totalFailures};
mbusCommSetStats(stats);
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-06 21:16:17 +01:00
eepromActiveDelay(7);
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
}