#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 __attribute__((__packed__)) 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 __attribute__((__packed__)) 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 uint8_t NUM_OF_BLOCKS; // static const uint16_t BLOCK_ADDR[] = { 64, 4128 }; typedef union { struct __attribute__((__packed__)) 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); } static uint16_t swap(uint16_t i) { return ((i & 0x00ff) << 8) | ((i & 0xff00) >> 8); } void eepromWrite(uint16_t addr, uint8_t *buf, uint8_t len) { t_spiMsg msg = { .s.cmd = EEPROM_WRITE, .s.addr = swap(addr) }; memcpy(msg.s.data, buf, len); coloredMsg(LOG_HIGH, "w: %02x %02x %02x", msg.b[0], msg.b[1], msg.b[2]); coloredMsg(LOG_HIGH, "b: %02x %02x %02x %02x %02x %02x %02x %02x", msg.s.data[0], msg.s.data[1], msg.s.data[2], msg.s.data[3], msg.s.data[4], msg.s.data[5], msg.s.data[6], msg.s.data[7]); uint8_t writeEnable = EEPROM_WREN; __EEPROM_CS(LOW); HAL_SPI_Transmit(&eepromSpi, &writeEnable, 1, HAL_MAX_DELAY); __EEPROM_CS(HIGH); __EEPROM_CS(LOW); HAL_SPI_Transmit(&eepromSpi, msg.b, ((uint16_t)(len+3)), HAL_MAX_DELAY); __EEPROM_CS(HIGH); } void eepromRead(uint16_t addr, uint8_t *buf, uint8_t len) { t_spiMsg txMsg = { .s.cmd = EEPROM_READ, .s.addr = swap(addr) }; coloredMsg(LOG_HIGH, "r: %02x %02x %02x", txMsg.b[0], txMsg.b[1], txMsg.b[2]); t_spiMsg rxMsg; __EEPROM_CS(LOW); HAL_SPI_TransmitReceive(&eepromSpi, txMsg.b, rxMsg.b, ((uint16_t)(len+3)), HAL_MAX_DELAY); __EEPROM_CS(HIGH); coloredMsg(LOG_HIGH, "b: %02x %02x %02x %02x %02x %02x %02x %02x", rxMsg.s.data[0], rxMsg.s.data[1], rxMsg.s.data[2], rxMsg.s.data[3], rxMsg.s.data[4], rxMsg.s.data[5], rxMsg.s.data[6], rxMsg.s.data[7]); 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, 10 * 1000); coloredMsg(LOG_GREEN, "eeI, hourly device stats update scheduled"); }