eeprom stuff
This commit is contained in:
@ -2,9 +2,12 @@
|
||||
#define EEPROM_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <spi.h>
|
||||
|
||||
|
||||
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_ */
|
||||
|
@ -1,38 +1,67 @@
|
||||
#include <main.h>
|
||||
#include <spi.h>
|
||||
#include <eeprom.h>
|
||||
// #include <stm32f1xx_hal.h>
|
||||
#include <string.h>
|
||||
#include <logger.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
|
||||
#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");
|
||||
}
|
@ -6,6 +6,7 @@
|
||||
#include <main.h>
|
||||
#include <usart.h>
|
||||
#include <adc.h>
|
||||
#include <spi.h>
|
||||
|
||||
#include <PontCoopScheduler.h>
|
||||
|
||||
@ -14,6 +15,7 @@
|
||||
#include <mbusComm.h>
|
||||
#include <logger.h>
|
||||
#include <frontend.h>
|
||||
#include <eeprom.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Submodule cube/hottislib updated: 1ac9a37755...6225e7c47d
Reference in New Issue
Block a user