SoftI2cMaster
General
The SoftI2cMaster class emulated an I2cMaster communication interface by two Gpios and an Timer. The Timer can be a hardware Timer or a SoftwareTimer. Because of the performance a hardware Timer is recommended.
Supported is a single master bus setting with acknolage, start, stop and restart conditions.
Take care that the Gpio‘s speed setting is as fast as possible.
The I2c frequency is half of the timer’s frequency.
Initialization
For initializing 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 "HardwareAbstraction/Stm32/stm32f4gpio.h" #include "HardwareAbstraction/Stm32/stm32f4timer.h" #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();
Usage
uint8_t slaveAddress = 0x50; // Read from i2c slave. void onI2cDataAvailable() { // do something } i2c.dataAvailable.connect(&onI2cDataAvailable); i2c.setAddress(slaveAddress); uint8_t readBuffer[12]; i2c.read(readBuffer, sizeof(readBuffer)); // or // Write to i2c slave void onI2cDataWritten() { // do something } i2c.dataWritten.connect(&onI2cDataWritten); 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() { // error handling } i2c.error.connect(&onI2cError);