Skip to content

Commit b8864e9

Browse files
authored
Merge pull request #4 from ALLTERCO/shelly
Various updates and changes (including one breaking change which LGTM). Thanks for the contribution (and for your excellent products).
2 parents 207bdb1 + b0fda50 commit b8864e9

7 files changed

+96
-42
lines changed

README.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,20 @@ enum mgos_app_init_result mgos_app_init(void) {
4848
struct mgos_ade7953 *ade;
4949
5050
// These constants are specific to the specific application circuit.
51-
const struct mgos_ade7953_config ade_cfg = {
51+
const struct mgos_config_ade7953 ade_cfg = {
5252
.voltage_scale = .0000382602,
5353
.voltage_offset = -0.068,
54-
.current_scale = {0.00000949523, 0.00000949523},
55-
.current_offset = {-0.017, -0.017},
56-
.apower_scale = {(1 / 164.0), (1 / 164.0)},
57-
.aenergy_scale = {(1 / 25240.0), (1 / 25240.0)},
54+
.current_scale_0 = 0.00000949523,
55+
.current_scale_1 = 0.00000949523,
56+
.current_offset_0 = -0.017,
57+
.current_offset_1 = -0.017,
58+
.apower_scale_0 = (1 / 164.0),
59+
.apower_scale_1 = (1 / 164.0),
60+
.aenergy_scale_0 = (1 / 25240.0),
61+
.aenergy_scale_1 = (1 / 25240.0),
62+
.voltage_pga_gain = MGOS_ADE7953_PGA_GAIN_1,
63+
.current_pga_gain_0 = MGOS_ADE7953_PGA_GAIN_8,
64+
.current_pga_gain_1 = MGOS_ADE7953_PGA_GAIN_8,
5865
};
5966
if (!(ade = mgos_ade7953_create(mgos_i2c_get_global(), &ade_cfg))) {
6067
return false;

include/mgos_ade7953.h

+14-20
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,26 @@ extern "C" {
2626

2727
struct mgos_ade7953;
2828

29-
struct mgos_ade7953_config {
30-
// Scaling factor to convert voltage channel ADC readings to voltage.
31-
// It depends on the parameters of the voltage divider used on the input.
32-
float voltage_scale;
33-
// Voltage measurement offset, volts.
34-
float voltage_offset;
35-
36-
// Scaling factoris to convert current channel ADC readings to amperes.
37-
// Depends on the shunt parameters.
38-
float current_scale[2];
39-
// Current measurement offsets, in amps.
40-
float current_offset[2];
41-
42-
// Scaling factors to convert active power to watts.
43-
float apower_scale[2];
44-
45-
// Scaling factors to convert active energy to watt-hours.
46-
float aenergy_scale[2];
29+
enum ade7953_pga_gain {
30+
MGOS_ADE7953_PGA_GAIN_1 = 0x00,
31+
MGOS_ADE7953_PGA_GAIN_2 = 0x01,
32+
MGOS_ADE7953_PGA_GAIN_4 = 0x02,
33+
MGOS_ADE7953_PGA_GAIN_8 = 0x03,
34+
MGOS_ADE7953_PGA_GAIN_16 = 0x04,
35+
MGOS_ADE7953_PGA_GAIN_22 = 0x05
4736
};
4837

4938
#if MGOS_ADE7953_ENABLE_I2C
5039
// Create an instance of the driver at the given I2C bus and address.
5140
// Returns a pointer to the object upon success, NULL otherwise.
5241
#include "mgos_i2c.h"
53-
struct mgos_ade7953 *mgos_ade7953_create_i2c(struct mgos_i2c *i2c, const struct mgos_ade7953_config *cfg);
42+
struct mgos_ade7953 *mgos_ade7953_create_i2c(struct mgos_i2c *i2c, const struct mgos_config_ade7953 *cfg);
5443
#define mgos_ade7953_create mgos_ade7953_create_i2c
5544
#endif
5645

5746
#if MGOS_ADE7953_ENABLE_SPI
5847
#include "mgos_spi.h"
59-
struct mgos_ade7953 *mgos_ade7953_create_spi(struct mgos_spi *spi, int cs, const struct mgos_ade7953_config *cfg);
48+
struct mgos_ade7953 *mgos_ade7953_create_spi(struct mgos_spi *spi, int cs, const struct mgos_config_ade7953 *cfg);
6049
#endif
6150

6251
// Write the detected voltage in Volts RMS in the *volts pointer.
@@ -79,6 +68,11 @@ bool mgos_ade7953_get_apower(struct mgos_ade7953 *dev, int channel, float *watts
7968
// If reset is true, resets the accumulator.
8069
bool mgos_ade7953_get_aenergy(struct mgos_ade7953 *dev, int channel, bool reset, float *wh);
8170

71+
// Write the measured power factor to the *pf pointer.
72+
// Power factor is a dimensionless number in the closed interval of −1 to 1.
73+
// Returns true on success, false otherwise.
74+
bool mgos_ade7953_get_pf(struct mgos_ade7953 *dev, int channel, float *pf);
75+
8276
// Advanced usage: functions to read/write ADE7953 registers.
8377
bool mgos_ade7953_read_reg(struct mgos_ade7953 *dev, uint16_t reg, bool is_signed, int32_t *val);
8478
bool mgos_ade7953_write_reg(struct mgos_ade7953 *dev, uint16_t reg, int32_t val);

mos.yml

+19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ conds:
1616
- src/mgos_ade7953_spi.c
1717
libs:
1818
- location: https://github.com/mongoose-os-libs/spi
19+
cdefs:
20+
MGOS_ADE7953_SPI_FREQ: 1000000
1921

2022
- when: cdefs.MGOS_ADE7953_ENABLE_I2C == "1"
2123
apply:
@@ -24,6 +26,23 @@ conds:
2426
libs:
2527
- location: https://github.com/mongoose-os-libs/i2c
2628

29+
config_schema:
30+
# Abstract data, saved in separate storage
31+
- ["ade7953", "o", {title: "ADE7953 driver configuration", abstract: true}]
32+
- ["ade7953.voltage_scale", "f", {title: "Scaling factor to convert voltage channel ADC readings to voltage"}]
33+
- ["ade7953.voltage_offset", "f", {title: "Voltage measurement offset, volts"}]
34+
- ["ade7953.current_scale_0", "f", {title: "Scaling factor to convert current channel ADC readings to amperes, channel 0"}]
35+
- ["ade7953.current_scale_1", "f", {title: "Scaling factor to convert current channel ADC readings to amperes, channel 1"}]
36+
- ["ade7953.current_offset_0", "f", {title: "Current measurement offsets, in amps, channel 0"}]
37+
- ["ade7953.current_offset_1", "f", {title: "Current measurement offsets, in amps, channel 1"}]
38+
- ["ade7953.apower_scale_0", "f", {title: "Scaling factor to convert active power to watts, channel 0"}]
39+
- ["ade7953.apower_scale_1", "f", {title: "Scaling factor to convert active power to watts, channel 1"}]
40+
- ["ade7953.aenergy_scale_0", "f", {title: "Scaling factor to convert active energy to watt-hours, channel 0"}]
41+
- ["ade7953.aenergy_scale_1", "f", {title: "Scaling factor to convert active energy to watt-hours, channel 1"}]
42+
- ["ade7953.voltage_pga_gain", "i", {title: "Voltage PGA gain, see enum ade7953_pga_gain in mgos_ade7953.h"}]
43+
- ["ade7953.current_pga_gain_0", "i", {title: "Current PGA gain, channel 0, see enum ade7953_pga_gain in mgos_ade7953.h"}]
44+
- ["ade7953.current_pga_gain_1", "i", {title: "Current PGA gain, channel 1, see enum ade7953_pga_gain in mgos_ade7953.h"}]
45+
2746
cdefs:
2847
MGOS_ADE7953_ENABLE_SPI: 0
2948
MGOS_ADE7953_ENABLE_I2C: 1

src/mgos_ade7953.c

+42-10
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,16 @@ bool mgos_ade7953_read_reg(struct mgos_ade7953 *dev, uint16_t reg, bool is_signe
8282
return true;
8383
}
8484

85-
bool mgos_ade7953_create_common(struct mgos_ade7953 *dev, const struct mgos_ade7953_config *cfg) {
85+
bool mgos_ade7953_create_common(struct mgos_ade7953 *dev, const struct mgos_config_ade7953 *cfg) {
8686
int32_t version;
8787

8888
dev->voltage_scale = cfg->voltage_scale;
89-
for (int i = 0; i < 2; i++) {
90-
dev->current_scale[i] = cfg->current_scale[i];
91-
dev->apower_scale[i] = cfg->apower_scale[i];
92-
dev->aenergy_scale[i] = cfg->aenergy_scale[i];
93-
}
89+
dev->current_scale[0] = cfg->current_scale_0;
90+
dev->apower_scale[0] = cfg->apower_scale_0;
91+
dev->aenergy_scale[0] = cfg->aenergy_scale_0;
92+
dev->current_scale[1] = cfg->current_scale_0;
93+
dev->apower_scale[1] = cfg->apower_scale_0;
94+
dev->aenergy_scale[1] = cfg->aenergy_scale_0;
9495

9596
if (mgos_ade7953_read_reg(dev, MGOS_ADE7953_REG_VERSION, false, &version)) {
9697
LOG(LL_INFO, ("ADE7953 silicon version: 0x%02x (%d)", (int) version, (int) version));
@@ -113,11 +114,22 @@ bool mgos_ade7953_create_common(struct mgos_ade7953 *dev, const struct mgos_ade7
113114
if (cfg->voltage_offset != 0) {
114115
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_VRMSOS, (int32_t)(cfg->voltage_offset / cfg->voltage_scale));
115116
}
116-
if (cfg->current_offset[0] != 0) {
117-
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_AIRMSOS, (int32_t)(cfg->current_offset[0] / cfg->current_scale[0]));
117+
if (cfg->current_offset_0 != 0) {
118+
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_AIRMSOS, (int32_t)(cfg->current_offset_0 / cfg->current_scale_0));
119+
}
120+
if (cfg->current_offset_1 != 0) {
121+
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_BIRMSOS, (int32_t)(cfg->current_offset_1 / cfg->current_scale_1));
122+
}
123+
124+
// Set PGA gains.
125+
if (cfg->voltage_pga_gain != 0) {
126+
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_PGA_V, cfg->voltage_pga_gain);
127+
}
128+
if (cfg->current_pga_gain_0 != 0) {
129+
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_PGA_IA, cfg->current_pga_gain_0);
118130
}
119-
if (cfg->current_offset[1] != 0) {
120-
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_BIRMSOS, (int32_t)(cfg->current_offset[1] / cfg->current_scale[1]));
131+
if (cfg->current_pga_gain_1 != 0) {
132+
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_PGA_IB, cfg->current_pga_gain_1);
121133
}
122134

123135
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_LCYCMODE, 0x40);
@@ -215,6 +227,26 @@ bool mgos_ade7953_get_aenergy(struct mgos_ade7953 *dev, int channel, bool reset,
215227
return true;
216228
}
217229

230+
bool mgos_ade7953_get_pf(struct mgos_ade7953 *dev, int channel, float *pf) {
231+
uint16_t reg;
232+
int32_t val;
233+
if (!dev || !pf) return false;
234+
if (channel == 0)
235+
reg = MGOS_ADE7953_REG_PFA;
236+
else if (channel == 1)
237+
reg = MGOS_ADE7953_REG_PFB;
238+
else
239+
return false;
240+
if (!mgos_ade7953_read_reg(dev, reg, true, &val)) {
241+
return false;
242+
}
243+
val &= 0x0000FFFF;
244+
// bit 16 of val determines the sign, bits [0:15] represent the absolute part
245+
// 2^-15 = 0.000030518
246+
*pf = (val & 0x8000) ? /*negative sign*/ -((val & ~0x8000) * 0.000030518) : /*positive sign*/ (val * 0.000030518);
247+
return true;
248+
}
249+
218250
bool mgos_ade7953_destroy(struct mgos_ade7953 **dev) {
219251
if (!*dev) return false;
220252
free(*dev);

src/mgos_ade7953_i2c.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "mgos_ade7953_internal.h"
22
#include "mgos_i2c.h"
33

4-
struct mgos_ade7953 *mgos_ade7953_create_i2c(struct mgos_i2c *i2c, const struct mgos_ade7953_config *cfg) {
4+
struct mgos_ade7953 *mgos_ade7953_create_i2c(struct mgos_i2c *i2c, const struct mgos_config_ade7953 *cfg) {
55
struct mgos_ade7953 *dev = NULL;
66

77
if (!i2c) return NULL;

src/mgos_ade7953_internal.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#define MGOS_ADE7953_REG_V 0x31C
5353
#define MGOS_ADE7953_REG_ANENERGYA 0x31E
5454
#define MGOS_ADE7953_REG_ANENERGYB 0x31F
55+
#define MGOS_ADE7953_REG_PFA 0x10A
56+
#define MGOS_ADE7953_REG_PFB 0x10B
5557

5658
#define MGOS_ADE7953_REG_IRQSTATA 0x32D
5759
#define MGOS_ADE7953_REG_IRQSTATA_RESET (1 << 20)
@@ -77,7 +79,7 @@ struct mgos_ade7953 {
7779
float aenergy_scale[2];
7880
};
7981

80-
bool mgos_ade7953_create_common(struct mgos_ade7953 *dev, const struct mgos_ade7953_config *cfg);
82+
bool mgos_ade7953_create_common(struct mgos_ade7953 *dev, const struct mgos_config_ade7953 *cfg);
8183

8284
#if MGOS_ADE7953_ENABLE_I2C
8385
bool mgos_ade7953_write_reg_i2c(struct mgos_ade7953 *dev, uint16_t reg, int size, int32_t val);

src/mgos_ade7953_spi.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "mgos_ade7953_internal.h"
22
#include "mgos_spi.h"
33

4-
struct mgos_ade7953 *mgos_ade7953_create_spi(struct mgos_spi *spi, int cs, const struct mgos_ade7953_config *cfg) {
4+
struct mgos_ade7953 *mgos_ade7953_create_spi(struct mgos_spi *spi, int cs, const struct mgos_config_ade7953 *cfg) {
55
struct mgos_ade7953 *dev = NULL;
66

77
if (!spi) return NULL;
@@ -31,8 +31,8 @@ bool mgos_ade7953_write_reg_spi(struct mgos_ade7953 *dev, uint16_t reg, int size
3131

3232
struct mgos_spi_txn txn = {
3333
.cs = dev->spi_cs,
34-
.mode = 3,
35-
.freq = 1e6,
34+
.mode = 0,
35+
.freq = MGOS_ADE7953_SPI_FREQ,
3636
.hd =
3737
{
3838
.tx_data = data_out,
@@ -60,8 +60,8 @@ bool mgos_ade7953_read_reg_spi(struct mgos_ade7953 *dev, uint16_t reg, int size,
6060

6161
struct mgos_spi_txn txn = {
6262
.cs = dev->spi_cs,
63-
.mode = 3,
64-
.freq = 1e6,
63+
.mode = 0,
64+
.freq = MGOS_ADE7953_SPI_FREQ,
6565
.hd =
6666
{
6767
.tx_data = data_out,

0 commit comments

Comments
 (0)