7static const char *
const TAG =
"cs5460a";
29 uint32_t config = (1 << 0) |
56 float voltage_full_scale = 0.25;
81 ESP_LOGE(TAG,
"CS5460A reset failed!");
87 ESP_LOGCONFIG(TAG,
" Version: %" PRIx32, (
status >> 6) & 7);
98 ESP_LOGCONFIG(TAG,
" Init ok");
135 time_left -= time_left / 5;
137 if (time_left > -500) {
148 if (time_left > -15000) {
152 ESP_LOGCONFIG(TAG,
"Device officially stuck, resetting");
159 this->
set_timeout(
"status-check", time_left, [
this]() {
169 if (!(status & 0xcbf83c)) {
174 uint32_t clear = 1 << 20;
180 ESP_LOGE(TAG,
"Low supply detected");
184 if (status & (1 << 3)) {
186 ESP_LOGE(TAG,
"Modulator oscillation on current channel");
190 if (status & (1 << 4)) {
192 ESP_LOGE(TAG,
"Modulator oscillation on voltage channel");
196 if (status & (1 << 5)) {
198 ESP_LOGE(TAG,
"Watch-dog timeout");
202 if (status & (1 << 11)) {
204 ESP_LOGE(TAG,
"EOUT Energy Accumulation Register out of range");
208 if (status & (1 << 12)) {
210 ESP_LOGE(TAG,
"Energy out of range");
214 if (status & (1 << 13)) {
216 ESP_LOGE(TAG,
"RMS voltage out of range");
220 if (status & (1 << 14)) {
222 ESP_LOGE(TAG,
"RMS current out of range");
226 if (status & (1 << 15)) {
228 ESP_LOGE(TAG,
"Power calculation out of range");
232 if (status & (1 << 16)) {
234 ESP_LOGE(TAG,
"Voltage out of range");
238 if (status & (1 << 17)) {
240 ESP_LOGE(TAG,
"Current out of range");
244 if (status & (1 << 19)) {
246 ESP_LOGE(TAG,
"Divide overflowed");
250 bool dir =
status & (1 << 21);
253 ESP_LOGI(TAG,
"Energy counter %s pulse", dir ?
"negative" :
"positive");
257 uint32_t raw_current = 0;
259 uint32_t raw_energy = 0;
271 if (status & ((1 << 23) | (1 << 5))) {
294 if (status & (1 << 23)) {
306 int32_t
raw = (int32_t) (raw_energy << 8) >> 8;
324 LOG_PIN(
" CS Pin: ",
cs_);
326 " Samples / cycle: %" PRIu32
"\n"
327 " Phase offset: %i\n"
329 " Current gain: %.5f\n"
330 " Voltage gain: %.5f\n"
333 " Pulse energy: %.2f Wh",
virtual void mark_failed()
Mark this component as failed.
uint8_t get_component_state() const
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
void status_momentary_warning(const std::string &name, uint32_t length=5000)
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
float current_multiplier_
void dump_config() override
uint32_t prev_raw_energy_
void write_register_(enum CS5460ARegister addr, uint32_t value)
sensor::Sensor * voltage_sensor_
uint32_t read_register_(uint8_t addr)
void schedule_next_check_()
float voltage_multiplier_
sensor::Sensor * power_sensor_
uint32_t prev_raw_current_
sensor::Sensor * current_sensor_
void publish_state(float state)
Publish a new state to the front-end.
void spi_setup() override
void write_byte(uint8_t data)
uint8_t transfer_byte(uint8_t data)
Providing packet encoding functions for exchanging data with a remote host.
const uint8_t COMPONENT_STATE_FAILED
const uint8_t COMPONENT_STATE_LOOP
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()