ESPHome  2024.12.2
ade7953_i2c.cpp
Go to the documentation of this file.
1 #include "ade7953_i2c.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace ade7953_i2c {
7 
8 static const char *const TAG = "ade7953";
9 
11  ESP_LOGCONFIG(TAG, "ADE7953_i2c:");
12  LOG_I2C_DEVICE(this);
14 }
15 bool AdE7953I2c::ade_write_8(uint16_t reg, uint8_t value) {
16  uint8_t data[3];
17  data[0] = reg >> 8;
18  data[1] = reg >> 0;
19  data[2] = value;
20  return this->write(data, 3) != i2c::ERROR_OK;
21 }
22 bool AdE7953I2c::ade_write_16(uint16_t reg, uint16_t value) {
23  uint8_t data[4];
24  data[0] = reg >> 8;
25  data[1] = reg >> 0;
26  data[2] = value >> 8;
27  data[3] = value >> 0;
28  return this->write(data, 4) != i2c::ERROR_OK;
29 }
30 bool AdE7953I2c::ade_write_32(uint16_t reg, uint32_t value) {
31  uint8_t data[6];
32  data[0] = reg >> 8;
33  data[1] = reg >> 0;
34  data[2] = value >> 24;
35  data[3] = value >> 16;
36  data[4] = value >> 8;
37  data[5] = value >> 0;
38  return this->write(data, 6) != i2c::ERROR_OK;
39 }
40 bool AdE7953I2c::ade_read_8(uint16_t reg, uint8_t *value) {
41  uint8_t reg_data[2];
42  reg_data[0] = reg >> 8;
43  reg_data[1] = reg >> 0;
44  i2c::ErrorCode err = this->write(reg_data, 2);
45  if (err != i2c::ERROR_OK)
46  return true;
47  err = this->read(value, 1);
48  return (err != i2c::ERROR_OK);
49 }
50 bool AdE7953I2c::ade_read_16(uint16_t reg, uint16_t *value) {
51  uint8_t reg_data[2];
52  reg_data[0] = reg >> 8;
53  reg_data[1] = reg >> 0;
54  i2c::ErrorCode err = this->write(reg_data, 2);
55  if (err != i2c::ERROR_OK)
56  return true;
57  uint8_t recv[2];
58  err = this->read(recv, 2);
59  if (err != i2c::ERROR_OK)
60  return true;
61  *value = encode_uint16(recv[0], recv[1]);
62  return false;
63 }
64 bool AdE7953I2c::ade_read_32(uint16_t reg, uint32_t *value) {
65  uint8_t reg_data[2];
66  reg_data[0] = reg >> 8;
67  reg_data[1] = reg >> 0;
68  i2c::ErrorCode err = this->write(reg_data, 2);
69  if (err != i2c::ERROR_OK)
70  return true;
71  uint8_t recv[4];
72  err = this->read(recv, 4);
73  if (err != i2c::ERROR_OK)
74  return true;
75  *value = encode_uint32(recv[0], recv[1], recv[2], recv[3]);
76  return false;
77 }
78 
79 } // namespace ade7953_i2c
80 } // namespace esphome
bool ade_read_32(uint16_t reg, uint32_t *value) override
Definition: ade7953_i2c.cpp:64
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition: i2c.h:149
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
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:187
bool ade_read_8(uint16_t reg, uint8_t *value) override
Definition: ade7953_i2c.cpp:40
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
No error found during execution of method.
Definition: i2c_bus.h:13
bool ade_write_32(uint16_t reg, uint32_t value) override
Definition: ade7953_i2c.cpp:30
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:183
bool ade_write_8(uint16_t reg, uint8_t value) override
Definition: ade7953_i2c.cpp:15
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool ade_read_16(uint16_t reg, uint16_t *value) override
Definition: ade7953_i2c.cpp:50
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11
bool ade_write_16(uint16_t reg, uint16_t value) override
Definition: ade7953_i2c.cpp:22