Hello I’m trying to initialize an ADE7953 trough SPI using an ESP32, i’m using the mos library.
#include "mgos.h"
#include "mgos_spi.h"
#include "mgos_ade7953.h"
static void ade7953_cb (void *user_data) {
struct mgos_ade7953 *ade = (struct mgos_ade7953 *) user_data;
float f = 0, v = 0, ia = 0, ib = 0, aea = 0, aeb = 0, apa = 0, apb = 0;
if (!ade) return;
mgos_ade7953_get_frequency(ade, &f);
mgos_ade7953_get_voltage(ade, &v);
mgos_ade7953_get_current(ade, 0, &ia);
mgos_ade7953_get_current(ade, 1, &ib);
mgos_ade7953_get_apower(ade, 0, &apa);
mgos_ade7953_get_apower(ade, 1, &apb);
mgos_ade7953_get_aenergy(ade, 0, false /* reset */, &aea);
mgos_ade7953_get_aenergy(ade, 1, false /* reset */, &aeb);
LOG(LL_INFO, ("V=%.3fV f=%.2fHz | IA=%.3fA VIA=%.3f APA=%.3f AEA=%.3f | IB=%.3fA VIB=%.3f APB=%.3f AEB=%.3f",
v, f, ia, (v * ia), apa, aea, ib, (v * ib), apb, aeb));
}
enum mgos_app_init_result mgos_app_init(void) {
static struct mgos_spi *spi;
spi = mgos_spi_get_global();
if (spi == NULL) {
LOG(LL_ERROR, ("No SPI"));
return false;
}
struct mgos_ade7953 *ade;
// These constants are specific to the specific application circuit.
const struct mgos_config_ade7953 ade_cfg = {
.voltage_scale = .0000382602,
.voltage_offset = -0.068,
.current_scale_0 = 0.00000949523,
.current_scale_1 = 0.00000949523,
.current_offset_0 = -0.017,
.current_offset_1 = -0.017,
.apower_scale_0 = (1 / 164.0),
.apower_scale_1 = (1 / 164.0),
.aenergy_scale_0 = (1 / 25240.0),
.aenergy_scale_1 = (1 / 25240.0),
.voltage_pga_gain = MGOS_ADE7953_PGA_GAIN_1,
.current_pga_gain_0 = MGOS_ADE7953_PGA_GAIN_8,
.current_pga_gain_1 = MGOS_ADE7953_PGA_GAIN_8,
};
//For I2C
// if (!(ade = mgos_ade7953_create(mgos_i2c_get_global(), &ade_cfg))) {
// return false;
// }
//For SPI
if (!(ade = mgos_ade7953_create_spi(spi, mgos_sys_config_get_spi_cs0_gpio(), &ade_cfg))) {
LOG(LL_ERROR, ("No ADE"));
return false;
}
mgos_set_timer(1000, true, ade7953_cb, ade);
return MGOS_APP_INIT_SUCCESS;
}
My yml entries :
cdefs:
MG_ENABLE_SSL: 1
MGOS_ETH_PHY_IP101: 0
MGOS_ETH_PHY_RTL8201: 0
MGOS_ETH_PHY_LAN87x0: 1
MGOS_ETH_PHY_DP83848: 0
MG_ENABLE_SNTP: 1
MGOS_ADE7953_ENABLE_SPI: 1
MGOS_ADE7953_ENABLE_I2C: 0
MGOS_ENABLE_SPI_GPIO: 0
MGOS_ADE7953_SPI_FREQ: 1000000
# SPI
- ["spi.enable", true] # Enable SPI
- ["spi.debug", false] # Disable SPI debugging
- ["spi.miso_gpio", 12] # MISO pin
- ["spi.mosi_gpio", 13] # MOSI pin
- ["spi.sclk_gpio", 14] # SCLK pin
- ["spi.cs0_gpio", 0] # CS0 pin
- ["spi.cs1_gpio", 15] # CS1 pin
- ["spi.cs2_gpio", -1] # CS2 pin not used
- ["spi.unit_no", 2] # SPI Unit Number
- ["spi.cs0_mode", 3]
libs:
- origin: https://github.com/mongoose-os-libs/ethernet
- origin: https://github.com/mongoose-os-libs/adc
- origin: https://github.com/mongoose-os-libs/atca
- origin: https://github.com/mongoose-os-libs/wifi
- origin: https://github.com/mongoose-os-libs/ade7953
During boot i get what is expected:
esp32_spi_master.c:94 SPI2 init ok (MISO: 12, MOSI: 13, SCLK: 14; CS0/1/2: 0/15/-1; native? yes)
But i dont get the data as expected from the ADE,
a reading from the register 0x702 give me 0xff (255) when i should get another value
Received
mgos_ade7953.c:98 ADE7953 silicon version: 0xff (255)
Expected
mgos_ade7953.c:98 ADE7953 silicon version: 0x02 (2)
I have tested different SPI modes
Am i missing something? (too many hours with the same problem)