ESPHome 2025.8.0b2
Loading...
Searching...
No Matches
i2c_bus_esp_idf.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP_IDF
4
5#include "esp_idf_version.h"
7#include "i2c_bus.h"
8#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 2)
9#include <driver/i2c_master.h>
10#else
11#include <driver/i2c.h>
12#endif
13
14namespace esphome {
15namespace i2c {
16
22
23class IDFI2CBus : public InternalI2CBus, public Component {
24 public:
25 void setup() override;
26 void dump_config() override;
27 ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override;
28 ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) override;
29 float get_setup_priority() const override { return setup_priority::BUS; }
30
31 void set_scan(bool scan) { scan_ = scan; }
32 void set_sda_pin(uint8_t sda_pin) { sda_pin_ = sda_pin; }
33 void set_sda_pullup_enabled(bool sda_pullup_enabled) { sda_pullup_enabled_ = sda_pullup_enabled; }
34 void set_scl_pin(uint8_t scl_pin) { scl_pin_ = scl_pin; }
35 void set_scl_pullup_enabled(bool scl_pullup_enabled) { scl_pullup_enabled_ = scl_pullup_enabled; }
37 void set_timeout(uint32_t timeout) { timeout_ = timeout; }
38
39 int get_port() const override { return static_cast<int>(this->port_); }
40
41 private:
42 void recover_();
43 RecoveryCode recovery_result_;
44
45 protected:
46#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 2)
47 i2c_master_dev_handle_t dev_;
48 i2c_master_bus_handle_t bus_;
49 void i2c_scan() override;
50#endif
51 i2c_port_t port_;
52 uint8_t sda_pin_;
54 uint8_t scl_pin_;
56 uint32_t frequency_;
57 uint32_t timeout_ = 0;
58 bool initialized_ = false;
59};
60
61} // namespace i2c
62} // namespace esphome
63
64#endif // USE_ESP_IDF
uint8_t address
Definition bl0906.h:4
uint16_le_t frequency
Definition bl0942.h:6
bool scan_
Should we scan ? Can be set in the yaml.
Definition i2c_bus.h:108
void set_sda_pullup_enabled(bool sda_pullup_enabled)
float get_setup_priority() const override
ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t cnt) override
void set_frequency(uint32_t frequency)
void set_scl_pullup_enabled(bool scl_pullup_enabled)
i2c_master_bus_handle_t bus_
ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop) override
int get_port() const override
i2c_master_dev_handle_t dev_
void set_scl_pin(uint8_t scl_pin)
void set_timeout(uint32_t timeout)
void set_sda_pin(uint8_t sda_pin)
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
const float BUS
For communication buses like i2c/spi.
Definition component.cpp:47
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
the ReadBuffer structure stores a pointer to a read buffer and its length
Definition i2c_bus.h:24
the WriteBuffer structure stores a pointer to a write buffer and its length
Definition i2c_bus.h:30