ESPHome  2024.2.1
i2c_bus.h
Go to the documentation of this file.
1 #pragma once
2 #include <cstdint>
3 #include <cstddef>
4 #include <utility>
5 #include <vector>
6 
7 namespace esphome {
8 namespace i2c {
9 
11 enum ErrorCode {
12  NO_ERROR = 0,
13  ERROR_OK = 0,
20  ERROR_CRC = 7,
21 };
22 
24 struct ReadBuffer {
25  uint8_t *data;
26  size_t len;
27 };
28 
30 struct WriteBuffer {
31  const uint8_t *data;
32  size_t len;
33 };
34 
40 class I2CBus {
41  public:
47  virtual ErrorCode read(uint8_t address, uint8_t *buffer, size_t len) {
48  ReadBuffer buf;
49  buf.data = buffer;
50  buf.len = len;
51  return readv(address, &buf, 1);
52  }
53 
60  virtual ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t count) = 0;
61 
62  virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len) {
63  return write(address, buffer, len, true);
64  }
65 
73  virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len, bool stop) {
74  WriteBuffer buf;
75  buf.data = buffer;
76  buf.len = len;
77  return writev(address, &buf, 1, stop);
78  }
79 
80  virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt) {
81  return writev(address, buffers, cnt, true);
82  }
83 
92  virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t count, bool stop) = 0;
93 
94  protected:
97  void i2c_scan_() {
98  for (uint8_t address = 8; address < 120; address++) {
99  auto err = writev(address, nullptr, 0);
100  if (err == ERROR_OK) {
101  scan_results_.emplace_back(address, true);
102  } else if (err == ERROR_UNKNOWN) {
103  scan_results_.emplace_back(address, false);
104  }
105  }
106  }
107  std::vector<std::pair<uint8_t, bool>> scan_results_;
108  bool scan_{false};
109 };
110 
111 } // namespace i2c
112 } // namespace esphome
the WriteBuffer structure stores a pointer to a write buffer and its length
Definition: i2c_bus.h:30
void i2c_scan_()
Scans the I2C bus for devices.
Definition: i2c_bus.h:97
std::vector< std::pair< uint8_t, bool > > scan_results_
array containing scan results
Definition: i2c_bus.h:107
virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len, bool stop)
Creates a WriteBuffer and calls the writev() method to send the bytes from this buffer.
Definition: i2c_bus.h:73
the ReadBuffer structure stores a pointer to a read buffer and its length
Definition: i2c_bus.h:24
method called invalid argument(s)
Definition: i2c_bus.h:14
uint8_t * data
pointer to the read buffer
Definition: i2c_bus.h:25
timeout while waiting to receive bytes
Definition: i2c_bus.h:16
No error found during execution of method.
Definition: i2c_bus.h:12
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt)
Definition: i2c_bus.h:80
size_t len
length of the buffer
Definition: i2c_bus.h:26
No error found during execution of method.
Definition: i2c_bus.h:13
I2C bus acknowledgment not received.
Definition: i2c_bus.h:15
bytes received with a CRC error
Definition: i2c_bus.h:20
This Class provides the methods to read and write bytes from an I2CBus.
Definition: i2c_bus.h:40
size_t len
length of the buffer
Definition: i2c_bus.h:32
requested a transfer larger than buffers can hold
Definition: i2c_bus.h:18
virtual ErrorCode read(uint8_t address, uint8_t *buffer, size_t len)
Creates a ReadBuffer and calls the virtual readv() method to read bytes into this buffer...
Definition: i2c_bus.h:47
virtual ErrorCode write(uint8_t address, const uint8_t *buffer, size_t len)
Definition: i2c_bus.h:62
const uint8_t * data
pointer to the write buffer
Definition: i2c_bus.h:31
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11
miscellaneous I2C error during execution
Definition: i2c_bus.h:19
call method to a not initialized bus
Definition: i2c_bus.h:17