XPoint 0.1.0
Hardware-agnostic crosspoint matrix routing library
Loading...
Searching...
No Matches
TCA9548AInterface.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (c) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
3// https://github.com/dstroy0/XPoint
4
46#ifndef TCA9548A_INTERFACE_H
47#define TCA9548A_INTERFACE_H
48
49#include "I2CInterface.h"
50
63{
64 I2CInterface *_bus;
65 uint8_t _muxAddr;
66 uint8_t _channelMask;
67
68 /* Per-mux-address cache of the last channel-select byte written.
69 * Indexed by (_muxAddr & 0x7): slot 0 = 0x70, …, slot 7 = 0x77.
70 * Sentinel 0xFFu means "unknown / needs re-select".
71 *
72 * Defined inside the class body → implicitly inline in C++11, so the
73 * function-local static `s[]` is shared across all translation units. */
74 static uint8_t &_lastSel(uint8_t slot)
75 {
76 static uint8_t s[8] = {0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu};
77 return s[slot];
78 }
79
80 /* Select this channel on the mux only if it differs from the cached value.
81 * On a cache hit: zero extra I2C transactions. */
82 inline void _selectIfNeeded()
83 {
84 uint8_t &sel = _lastSel(_muxAddr & 0x7u);
85 if (sel != _channelMask)
86 {
87 _bus->writeByte(_muxAddr, _channelMask);
88 sel = _channelMask;
89 }
90 }
91
92 public:
98 TCA9548AInterface(I2CInterface *bus, uint8_t muxAddr, uint8_t channel)
99 : _bus(bus), _muxAddr(muxAddr), _channelMask((uint8_t)(1u << (channel & 0x7u)))
100 {
101 }
102
110 void begin() override
111 {
112 _bus->begin();
113 _lastSel(_muxAddr & 0x7u) = 0xFFu;
114 }
115
126 void writeRegister(uint8_t devAddr, uint8_t reg, uint8_t val) override
127 {
128 _selectIfNeeded();
129 _bus->writeRegister(devAddr, reg, val);
130 }
131};
132
133#endif // TCA9548A_INTERFACE_H
Minimal abstract I2C interface consumed by MCP23017Driver.
Abstract I2C bus interface.
Definition I2CInterface.h:28
virtual void begin()
Initialize the I2C bus.
Definition I2CInterface.h:40
virtual void writeRegister(uint8_t addr, uint8_t reg, uint8_t val)=0
Write one byte to a device register.
virtual void writeByte(uint8_t addr, uint8_t data)
Write a single data byte to a device (no register address byte).
Definition I2CInterface.h:67
I2C channel-select decorator for the TCA9548A / PCA9548A.
Definition TCA9548AInterface.h:63
void writeRegister(uint8_t devAddr, uint8_t reg, uint8_t val) override
Activate this channel (if not already selected), then forward the write.
Definition TCA9548AInterface.h:126
void begin() override
Initialise the underlying bus and invalidate this mux's channel cache.
Definition TCA9548AInterface.h:110
TCA9548AInterface(I2CInterface *bus, uint8_t muxAddr, uint8_t channel)
Definition TCA9548AInterface.h:98