Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def generate_header(hb_coeffs, N):
f_out.write("/* AUTO GENERATED by scripts/filter.py */\n\n")

f_out.write("#include <stdint.h>\n\n")
f_out.write(f"#define DOWNSAMPLE_TAPS {N + 1}\n\n")
f_out.write(f"#define DOWNSAMPLE_TAPS {N + 1}u\n")
f_out.write(f"#define COEFF_UNIQUE_NUM {num_coeffs}u\n\n")
f_out.write("static const int downsample_taps = DOWNSAMPLE_TAPS;\n")
f_out.write(f"static const int numCoeffUnique = {num_coeffs}u;\n")
f_out.write(f"static const int16_t firCoeffs[{num_coeffs}] = {{")
for c in hb_coeffs:
x += 1
Expand Down
41 changes: 29 additions & 12 deletions src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ static bool unsavedChange = false;

/*! @brief Set all configuration values to defaults */
static void configDefault(void) {

(void)memset(&config, 0, sizeof(config));

config.key = CONFIG_NVM_KEY;

/* Single phase, 50 Hz, 240 VAC, 10 s report period */
Expand All @@ -137,12 +137,13 @@ static void configDefault(void) {
for (int32_t idxV = 0u; idxV < NUM_V; idxV++) {
config.voltageCfg[idxV].voltageCal = 100.0f;
config.voltageCfg[idxV].vActive = (0 == idxV);
config.voltageCfg[idxV].phase = 0.0f;
}

/* 4.2 degree shift @ 50 Hz. Initialize ALL slots including reserved. */
for (int32_t idxCT = 0u; idxCT < (NUM_CT + CT_RES); idxCT++) {
config.ctCfg[idxCT].ctCal = 100.0f;
config.ctCfg[idxCT].phase = 4.2f;
config.ctCfg[idxCT].phase = 3.2f;
config.ctCfg[idxCT].vChan1 = 0;
config.ctCfg[idxCT].vChan2 = 0;
config.ctCfg[idxCT].ctActive = (idxCT < NUM_CT_ACTIVE_DEF);
Expand Down Expand Up @@ -245,12 +246,12 @@ static bool configureAnalog(void) {
return false;
}

if ((0 == posCalib) || (0 == posActive)) {
if ((0 == posCalib) || (0 == posActive) || (0 == posPhase)) {
return false;
}

if (ch >= NUM_V) {
if ((0 == posPhase) || (0 == posV1) || (0 == posV2)) {
if ((0 == posV1) || (0 == posV2)) {
return false;
}
}
Expand All @@ -270,28 +271,40 @@ static bool configureAnalog(void) {
}
calAmpl = convF.val;

convF = utilAtof(inBuffer + posPhase);
if (!convF.valid) {
return false;
}
calPhase = convF.val;

if (NUM_V > ch) {

if ((calAmpl <= 25.0f) || (calAmpl >= 150.0f)) {
return false;
}

bool reconfigureCT = calPhase != ecmCfg->vCfg[ch].phase;

config.voltageCfg[ch].vActive = active;
config.voltageCfg[ch].voltageCal = calAmpl;
config.voltageCfg[ch].phase = calPhase;
ecmCfg->vCfg[ch].vActive = active;
ecmCfg->vCfg[ch].voltageCalRaw = calAmpl;
ecmCfg->vCfg[ch].phase = calPhase;

printSettingV(ch);

ecmConfigChannel(ch);
return true;
}

convF = utilAtof(inBuffer + posPhase);
if (!convF.valid) {
return false;
/* If the voltage phase was changed reconfigure all CTs as well */
if (reconfigureCT) {
for (size_t i = 0; i < NUM_CT; i++) {
ecmConfigChannel(i + NUM_V);
}
}

return true;
}
calPhase = convF.val;

convI = utilAtoi(inBuffer + posV1, ITOA_BASE10);
if (!convI.valid) {
Expand Down Expand Up @@ -879,6 +892,8 @@ static void printSettingRFFreq(void) {
static void printSettingV(const int32_t ch) {
printf_("vCal%d = ", (ch + 1));
putFloat(config.voltageCfg[ch].voltageCal, 0);
printf_(",vLead%d = ", (ch + 1));
putFloat(config.voltageCfg[ch].phase, 0);
printf_(", vActive%d = %s\r\n", (ch + 1),
config.voltageCfg[ch].vActive ? "1" : "0");
}
Expand Down Expand Up @@ -985,7 +1000,9 @@ static void printSettingsHR(void) {
printf_("| %2d | V %2d | %c | ", (i + 1), (i + 1),
(config.voltageCfg[i].vActive ? 'Y' : 'N'));
putFloat(config.voltageCfg[i].voltageCal, 6);
serialPuts(" | | | |\r\n");
serialPuts(" | ");
putFloat(config.voltageCfg[i].phase, 6);
serialPuts(" | | |\r\n");
}
for (int32_t i = 0; i < NUM_CT; i++) {
printf_("| %2d | CT %2d | %c | ", (i + 1 + NUM_V), (i + 1),
Expand Down Expand Up @@ -1384,7 +1401,7 @@ void configProcessCmd(void) {
" - x: : channel (1-3 -> V; 4... -> CT)\r\n"
" - a: : channel active. a = 0: DISABLED, a = 1: ENABLED\r\n"
" - y.y : V/CT calibration constant\r\n"
" - z.z : CT phase calibration value\r\n"
" - z.z : V/CT phase calibration value\r\n"
" - v1 : CT voltage channel 1\r\n"
" - v2 : CT voltage channel 2\r\n"
" - l : list settings\r\n"
Expand Down
35 changes: 18 additions & 17 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,29 @@ typedef struct __attribute__((__packed__)) BaseCfg_ {
} BaseCfg_t;

typedef struct __attribute__((__packed__)) DataTxCfg_ {
bool useRFM;
bool useRFM; /* Send over wireless link */
uint8_t rfmFreq; /* 0: 868 MHz, 1: 915 MHz, 2: 433.00 MHz, 3: 433.92 MHz */
uint8_t rfmPwr;
uint8_t rfmPwr; /* RFM power level */
uint8_t res0;
} DataTxCfg_t;

typedef struct __attribute__((__packed__)) OpaCfgPacked_ {
uint8_t period;
uint8_t func; /* 'o': OneWire; 'r', 'b', 'f': pulse */
bool opaActive;
bool puEn; /* Pull up enabled */
uint8_t period; /* Blank time (pulse only) */
uint8_t func; /* 'o': OneWire; 'r', 'b', 'f': pulse */
bool opaActive; /* Channel active */
bool puEn; /* Pull up enabled */
} OpaCfgPacked_t;

typedef struct __attribute__((__packed__)) VoltageCfg_ {
float voltageCal; /* Conversion to real V value */
bool vActive;
float phase; /* Transformer phase */
bool vActive; /* Channel active */
uint8_t res0[3];
} VoltageCfgPacked_t;

typedef struct __attribute__((__packed__)) CTCfg_ {
float ctCal; /* Conversion to real I value */
float phase; /* Phase angle, recalculated to fixed point */
float phase; /* Phase angle */
uint8_t vChan1;
bool ctActive;
uint8_t vChan2;
Expand All @@ -67,23 +68,23 @@ typedef struct __attribute__((__packed__)) Emon32Config_ {
uint16_t crc16_ccitt;
} Emon32Config_t;

typedef struct VersionInfo_ {
const char *version;
const char *revision;
} VersionInfo_t;
/* Check the configuration struct will fit within the "static" area */
_Static_assert((sizeof(Emon32Config_t) <= EEPROM_WL_OFFSET),
"Emon32Config_t >= EEPROM_WL_OFFSET");

_Static_assert((sizeof(BaseCfg_t) == 24), "BaseCfg_t is not 24 bytes wide.");
_Static_assert((sizeof(DataTxCfg_t) == 4), "DataTxCfg_t is not 4 bytes wide.");
_Static_assert((sizeof(OpaCfgPacked_t) == 4),
"OpaCfgPacked_t is not 4 bytes wide.");
_Static_assert((sizeof(VoltageCfgPacked_t) == 8),
"VoltageCfgPacked_t is not 8 bytes wide.");
_Static_assert((sizeof(VoltageCfgPacked_t) == 12),
"VoltageCfgPacked_t is not 12 bytes wide.");
_Static_assert((sizeof(CTCfgPacked_t) == 12),
"CTCfgPacked_t is not 12 bytes wide.");

/* Check the configuration struct will fit within the "static" area */
_Static_assert((sizeof(Emon32Config_t) < EEPROM_WL_OFFSET),
"Emon32Config_t >= EEPROM_WL_OFFSET");
typedef struct VersionInfo_ {
const char *version;
const char *revision;
} VersionInfo_t;

/*! @brief Add a character to the command stream
* @param [in] c : character to add
Expand Down
Loading