SatCat5
cfgbus_multiserial.h
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Partial driver for the multipurpose serial peripheral
6 
7 #pragma once
8 
9 #include <satcat5/cfgbus_interrupt.h>
10 #include <satcat5/pkt_buffer.h>
11 
12 namespace satcat5 {
13  namespace cfg {
26  , protected satcat5::io::EventListener
28  {
29  public:
31  inline u8 get_percent_full() const
32  {return m_tx.get_percent_full();}
33 
35  inline bool idle() const
36  {return m_cmd_queued == 0;}
37 
38  protected:
42  satcat5::cfg::ConfigBus* cfg, // ConfigBus interface object
43  unsigned devaddr, // ConfigBus device address
44  unsigned maxpkt, // Maximum queued commands
45  u8* txbuff, unsigned txsize, // Command buffer (ptr + len)
46  u8* rxbuff, unsigned rxsize); // Reply buffer (ptr + len)
47  ~MultiSerial() {}
48 
51  bool write_check(
52  unsigned ncmd, // Number of opcodes in this transaction
53  unsigned nread); // Number of reads in this transaction
54 
60  unsigned write_finish();
61 
67  virtual void read_done(unsigned cidx) = 0;
68 
69  // ConfigBus register map.
70  static const unsigned REGADDR_IRQ;
71  static const unsigned REGADDR_CFG;
72  static const unsigned REGADDR_STATUS;
73  static const unsigned REGADDR_DATA;
74 
75  // ConfigBus interface object.
77 
78  // Command and reply buffers
81 
82  private:
83  // Internal event handlers
84  void data_rcvd(satcat5::io::Readable* src) override;
85  void irq_event(); // ConfigBus interrupt
86  void poll_demand(); // Deferred interrupt
87 
88  // Internal state, not accessible to children.
89  unsigned const m_cmd_max; // Max queued commands
90  unsigned m_cmd_cbidx; // Index of next callback
91  unsigned m_cmd_queued; // Number of queued commands
92  unsigned m_new_wralloc; // Expected command length of new command
93  unsigned m_new_rdalloc; // Expected reply length for new command
94  unsigned m_rdalloc; // Unallocated space in reply buffer
95  unsigned m_irq_wrrem; // Remaining opcodes in current command
96  unsigned m_irq_rdrem; // Remaining reads in current command
97  };
98  }
99 }
Generic ConfigBus API.
Definition: cfgbus_core.h:124
Event-handler for individual ConfigBus interrupts.
Partial driver for the multipurpose serial peripheral.
u8 get_percent_full() const
How full is the transmit queue? (0-100%)
bool idle() const
Is the queue empty and the bus idle?
MultiSerial(satcat5::cfg::ConfigBus *cfg, unsigned devaddr, unsigned maxpkt, u8 *txbuff, unsigned txsize, u8 *rxbuff, unsigned rxsize)
Set all parameters for this Multiserial instance.
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
satcat5::io::PacketBuffer m_tx
Buffer for hardware commands.
void poll_demand()
Deferred event handler, called after request().
satcat5::io::PacketBuffer m_rx
Buffer for reply data.
virtual void read_done(unsigned cidx)=0
Callback when each transaction is finished.
unsigned write_finish()
Enqueue transaction after calling write_check().
bool write_check(unsigned ncmd, unsigned nread)
Is there enough space in the software queue? If it returns true, write each opcode and then call writ...
void irq_event()
Interrupt service routine.
Pointer-like wrapper for one or more ConfigBus registers.
Definition: cfgbus_core.h:76
Event-handler interface for newly received data.
Definition: io_readable.h:43
The PacketBuffer class is a wrapper for a circular buffer, with optional logic to support retention o...
Definition: pkt_buffer.h:66
u8 get_percent_full() const
Get overall buffer occupancy as percentage full (0-100%).
Definition: pkt_buffer.cc:41
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
An "OnDemand" object is polled only on request.
Definition: polling.h:131