Skip to content

Commit

Permalink
reset
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Dec 6, 2024
1 parent ebe2a92 commit 00681ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
30 changes: 18 additions & 12 deletions src/BL0942/shelly_pm_bl0942.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ BL0942PowerMeter::~BL0942PowerMeter() {
}

#define BL_READ 0x58

#define BL_WRITE 0xA8

#define BL_SOFT_RESET 0x1C
#define BL_USR_WRPROT 0x1D
#define BL_MODE 0x19
#define BL_TPS_CTRL 0x1B
#define BL_I_FAST_RMS_CTRL 0x10

#define BL_ADDR 0x0

Expand Down Expand Up @@ -68,7 +71,12 @@ Status BL0942PowerMeter::Init() {
meas_timer_.Reset(meas_time_ * 1000, MGOS_TIMER_REPEAT);
LOG(LL_INFO, ("BL0942 @ %d/%d", rx_pin_, tx_pin_));

// this->WriteReg(BL_SOFT_RESET, 0x5a5a5a5a);
this->WriteReg(BL_SOFT_RESET, 0x5a5a5a);
this->WriteReg(BL_USR_WRPROT, 0x550000);
this->WriteReg(BL_MODE, 0x001000);
this->WriteReg(BL_TPS_CTRL, 0xFF4700);
this->WriteReg(BL_I_FAST_RMS_CTRL, 0x1C1800);


return Status::OK();
}
Expand All @@ -84,9 +92,9 @@ StatusOr<float> BL0942PowerMeter::GetEnergyWH() {
bool BL0942PowerMeter::WriteReg(uint8_t reg, uint32_t val) {
uint8_t tx_buf[6] = {BL_WRITE | BL_ADDR,
reg,
(uint8_t) ((val >> 0) & 0xFF),
(uint8_t) ((val >> 8) & 0xFF),
(uint8_t) ((val >> 16) & 0xFF),
(uint8_t) ((val >> 8) & 0xFF),
(uint8_t) ((val >> 0) & 0xFF),
0};

for (int i = 0; i < 5; i++) {
Expand All @@ -95,23 +103,21 @@ bool BL0942PowerMeter::WriteReg(uint8_t reg, uint32_t val) {
tx_buf[5] = tx_buf[5] ^ 0xFF;
mgos_uart_write(uart_no_, tx_buf, 6);
mgos_uart_flush(uart_no_);
mgos_msleep(1);
return true;
}

bool BL0942PowerMeter::ReadReg(uint8_t reg, uint8_t *rx_buf, size_t len) {
uint8_t tx_buf[2] = {BL_READ | BL_ADDR, reg};
size_t j = mgos_uart_write(uart_no_, tx_buf, 2);
size_t j1 = mgos_uart_write_avail(uart_no_);
//mgos_uart_flush(uart_no_);
LOG(LL_ERROR, ("tx %i %i %02X %02X", j, j1, tx_buf[0], tx_buf[1]));
mgos_uart_flush(uart_no_);

// Delay to allow data to be available
int baud = 4800;
// mgos_msleep(roundf(len * 8 / baud) * 1e3);
bool bla = mgos_uart_is_rx_enabled(uart_no_);
int baud = 9600;
mgos_msleep(roundf(len * 8 / baud) * 1e3);

int read_len = mgos_uart_read(uart_no_, rx_buf, len);
LOG(LL_ERROR, ("rx %i %i", read_len, bla));
LOG(LL_ERROR, ("rx %i", read_len));

uint8_t chksum = tx_buf[0] + tx_buf[1];
for (int i = 0; i < len; i++) {
Expand All @@ -133,7 +139,7 @@ void BL0942PowerMeter::MeasureTimerCB() {
uint8_t rx_buf[len] = {};
// if (this->ReadReg(0xAA, rx_buf, len)) {
// }
if (this->ReadReg(BL_WATT, rx_buf, 4)) {
if (this->ReadReg(0xAA, rx_buf, 4)) {
uint32_t d = rx_buf[2] << 16 | rx_buf[1] << 8 | rx_buf[0];
if (d & (1 << 23)) {
d |= 0xFF000000;
Expand Down
6 changes: 3 additions & 3 deletions src/ShellyMini1PMGen3/shelly_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
sys_temp->reset(new TempSensorSDNT1608X103F3950(3, 3.3f, 10000.0f));
#endif

std::unique_ptr<PowerMeter> pm(new BL0942PowerMeter(1, 6, 7, 20, 1));
std::unique_ptr<PowerMeter> pm(new BL0942PowerMeter(1, 6, 7, 1, 1));
// BL0942 GPIO6 TX GPIO7 RX
const Status &st = pm->Init();
if (st.ok()) {
Expand All @@ -50,8 +50,8 @@ void CreatePeripherals(std::vector<std::unique_ptr<Input>> *inputs,
LOG(LL_ERROR, ("PM init failed: %s", s.c_str()));
}

InitSysLED(LED_GPIO, LED_ON);
InitSysBtn(BTN_GPIO, BTN_DOWN);
//InitSysLED(LED_GPIO, LED_ON);
//InitSysBtn(BTN_GPIO, BTN_DOWN);
}

void CreateComponents(std::vector<std::unique_ptr<Component>> *comps,
Expand Down

0 comments on commit 00681ce

Please sign in to comment.