8 static const char *
const TAG =
"cse7761";
18 static const int CSE7761_UREF = 42563;
19 static const int CSE7761_IREF = 52241;
20 static const int CSE7761_PREF = 44513;
22 static const uint8_t CSE7761_REG_SYSCON = 0x00;
23 static const uint8_t CSE7761_REG_EMUCON = 0x01;
24 static const uint8_t CSE7761_REG_EMUCON2 = 0x13;
25 static const uint8_t CSE7761_REG_PULSE1SEL = 0x1D;
27 static const uint8_t CSE7761_REG_RMSIA = 0x24;
28 static const uint8_t CSE7761_REG_RMSIB = 0x25;
29 static const uint8_t CSE7761_REG_RMSU = 0x26;
30 static const uint8_t CSE7761_REG_POWERPA = 0x2C;
31 static const uint8_t CSE7761_REG_POWERPB = 0x2D;
32 static const uint8_t CSE7761_REG_SYSSTATUS = 0x43;
34 static const uint8_t CSE7761_REG_COEFFCHKSUM = 0x6F;
35 static const uint8_t CSE7761_REG_RMSIAC = 0x70;
37 static const uint8_t CSE7761_SPECIAL_COMMAND = 0xEA;
38 static const uint8_t CSE7761_CMD_RESET = 0x96;
39 static const uint8_t CSE7761_CMD_CLOSE_WRITE = 0xDC;
40 static const uint8_t CSE7761_CMD_ENABLE_WRITE = 0xE5;
45 ESP_LOGCONFIG(TAG,
"Setting up CSE7761...");
46 this->
write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_RESET);
47 uint16_t syscon = this->
read_(0x00, 2);
48 if ((0x0A04 == syscon) && this->
chip_init_()) {
49 this->
write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_CLOSE_WRITE);
50 ESP_LOGD(TAG,
"CSE7761 found");
58 ESP_LOGCONFIG(TAG,
"CSE7761:");
60 ESP_LOGE(TAG,
"Communication with CSE7761 failed!");
62 LOG_UPDATE_INTERVAL(
this);
82 buffer[2] = data & 0xFF;
85 buffer[2] = (data >> 8) & 0xFF;
86 buffer[3] = data & 0xFF;
90 for (uint32_t i = 0; i <
len; i++) {
107 uint8_t buffer[8] = {0};
110 for (uint32_t i = 0; i <= size; i++) {
111 int value = this->
read();
112 if (value > -1 && rcvd <
sizeof(buffer) - 1) {
113 buffer[rcvd++] = value;
118 ESP_LOGD(TAG,
"Received 0 bytes for register %hhu", reg);
125 uint8_t
crc = 0xA5 + reg;
126 for (uint32_t i = 0; i < rcvd; i++) {
127 result = (result << 8) | buffer[i];
131 if (crc != buffer[rcvd]) {
143 while (!result && retry > 0) {
148 ESP_LOGE(TAG,
"Reading register %hhu failed!", reg);
165 uint16_t calc_chksum = 0xFFFF;
166 for (uint32_t i = 0; i < 8; i++) {
170 calc_chksum = ~calc_chksum;
171 uint16_t coeff_chksum = this->
read_(CSE7761_REG_COEFFCHKSUM, 2);
172 if ((calc_chksum != coeff_chksum) || (!calc_chksum)) {
173 ESP_LOGD(TAG,
"Default calibration");
179 this->
write_(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
181 uint8_t sys_status = this->
read_(CSE7761_REG_SYSSTATUS, 1);
182 if (sys_status & 0x10) {
183 this->
write_(CSE7761_REG_SYSCON | 0x80, 0xFF04);
184 this->
write_(CSE7761_REG_EMUCON | 0x80, 0x1183);
185 this->
write_(CSE7761_REG_EMUCON2 | 0x80, 0x0FC1);
186 this->
write_(CSE7761_REG_PULSE1SEL | 0x80, 0x3290);
188 ESP_LOGD(TAG,
"Write failed at chip_init");
200 uint32_t value = this->
read_(CSE7761_REG_RMSU, 3);
203 value = this->
read_(CSE7761_REG_RMSIA, 3);
204 this->
data_.
current_rms[0] = ((value >= 0x800000) || (value < 1600)) ? 0 : value;
205 value = this->
read_(CSE7761_REG_POWERPA, 4);
208 value = this->
read_(CSE7761_REG_RMSIB, 3);
209 this->
data_.
current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value;
210 value = this->
read_(CSE7761_REG_POWERPB, 4);
220 for (uint8_t channel = 0; channel < 2; channel++) {
224 ESP_LOGD(TAG,
"Channel %d power %f W, current %f A", channel + 1, active_power, amps);
232 }
else if (channel == 1) {
const float DATA
For components that import data from directly connected sensors like DHT.
void write_array(const uint8_t *data, size_t len)
sensor::Sensor * power_sensor_2_
float get_setup_priority() const override
sensor::Sensor * power_sensor_1_
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...
uint32_t read_(uint8_t reg, uint8_t size)
void publish_state(float state)
Publish a new state to the front-end.
uint32_t coefficient_by_unit_(uint32_t unit)
sensor::Sensor * current_sensor_1_
virtual void mark_failed()
Mark this component as failed.
sensor::Sensor * current_sensor_2_
void dump_config() override
Implementation of SPI Controller mode.
void write_(uint8_t reg, uint16_t data)
sensor::Sensor * voltage_sensor_
bool read_once_(uint8_t reg, uint8_t size, uint32_t *value)