From 544163f4a332f002447c4a774afadc84aebf6d7a Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Fri, 27 Nov 2020 15:54:16 +0100 Subject: [PATCH] configuration --- cube/User/Inc/config.h | 5 ++++- cube/User/Src/config.c | 10 ++++++++-- cube/User/Src/eeprom.c | 20 ++++++++++++++------ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/cube/User/Inc/config.h b/cube/User/Inc/config.h index 5d1e360..58ebff3 100644 --- a/cube/User/Inc/config.h +++ b/cube/User/Inc/config.h @@ -5,7 +5,10 @@ #include +#define CONFIG_MAGIC 0xdead0000 + typedef struct __attribute__((__packed__)) s_configBlock { + uint32_t configMagic; char deviceName[16]; uint8_t macAddress[6]; int32_t frontendThreshold; @@ -15,7 +18,7 @@ typedef struct __attribute__((__packed__)) s_configBlock { char statusTopic[64]; char mbusDataTopic[64]; char syslogServerName[64]; - uint8_t filler[3]; + uint8_t filler[2]; } t_configBlock; void configInit(); diff --git a/cube/User/Src/config.c b/cube/User/Src/config.c index b7972c8..9ec6f53 100644 --- a/cube/User/Src/config.c +++ b/cube/User/Src/config.c @@ -2,9 +2,10 @@ #include #include - +#include t_configBlock defaultConfigBlock = { + .configMagic = CONFIG_MAGIC, .deviceName = "MBGW3", .macAddress = { 0x00, 0xA0, 0x57, 0x05, 0x3E, 0x0D }, .frontendThreshold = 240, @@ -14,7 +15,7 @@ t_configBlock defaultConfigBlock = { .statusTopic = "IoT/MBGW3/Status", .mbusDataTopic = "IoT/MBGW3/Measurement", .syslogServerName = "syslogserver", - .filler = { 0, 0, 0 } + .filler = { 0, 0 } }; @@ -28,5 +29,10 @@ t_configBlock* getConfig() { void configInit() { + coloredMsg(LOG_BLUE, false, "cfg ci Reading configuration block from eeprom"); eepromReadConfigBlock(0, &mainConfigBlock); + + if (mainConfigBlock.configMagic != CONFIG_MAGIC) { + coloredMsg(LOG_BLUE, false, "cfg ci Invalid configuration block read from eeprom"); + } } \ No newline at end of file diff --git a/cube/User/Src/eeprom.c b/cube/User/Src/eeprom.c index 471a3e4..c162411 100644 --- a/cube/User/Src/eeprom.c +++ b/cube/User/Src/eeprom.c @@ -7,6 +7,8 @@ #include #include +#include + #define HIGH GPIO_PIN_SET #define LOW GPIO_PIN_RESET @@ -48,7 +50,7 @@ static t_deviceStatsBlock deviceStats; static const uint8_t NUM_OF_BLOCKS = 2; static const uint16_t BLOCK_ADDR[] = { 64, 4128 }; -static const uint8_t EEPROM_WRITE_BLOCK_SIZE = 32; +#define EEPROM_WRITE_BLOCK_SIZE 32 typedef union { @@ -128,14 +130,20 @@ static void eepromHourlyUpdateDeviceStats(void *handle) { } void eepromReadConfigBlock(uint8_t blockNum, t_configBlock *destConfigBlock) { - uint8_t configBlockSizeMod = sizeof(*destConfigBlock) % EEPROM_WRITE_BLOCK_SIZE; - if (configBlockSizeMod != 0) { - coloredMsg(LOG_RED, false, "eeRCB, config block size must be dividable by 32"); - coloredMsg(LOG_RED, false, "eeRCB, current size is %d", sizeof(*destConfigBlock)); - coloredMsg(LOG_RED, false, "eeRCB, append %d filler octets", configBlockSizeMod); + static_assert((sizeof(*destConfigBlock) % EEPROM_WRITE_BLOCK_SIZE == 0), "config block has illegal size, must be dividable by 32"); + + for (uint8_t i = 0; i < (sizeof(*destConfigBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) { + eepromRead(BLOCK_ADDR[blockNum] + (i * EEPROM_WRITE_BLOCK_SIZE), (uint8_t*) (destConfigBlock + (i * EEPROM_WRITE_BLOCK_SIZE)), EEPROM_WRITE_BLOCK_SIZE); } } +void eepromWriteConfigBlock(uint8_t blockNum, t_configBlock *destConfigBlock) { + for (uint8_t i = 0; i < (sizeof(*destConfigBlock) / EEPROM_WRITE_BLOCK_SIZE); i++) { + eepromWrite(BLOCK_ADDR[blockNum] + (i * EEPROM_WRITE_BLOCK_SIZE), (uint8_t*) (destConfigBlock + (i * EEPROM_WRITE_BLOCK_SIZE)), EEPROM_WRITE_BLOCK_SIZE); + } +} + + // active waiting, use only during initialization! static void eepromActiveDelay(uint8_t delay_ms) { activeDelay(delay_ms);