From cc24dc9640b841a29b8ddd2d5c5516d073a9c22d Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 5 Nov 2020 14:38:10 +0100 Subject: [PATCH] eeprom stuff --- cube/User/Inc/eeprom.h | 5 +- cube/User/Src/eeprom.c | 108 +++++++++++++++++++++++++++++++++++++---- cube/User/Src/main2.c | 9 ++++ cube/hottislib | 2 +- 4 files changed, 112 insertions(+), 12 deletions(-) diff --git a/cube/User/Inc/eeprom.h b/cube/User/Inc/eeprom.h index 5da968c..dcaf688 100644 --- a/cube/User/Inc/eeprom.h +++ b/cube/User/Inc/eeprom.h @@ -2,9 +2,12 @@ #define EEPROM_H_ #include +#include - +void eepromInit(); void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len); void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len); +void eepromSpiTxCpltCallback(SPI_HandleTypeDef *hspi); + #endif /* EEPROM_H_ */ diff --git a/cube/User/Src/eeprom.c b/cube/User/Src/eeprom.c index 404a26d..48a8bdf 100644 --- a/cube/User/Src/eeprom.c +++ b/cube/User/Src/eeprom.c @@ -1,38 +1,67 @@ #include #include #include -// #include #include +#include +#include #define HIGH GPIO_PIN_SET #define LOW GPIO_PIN_RESET -const uint8_t EEPROM_READ = 0x03; -const uint8_t EEPROM_WRITE = 0x02; -const uint8_t EEPROM_WRDI = 0x04; -const uint8_t EEPROM_WREN = 0x06; -const uint8_t EEPROM_RDSR = 0x05; -const uint8_t EEPROM_WRSR = 0x01; +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; -const uint32_t STORAGE_MAGIC = 0xaffe0000; +static const uint32_t EEPROM_MAGIC = 0xaffe0000; +static const uint8_t NUM_OF_BLOCKS; +typedef union { + struct s_eepromHeader { + 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 { + struct s_deviceStats { + 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; + +static const uint16_t BLOCK_ADDR[] = { 64, 4128 }; typedef union { - struct { + struct s_spiMsg { uint8_t cmd; uint16_t addr; uint8_t data[32]; } s; - uint8_t b[35]; + uint8_t b[sizeof(struct s_spiMsg)]; } t_spiMsg; + inline static void __EEPROM_CS(GPIO_PinState v) { HAL_GPIO_WritePin(EEPROM_CS_GPIO_Port, EEPROM_CS_Pin, v); } @@ -69,3 +98,62 @@ void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len) { memcpy(buf, rxMsg.s.data, len); } + +void eepromSpiTxCpltCallback(SPI_HandleTypeDef *hspi) { + +} + +static void eepromHourlyUpdateDeviceStats(void *handle) { + deviceStats.s.totalRunningHours += 1; + logMsg("eeI, about to write updated device stats"); + eepromWrite(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats)); +} + +void eepromInit() { + logMsg("eeI, read header"); + eepromRead(EEPROM_HEADER_ADDR, eepromHeader.b, sizeof(eepromHeader)); + + if (eepromHeader.s.magic != EEPROM_MAGIC) { + coloredMsg(LOG_RED, "eeI, eeprom is uninitialized"); + + deviceStats.s.totalPowercycles = 0; + deviceStats.s.totalRunningHours = 0; + deviceStats.s.totalRequests = 0; + 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)); + + uint8_t emptyBlock[32]; + memset(emptyBlock, 0, sizeof(emptyBlock)); + for (uint8_t i = 0; i < 2; 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)); + } + } + + 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)); + coloredMsg(LOG_GREEN, "eeI, eeprom has been initialized"); + } else { + coloredMsg(LOG_GREEN, "eeI, eeprom is initialized"); + } + + coloredMsg(LOG_GREEN, "eeI, about to read device stats"); + eepromRead(DEVICE_STATS_ADDR, deviceStats.b, sizeof(deviceStats)); + coloredMsg(LOG_GREEN, "eeI, total powercycles so far: %d", deviceStats.s.totalPowercycles); + coloredMsg(LOG_GREEN, "eeI, total running hours so far: %d", deviceStats.s.totalRunningHours); + coloredMsg(LOG_GREEN, "eeI, total requests so far: %d", deviceStats.s.totalRequests); + coloredMsg(LOG_GREEN, "eeI, total failures so far: %d", deviceStats.s.totalFailures); + + 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)); + + schAdd(eepromHourlyUpdateDeviceStats, NULL, 0, 60 * 1000); + coloredMsg(LOG_GREEN, "eeI, hourly device stats update scheduled"); +} \ No newline at end of file diff --git a/cube/User/Src/main2.c b/cube/User/Src/main2.c index 631e427..20db4a1 100644 --- a/cube/User/Src/main2.c +++ b/cube/User/Src/main2.c @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -14,6 +15,7 @@ #include #include #include +#include void my_setup_1() { schInit(); @@ -182,6 +184,8 @@ void my_setup_2() { show(LED_RED, OFF); show(LED_GREEN, ON); + eepromInit(); + frontendInit(); frontendSetThreshold(240); @@ -224,3 +228,8 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { } } +void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) { + if (hspi == &eepromSpi) { + eepromSpiTxCpltCallback(hspi); + } +} diff --git a/cube/hottislib b/cube/hottislib index 1ac9a37..6225e7c 160000 --- a/cube/hottislib +++ b/cube/hottislib @@ -1 +1 @@ -Subproject commit 1ac9a377550e0cebdc62f3d9f2423968b387ea41 +Subproject commit 6225e7c47d3ef45b93baa42166fe8690255ce85a