ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
i2c_bus_software.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_LIBRETINY
4
6#include "esphome/core/gpio.h"
7#include "i2c_bus.h"
8
9namespace esphome::i2c {
10
14class SoftwareI2CBus final : public I2CBus, public Component {
15 public:
16 void setup() override;
17 void dump_config() override;
18 ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
19 size_t read_count) override {
20 return this->transfer_(address, nullptr, 0, write_buffer, write_count, read_buffer, read_count);
21 }
22 ErrorCode write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *data,
23 size_t len) override {
24 return this->transfer_(address, prefix, prefix_len, data, len, nullptr, 0);
25 }
26 float get_setup_priority() const override { return setup_priority::BUS; }
27
28 void set_scan(bool scan) { this->scan_ = scan; }
29 void set_sda_pin(InternalGPIOPin *sda_pin) { this->sda_pin_ = sda_pin; }
30 void set_scl_pin(InternalGPIOPin *scl_pin) { this->scl_pin_ = scl_pin; }
32#ifdef I2C_PORT_FREQUENCY_COUNT
34 uint32_t get_frequency() const override { return this->frequency_; }
35#endif
37 void set_timeout(uint32_t timeout) { this->timeout_ = timeout; }
38
39 protected:
41 ErrorCode transfer_(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *write_buffer,
42 size_t write_count, uint8_t *read_buffer, size_t read_count);
43 bool recover_();
45 void start_();
46 void stop_();
47 bool release_scl_();
48 bool write_byte_(uint8_t data);
49 uint8_t read_byte_(bool ack);
50
56 bool timed_out_{false};
57 bool recovered_{false};
58 bool initialized_{false};
59 bool bus_stuck_{false};
60};
61
62} // namespace esphome::i2c
63
64#endif // USE_LIBRETINY
uint16_le_t frequency
Definition bl0942.h:6
This Class provides the methods to read and write bytes from an I2CBus.
Definition i2c_bus.h:28
bool scan_
Should we scan ? Can be set in the yaml.
Definition i2c_bus.h:77
Bit-banged I2C controller for LibreTiny chips without an Arduino Wire implementation (BK72xx).
uint32_t get_frequency() const override
float get_setup_priority() const override
void set_timeout(uint32_t timeout)
Maximum time in microseconds a device may hold SCL low (clock stretching).
void set_sda_pin(InternalGPIOPin *sda_pin)
ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count) override
ErrorCode switch_frequency(uint32_t frequency) override
ErrorCode write_prefixed(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *data, size_t len) override
void set_scl_pin(InternalGPIOPin *scl_pin)
ErrorCode transfer_(uint8_t address, const uint8_t *prefix, size_t prefix_len, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count)
Writes prefix then write_buffer in one transaction, then reads.
bool write_byte_(uint8_t data)
Shift out one byte, returns true if the device acknowledged it.
void set_frequency(uint32_t frequency)
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:39
const void size_t len
Definition hal.h:68
static void uint32_t
uint8_t ack