Class Host

Inheritance Relationships

Base Types

Derived Types

Class Documentation

class Host : public TWI::EndPoint, public CycleTimer

Base abstract definition for a TWI host.

See also

EndPoint, Client This class implements the basic state machine to interface a TWI bus as a host. It is design to be controlled by a upper layer object (a controller). The interface notifies the controller of bus events (start, address, etc) via the signals and the controller shall use the API of this class to react accordingly.

Subclassed by ArchAVR_TWI::_Host, ArchXT_TWI::_Host

Public Types

enum State

Values:

enumerator State_Disabled

Host disabled.

enumerator State_Idle

Host idle.

enumerator State_Start

Sending a START condition.

enumerator State_AddressTx

Sending an address/RW byte.

enumerator State_AddressAck

Waiting for a ACK/NACK bit after an address/RW byte.

enumerator State_DataTx

Sending a data byte.

enumerator State_DataTxAck

Waiting for a ACK/NACK bit after sending a data byte.

enumerator State_DataRx

Receiving a data byte.

enumerator State_DataRxAck

Sending a ACK/NACK bit after receiving a data byte.

enumerator State_Stop

Sending a STOP condition.

enumerator State_BusBusy

START condition by another host detected on the bus.

enumerator State_ArbLost

Arbitration lost.

enumerator State_Count

Total number of states.

Public Functions

Host()

Construct a host interface

void init(CycleManager &manager)

Initialisation of the interface, must be called before any operation.

inline State state() const

Getter for the state of the host.

void set_enabled(bool enabled)

Enable/disable the interface.

inline bool enabled() const

Returns true if the host is enabled, false if disabled.

void reset()

Reset the interface to the Idle state. No-op if the interface is disabled.

void set_bit_delay(cycle_count_t delay)

Set the duration of one bit.

Parameters:

delay – bit duration in simulation cycles

bool bus_busy() const

Returns whether the bus is currently busy.

bool active() const

Returns whether the host is currently active, i.e. participating in bus traffic. For a host, it means it’s currently owning the bus.

inline bool clock_hold() const

Returns whether the host is currently holding the bus clock.

bool clock_stretched() const

Returns whether the clock line is currently stretched by an end point other than this host.

inline unsigned char rw() const

Returns the direction of the current request, depending on the latest RW bit sent.

Returns:

1 for a Read Request, 0 for a Write Request

inline bool ack() const

Returns the latest state of the ACK bit, either sent by this host, or received by a client, after an address or a data byte.

Returns:

true for ACK, false for NACK

bool start_transfer()

Start a transfer on the bus. A Start or Repeated Start condition will be transmitted.

Returns:

true if the call was ‘legal’, false otherwise

bool set_address(uint8_t addr_rw)

Send an address byte on the bus.

Returns:

true if the call was ‘legal’, false otherwise

bool start_data_tx(uint8_t data)

Start transmitting a data byte, for a Write Request.

Parameters:

data – byte to be transmitted

Returns:

true if the call was ‘legal’, false otherwise

bool start_data_rx()

Start receiving a data byte, for a Read Request.

Returns:

true if the call was ‘legal’, false otherwise

bool set_ack(bool ack)

Set the ack reply, after a data byte has been received.

Parameters:

ack – true for ACK, false for NACK

Returns:

true if the call was ‘legal’ i.e. the interface was waiting for a ACK, false otherwise

bool stop_transfer()

End a transfer, a Stop condition will be transmitted.

Returns:

true if the call was ‘legal’, false otherwise

inline Signal &signal()

Getter for the host signal.

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.

Protected Functions

virtual void clock_level_changed(bool level) override
virtual void data_level_changed(bool level) override