Class SPI
Defined in File sim_spi.h
Inheritance Relationships
Base Types
public SPIClient(Class SPIClient)public CycleTimer(Class CycleTimer)
Class Documentation
-
class SPI : public SPIClient, public CycleTimer
Generic model defining an serial peripheral interface a.k.a. SPI.
The interface can act in either host or client mode.
The class is composed of two FIFOs, one for TX, the other for RX. The transfer of a frame starts immediately after pushing it in the TX FIFO.
Public Types
Public Functions
-
SPI()
-
void init(CycleManager &cycle_manager, Logger &logger)
Initialise the interface.
- Parameters:
cycle_manager – Cycle manager used for time-related operations
logger – Logger used for the interface
-
void reset()
Reset the interface.
-
void set_host_mode(bool mode)
Set the interface mode.
- Parameters:
mode – true=host, false=client
-
inline bool is_host_mode() const
-
void set_frame_delay(cycle_count_t delay)
Set the delay to emit or receive a frame.
- Parameters:
delay – Delay in cycles. The minimum valid value is 1.
-
void add_client(SPIClient &client)
Add a client to the interface.
- Parameters:
client – Client to add
-
void set_selected(bool selected)
Set the interface as selected (client mode only)
-
void set_tx_buffer_limit(size_t limit)
Set the TX buffer limit and trim the buffer if necessary.
- Parameters:
limit – New buffer limit. Zero means unlimited.
-
void push_tx(uint8_t frame)
Push a 8-bits frame to be emitted by the interface. In host mode, if no transfer is already ongoing, one will start immediately. Otherwise the frame is added to the TX FIFO. In client mode, the frame stays in the TX FIFO, waiting for the host to start a transfer.
-
void cancel_tx()
Cancel all pending and current transfers. (host mode only)
-
void set_rx_buffer_limit(size_t limit)
Set the RX buffer limit and trim the buffer if necessary.
- Parameters:
limit – New buffer limit. Zero means unlimited.
-
inline bool tfr_in_progress() const
Getter indicating if a transfer is in progress.
-
inline size_t rx_available() const
Getter for the count of frames in the RX buffer.
-
uint8_t pop_rx()
Pop a frame from the RX buffer, return 0 if there aren’t any.
-
virtual cycle_count_t next(cycle_count_t when) override
Callback from the cycle loop.
Note
there’s no guarantee the method will be called exactly on the required ‘when’ cycle. The only guarantee is “called ‘when’ <= ‘current cycle’”, the implementations must account for this.
Note
The next ‘when’ can be in the ‘past’ (i.e. <= ‘current cycle’). In this case, the timer will be called again within the same cycle with the given next ‘when’. The only constraint is that it must be greater than the previous ‘when’. If it’s negative or zero, the timer is removed from the queue.
- Parameters:
when – current ‘when’ cycle, at which the timer was scheduled
- Returns:
the next ‘when’ the timer requires to be called at.
-
virtual bool selected() const override
Used by the SPI host to interrogate the selection state of the client.
-
virtual uint8_t start_transfer(uint8_t mosi_frame) override
Called by the SPI host to start a transfer of one frame.
- Parameters:
mosi_frame – MOSI frame emitted by the host
- Returns:
the MISO frame simultaneously emitted by the client.
-
virtual void end_transfer(bool ok) override
Called by the host at the end of a transfer.
- Parameters:
ok – indicates the success of the transfer, or false if it was aborted.
-
SPI()