SatCat5
cfg_i2c.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 // Generic I2C interface
6 
7 #pragma once
8 
9 #include <satcat5/types.h>
10 
11 namespace satcat5 {
12  namespace util {
30  struct I2cAddr final { // GCOVR_EXCL_START
31  public:
32  I2cAddr() = delete;
33  I2cAddr(const I2cAddr& t) = default;
34  I2cAddr& operator=(const I2cAddr& t) = default;
35 
38  static constexpr I2cAddr addr7(u8 addr)
39  {return I2cAddr(2 * addr);}
40 
43  static constexpr I2cAddr addr8(u8 addr)
44  {return I2cAddr(addr & 0xFE);}
45 
48  static constexpr I2cAddr addr10(u16 addr)
49  {return I2cAddr(0xF000 | ((addr&0x300)<<1) | (addr&0xFF));}
50 
52  static constexpr I2cAddr native(u16 addr)
53  {return I2cAddr(addr);}
54 
56  inline bool is_10b() const {return m_addr > 255;}
57 
60  inline bool operator==(const I2cAddr& other) const
61  {return m_addr == other.m_addr;}
62  inline bool operator!=(const I2cAddr& other) const
63  {return m_addr != other.m_addr;}
65 
67  const u16 m_addr;
68 
69  private:
70  explicit constexpr I2cAddr(u16 addr) : m_addr(addr) {}
71  }; // GCOVR_EXCL_STOP
72  }
73 
74  namespace cfg {
79  public:
80  virtual void i2c_done(
81  bool noack, // Missing ACK during this command?
82  const satcat5::util::I2cAddr& devaddr, // Device address
83  u32 regaddr, // Register address (if applicable)
84  unsigned nread, // Number of bytes read (if applicable)
85  const u8* rdata) // Pointer to read buffer
86  = 0; // Child MUST override this method
87  protected:
88  ~I2cEventListener() {}
89  };
90 
95  class I2cGeneric {
96  public:
98  virtual bool busy() = 0;
99 
109  virtual bool read(const satcat5::util::I2cAddr& devaddr,
110  u8 regbytes, u32 regaddr, u8 nread,
111  satcat5::cfg::I2cEventListener* callback = 0) = 0;
112 
121  virtual bool write(const satcat5::util::I2cAddr& devaddr,
122  u8 regbytes, u32 regaddr, u8 nwrite, const u8* data,
123  satcat5::cfg::I2cEventListener* callback = 0) = 0;
124  };
125  }
126 }
Callback interface for cfg::I2cGeneric events.
Definition: cfg_i2c.h:78
Polymorphic API for a generic I2C interface.
Definition: cfg_i2c.h:95
virtual bool busy()=0
Is the I2C controller currently busy?
virtual bool read(const satcat5::util::I2cAddr &devaddr, u8 regbytes, u32 regaddr, u8 nread, satcat5::cfg::I2cEventListener *callback=0)=0
Add a read operation to the queue.
virtual bool write(const satcat5::util::I2cAddr &devaddr, u8 regbytes, u32 regaddr, u8 nwrite, const u8 *data, satcat5::cfg::I2cEventListener *callback=0)=0
Add a write operation to the queue.
Conversion function for I2C device addresses.
Definition: cfg_i2c.h:30
const u16 m_addr
Native internal address representation.
Definition: cfg_i2c.h:67
static constexpr I2cAddr addr8(u8 addr)
Create I2C address from an 8-bit input (left-justified) Example: I2cAddr my_addr = I2cAddr::addr8(0xE...
Definition: cfg_i2c.h:43
bool operator!=(const I2cAddr &other) const
Comparison operators:
Definition: cfg_i2c.h:62
static constexpr I2cAddr addr7(u8 addr)
Create I2C address from a 7-bit input (right-justified) Example: I2cAddr my_addr = I2cAddr::addr7(0x7...
Definition: cfg_i2c.h:38
bool operator==(const I2cAddr &other) const
Comparison operators:
Definition: cfg_i2c.h:60
bool is_10b() const
Is this a 10-bit address?
Definition: cfg_i2c.h:56
static constexpr I2cAddr addr10(u16 addr)
Create I2C address from a 10-bit input (right-justified) Example: I2cAddr my_addr = I2cAddr::addr10(0...
Definition: cfg_i2c.h:48
static constexpr I2cAddr native(u16 addr)
Create I2C address from its native internal representation.
Definition: cfg_i2c.h:52
Basic type aliases and prototypes used throughout SatCat5.