Skip to content

Commit

Permalink
Merge pull request #5 from ALLTERCO/shelly
Browse files Browse the repository at this point in the history
Various fixes h/t @andrey-hinkov
  • Loading branch information
pimvanpelt authored Feb 24, 2021
2 parents b8864e9 + e1fe93f commit 0f7aecc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
17 changes: 10 additions & 7 deletions src/mgos_ade7953.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool mgos_ade7953_write_reg(struct mgos_ade7953 *dev, uint16_t reg, int32_t val)

bool mgos_ade7953_read_reg(struct mgos_ade7953 *dev, uint16_t reg, bool is_signed, int32_t *val) {
int32_t v = 0;
uint8_t data[4];
uint8_t data[4] = {0};
int size = mgos_ade7953_regsize(reg);
bool res = false;

Expand All @@ -72,6 +72,7 @@ bool mgos_ade7953_read_reg(struct mgos_ade7953 *dev, uint16_t reg, bool is_signe
if (size == 1) sign_mask = (1 << 7);
if (size == 2) sign_mask = (1 << 15);
if (size == 3) sign_mask = (1 << 23);
if (size == 4) sign_mask = (1 << 31);
if ((v & sign_mask) != 0) {
v &= ~sign_mask;
v |= (1 << 31);
Expand All @@ -89,9 +90,9 @@ bool mgos_ade7953_create_common(struct mgos_ade7953 *dev, const struct mgos_conf
dev->current_scale[0] = cfg->current_scale_0;
dev->apower_scale[0] = cfg->apower_scale_0;
dev->aenergy_scale[0] = cfg->aenergy_scale_0;
dev->current_scale[1] = cfg->current_scale_0;
dev->apower_scale[1] = cfg->apower_scale_0;
dev->aenergy_scale[1] = cfg->aenergy_scale_0;
dev->current_scale[1] = cfg->current_scale_1;
dev->apower_scale[1] = cfg->apower_scale_1;
dev->aenergy_scale[1] = cfg->aenergy_scale_1;

if (mgos_ade7953_read_reg(dev, MGOS_ADE7953_REG_VERSION, false, &version)) {
LOG(LL_INFO, ("ADE7953 silicon version: 0x%02x (%d)", (int) version, (int) version));
Expand All @@ -104,6 +105,9 @@ bool mgos_ade7953_create_common(struct mgos_ade7953 *dev, const struct mgos_conf
mgos_msleep(10);
} while (!mgos_ade7953_read_reg(dev, MGOS_ADE7953_REG_IRQSTATA, false, &val) || !(val & MGOS_ADE7953_REG_IRQSTATA_RESET));

// Lock comms interface, enable high pass filter
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_CONFIG, 0x04);

// Unlock unnamed (!) register 0x120 (see datasheet, page 18)
mgos_ade7953_write_reg(dev, MGOS_ADE7953_REG_UNNAMED, 0xAD);

Expand Down Expand Up @@ -240,10 +244,9 @@ bool mgos_ade7953_get_pf(struct mgos_ade7953 *dev, int channel, float *pf) {
if (!mgos_ade7953_read_reg(dev, reg, true, &val)) {
return false;
}
val &= 0x0000FFFF;
// bit 16 of val determines the sign, bits [0:15] represent the absolute part
// bit 31 of val determines the sign, bits [0:30] represent the absolute part
// 2^-15 = 0.000030518
*pf = (val & 0x8000) ? /*negative sign*/ -((val & ~0x8000) * 0.000030518) : /*positive sign*/ (val * 0.000030518);
*pf = (val & (1 << 31)) ? /*negative sign*/ -((val & ~(1 << 31)) * 0.000030518) : /*positive sign*/ (val * 0.000030518);
return true;
}

Expand Down
12 changes: 6 additions & 6 deletions src/mgos_ade7953_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct mgos_ade7953 *mgos_ade7953_create_spi(struct mgos_spi *spi, int cs, const
if (!spi) return NULL;
if (!(dev = calloc(1, sizeof(*dev)))) return NULL;
dev->spi = spi;
dev->spi_cs = cs;

if (mgos_ade7953_create_common(dev, cfg)) {
return dev;
Expand All @@ -15,18 +16,17 @@ struct mgos_ade7953 *mgos_ade7953_create_spi(struct mgos_spi *spi, int cs, const
}

bool mgos_ade7953_write_reg_spi(struct mgos_ade7953 *dev, uint16_t reg, int size, int32_t val) {
uint8_t data_out[7];
int i;
uint8_t data_out[7] = {0};

if (!dev || size > 4 || size < 0) return false;

data_out[0] = (reg >> 8) & 0xff;
data_out[1] = reg & 0xff;
data_out[2] = 0x00;

i = 3;
while (size--) {
data_out[i++] = (val >> (8 * size)) & 0xff;
for (int i = 0; i < size; i++) {
int shift = size - (i + 1);
data_out[i + 3] = (val >> (8 * shift)) & 0xff;
}

struct mgos_spi_txn txn = {
Expand All @@ -36,7 +36,7 @@ bool mgos_ade7953_write_reg_spi(struct mgos_ade7953 *dev, uint16_t reg, int size
.hd =
{
.tx_data = data_out,
.tx_len = 7,
.tx_len = 3 + size,
.dummy_len = 0,
.rx_data = NULL,
.rx_len = 0,
Expand Down

0 comments on commit 0f7aecc

Please sign in to comment.