This module implements an I2C multi-master or slave bus.

The following is an example of how to write the I2C in an OS environment:

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sos/dev/i2c.h>

int fd;
i2c_attr_t attr;
u8 data[8];

fd = open("/dev/i2c0", O_RDWR);
if ( fd < 0 ){
  printf("Error opening peripheral (%d)\n", errno);
} else {
  attr.o_flags = I2C_FLAG_SET_MASTER;
  attr.freq = 4000000; //Use a 400KHz I2C clock frequency
  if( ioctl(fd, I_I2C_SETATTR, &attr) < 0 ){
    printf("Failed to set peripheral configuration (%d)\n", errno);
    return -1;

  //Now use ioctl to setup a transfer operation
  attr.slave_addr[0].addr8[0] = 0x3F; //This is the right-justified 7-bit or 10-bit slave address
  attr.o_flags = I2C_FLAG_PREPARE_PTR_DATA; //This is a "normal", see i2c_trasfer_mode_t for details
  ioctl(fd, I_I2C_SETATTR, &attr);

  //now read the I2C:  This will 1) Start 2) write the 8-bit ptr value 3) Restart 4) Read 8 bytes and 5) Stop
  lseek(fd, 0, SEEK_SET); //this is the ptr value
    if ( read(fd, data, 8) < 0 ){
    printf("Error Reading adc0 (%d)\n", errno);
    return -1;


define I2C_VERSION





define I_I2C_GETINFO

define I_I2C_SETATTR


define I_I2C_TOTAL

enum i2c_error
  • I2C_ERROR_NONE No errors
  • I2C_ERROR_START Error while starting
  • I2C_ERROR_WRITE Error while writing
  • I2C_ERROR_ACK Ack Error (most common error for a mis-wired hardware)
  • I2C_ERROR_STOP Error while stopping
  • I2C_ERROR_MASTER_ACK The master could not create an ACK
  • I2C_ERROR_BUS_BUSY The Bus is busy (happens with multi-masters on bus)
  • I2C_ERROR_ARBITRATION_LOST Arbitration lost on multi-master bus
  • I2C_ERROR_TIMEOUT Timeout error
  • I2C_ERROR_OVERFLOW Overflow error

This enumeration lists the errors which may occur when running an I2C operation.

enum i2c_flag_t
  • I2C_FLAG_NONE No Flags
  • I2C_FLAG_SET_MASTER Operate as a master I2C bus
  • I2C_FLAG_SET_SLAVE Operate as a slave (ignored if master is set)
  • I2C_FLAG_IS_SLAVE_ACK_GENERAL_CALL If slave operation, ack general call
  • I2C_FLAG_IS_PULLUP Enable internal pullups if available (ignore otherwise)
  • I2C_FLAG_PREPARE_PTR_DATA This prepares the driver to write the ptr then read/write data
  • I2C_FLAG_IS_PTR_16 This tells the driver to write a 16-bit ptr then read/write data (use with I2C_FLAG_PREPARE_PTR_DATA or I2C_FLAG_PREPARE_PTR)
  • I2C_FLAG_PREPARE_PTR This will write the ptr value only without writing or reading any data.
  • I2C_FLAG_PREPARE_DATA This will read/write data without first writing the pointer information
  • I2C_FLAG_IS_SLAVE_ADDR0 If hardware supports multiple slave addrs, use the first slot (default)
  • I2C_FLAG_IS_SLAVE_ADDR1 If hardware supports multiple slave addrs, use the second slot
  • I2C_FLAG_IS_SLAVE_ADDR2 If hardware supports multiple slave addrs, use the third slot
  • I2C_FLAG_IS_SLAVE_ADDR3 If hardware supports multiple slave addrs, use the fourth slot
  • I2C_FLAG_IS_SLAVE_PTR_8 Use a 8-bit address pointer when accessing data (default)
  • I2C_FLAG_IS_SLAVE_PTR_16 Use a 16-bit address pointer when accessing data (set automatically is size > 255)
  • I2C_FLAG_RESET Reset the state of the I2C
  • I2C_FLAG_IS_NO_STOP Don’t issue a stop condition when complete (use with I2C_FLAG_PREPARE_DATA)

I2C flags used with i2c_attr_t.o_flags and I_I2C_SETATTR.


u32 o_flags

Bitmask of supported flags

u32 o_events

Bitmask of supported events

u32 freq

Maximum supported bitrate

u32 err

The error of the most recent transaction

u32 resd



mcu_pin_t sda

Data pin

mcu_pin_t scl

Clock pin


u32 o_flags

Attribute flags

i2c_pin_assignment_t pin_assignment

The pin assignment

u32 freq

The bit frequency

i2c_slave_addr_t slave_addr

Slave address

u32 size

Memory size when setting up slave

void * data

Memory pointer to slave data

u32 resd


Thanks for Coming!

Subscribe to news and updates