/ firmware / include / i2c.h
i2c.h
 1  #pragma once
 2  
 3  #include <TCA9548.h>
 4  #include <stddef.h>
 5  #include <stdint.h>
 6  #include <Wire.h>
 7  
 8  namespace hardware::i2c {
 9  
10  enum class Bus : uint8_t {
11    Bus0 = 0,
12    Bus1 = 1,
13  };
14  
15  struct BusDescriptor {
16    Bus bus;
17    TwoWire *wire;
18    bool ready;
19  };
20  
21  struct TopologySnapshot {
22    bool legacy_power_enabled;
23    bool mux_present;
24    uint8_t mux_address;
25    bool mux_odd_power_enabled;
26    bool mux_even_power_enabled;
27  };
28  
29  struct DeviceAccessCommand {
30    Bus bus;
31    int8_t mux_channel;
32    TwoWire *wire;
33    bool ok;
34  };
35  
36  struct ScanCommand {
37    char *buffer;
38    size_t capacity;
39    int length;
40  };
41  
42  struct DiscoveredDevice {
43    uint8_t bus;
44    uint8_t address;
45    int8_t mux_channel;
46  };
47  
48  constexpr size_t MAX_DISCOVERED_DEVICES = 32;
49  
50  const char *deviceNameAt(uint8_t address, int8_t mux_channel = -1);
51  
52  extern TCA9548 mux;
53  
54  void enable();
55  void disable();
56  bool isEnabled();
57  
58  bool initialize();
59  bool accessBus(BusDescriptor *descriptor);
60  bool accessTopology(TopologySnapshot *snapshot);
61  bool accessDevice(DeviceAccessCommand *command);
62  void clearSelection();
63  bool scan(ScanCommand *command);
64  size_t discoverAll(DiscoveredDevice *devices, size_t capacity);
65  bool runDiscovery();
66  bool findDevice(uint8_t address, DiscoveredDevice *result);
67  
68  #ifdef PIO_UNIT_TESTING
69  void test();
70  #endif
71  
72  }
73