ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ade7953_spi.cpp
Go to the documentation of this file.
1#include "ade7953_spi.h"
3#include "esphome/core/log.h"
4
6
7static const char *const TAG = "ade7953";
8
9// Datasheet requires at least 1.2µs after clearing CONFIG LOCK_BIT before raising CS
10constexpr uint8_t CONFIG_LOCK_SETTLE_US = 2;
11
16
18 ESP_LOGCONFIG(TAG, "ADE7953_spi:");
19 LOG_PIN(" CS Pin: ", this->cs_);
21}
22
23bool AdE7953Spi::ade_write_8(uint16_t reg, uint8_t value) {
24 this->enable();
25 this->write_byte16(reg);
26 this->transfer_byte(0);
27 this->transfer_byte(value);
28 this->disable();
29 return false;
30}
31
32bool AdE7953Spi::ade_write_16(uint16_t reg, uint16_t value) {
33 this->enable();
34 this->write_byte16(reg);
35 this->transfer_byte(0);
36 this->write_byte16(value);
37 if (reg == ade7953_base::CONFIG_16) {
39 }
40 this->disable();
41 return false;
42}
43
44bool AdE7953Spi::ade_write_32(uint16_t reg, uint32_t value) {
45 this->enable();
46 this->write_byte16(reg);
47 this->transfer_byte(0);
48 this->write_byte16(value >> 16);
49 this->write_byte16(value & 0xFFFF);
50 this->disable();
51 return false;
52}
53
54bool AdE7953Spi::ade_read_8(uint16_t reg, uint8_t *value) {
55 this->enable();
56 this->write_byte16(reg);
57 this->transfer_byte(0x80);
58 *value = this->read_byte();
59 this->disable();
60 return false;
61}
62
63bool AdE7953Spi::ade_read_16(uint16_t reg, uint16_t *value) {
64 this->enable();
65 this->write_byte16(reg);
66 this->transfer_byte(0x80);
67 uint8_t recv[2];
68 this->read_array(recv, 2);
69 *value = encode_uint16(recv[0], recv[1]);
70 this->disable();
71 return false;
72}
73
74bool AdE7953Spi::ade_read_32(uint16_t reg, uint32_t *value) {
75 this->enable();
76 this->write_byte16(reg);
77 this->transfer_byte(0x80);
78 uint8_t recv[4];
79 this->read_array(recv, 4);
80 *value = encode_uint32(recv[0], recv[1], recv[2], recv[3]);
81 this->disable();
82 return false;
83}
84
85} // namespace esphome::ade7953_spi
bool ade_write_8(uint16_t reg, uint8_t value) override
bool ade_read_8(uint16_t reg, uint8_t *value) override
bool ade_read_32(uint16_t reg, uint32_t *value) override
bool ade_write_32(uint16_t reg, uint32_t value) override
bool ade_read_16(uint16_t reg, uint16_t *value) override
bool ade_write_16(uint16_t reg, uint16_t value) override
constexpr uint8_t CONFIG_LOCK_SETTLE_US
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:867
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:859
static void uint32_t