configuration

This commit is contained in:
Wolfgang Hottgenroth 2020-11-27 15:54:16 +01:00
parent 6d7119c0e2
commit 544163f4a3
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
3 changed files with 26 additions and 9 deletions

View File

@ -5,7 +5,10 @@
#include <spi.h>
#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();

View File

@ -2,9 +2,10 @@
#include <config.h>
#include <eeprom.h>
#include <logger.h>
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");
}
}

View File

@ -7,6 +7,8 @@
#include <PontCoopScheduler.h>
#include <utils.h>
#include <assert.h>
#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);