semf
semf::SoftI2cMaster Class Reference

This class used two GPIOs (SCL and SDA) and a Timer and implements a software I2C Master interface. More...

#include <softi2cmaster.h>

Inheritance diagram for semf::SoftI2cMaster:
Inheritance graph
Collaboration diagram for semf::SoftI2cMaster:
Collaboration graph

Public Types

enum class  ErrorCode : uint8_t { StopConditionSetSda_NackError = 0 }
 Error IDs for this class. Error ID identify a unique error() / onError call (excluding transferring). More...
 
- Public Types inherited from semf::I2cMasterHardware
enum class  ErrorCode : uint8_t {
  Write_IsBusy = 0 , Write_DataIsNullptr , Write_DataSizeIsZero , Read_IsBusy ,
  Read_BufferIsNullptr , Read_BufferSizeIsZero , SetAddress_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

 SoftI2cMaster (Gpio &scl, Gpio &sda, app::Timer &timer)
 Constructor. More...
 
 SoftI2cMaster (const SoftI2cMaster &other)=delete
 
virtual ~SoftI2cMaster ()=default
 
void init () override
 
void deinit () override
 
void stopWrite () override
 
void stopRead () override
 
- Public Member Functions inherited from semf::I2cMasterHardware
virtual ~I2cMasterHardware ()=default
 
virtual void checkAddress (uint8_t address)=0
 Checks if the given address is available on the bus. Emits address found if successful (ACK). error signal will be emitted, if no slave answers (NACK). More...
 
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...
 
uint8_t address () const override
 Returns the I2C slave device address. More...
 
void setAddress (uint8_t address) override
 
void setFrame (CommunicationHardware::Frame frame) override
 Sets the selected usage of start and stop condition. 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::CommunicationHardwareSynchronousMaster
virtual ~CommunicationHardwareSynchronousMaster ()=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...
 

Protected Member Functions

void writeHardware (const uint8_t data[], size_t size) override
 Hardware will write data. After finished write operation, onDataWritten() function will be called. More...
 
void readHardware (uint8_t buffer[], size_t bufferSize) override
 Hardware will read data. After finished read operation, onDataAvailable() function will be called. More...
 
- Protected Member Functions inherited from semf::I2cMasterHardware
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 setFrequencyHardware (uint32_t hz)=0
 Configures the frequency of the bus. More...
 
void onDataWritten ()
 
void onDataAvailable ()
 
void onError (Error thrown)
 Is called if an error occurred by hardware read or write access. Will emit error signal. More...
 

Additional Inherited Members

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

Detailed Description

This class used two GPIOs (SCL and SDA) and a Timer and implements a software I2C Master interface.

If possible, use a hardware timer. The I2C frequency is half of the timer frequency.

Attention
The used GPIOs have to be configured in open drain mode and please use the fastest possible GPIO clock setting.

Definition at line 29 of file softi2cmaster.h.

Member Enumeration Documentation

◆ ErrorCode

enum class semf::SoftI2cMaster::ErrorCode : uint8_t
strong

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

Enumerator
StopConditionSetSda_NackError 

Definition at line 35 of file softi2cmaster.h.

Constructor & Destructor Documentation

◆ SoftI2cMaster() [1/2]

semf::SoftI2cMaster::SoftI2cMaster ( Gpio scl,
Gpio sda,
app::Timer timer 
)

Constructor.

Attention
I2C frequency is half of the timer frequency.
Parameters
sclGPIO to use as SCL output.
sdaGPIO to use as SDA output and input.
timerIdeally use a hardware timer to handle the I2C timing.

Definition at line 15 of file softi2cmaster.cpp.

◆ SoftI2cMaster() [2/2]

semf::SoftI2cMaster::SoftI2cMaster ( const SoftI2cMaster other)
explicitdelete

◆ ~SoftI2cMaster()

virtual semf::SoftI2cMaster::~SoftI2cMaster ( )
virtualdefault

Member Function Documentation

◆ deinit()

void semf::SoftI2cMaster::deinit ( )
overridevirtual

Deinitializes the hardware

Implements semf::CommunicationHardware.

Definition at line 41 of file softi2cmaster.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init()

void semf::SoftI2cMaster::init ( )
overridevirtual

Initializes the hardware.

Implements semf::CommunicationHardware.

Definition at line 22 of file softi2cmaster.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readHardware()

void semf::SoftI2cMaster::readHardware ( uint8_t  buffer[],
size_t  bufferSize 
)
overrideprotectedvirtual

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

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

Implements semf::I2cMasterHardware.

Definition at line 86 of file softi2cmaster.cpp.

Here is the call graph for this function:

◆ stopRead()

void semf::SoftI2cMaster::stopRead ( )
overridevirtual

Stops the read process as fast as possible without destroying anything. The readStopped Signal gets emitted if the call was successful.

Implements semf::app::Communication.

Definition at line 56 of file softi2cmaster.cpp.

Here is the call graph for this function:

◆ stopWrite()

void semf::SoftI2cMaster::stopWrite ( )
overridevirtual

Stops the write process as fast as possible without destroying anything. The writeStopped Signal gets emitted if the call was successful.

Implements semf::app::Communication.

Definition at line 49 of file softi2cmaster.cpp.

Here is the call graph for this function:

◆ writeHardware()

void semf::SoftI2cMaster::writeHardware ( const uint8_t  data[],
size_t  dataSize 
)
overrideprotectedvirtual

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

Parameters
dataArray of data to write.
dataSizeSize of data.

Implements semf::I2cMasterHardware.

Definition at line 63 of file softi2cmaster.cpp.

Here is the call graph for this function: