8static const char *
const TAG =
"cse7761";
18static const int CSE7761_UREF = 42563;
19static const int CSE7761_IREF = 52241;
20static const int CSE7761_PREF = 44513;
22static const uint8_t CSE7761_REG_SYSCON = 0x00;
23static const uint8_t CSE7761_REG_EMUCON = 0x01;
24static const uint8_t CSE7761_REG_EMUCON2 = 0x13;
25static const uint8_t CSE7761_REG_PULSE1SEL = 0x1D;
27static const uint8_t CSE7761_REG_RMSIA = 0x24;
28static const uint8_t CSE7761_REG_RMSIB = 0x25;
29static const uint8_t CSE7761_REG_RMSU = 0x26;
30static const uint8_t CSE7761_REG_POWERPA = 0x2C;
31static const uint8_t CSE7761_REG_POWERPB = 0x2D;
32static const uint8_t CSE7761_REG_SYSSTATUS = 0x43;
34static const uint8_t CSE7761_REG_COEFFCHKSUM = 0x6F;
35static const uint8_t CSE7761_REG_RMSIAC = 0x70;
37static const uint8_t CSE7761_SPECIAL_COMMAND = 0xEA;
38static const uint8_t CSE7761_CMD_RESET = 0x96;
39static const uint8_t CSE7761_CMD_CLOSE_WRITE = 0xDC;
40static const uint8_t CSE7761_CMD_ENABLE_WRITE = 0xE5;
45 this->
write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_RESET);
46 uint16_t syscon = this->
read_(0x00, 2);
47 if ((0x0A04 == syscon) && this->
chip_init_()) {
48 this->
write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_CLOSE_WRITE);
49 ESP_LOGD(TAG,
"CSE7761 found");
57 ESP_LOGCONFIG(TAG,
"CSE7761:");
59 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
61 LOG_UPDATE_INTERVAL(
this);
81 buffer[2] = data & 0xFF;
84 buffer[2] = (data >> 8) & 0xFF;
85 buffer[3] = data & 0xFF;
89 for (uint32_t i = 0; i <
len; i++) {
106 uint8_t buffer[8] = {0};
109 for (uint32_t i = 0; i <= size; i++) {
110 int value = this->
read();
111 if (value > -1 && rcvd <
sizeof(buffer) - 1) {
112 buffer[rcvd++] = value;
117 ESP_LOGD(TAG,
"Received 0 bytes for register %hhu", reg);
124 uint8_t crc = 0xA5 + reg;
125 for (uint32_t i = 0; i < rcvd; i++) {
126 result = (result << 8) | buffer[i];
130 if (crc != buffer[rcvd]) {
142 while (!result && retry > 0) {
147 ESP_LOGE(TAG,
"Reading register %hhu failed!", reg);
164 uint16_t calc_chksum = 0xFFFF;
165 for (uint32_t i = 0; i < 8; i++) {
169 calc_chksum = ~calc_chksum;
170 uint16_t coeff_chksum = this->
read_(CSE7761_REG_COEFFCHKSUM, 2);
171 if ((calc_chksum != coeff_chksum) || (!calc_chksum)) {
172 ESP_LOGD(TAG,
"Default calibration");
178 this->
write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
180 uint8_t sys_status = this->
read_(CSE7761_REG_SYSSTATUS, 1);
181 if (sys_status & 0x10) {
182 this->
write_(CSE7761_REG_SYSCON | 0x80, 0xFF04);
183 this->
write_(CSE7761_REG_EMUCON | 0x80, 0x1183);
184 this->
write_(CSE7761_REG_EMUCON2 | 0x80, 0x0FC1);
185 this->
write_(CSE7761_REG_PULSE1SEL | 0x80, 0x3290);
187 ESP_LOGD(TAG,
"Write failed at chip_init");
199 uint32_t value = this->
read_(CSE7761_REG_RMSU, 3);
202 value = this->
read_(CSE7761_REG_RMSIA, 3);
203 this->
data_.
current_rms[0] = ((value >= 0x800000) || (value < 1600)) ? 0 : value;
204 value = this->
read_(CSE7761_REG_POWERPA, 4);
207 value = this->
read_(CSE7761_REG_RMSIB, 3);
208 this->
data_.
current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value;
209 value = this->
read_(CSE7761_REG_POWERPB, 4);
219 for (uint8_t channel = 0; channel < 2; channel++) {
223 ESP_LOGD(TAG,
"Channel %d power %f W, current %f A", channel + 1, active_power, amps);
231 }
else if (channel == 1) {
virtual void mark_failed()
Mark this component as failed.
sensor::Sensor * current_sensor_1_
sensor::Sensor * power_sensor_2_
sensor::Sensor * voltage_sensor_
uint32_t coefficient_by_unit_(uint32_t unit)
uint32_t read_(uint8_t reg, uint8_t size)
sensor::Sensor * current_sensor_2_
sensor::Sensor * power_sensor_1_
bool read_once_(uint8_t reg, uint8_t size, uint32_t *value)
float get_setup_priority() const override
void dump_config() override
void write_(uint8_t reg, uint16_t data)
void publish_state(float state)
Publish a new state to the front-end.
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
void write_array(const uint8_t *data, size_t len)
const float DATA
For components that import data from directly connected sensors like DHT.
@ UART_CONFIG_PARITY_EVEN
Providing packet encoding functions for exchanging data with a remote host.