SatCat5
cfgbus_spi.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 // Interface driver for the cfgbus_spi_controller block
6 
7 #pragma once
8 
9 #include <satcat5/cfg_spi.h>
10 #include <satcat5/cfgbus_multiserial.h>
11 
12 // Default sizes for the SPI working buffers
13 // For reference: 256 bytes = 2.0 msec buffer @ 1 Mbaud
14 #ifndef SATCAT5_SPI_TXBUFF
15 #define SATCAT5_SPI_TXBUFF 256 // Up to N bytes of queued commands
16 #endif
17 
18 #ifndef SATCAT5_SPI_RXBUFF
19 #define SATCAT5_SPI_RXBUFF 64 // Up to N bytes of queued replies
20 #endif
21 
22 #ifndef SATCAT5_SPI_MAXCMD
23 #define SATCAT5_SPI_MAXCMD 16 // Each queue up to N transactions
24 #endif
25 
26 namespace satcat5 {
27  namespace cfg {
41  class Spi
44  {
45  public:
47  Spi(ConfigBus* cfg, unsigned devaddr);
48 
50  void configure(
51  unsigned clkref_hz, // ConfigBus clock frequency
52  unsigned baud_hz, // SPI baud-rate
53  unsigned mode = 3); // SPI mode (0/1/2/3)
54 
56  bool busy() override;
57 
59  bool exchange(
60  u8 devidx, const u8* wrdata, u8 rwbytes,
61  satcat5::cfg::SpiEventListener* callback = 0) override;
62 
65  bool query(
66  u8 devidx, const u8* wrdata, u8 wrbytes, u8 rdbytes,
67  satcat5::cfg::SpiEventListener* callback = 0) override;
68 
69  protected:
70  // Event handlers:
71  void read_done(unsigned cidx);
72 
73  // Metadata for queued commands.
74  satcat5::cfg::SpiEventListener* m_callback[SATCAT5_SPI_MAXCMD];
75 
76  // Working buffer for transmit and receive data.
77  u8 m_txbuff[SATCAT5_SPI_TXBUFF];
78  u8 m_rxbuff[SATCAT5_SPI_RXBUFF];
79  };
80  }
81 }
Generic ConfigBus API.
Definition: cfgbus_core.h:124
Partial driver for the multipurpose serial peripheral.
SPI Event Handler callback for use with SpiGeneric.
Definition: cfg_spi.h:15
Polymorphic API for a generic SPI interface.
Definition: cfg_spi.h:26
Interface driver for the cfgbus_spi_controller block.
Definition: cfgbus_spi.h:44
void configure(unsigned clkref_hz, unsigned baud_hz, unsigned mode=3)
Configure or reconfigure the SPI controller.
Definition: cfgbus_spi.cc:33
void read_done(unsigned cidx)
Callback when each transaction is finished.
Definition: cfgbus_spi.cc:93
bool busy() override
Is the SPI controller currently busy?
Definition: cfgbus_spi.cc:42
bool exchange(u8 devidx, const u8 *wrdata, u8 rwbytes, satcat5::cfg::SpiEventListener *callback=0) override
Queue a bus transaction that reads and writes concurrently.
Definition: cfgbus_spi.cc:47
bool query(u8 devidx, const u8 *wrdata, u8 wrbytes, u8 rdbytes, satcat5::cfg::SpiEventListener *callback=0) override
Queue a bus transation that reads, then writes.
Definition: cfgbus_spi.cc:69
Spi(ConfigBus *cfg, unsigned devaddr)
Link driver to a specific ConfigBus address.
Definition: cfgbus_spi.cc:25