I2cSlaveDevice
General
The I2cSlaveDevice class is a simple digital copy of an I2cSlave communicating with the internal I2cMasterHardware communication module.
Multiple slave devices can be handled by one master hardware interface.
All device specific settings can be set in the device class:
- Bus frequency and
- device address.
Definition
In the following example we define a simple I2C client, sending an one byte status and receiving an one byte answer. After the status is sent, the dataWritten signal is fired and after the answer byte is received, the dataAvailable signal is fired.
#include "Components/Communication/CommunicationDevice/i2cslavedevice.h" class ExampleDevice : public I2cSlaveDevice { public: /** Constructor.*/ ExampleDevice(I2cMasterHardware& i2cMaster); /** Destructor.*/ virtual ~ExampleDevice() = default; /** Writes an one byte status to the i2c chip.*/ void writeStatus(uint8_t& status); /** Reads an one byte answer from the i2c chip.*/ void readAnswer(uint8_t& answer); /**Signal is fired after status byte is written.*/ Signal<> statusWritten; /**Signal is fired after answer is read complete.*/ Signal<> answerAvailable; /**Signal is fired after an bus error occured.*/ Signal<> error; private: /** I2c handling class.*/ semf::I2cSlaveDevice m_i2cDevice; /** Buffering data.*/ uint8_t* m_data; } ExampleDevice::ExampleDevice(I2cMasterHardware& i2cMaster) :I2cSlaveDevice(0x50 /* slave address*/, i2cMaster) { // bus baud can be set for every slave specific m_i2cDevice.setSpeed(400000); } ExampleDevice::writeStatus(uint8_t& status) { m_i2cDevice.error.clear(); m_i2cDevice.error.connect(&error, &Signal<>::emitSignal); m_i2cDevice.dataWritten.clear(); m_i2cDevice.dataWritten.connect(&dataWritten, &Signal<>::emitSignal); m_data = &status; m_i2cDevice.write(m_data, sizeof(data)); } ExampleDevice::readAnswer(uint8_t& answer) { m_i2cDevice.error.clear(); m_i2cDevice.error.connect(&error, &Signal<>::emitSignal); m_i2cDevice.dataAvailable.clear(); m_i2cDevice.dataAvailable.connect(&dataAvailable, &Signal<>::emitSignal); m_data = &answer; m_i2cDevice.read(m_data, sizeof(data)); }
Initialization and Usage
#include "HardwareAbstraction/Stm32/stm32f7i2cmasterhardware.h" #include "exampledevice.h" #include "main.h" extern I2C_HandleTypeDef hi2c1; semf::Stm32F7I2cMaster i2cMaster(hi2c1); ExmapleDevice device(i2cMaster); // Write status void onDeviceStatusWritten() { // do something } device.dataWritten.connect(&onDeviceStatusWritten); uint8_t status = 0x24; device.writeStatus(status); // Read answer void onDeviceAnswerAvailable() { // do something } device.dataAvailable.connect(&onDeviceAnswerAvailable); uint8_t answer; device.readAnswer(answer);