semf
semf::I2cSlaveHardware Class Referenceabstract

This class can be used for using the I2C bus as a slave. Call startListening() for receiving I/O-requests from the master. You can handle them by connecting the signals readExpected and writeExpected and performing the I/O-calls accordingly. More...

#include <i2cslavehardware.h>

Inheritance diagram for semf::I2cSlaveHardware:
Inheritance graph
Collaboration diagram for semf::I2cSlaveHardware:
Collaboration graph

Public Types

enum class  ErrorCode : uint8_t {
  Write_IsBusy = 0 , Write_DataIsNullptr , Write_DataSizeIsZero , Read_IsBusy ,
  Read_BufferIsNullptr , Read_BufferSizeIsZero , SetAddress_IsBusy , SetFrequency_IsBusy ,
  StartListening_IsBusy , StopListening_IsBusy
}
 Error codes for this class. Error ID identify a unique error() / onError call (excluding transferring). More...
 
- Public Types inherited from semf::CommunicationHardware
enum class  Type { SynchronousMaster = 1 , SynchronousSlave , Asynchronous }
 
enum class  Frame : uint8_t { First = 0x01 , Next = 0x08 , Last = 0x10 , FirstAndLast = 0x11 }
 

Public Member Functions

virtual ~I2cSlaveHardware ()=default
 
void write (const uint8_t data[], size_t dataSize) override
 For writing data, dataWritten signal will be emitted after successful write. More...
 
void read (uint8_t buffer[], size_t bufferSize) override
 For reading data, dataAvailable signal will be emitted after successful read. More...
 
bool isBusyReading () const override
 Communication hardware is busy reading at the moment. More...
 
bool isBusyWriting () const override
 Communication hardware is busy writing at the moment. More...
 
void setFrame (CommunicationHardware::Frame frame) override
 Sets the selected usage of start and stop condition. More...
 
uint8_t address () const override
 Returns the I2C slave device address. More...
 
void setAddress (uint8_t address) override
 Sets the address of the slave. More...
 
void setFrequency (uint32_t hz) override
 Sets the speed (I2C baud rate). More...
 
void startListening ()
 Starts to react on getting addressed. More...
 
void stopListening ()
 Quits reacting on getting addressed. More...
 
bool isListening () const
 Returns the listening flag. More...
 
- Public Member Functions inherited from semf::I2c
virtual ~I2c ()=default
 
virtual void setFrequency (uint32_t hz)=0
 Sets the speed (I2C baud rate). More...
 
virtual uint8_t address () const =0
 Returns the I2C slave device address. More...
 
virtual void setAddress (uint8_t address)=0
 
- Public Member Functions inherited from semf::CommunicationHardwareSynchronousSlave
virtual ~CommunicationHardwareSynchronousSlave ()=default
 
Type type () const override
 Returns the hardware type. More...
 
- Public Member Functions inherited from semf::CommunicationHardware
virtual ~CommunicationHardware ()=default
 
virtual void init ()=0
 
virtual void deinit ()=0
 
virtual Type type () const =0
 Returns the hardware type. More...
 
virtual void setFrame (Frame frame)=0
 Sets the selected usage of start and stop condition. More...
 
- Public Member Functions inherited from semf::app::Communication
virtual ~Communication ()=default
 
virtual void write (const uint8_t data[], size_t dataSize)=0
 For writing data, dataWritten signal will be emitted after successful write. More...
 
virtual void read (uint8_t buffer[], size_t bufferSize)=0
 For reading data, dataAvailable signal will be emitted after successful read. More...
 
virtual void stopRead ()=0
 
virtual void stopWrite ()=0
 
virtual bool isBusyReading () const =0
 Communication hardware is busy reading at the moment. More...
 
virtual bool isBusyWriting () const =0
 Communication hardware is busy writing at the moment. More...
 

Public Attributes

Signal readExpected
 
Signal writeExpected
 
- Public Attributes inherited from semf::app::Communication
Signal dataWritten
 
Signal dataAvailable
 
Signal writeStopped
 
Signal readStopped
 
Signal< Errorerror
 

Protected Member Functions

void setBusy (bool isBusy)
 Sets the busy flag. More...
 
CommunicationHardware::Frame frame () const
 Returns the actual frame mode setting. More...
 
virtual void writeHardware (const uint8_t data[], size_t dataSize)=0
 Hardware will write data. After finished write operation, onDataWritten() function will be called. More...
 
virtual void readHardware (uint8_t buffer[], size_t bufferSize)=0
 Hardware will read data. After finished read operation, onDataAvailable() function will be called. More...
 
virtual void startListeningHardware ()=0
 
virtual void stopListeningHardware ()=0
 
virtual void setAddressHardware (uint8_t address)=0
 Sets the address of the i2c hardware. More...
 
void setListening (bool listening)
 Sets the listening flag. More...
 
virtual void setFrequencyHardware (uint32_t hz)=0
 Configures the frequency of the bus. More...
 
void onDataWritten ()
 Is called after data are written by the hardware. Will emit dataWritten signal. More...
 
void onDataAvailable ()
 Is called after data is available in the hardware. Will emit dataAvailable signal. More...
 
void onError (Error thrown)
 Is called if an error occurred by hardware read or write access. Will emit error signal. More...
 

Detailed Description

This class can be used for using the I2C bus as a slave. Call startListening() for receiving I/O-requests from the master. You can handle them by connecting the signals readExpected and writeExpected and performing the I/O-calls accordingly.

Definition at line 22 of file i2cslavehardware.h.

Member Enumeration Documentation

◆ ErrorCode

enum class semf::I2cSlaveHardware::ErrorCode : uint8_t
strong

Error codes for this class. Error ID identify a unique error() / onError call (excluding transferring).

Enumerator
Write_IsBusy 
Write_DataIsNullptr 
Write_DataSizeIsZero 
Read_IsBusy 
Read_BufferIsNullptr 
Read_BufferSizeIsZero 
SetAddress_IsBusy 
SetFrequency_IsBusy 
StartListening_IsBusy 
StopListening_IsBusy 

Definition at line 28 of file i2cslavehardware.h.

Constructor & Destructor Documentation

◆ ~I2cSlaveHardware()

virtual semf::I2cSlaveHardware::~I2cSlaveHardware ( )
virtualdefault

Member Function Documentation

◆ address()

uint8_t semf::I2cSlaveHardware::address ( ) const
overridevirtual

Returns the I2C slave device address.

Returns
Device address.

Implements semf::I2c.

Definition at line 106 of file i2cslavehardware.cpp.

Here is the caller graph for this function:

◆ frame()

CommunicationHardware::Frame semf::I2cSlaveHardware::frame ( ) const
protected

Returns the actual frame mode setting.

Returns
Frame mode for setting start and stop condition.

Definition at line 170 of file i2cslavehardware.cpp.

Here is the caller graph for this function:

◆ isBusyReading()

bool semf::I2cSlaveHardware::isBusyReading ( ) const
overridevirtual

Communication hardware is busy reading at the moment.

Returns
State of the communication hardware.

Implements semf::app::Communication.

Definition at line 75 of file i2cslavehardware.cpp.

Here is the caller graph for this function:

◆ isBusyWriting()

bool semf::I2cSlaveHardware::isBusyWriting ( ) const
overridevirtual

Communication hardware is busy writing at the moment.

Returns
State of the communication hardware.

Implements semf::app::Communication.

Definition at line 80 of file i2cslavehardware.cpp.

Here is the caller graph for this function:

◆ isListening()

bool semf::I2cSlaveHardware::isListening ( ) const

Returns the listening flag.

Returns
Listening flag.

Definition at line 160 of file i2cslavehardware.cpp.

Here is the caller graph for this function:

◆ onDataAvailable()

void semf::I2cSlaveHardware::onDataAvailable ( )
protected

Is called after data is available in the hardware. Will emit dataAvailable signal.

Definition at line 187 of file i2cslavehardware.cpp.

◆ onDataWritten()

void semf::I2cSlaveHardware::onDataWritten ( )
protected

Is called after data are written by the hardware. Will emit dataWritten signal.

Definition at line 180 of file i2cslavehardware.cpp.

◆ onError()

void semf::I2cSlaveHardware::onError ( Error  thrown)
protected

Is called if an error occurred by hardware read or write access. Will emit error signal.

Parameters
thrownA thrown error object.

Definition at line 194 of file i2cslavehardware.cpp.

Here is the caller graph for this function:

◆ read()

void semf::I2cSlaveHardware::read ( uint8_t  buffer[],
size_t  bufferSize 
)
overridevirtual

For reading data, dataAvailable signal will be emitted after successful read.

Parameters
bufferBuffer to store received bytes in.
bufferSizeSize of receive buffer.
Exceptions
Read_IsBusyIf this is busy.
Read_BufferIsNullptrIf buffer is nullptr.
Read_BufferSizeIsZeroIf bufferSize is zero.
Attention
You should call startListening before calling this function. If write gets called from idle-state the behavior is unspecified!

Implements semf::app::Communication.

Definition at line 45 of file i2cslavehardware.cpp.

Here is the call graph for this function:

◆ readHardware()

virtual void semf::I2cSlaveHardware::readHardware ( uint8_t  buffer[],
size_t  bufferSize 
)
protectedpure virtual

Hardware will read data. After finished read operation, onDataAvailable() function will be called.

Parameters
bufferBuffer to store received data in.
bufferSizeSize of data.

Implemented in semf::Stm32I2cSlave.

Here is the caller graph for this function:

◆ setAddress()

void semf::I2cSlaveHardware::setAddress ( uint8_t  address)
overridevirtual

Sets the address of the slave.

Parameters
addressnew address of the slave.
Exceptions
SetAddress_IsBusyIf this is busy.

Implements semf::I2c.

Definition at line 111 of file i2cslavehardware.cpp.

Here is the call graph for this function:

◆ setAddressHardware()

virtual void semf::I2cSlaveHardware::setAddressHardware ( uint8_t  address)
protectedpure virtual

Sets the address of the i2c hardware.

Parameters
addressAddress of the slave.

Implemented in semf::Stm32I2cSlave.

Here is the caller graph for this function:

◆ setBusy()

void semf::I2cSlaveHardware::setBusy ( bool  isBusy)
protected

Sets the busy flag.

Note
In the functions write(), read() busy is set to true. In onDataAvailable(), onDataWritten() and onError() is busy reseted to false.
Parameters
isBusytrue for busy, false for idle.

Definition at line 165 of file i2cslavehardware.cpp.

◆ setFrame()

void semf::I2cSlaveHardware::setFrame ( CommunicationHardware::Frame  frame)
overridevirtual

Sets the selected usage of start and stop condition.

Parameters
frameframe mode

Implements semf::CommunicationHardware.

Definition at line 85 of file i2cslavehardware.cpp.

Here is the call graph for this function:

◆ setFrequency()

void semf::I2cSlaveHardware::setFrequency ( uint32_t  hz)
overridevirtual

Sets the speed (I2C baud rate).

Parameters
hzFrequency in hz.
Exceptions
SetFrequency_IsBusyIf this is busy.

Implements semf::I2c.

Reimplemented in semf::Stm32I2cSlave.

Definition at line 124 of file i2cslavehardware.cpp.

Here is the call graph for this function:

◆ setFrequencyHardware()

virtual void semf::I2cSlaveHardware::setFrequencyHardware ( uint32_t  hz)
protectedpure virtual

Configures the frequency of the bus.

Parameters
hzFrequency in hertz.
Here is the caller graph for this function:

◆ setListening()

void semf::I2cSlaveHardware::setListening ( bool  listening)
protected

Sets the listening flag.

Parameters
listeningThe new listening flag.

Definition at line 175 of file i2cslavehardware.cpp.

Here is the caller graph for this function:

◆ startListening()

void semf::I2cSlaveHardware::startListening ( )

Starts to react on getting addressed.

Exceptions
StartListening_IsBusyIf this is busy.

Definition at line 136 of file i2cslavehardware.cpp.

Here is the call graph for this function:

◆ startListeningHardware()

virtual void semf::I2cSlaveHardware::startListeningHardware ( )
protectedpure virtual

Hardware will react on getting addressed.

Implemented in semf::Stm32I2cSlave.

Here is the caller graph for this function:

◆ stopListening()

void semf::I2cSlaveHardware::stopListening ( )

Quits reacting on getting addressed.

Exceptions
StopListening_IsBusyIf this is listening.

Definition at line 148 of file i2cslavehardware.cpp.

Here is the call graph for this function:

◆ stopListeningHardware()

virtual void semf::I2cSlaveHardware::stopListeningHardware ( )
protectedpure virtual

Hardware will ignore getting addressed.

Implemented in semf::Stm32I2cSlave.

Here is the caller graph for this function:

◆ write()

void semf::I2cSlaveHardware::write ( const uint8_t  data[],
size_t  dataSize 
)
overridevirtual

For writing data, dataWritten signal will be emitted after successful write.

Parameters
dataArray, containing data to write.
dataSizeSize of write data.
Exceptions
Write_IsBusyIf this is busy.
Write_DataIsNullptrIf data is nullptr.
Write_DataSizeIsZeroIf dataSize is zero.
Attention
You should call startListening before calling this function. If write gets called from idle-state the behavior is unspecified!

Implements semf::app::Communication.

Definition at line 15 of file i2cslavehardware.cpp.

Here is the call graph for this function:

◆ writeHardware()

virtual void semf::I2cSlaveHardware::writeHardware ( const uint8_t  data[],
size_t  dataSize 
)
protectedpure virtual

Hardware will write data. After finished write operation, onDataWritten() function will be called.

Parameters
dataArray of data to write.
dataSizeSize of data.

Implemented in semf::Stm32I2cSlave.

Here is the caller graph for this function:

Member Data Documentation

◆ readExpected

Signal semf::I2cSlaveHardware::readExpected

Gets emitted if the master requests to write.

Definition at line 92 of file i2cslavehardware.h.

◆ writeExpected

Signal semf::I2cSlaveHardware::writeExpected

Gets emitted if the master requests to read.

Definition at line 94 of file i2cslavehardware.h.