7 static const char *
const TAG =
"cs5460a";
29 uint32_t config = (1 << 0) |
55 ESP_LOGCONFIG(TAG,
"Setting up CS5460A...");
58 float voltage_full_scale = 0.25;
83 ESP_LOGE(TAG,
"CS5460A reset failed!");
89 ESP_LOGCONFIG(TAG,
" Version: %" PRIx32, (status >> 6) & 7);
100 ESP_LOGCONFIG(TAG,
" Init ok");
137 time_left -= time_left / 5;
139 if (time_left > -500) {
150 if (time_left > -15000) {
154 ESP_LOGCONFIG(TAG,
"Device officially stuck, resetting");
161 this->
set_timeout(
"status-check", time_left, [
this]() {
171 if (!(status & 0xcbf83c)) {
176 uint32_t clear = 1 << 20;
180 if (status & (1 << 2)) {
182 ESP_LOGE(TAG,
"Low supply detected");
186 if (status & (1 << 3)) {
188 ESP_LOGE(TAG,
"Modulator oscillation on current channel");
192 if (status & (1 << 4)) {
194 ESP_LOGE(TAG,
"Modulator oscillation on voltage channel");
198 if (status & (1 << 5)) {
200 ESP_LOGE(TAG,
"Watch-dog timeout");
204 if (status & (1 << 11)) {
206 ESP_LOGE(TAG,
"EOUT Energy Accumulation Register out of range");
210 if (status & (1 << 12)) {
212 ESP_LOGE(TAG,
"Energy out of range");
216 if (status & (1 << 13)) {
218 ESP_LOGE(TAG,
"RMS voltage out of range");
222 if (status & (1 << 14)) {
224 ESP_LOGE(TAG,
"RMS current out of range");
228 if (status & (1 << 15)) {
230 ESP_LOGE(TAG,
"Power calculation out of range");
234 if (status & (1 << 16)) {
236 ESP_LOGE(TAG,
"Voltage out of range");
240 if (status & (1 << 17)) {
242 ESP_LOGE(TAG,
"Current out of range");
246 if (status & (1 << 19)) {
248 ESP_LOGE(TAG,
"Divide overflowed");
251 if (status & (1 << 22)) {
252 bool dir = status & (1 << 21);
255 ESP_LOGI(TAG,
"Energy counter %s pulse", dir ?
"negative" :
"positive");
259 uint32_t raw_current = 0;
261 uint32_t raw_energy = 0;
263 if (status & (1 << 23)) {
273 if (status & ((1 << 23) | (1 << 5))) {
296 if (status & (1 << 23)) {
308 int32_t
raw = (int32_t) (raw_energy << 8) >> 8;
322 ESP_LOGCONFIG(TAG,
"CS5460A:");
323 ESP_LOGCONFIG(TAG,
" Init status: %s",
325 LOG_PIN(
" CS Pin: ",
cs_);
326 ESP_LOGCONFIG(TAG,
" Samples / cycle: %" PRIu32,
samples_);
331 ESP_LOGCONFIG(TAG,
" Current HPF: %s",
current_hpf_ ?
"enabled" :
"disabled");
332 ESP_LOGCONFIG(TAG,
" Voltage HPF: %s",
voltage_hpf_ ?
"enabled" :
"disabled");
const uint32_t COMPONENT_STATE_LOOP
const uint32_t COMPONENT_STATE_FAILED
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
uint32_t prev_raw_current_
void spi_setup() override
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.
uint8_t transfer_byte(uint8_t data)
void dump_config() override
void write_byte(uint8_t data)
uint32_t IRAM_ATTR HOT millis()
uint32_t read_register_(uint8_t addr)
uint32_t prev_raw_energy_
uint32_t get_component_state() const
void write_register_(enum CS5460ARegister addr, uint32_t value)
void publish_state(float state)
Publish a new state to the front-end.
float current_multiplier_
float voltage_multiplier_
sensor::Sensor * voltage_sensor_
sensor::Sensor * current_sensor_
sensor::Sensor * power_sensor_
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
void schedule_next_check_()
void IRAM_ATTR HOT delay(uint32_t ms)