ESPHome  2024.10.0
tca9548a.cpp
Go to the documentation of this file.
1 #include "tca9548a.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace tca9548a {
6 
7 static const char *const TAG = "tca9548a";
8 
10  auto err = this->parent_->switch_to_channel(channel_);
11  if (err != i2c::ERROR_OK)
12  return err;
13  err = this->parent_->bus_->readv(address, buffers, cnt);
15  return err;
16 }
17 i2c::ErrorCode TCA9548AChannel::writev(uint8_t address, i2c::WriteBuffer *buffers, size_t cnt, bool stop) {
18  auto err = this->parent_->switch_to_channel(channel_);
19  if (err != i2c::ERROR_OK)
20  return err;
21  err = this->parent_->bus_->writev(address, buffers, cnt, stop);
23  return err;
24 }
25 
27  ESP_LOGCONFIG(TAG, "Setting up TCA9548A...");
28  uint8_t status = 0;
29  if (this->read(&status, 1) != i2c::ERROR_OK) {
30  ESP_LOGE(TAG, "TCA9548A failed");
31  this->mark_failed();
32  return;
33  }
34  ESP_LOGD(TAG, "Channels currently open: %d", status);
35 }
37  ESP_LOGCONFIG(TAG, "TCA9548A:");
38  LOG_I2C_DEVICE(this);
39 }
40 
42  if (this->is_failed())
44 
45  uint8_t channel_val = 1 << channel;
46  return this->write(&channel_val, 1);
47 }
48 
50  if (this->write(&TCA9548A_DISABLE_CHANNELS_COMMAND, 1) != i2c::ERROR_OK) {
51  ESP_LOGE(TAG, "Failed to disable all channels.");
52  this->status_set_error(); // couldn't disable channels, set error status
53  }
54 }
55 
56 } // namespace tca9548a
57 } // namespace esphome
the WriteBuffer structure stores a pointer to a write buffer and its length
Definition: i2c_bus.h:30
i2c::ErrorCode writev(uint8_t address, i2c::WriteBuffer *buffers, size_t cnt, bool stop) override
Definition: tca9548a.cpp:17
the ReadBuffer structure stores a pointer to a read buffer and its length
Definition: i2c_bus.h:24
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt)
Definition: i2c_bus.h:80
TCA9548AComponent * parent_
Definition: tca9548a.h:22
No error found during execution of method.
Definition: i2c_bus.h:13
uint8_t status
Definition: bl0942.h:74
I2CBus * bus_
pointer to I2CBus instance
Definition: i2c.h:270
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
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
i2c::ErrorCode switch_to_channel(uint8_t channel)
Definition: tca9548a.cpp:41
i2c::ErrorCode readv(uint8_t address, i2c::ReadBuffer *buffers, size_t cnt) override
Definition: tca9548a.cpp:9
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11
call method to a not initialized bus
Definition: i2c_bus.h:17
virtual ErrorCode readv(uint8_t address, ReadBuffer *buffers, size_t count)=0
This virtual method reads bytes from an I2CBus into an array of ReadBuffer.