mirror of
https://github.com/esphome/esphome.git
synced 2025-10-13 07:13:47 +01:00
bl0942: Fix init sequence, add address and line_frequency options (#7250)
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
#include "esphome/core/log.h"
|
||||
#include <cinttypes>
|
||||
|
||||
// Datasheet: https://www.belling.com.cn/media/file_object/bel_product/BL0942/datasheet/BL0942_V1.06_en.pdf
|
||||
|
||||
namespace esphome {
|
||||
namespace bl0942 {
|
||||
|
||||
@@ -12,33 +14,41 @@ static const uint8_t BL0942_FULL_PACKET = 0xAA;
|
||||
static const uint8_t BL0942_PACKET_HEADER = 0x55;
|
||||
|
||||
static const uint8_t BL0942_WRITE_COMMAND = 0xA8;
|
||||
static const uint8_t BL0942_REG_I_FAST_RMS_CTRL = 0x10;
|
||||
static const uint8_t BL0942_REG_MODE = 0x18;
|
||||
static const uint8_t BL0942_REG_SOFT_RESET = 0x19;
|
||||
static const uint8_t BL0942_REG_USR_WRPROT = 0x1A;
|
||||
|
||||
static const uint8_t BL0942_REG_I_RMSOS = 0x12;
|
||||
static const uint8_t BL0942_REG_WA_CREEP = 0x14;
|
||||
static const uint8_t BL0942_REG_I_FAST_RMS_TH = 0x15;
|
||||
static const uint8_t BL0942_REG_I_FAST_RMS_CYC = 0x16;
|
||||
static const uint8_t BL0942_REG_FREQ_CYC = 0x17;
|
||||
static const uint8_t BL0942_REG_OT_FUNX = 0x18;
|
||||
static const uint8_t BL0942_REG_MODE = 0x19;
|
||||
static const uint8_t BL0942_REG_SOFT_RESET = 0x1C;
|
||||
static const uint8_t BL0942_REG_USR_WRPROT = 0x1D;
|
||||
static const uint8_t BL0942_REG_TPS_CTRL = 0x1B;
|
||||
|
||||
// TODO: Confirm insialisation works as intended
|
||||
const uint8_t BL0942_INIT[5][6] = {
|
||||
// Reset to default
|
||||
{BL0942_WRITE_COMMAND, BL0942_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x38},
|
||||
// Enable User Operation Write
|
||||
{BL0942_WRITE_COMMAND, BL0942_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xF0},
|
||||
// 0x0100 = CF_UNABLE energy pulse, AC_FREQ_SEL 50Hz, RMS_UPDATE_SEL 800mS
|
||||
{BL0942_WRITE_COMMAND, BL0942_REG_MODE, 0x00, 0x10, 0x00, 0x37},
|
||||
// 0x47FF = Over-current and leakage alarm on, Automatic temperature measurement, Interval 100mS
|
||||
{BL0942_WRITE_COMMAND, BL0942_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xFE},
|
||||
// 0x181C = Half cycle, Fast RMS threshold 6172
|
||||
{BL0942_WRITE_COMMAND, BL0942_REG_I_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x1B}};
|
||||
static const uint32_t BL0942_REG_MODE_RESV = 0x03;
|
||||
static const uint32_t BL0942_REG_MODE_CF_EN = 0x04;
|
||||
static const uint32_t BL0942_REG_MODE_RMS_UPDATE_SEL = 0x08;
|
||||
static const uint32_t BL0942_REG_MODE_FAST_RMS_SEL = 0x10;
|
||||
static const uint32_t BL0942_REG_MODE_AC_FREQ_SEL = 0x20;
|
||||
static const uint32_t BL0942_REG_MODE_CF_CNT_CLR_SEL = 0x40;
|
||||
static const uint32_t BL0942_REG_MODE_CF_CNT_ADD_SEL = 0x80;
|
||||
static const uint32_t BL0942_REG_MODE_UART_RATE_19200 = 0x200;
|
||||
static const uint32_t BL0942_REG_MODE_UART_RATE_38400 = 0x300;
|
||||
static const uint32_t BL0942_REG_MODE_DEFAULT =
|
||||
BL0942_REG_MODE_RESV | BL0942_REG_MODE_CF_EN | BL0942_REG_MODE_CF_CNT_ADD_SEL;
|
||||
|
||||
static const uint32_t BL0942_REG_SOFT_RESET_MAGIC = 0x5a5a5a;
|
||||
static const uint32_t BL0942_REG_USR_WRPROT_MAGIC = 0x55;
|
||||
|
||||
void BL0942::loop() {
|
||||
DataPacket buffer;
|
||||
if (!this->available()) {
|
||||
return;
|
||||
}
|
||||
if (read_array((uint8_t *) &buffer, sizeof(buffer))) {
|
||||
if (validate_checksum(&buffer)) {
|
||||
received_package_(&buffer);
|
||||
if (this->read_array((uint8_t *) &buffer, sizeof(buffer))) {
|
||||
if (this->validate_checksum_(&buffer)) {
|
||||
this->received_package_(&buffer);
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
|
||||
@@ -47,8 +57,8 @@ void BL0942::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
bool BL0942::validate_checksum(DataPacket *data) {
|
||||
uint8_t checksum = BL0942_READ_COMMAND;
|
||||
bool BL0942::validate_checksum_(DataPacket *data) {
|
||||
uint8_t checksum = BL0942_READ_COMMAND | this->address_;
|
||||
// Whole package but checksum
|
||||
uint8_t *raw = (uint8_t *) data;
|
||||
for (uint32_t i = 0; i < sizeof(*data) - 1; i++) {
|
||||
@@ -61,17 +71,58 @@ bool BL0942::validate_checksum(DataPacket *data) {
|
||||
return checksum == data->checksum;
|
||||
}
|
||||
|
||||
void BL0942::update() {
|
||||
void BL0942::write_reg_(uint8_t reg, uint32_t val) {
|
||||
uint8_t pkt[6];
|
||||
|
||||
this->flush();
|
||||
this->write_byte(BL0942_READ_COMMAND);
|
||||
pkt[0] = BL0942_WRITE_COMMAND | this->address_;
|
||||
pkt[1] = reg;
|
||||
pkt[2] = (val & 0xff);
|
||||
pkt[3] = (val >> 8) & 0xff;
|
||||
pkt[4] = (val >> 16) & 0xff;
|
||||
pkt[5] = (pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4]) ^ 0xff;
|
||||
this->write_array(pkt, 6);
|
||||
delay(1);
|
||||
}
|
||||
|
||||
int BL0942::read_reg_(uint8_t reg) {
|
||||
union {
|
||||
uint8_t b[4];
|
||||
uint32_le_t le32;
|
||||
} resp;
|
||||
|
||||
this->write_byte(BL0942_READ_COMMAND | this->address_);
|
||||
this->write_byte(reg);
|
||||
this->flush();
|
||||
if (this->read_array(resp.b, 4) &&
|
||||
resp.b[3] ==
|
||||
(uint8_t) ((BL0942_READ_COMMAND + this->address_ + reg + resp.b[0] + resp.b[1] + resp.b[2]) ^ 0xff)) {
|
||||
resp.b[3] = 0;
|
||||
return resp.le32;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void BL0942::update() {
|
||||
this->write_byte(BL0942_READ_COMMAND | this->address_);
|
||||
this->write_byte(BL0942_FULL_PACKET);
|
||||
}
|
||||
|
||||
void BL0942::setup() {
|
||||
for (auto *i : BL0942_INIT) {
|
||||
this->write_array(i, 6);
|
||||
delay(1);
|
||||
}
|
||||
this->write_reg_(BL0942_REG_USR_WRPROT, BL0942_REG_USR_WRPROT_MAGIC);
|
||||
this->write_reg_(BL0942_REG_SOFT_RESET, BL0942_REG_SOFT_RESET_MAGIC);
|
||||
|
||||
uint32_t mode = BL0942_REG_MODE_DEFAULT;
|
||||
mode |= BL0942_REG_MODE_RMS_UPDATE_SEL; /* 800ms refresh time */
|
||||
if (this->line_freq_ == LINE_FREQUENCY_60HZ)
|
||||
mode |= BL0942_REG_MODE_AC_FREQ_SEL;
|
||||
this->write_reg_(BL0942_REG_MODE, mode);
|
||||
|
||||
this->write_reg_(BL0942_REG_USR_WRPROT, 0);
|
||||
|
||||
if (this->read_reg_(BL0942_REG_MODE) != mode)
|
||||
this->status_set_warning("BL0942 setup failed!");
|
||||
|
||||
this->flush();
|
||||
}
|
||||
|
||||
@@ -104,13 +155,15 @@ void BL0942::received_package_(DataPacket *data) {
|
||||
if (frequency_sensor_ != nullptr) {
|
||||
frequency_sensor_->publish_state(frequency);
|
||||
}
|
||||
|
||||
this->status_clear_warning();
|
||||
ESP_LOGV(TAG, "BL0942: U %fV, I %fA, P %fW, Cnt %" PRId32 ", ∫P %fkWh, frequency %fHz, status 0x%08X", v_rms, i_rms,
|
||||
watt, cf_cnt, total_energy_consumption, frequency, data->status);
|
||||
}
|
||||
|
||||
void BL0942::dump_config() { // NOLINT(readability-function-cognitive-complexity)
|
||||
ESP_LOGCONFIG(TAG, "BL0942:");
|
||||
ESP_LOGCONFIG(TAG, " Address: %d", this->address_);
|
||||
ESP_LOGCONFIG(TAG, " Nominal line frequency: %d Hz", this->line_freq_);
|
||||
LOG_SENSOR("", "Voltage", this->voltage_sensor_);
|
||||
LOG_SENSOR("", "Current", this->current_sensor_);
|
||||
LOG_SENSOR("", "Power", this->power_sensor_);
|
||||
|
Reference in New Issue
Block a user