#include #include #include #include #include #include #define HIGH GPIO_PIN_SET #define LOW GPIO_PIN_RESET 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; 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 s_spiMsg { uint8_t cmd; uint16_t addr; uint8_t data[32]; } s; 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); } void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len) { t_spiMsg msg = { .s.cmd = EEPROM_WRITE, .s.addr = addr }; memcpy(msg.s.data, buf, len); uint8_t writeEnable = EEPROM_WREN; coloredMsg(LOG_HIGH, "1"); __EEPROM_CS(LOW); HAL_SPI_Transmit(&eepromSpi, &writeEnable, 1, HAL_MAX_DELAY); __EEPROM_CS(HIGH); coloredMsg(LOG_HIGH, "2"); __EEPROM_CS(LOW); HAL_SPI_Transmit(&eepromSpi, msg.b, ((uint16_t)(len+3)), HAL_MAX_DELAY); __EEPROM_CS(HIGH); coloredMsg(LOG_HIGH, "3"); } void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len) { t_spiMsg txMsg = { .s.cmd = EEPROM_READ, .s.addr = addr }; t_spiMsg rxMsg; coloredMsg(LOG_YELLOW, "len: %d", len); __EEPROM_CS(LOW); HAL_SPI_TransmitReceive(&eepromSpi, txMsg.b, rxMsg.b, ((uint16_t)(len+3)), HAL_MAX_DELAY); __EEPROM_CS(HIGH); 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)); coloredMsg(LOG_RED, "eeI, magic: %08x", eepromHeader.s.magic); 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"); }