ESPHome  2024.11.1
ade7953_spi.cpp
Go to the documentation of this file.
1 #include "ade7953_spi.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace ade7953_spi {
7 
8 static const char *const TAG = "ade7953";
9 
11  this->spi_setup();
13 }
14 
16  ESP_LOGCONFIG(TAG, "ADE7953_spi:");
17  LOG_PIN(" CS Pin: ", this->cs_);
19 }
20 
21 bool AdE7953Spi::ade_write_8(uint16_t reg, uint8_t value) {
22  this->enable();
23  this->write_byte16(reg);
24  this->transfer_byte(0);
25  this->transfer_byte(value);
26  this->disable();
27  return false;
28 }
29 
30 bool AdE7953Spi::ade_write_16(uint16_t reg, uint16_t value) {
31  this->enable();
32  this->write_byte16(reg);
33  this->transfer_byte(0);
34  this->write_byte16(value);
35  this->disable();
36  return false;
37 }
38 
39 bool AdE7953Spi::ade_write_32(uint16_t reg, uint32_t value) {
40  this->enable();
41  this->write_byte16(reg);
42  this->transfer_byte(0);
43  this->write_byte16(value >> 16);
44  this->write_byte16(value & 0xFFFF);
45  this->disable();
46  return false;
47 }
48 
49 bool AdE7953Spi::ade_read_8(uint16_t reg, uint8_t *value) {
50  this->enable();
51  this->write_byte16(reg);
52  this->transfer_byte(0x80);
53  *value = this->read_byte();
54  this->disable();
55  return false;
56 }
57 
58 bool AdE7953Spi::ade_read_16(uint16_t reg, uint16_t *value) {
59  this->enable();
60  this->write_byte16(reg);
61  this->transfer_byte(0x80);
62  uint8_t recv[2];
63  this->read_array(recv, 2);
64  *value = encode_uint16(recv[0], recv[1]);
65  this->disable();
66  return false;
67 }
68 
69 bool AdE7953Spi::ade_read_32(uint16_t reg, uint32_t *value) {
70  this->enable();
71  this->write_byte16(reg);
72  this->transfer_byte(0x80);
73  uint8_t recv[4];
74  this->read_array(recv, 4);
75  *value = encode_uint32(recv[0], recv[1], recv[2], recv[3]);
76  this->disable();
77  return false;
78 }
79 
80 } // namespace ade7953_spi
81 } // namespace esphome
bool ade_write_8(uint16_t reg, uint8_t value) override
Definition: ade7953_spi.cpp:21
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
GPIOPin * cs_
Definition: spi.h:378
bool ade_write_32(uint16_t reg, uint32_t value) override
Definition: ade7953_spi.cpp:39
bool ade_read_8(uint16_t reg, uint8_t *value) override
Definition: ade7953_spi.cpp:49
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_read_32(uint16_t reg, uint32_t *value) override
Definition: ade7953_spi.cpp:69
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool ade_read_16(uint16_t reg, uint16_t *value) override
Definition: ade7953_spi.cpp:58
bool ade_write_16(uint16_t reg, uint16_t value) override
Definition: ade7953_spi.cpp:30