semf
SoftI2cMaster

General

The semf::SoftI2cMaster class emulates an semf::I2c communication interface with two semf::Gpio and a semf::app::Timer. The timer can be a hardware timer or a semf::SoftwareTimer. Because of the performance a hardware timer is recommended.

A single master bus setting with acknowledge, start, stop and restart conditions is supported.

Take care that the semf::Gpio ’s speed setting is as fast as possible.

The I2c frequency is half of the timer’s frequency.

Initialization

To initialize you only need two Gpios and a Timer object. Both Gpios have to be configured as an output in opendrain mode. For the Timer a hardware Timer is recommended.

#include "main.h"
extern TIM_HandleTypeDef htim1;
semf::Stm32F4Timer timer(htim1);
semf::Stm32F4Gpio gpioScl(GPIOB, GPIO_PIN_6);
semf::Stm32F4Gpio gpioSda(GPIOB, GPIO_PIN_7);
semf::SoftI2cMaster i2c(gpioScl, gpioSda, true, timer);
i2c.init();
This class used two GPIOs (SCL and SDA) and a Timer and implements a software I2C Master interface.
Definition: softi2cmaster.h:30

Usage

uint8_t slaveAddress = 0x50;
// Read from i2c slave.
void onI2cDataAvailable()
{
// do something
}
semf::StaticSlot<> onI2cDataAvailableSlot(onI2cDataAvailable);
i2c.dataAvailable.connect(onI2cDataAvailableSlot);
i2c.setAddress(slaveAddress);
static uint8_t readBuffer[12];
i2c.read(readBuffer, sizeof(readBuffer));
// or
// Write to i2c slave
void onI2cDataWritten()
{
// do something
}
semf::StaticSlot<> onI2cDataWrittenSlot(onI2cDataWritten);
i2c.dataWritten.connect(onI2cDataWrittenSlot);
i2c.setAddress(slaveAddress);
uint8_t writeBuffer[4] = { 1, 2, 3, 4};
i2c.write(writeBuffer, sizeof(writeBuffer));
// For error handling please connect the error signal with a slot
void onI2cError(semf::Error error)
{
// error handling
}
semf::StaticSlot<semf::Error> onI2cErrorSlot(onI2cError);
i2c.error.connect(onI2cErrorSlot);
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
StaticSlot for lightweight signal/slot implementation. This StaticSlot ist for connecting a signal to...
Definition: staticslot.h:27