ESPHome 2026.1.5
Loading...
Searching...
No Matches
i2c_bus.h
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
3#include <cstdint>
4#include <cstring>
5#include <memory>
6#include <utility>
7#include <vector>
8
10
11namespace esphome {
12namespace i2c {
13
26
28struct ReadBuffer {
29 uint8_t *data;
30 size_t len;
31};
32
35 const uint8_t *data;
36 size_t len;
37};
38
44class I2CBus {
45 public:
46 virtual ~I2CBus() = default;
47
58 virtual ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
59 size_t read_count) = 0;
60
61 // Legacy functions for compatibility
62
63 ErrorCode read(uint8_t address, uint8_t *buffer, size_t len) {
64 return this->write_readv(address, nullptr, 0, buffer, len);
65 }
66
67 ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len, bool stop = true) {
68 return this->write_readv(address, buffer, len, nullptr, 0);
69 }
70
71 ESPDEPRECATED("This method is deprecated and will be removed in ESPHome 2026.3.0. Use write_readv() instead.",
72 "2025.9.0")
73 ErrorCode readv(uint8_t address, ReadBuffer *read_buffers, size_t count) {
74 size_t total_len = 0;
75 for (size_t i = 0; i != count; i++) {
76 total_len += read_buffers[i].len;
77 }
78
79 SmallBufferWithHeapFallback<128> buffer_alloc(total_len); // Most I2C reads are small
80 uint8_t *buffer = buffer_alloc.get();
81
82 auto err = this->write_readv(address, nullptr, 0, buffer, total_len);
83 if (err != ERROR_OK)
84 return err;
85 size_t pos = 0;
86 for (size_t i = 0; i != count; i++) {
87 if (read_buffers[i].len != 0) {
88 std::memcpy(read_buffers[i].data, buffer + pos, read_buffers[i].len);
89 pos += read_buffers[i].len;
90 }
91 }
92 return ERROR_OK;
93 }
94
95 ESPDEPRECATED("This method is deprecated and will be removed in ESPHome 2026.3.0. Use write_readv() instead.",
96 "2025.9.0")
97 ErrorCode writev(uint8_t address, const WriteBuffer *write_buffers, size_t count, bool stop = true) {
98 size_t total_len = 0;
99 for (size_t i = 0; i != count; i++) {
100 total_len += write_buffers[i].len;
101 }
102
103 SmallBufferWithHeapFallback<128> buffer_alloc(total_len); // Most I2C writes are small
104 uint8_t *buffer = buffer_alloc.get();
105
106 size_t pos = 0;
107 for (size_t i = 0; i != count; i++) {
108 std::memcpy(buffer + pos, write_buffers[i].data, write_buffers[i].len);
109 pos += write_buffers[i].len;
110 }
111
112 return this->write_readv(address, buffer, total_len, nullptr, 0);
113 }
114
115 protected:
118 void i2c_scan_();
119 std::vector<std::pair<uint8_t, bool>> scan_results_;
120 bool scan_{false};
121};
122
123class InternalI2CBus : public I2CBus {
124 public:
127 virtual int get_port() const = 0;
128};
129
130} // namespace i2c
131} // namespace esphome
uint8_t address
Definition bl0906.h:4
Helper class for efficient buffer allocation - uses stack for small sizes, heap for large This is use...
Definition helpers.h:368
This Class provides the methods to read and write bytes from an I2CBus.
Definition i2c_bus.h:44
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...
bool scan_
Should we scan ? Can be set in the yaml.
Definition i2c_bus.h:120
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
Definition i2c_bus.h:119
uint8_t * buffer
Definition i2c_bus.h:80
ReadBuffer size_t count
Definition i2c_bus.h:73
virtual ~I2CBus()=default
ESPDEPRECATED("This method is deprecated and will be removed in ESPHome 2026.3.0. Use write_readv() instead.", "2025.9.0") ErrorCode readv(uint8_t address
ReadBuffer * read_buffers
Definition i2c_bus.h:73
const WriteBuffer size_t bool stop
Definition i2c_bus.h:97
SmallBufferWithHeapFallback< 128 > buffer_alloc(total_len)
const WriteBuffer * write_buffers
Definition i2c_bus.h:97
ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len, bool stop=true)
Definition i2c_bus.h:67
ESPDEPRECATED("This method is deprecated and will be removed in ESPHome 2026.3.0. Use write_readv() instead.", "2025.9.0") ErrorCode writev(uint8_t address
ErrorCode read(uint8_t address, uint8_t *buffer, size_t len)
Definition i2c_bus.h:63
virtual int get_port() const =0
Returns the I2C port number.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:15
@ ERROR_CRC
bytes received with a CRC error
Definition i2c_bus.h:24
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:17
@ ERROR_TOO_LARGE
requested a transfer larger than buffers can hold
Definition i2c_bus.h:22
@ ERROR_INVALID_ARGUMENT
method called invalid argument(s)
Definition i2c_bus.h:18
@ NO_ERROR
No error found during execution of method.
Definition i2c_bus.h:16
@ ERROR_TIMEOUT
timeout while waiting to receive bytes
Definition i2c_bus.h:20
@ ERROR_NOT_ACKNOWLEDGED
I2C bus acknowledgment not received.
Definition i2c_bus.h:19
@ ERROR_NOT_INITIALIZED
call method to a not initialized bus
Definition i2c_bus.h:21
@ ERROR_UNKNOWN
miscellaneous I2C error during execution
Definition i2c_bus.h:23
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:595
the ReadBuffer structure stores a pointer to a read buffer and its length
Definition i2c_bus.h:28
size_t len
length of the buffer
Definition i2c_bus.h:30
uint8_t * data
pointer to the read buffer
Definition i2c_bus.h:29
the WriteBuffer structure stores a pointer to a write buffer and its length
Definition i2c_bus.h:34
size_t len
length of the buffer
Definition i2c_bus.h:36
const uint8_t * data
pointer to the write buffer
Definition i2c_bus.h:35