SatCat5
cfgbus_core.h
Go to the documentation of this file.
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
28 
29 #pragma once
30 
31 #include <satcat5/interrupts.h>
32 #include <satcat5/list.h>
33 
36 #ifndef SATCAT5_CFGBUS_DIRECT
37 #define SATCAT5_CFGBUS_DIRECT 0
38 #endif
39 
40 namespace satcat5 {
41  namespace cfg {
44  const unsigned DEVS_PER_CFGBUS = 256;
45  const unsigned REGS_PER_DEVICE = 1024;
46  const unsigned MAX_DEVICES = 256;
49 
51  const unsigned REGADDR_ANY = 0;
52 
60  public:
61  WrappedRegister(ConfigBus* cfg, unsigned reg);
62  operator u32(); // Read from register
63  void operator=(u32 wrval); // Write to register
64  void write_repeat(unsigned count, const u32* data);
65  protected:
66  satcat5::cfg::ConfigBus* const m_cfg; // Parent interface
67  const unsigned m_reg; // Device + register index
68  };
69 
77  public:
78  WrappedRegisterPtr(ConfigBus* cfg, unsigned reg);
79  bool operator!() const; // Valid register?
80  satcat5::cfg::WrappedRegister operator*(); // Pointer dereference
81  satcat5::cfg::WrappedRegister operator[](unsigned idx);
82  satcat5::cfg::WrappedRegisterPtr operator+(unsigned idx);
83  protected:
84  satcat5::cfg::ConfigBus* const m_cfg; // Parent interface
85  const unsigned m_reg; // Device + register index
86  };
87 
88  // Prefer the direct or indirect interface?
89  #if SATCAT5_CFGBUS_DIRECT
90  typedef volatile u32* Register;
91  #define SATCAT5_NULL_REGISTER 0
92  #else
94  #define SATCAT5_NULL_REGISTER satcat5::cfg::WrappedRegisterPtr(0,0)
95  #endif
96 
99  enum class IoStatus {
100  OK = 0, // Operation successful
101  BUSERROR, // ConfigBus error
102  CMDERROR, // Invalid command
103  TIMEOUT, // Network timeout
104  };
105 
106  // Constants for legacy compatibility (deprecated).
107  constexpr auto IOSTATUS_OK =
108  satcat5::cfg::IoStatus::OK;
109  constexpr auto IOSTATUS_BUSERROR =
110  satcat5::cfg::IoStatus::BUSERROR;
111  constexpr auto IOSTATUS_CMDERROR =
112  satcat5::cfg::IoStatus::CMDERROR;
113  constexpr auto IOSTATUS_TIMEOUT =
114  satcat5::cfg::IoStatus::TIMEOUT;
115 
124  class ConfigBus {
125  public:
127  virtual satcat5::cfg::IoStatus read(unsigned regaddr, u32& rdval) = 0;
129  virtual satcat5::cfg::IoStatus write(unsigned regaddr, u32 wrval) = 0;
130 
135  unsigned regaddr, unsigned count, u32* result);
138  unsigned regaddr, unsigned count, u32* result);
141  unsigned regaddr, unsigned count, const u32* data);
144  unsigned regaddr, unsigned count, const u32* data);
145 
147  inline unsigned get_regaddr(unsigned dev, unsigned reg) const
148  {return satcat5::cfg::REGS_PER_DEVICE * dev + reg;}
149 
157 
159  unsigned count_irq() const;
160 
163  satcat5::cfg::Register get_register(unsigned dev, unsigned reg = 0);
164 
168  void irq_poll();
169 
170  protected:
171  // Constructor should only be called by children.
172  // (Only children should create or destroy base class.)
173  explicit ConfigBus(void* base_ptr = 0);
174  ~ConfigBus() {}
175 
176  // Direct-access pointer, if applicable.
177  volatile u32* m_base_ptr;
178 
179  // Linked-list of interrupt handlers.
181  };
182 
197  : public satcat5::cfg::ConfigBus
198  , public satcat5::irq::Handler
199  {
200  public:
203  ConfigBusMmap(void* base_ptr, int irq);
204  ~ConfigBusMmap() {}
205 
206  // Basic read and write operations.
207  satcat5::cfg::IoStatus read(unsigned regaddr, u32& val) override;
208  satcat5::cfg::IoStatus write(unsigned regaddr, u32 val) override;
209 
211  void* get_device_mmap(unsigned dev) const;
212 
214  inline volatile u32* get_register_mmap(unsigned addr) const
215  {return m_base_ptr + addr;}
216 
217  protected:
218  void irq_event() override;
219  };
220  }
221 }
IoStatus
Status codes for ConfigBus read/write operations.
Definition: cfgbus_core.h:99
const unsigned MAX_TOTAL_REGS
Fixed ConfigBus gateware interface parameters.
Definition: cfgbus_core.h:47
const unsigned REGS_PER_DEVICE
Fixed ConfigBus gateware interface parameters.
Definition: cfgbus_core.h:45
const unsigned MAX_DEVICES
Fixed ConfigBus gateware interface parameters.
Definition: cfgbus_core.h:46
const unsigned DEVS_PER_CFGBUS
Fixed ConfigBus gateware interface parameters.
Definition: cfgbus_core.h:44
const unsigned REGADDR_ANY
Shortcut for don't-care register address.
Definition: cfgbus_core.h:51
Generic ConfigBus API.
Definition: cfgbus_core.h:124
virtual satcat5::cfg::IoStatus read(unsigned regaddr, u32 &rdval)=0
Basic single-register read operation.
satcat5::cfg::Register get_register(unsigned dev, unsigned reg=0)
Create register-map object for the given device address.
Definition: cfgbus_core.cc:101
void irq_poll()
Poll all registered ConfigBus interrupt handlers.
Definition: cfgbus_core.cc:111
virtual satcat5::cfg::IoStatus write(unsigned regaddr, u32 wrval)=0
Basic single-register write operation.
virtual satcat5::cfg::IoStatus read_repeat(unsigned regaddr, unsigned count, u32 *result)
Bulk-read the same register N times.
Definition: cfgbus_core.cc:138
unsigned get_regaddr(unsigned dev, unsigned reg) const
Convert device + register to combined address.
Definition: cfgbus_core.h:147
virtual satcat5::cfg::IoStatus write_repeat(unsigned regaddr, unsigned count, const u32 *data)
Bulk-write the same register N times.
Definition: cfgbus_core.cc:160
void unregister_irq(satcat5::cfg::Interrupt *obj)
Remove an interrupt handler.
Definition: cfgbus_core.cc:91
virtual satcat5::cfg::IoStatus write_array(unsigned regaddr, unsigned count, const u32 *data)
Bulk-write with auto-increment (regaddr, regaddr+1, ...)
Definition: cfgbus_core.cc:149
void register_irq(satcat5::cfg::Interrupt *obj)
Add an interrupt handler.
Definition: cfgbus_core.cc:80
unsigned count_irq() const
Count attached interrupt handlers.
Definition: cfgbus_core.cc:96
virtual satcat5::cfg::IoStatus read_array(unsigned regaddr, unsigned count, u32 *result)
Bulk-read with auto-increment (regaddr, regaddr+1, ...) Bulk read/write operations are more efficient...
Definition: cfgbus_core.cc:127
Memory-mapped local ConfigBus.
Definition: cfgbus_core.h:199
satcat5::cfg::IoStatus write(unsigned regaddr, u32 val) override
Basic single-register write operation.
Definition: cfgbus_core.cc:176
void * get_device_mmap(unsigned dev) const
Get a raw pointer to the designated device-address.
Definition: cfgbus_core.cc:188
volatile u32 * get_register_mmap(unsigned addr) const
Get a raw pointer to the designated combined-address.
Definition: cfgbus_core.h:214
void irq_event() override
Method called whenever an interrupt is triggered.
Definition: cfgbus_core.cc:193
satcat5::cfg::IoStatus read(unsigned regaddr, u32 &val) override
Basic single-register read operation.
Definition: cfgbus_core.cc:171
ConfigBusMmap(void *base_ptr, int irq)
Constructor accepts the base pointer for the memory-map interface, and the interrupt-index for the sh...
Definition: cfgbus_core.cc:181
Event-handler for individual ConfigBus interrupts.
Generic wrapper for a specific ConfigBus register.
Definition: cfgbus_core.h:59
Pointer-like wrapper for one or more ConfigBus registers.
Definition: cfgbus_core.h:76
Parent object for receiving interrupt-handler callbacks.
Templated functions for manipulating singly-linked lists.
Platform-agnostic API for interrupt management.