Software I2C library

The library can be downloaded from Github: https://github.com/felias-fogg/SoftI2CMaster.

Why another I2C library?

The standard I2C library for the Arduino is the Wire library. While this library is sufficient most of the time, there are situations when it cannot be used:

  • the I2C pins A4/A5 are in use already for other purposes,
  • the code shall run on both an ATmega processor with 16 MhZ and an ATtiny processor with 1 MHz,
  • you need to use the repeated start condition, for instance when interfacing to the infrared thermometer MLX90614.

In order to deal with these cases, one could use the I2cMaster library by landis. However, it is very slow. For example, on a 8Mhz processor, the I2C bus frequency is 20kHz. So you cannot use it to interface to SMbus devices on processors with a clock less than 8MHz.

I adapted Peter Fleury's I2C software library that is written in assembler, extremely light weight and very fast. Even on an ATtiny running with 1MHz, one can still operate the bus with 33 kHz, which implies that you can drive slave devices that use the SMBus protocol (which timeout if the the bus frequency is below 10 kHz).

Installation

Just download the Zip-File from github, uncompress, rename the directory to SoftI2CMaster and move it into the libraries folder. In case you have not installed a library before, consult the the respective help page.

Importing the library

In order to use the library, you have to import it using the include statement:
#include <SoftI2C.h>

In the program text before the include statement, some compile-time parameters have to be specified, such as which pins are used for the data (SDA) and clock (SCL) lines. These pins have to be specified in a way so that port manipulation commands can be used. So instead of specifying the number of the digital pin (0-19) the port (PORTB, PORTC, PORTD) and the port pin has to be specified. The mapping is explained here. For example, if you want to use digital pin 2 for SCL and digital pin 14 (= analog pin 0) for SDA, you have to specify port pin 2 of PORTD for SCL and port pin 0 of PORTC for SDA:
#define SCL_PIN 2
#define SCL_PORT PORTD
#define SDA_PIN 0
#define SDA_PORT PORTC
#include <SoftI2C.h>

Configuration

There are a few other constants that you can define in order to control the behavior of the library.

#define I2C_TIMEOUT ...
Since slave devices can stretch the low period of the clock indefinitely, they can lock up the MCU. In order to avoid this, one can define I2C_TIMEOUT. Specify the number of milliseconds after which the I2C functions will time out. Possible values are 0 (no time out) to 10000 (i.e., 10 seconds).

#define I2C_NOINTERRUPT 1
With this definition you disable interrupts while between issuing a start condition and termininating the transfer with a stop condition. Usually, this is not necessary. However, if you have an SMbus device that can timeout, it might be a good idea to use this feature. Note however, that in this case interrupts become unconditionally enabled after calling i2c_stop().

#define I2C_CPUFREQ ...
If you are changing the CPU frequency dynamically using the clock prescale register CLKPR and intend to call the I2C functions with a frequency different from F_CPU, then define this constant with the correct frequency. For instance, if you used a prescale factor of 8, then the following definition would be adequate:
#define I2C_CPUFREQ (F_CPU/8)

#define I2C_FASTMODE 1
The standard I2C bus frequency is 100kHz. Often, however, devices permit for faster transfers up to 400kHz. If you want to allow for the higher frequency, then the above definition should be used.

#define I2C_SLOWMODE 1
In case you want to slow down the transfer to 25kHz, you can use this definition (in this case, do not define I2C_FASTMODE).

I have measured the bus speed with different processor frequencies. The results are displayed in the following table.

 1MHz2MHz4MHz8MHz16MHz20MHz
I2C slow mode kbit/sec252525252525
I2C standard mode kbit/sec4080100100100100
I2C fast mode kbit/sec4080150300400400

Interface

The following functions are provided by the library:

i2c_init()
Initialize the I2C system. Must be called once in setup(). Will return false if SDA or SCL is on a low level, which means that the bus is locked.

i2c_start(addr | R/W-bit)
Initiates a transfer to the slave device with the (8-bit) I2C address addr. Note that this library uses the 8-bit addressing scheme different from the 7-bit scheme in the Wire library. In addition the R/W-bit must be specified as I2C_WRITE (=0) or I2C_READ (=1).

i2c_start_wait(addr | R/W-bit)
Similar to the i2c_start function. However, it tries repeatedly to start the transfer until the device sends an acknowledge.

i2c_rep_start(addr | R/W-bit)
Sends a repeated start condition, i.e., it starts a new transfer without sending first a stop condition.

i2c_stop()
Sends a stop condition and thereby releases the bus.

i2c_write(byte)
Sends a byte to the previously addressed device. Returns true if the device replies with an ACK.

i2c_read(last)
Requests to receive a byte from the slave device. If last is true, then a NAK is sent after receiving the byte finishing the read transfer sequence.

Example

As a small example, let us consider reading the values from the BMA020 acceleration sensor.

// Simple sketch to read out BMA020 using SoftI2C
#define SDA_PORT PORTD
#define SDA_PIN 3
#define SCL_PORT PORTD
#define SCL_PIN 5
#include <SoftI2C.h>


#define BMAADDR 0x70

int xval, yval, zval;


boolean setControlBits(uint8_t cntr)
{
  Serial.println(F("Soft reset"));
  if (!i2c_start(BMAADDR | I2C_WRITE)) {
    return false;
  }
  if (!i2c_write(0x0A)) {
    return false;
  }
  if (!i2c_write(cntr)) {
    return false;
  }
  i2c_stop();
  return true;
}

boolean initBma(void)
{
  if (!setControlBits(B00000010)) return false;;
  delay(100);
  return true;
}

int readOneVal(boolean last)
{
  uint8_t msb, lsb;
  lsb = i2c_read(false);
  msb = i2c_read(last);
  return (int)((msb<<8)|lsb)/64;
}

boolean readBma(void)
{
  xval = 0xFFFF;
  yval = 0xFFFF;
  zval = 0xFFFF;
  if (!i2c_start(BMAADDR | I2C_WRITE)) return false;
  if (!i2c_write(0x02)) return false;
  if (!i2c_rep_start(BMAADDR | I2C_READ)) return false;
  xval = readOneVal(false);
  yval = readOneVal(false);
  zval = readOneVal(true);
  i2c_stop();
  return true;
}



//---------------------------------------------------------
void setup(void) {
  Serial.begin(19200); /
  if (!initBma()) {
    Serial.println(F("INIT ERROR"));
  }

}

void loop(void){
  if (!readBma()) Serial.println(F("READ ERROR"));
  Serial.print(F("X="));
  Serial.print(xval);
  Serial.print(F("  Y="));
  Serial.print(yval);
  Serial.print(F("  Z="));
  Serial.println(zval);
  delay(1000);
}

Share