13#define LOG_I2C_DEVICE(this) ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
22static_assert(
sizeof(
I2CRegValue) == 2,
"each entry is sent to the bus as is");
63 explicit operator uint8_t()
const {
return get(); }
118 explicit operator uint8_t()
const {
return get(); }
205 ErrorCode write_read(
const uint8_t *write_data,
size_t write_len, uint8_t *read_data,
size_t read_len)
const {
235 if (err ==
ERROR_OK && (
id & mask) == (expected & mask))
239 component->mark_failed(err ==
ERROR_OK ? LOG_STR(
"Wrong chip ID") : LOG_STR(ESP_LOG_MSG_COMM_FAIL));
255 template<
size_t N> optional<std::array<uint8_t, N>>
read_bytes(uint8_t a_register) {
256 std::array<uint8_t, N> res;
257 if (!this->
read_bytes(a_register, res.data(), N)) {
263 std::array<uint8_t, N> res;
287 for (
size_t i = 0; i <
len; i++)
302 bool write_bytes(uint8_t a_register,
const std::vector<uint8_t> &data)
const {
303 return write_bytes(a_register, data.data(), data.size());
306 template<
size_t N>
bool write_bytes(uint8_t a_register,
const std::array<uint8_t, N> &data) {
307 return write_bytes(a_register, data.data(), data.size());
310 bool write_bytes_16(uint8_t a_register,
const uint16_t *data, uint8_t
len)
const;
This Class provides the methods to read and write bytes from an I2CBus.
virtual ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count)=0
This virtual method writes bytes to an I2CBus from an array, then reads bytes into an array of ReadBu...
This Class provides the methods to read/write bytes from/to an i2c device.
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
ErrorCode write_read(const uint8_t *write_data, size_t write_len, uint8_t *read_data, size_t read_len) const
writes an array of bytes to a device, then reads an array, as a single transaction
bool read_bytes_raw(uint8_t *data, uint8_t len) const
bool write_bytes(uint8_t a_register, const std::array< uint8_t, N > &data)
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len) const
write an array of bytes to a specific register in the I²C device
bool write_byte_16_le(uint8_t a_register, uint16_t data) const
Write a 16-bit value least significant byte first.
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
bool write_bytes_16(uint8_t a_register, const uint16_t *data, uint8_t len) const
void set_i2c_bus(I2CBus *bus)
we store the pointer to the I2CBus to use
bool read_bytes_16_le(uint8_t a_register, uint16_t *data, uint8_t len)
Read len 16-bit values stored least significant byte first; no conversion is compiled on little-endia...
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
uint8_t get_i2c_address() const
Returns the I2C address of the object.
bool read_bytes_16_le(uint8_t a_register, int16_t *data, uint8_t len)
bool write_registers(const I2CRegValue(&table)[N]) const
optional< std::array< uint8_t, N > > read_bytes_raw()
I2CBus * bus_
pointer to I2CBus instance
optional< uint8_t > read_byte(uint8_t a_register)
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
uint8_t address_
store the address of the device on the bus
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
bool read_byte_16(uint8_t a_register, uint16_t *data)
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
optional< std::array< uint8_t, N > > read_bytes(uint8_t a_register)
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
bool write_bytes(uint8_t a_register, const std::vector< uint8_t > &data) const
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
bool write_byte_16(uint8_t a_register, uint16_t data) const
I2CRegister16 reg16(uint16_t a_register)
calls the I2CRegister16 constructor
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
bool write_registers(const I2CRegValue *table, size_t count) const
Write each register and value of table in order, stopping at the first failure.
bool read_byte_16_le(uint8_t a_register, uint16_t *data)
Read a 16-bit value stored least significant byte first.
I2CDevice()=default
we use the C++ default constructor
This class is used to create I2CRegister16 objects that act as proxies to read/write internal registe...
uint16_t register_
the internal 16 bits address of the register
I2CRegister16(I2CDevice *parent, uint16_t a_register)
protected constructor that store the owning object and the register address.
I2CRegister16 & operator&=(uint8_t value)
overloads the compound &= operator.
I2CRegister16 & operator|=(uint8_t value)
overloads the compound |= operator.
I2CDevice * parent_
I2CDevice object pointer.
uint8_t get() const
returns the register value
I2CRegister16 & operator=(uint8_t value)
overloads the = operator.
This class is used to create I2CRegister objects that act as proxies to read/write internal registers...
I2CRegister(I2CDevice *parent, uint8_t a_register)
protected constructor that stores the owning object and the register address.
I2CRegister & operator|=(uint8_t value)
overloads the compound |= operator.
uint8_t get() const
returns the register value
I2CRegister & operator&=(uint8_t value)
overloads the compound &= operator.
I2CDevice * parent_
I2CDevice object pointer.
uint8_t register_
the internal address of the register
I2CRegister & operator=(uint8_t value)
overloads the = operator.
const Component * component
uint16_t i2ctohs(uint16_t i2cshort)
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
void log_wrong_chip_id(uint8_t id, uint8_t expected)
Out of line so each check_chip_id() caller only pays for a call.
uint16_t htoi2cs(uint16_t hostshort)
constexpr T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order.
constexpr T convert_little_endian(T val)
Convert a value between host byte order and little endian (least significant byte first) order.
One register write for I2CDevice::write_registers(); tables of these may live in PROGMEM.