SatCat5
i2c_tca9548.h
1 // Copyright 2023-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Device driver for PCA9548A / TCA9548A I2C switches.
6 
7 #pragma once
8 
9 #include <satcat5/cfg_i2c.h>
10 #include <satcat5/utils.h>
11 
12 #ifndef SATCAT5_I2C_MAXCMD
13 #define SATCAT5_I2C_MAXCMD 16 // Each queue up to N transactions
14 #endif
15 
16 namespace satcat5 {
17  namespace device {
18  namespace i2c {
28  class Tca9548
31  {
32  public:
34  Tca9548(
35  satcat5::cfg::I2cGeneric* i2c, // Parent interface
36  const satcat5::util::I2cAddr& devaddr); // Device address
37 
42  bool select_mask(u8 mask);
43 
45  inline bool select_channel(unsigned n)
46  {return select_mask((u8)(1u << n));}
47 
48  // Forward read/write calls to the parent.
49  bool busy() override;
50  bool read(const satcat5::util::I2cAddr& devaddr,
51  u8 regbytes, u32 regaddr, u8 nread,
52  satcat5::cfg::I2cEventListener* callback = 0) override;
53  bool write(const satcat5::util::I2cAddr& devaddr,
54  u8 regbytes, u32 regaddr, u8 nwrite, const u8* data,
55  satcat5::cfg::I2cEventListener* callback = 0) override;
56 
57  protected:
58  // Forward callbacks to the original caller.
59  void i2c_done(
60  bool noack, const satcat5::util::I2cAddr& devaddr,
61  u32 regaddr, unsigned nread, const u8* rdata) override;
62 
63  // Calculate write-index for the circular buffer.
64  inline unsigned cb_wridx() const
65  {return satcat5::util::modulo_add_uns(m_cb_rdidx + m_cb_count, SATCAT5_I2C_MAXCMD);}
66 
67  // Pointer to the parent interface.
68  satcat5::cfg::I2cGeneric* const m_parent;
69  const satcat5::util::I2cAddr m_devaddr;
70 
71  // Circular queue of pending callbacks.
72  unsigned m_cb_count;
73  unsigned m_cb_rdidx;
74  satcat5::cfg::I2cEventListener* m_cb_queue[SATCAT5_I2C_MAXCMD];
75  };
76 
79  typedef satcat5::device::i2c::Tca9548 Pca9548;
80  }
81  }
82 }
Callback interface for cfg::I2cGeneric events.
Definition: cfg_i2c.h:78
Polymorphic API for a generic I2C interface.
Definition: cfg_i2c.h:95
Device driver for PCA9548A / TCA9548A I2C switches.
Definition: i2c_tca9548.h:31
bool write(const satcat5::util::I2cAddr &devaddr, u8 regbytes, u32 regaddr, u8 nwrite, const u8 *data, satcat5::cfg::I2cEventListener *callback=0) override
Add a write operation to the queue.
Definition: i2c_tca9548.cc:51
bool select_mask(u8 mask)
Select a channel or channel(s).
Definition: i2c_tca9548.cc:23
bool busy() override
Is the I2C controller currently busy?
Definition: i2c_tca9548.cc:30
bool select_channel(unsigned n)
Shortcut for selecting a specific channel.
Definition: i2c_tca9548.h:45
bool read(const satcat5::util::I2cAddr &devaddr, u8 regbytes, u32 regaddr, u8 nread, satcat5::cfg::I2cEventListener *callback=0) override
Add a read operation to the queue.
Definition: i2c_tca9548.cc:35
Tca9548(satcat5::cfg::I2cGeneric *i2c, const satcat5::util::I2cAddr &devaddr)
Constructor links to the specified I2C bus.
Definition: i2c_tca9548.cc:14
Conversion function for I2C device addresses.
Definition: cfg_i2c.h:30
Miscellaneous mathematical utility functions.