class::hal::I2C

class hal::I2C
  : public hal::Periph< i2c_info_t, i2c_attr_t, I2CAttributes, 'i'>

This class implements I2C device peripherals.

An I2C ptr/data transfer (I2C::FLAG_PREPARE_PTR_DATA) updates the register pointer and then either reads or writes data. For example:

#include <sapi/hal.hpp>

int main(int argc, char * argv[]){
   u16 data;
   I2C i2c(0); //use I2C port 0
   i2c.init(); //Initialize the I2C using the BSP defaults
   i2c.prepare(0x4C); //slave addr is 0x4C - operation is 8-bit ptr plus data
   i2c.read(5, &data, sizeof(data)); //read 2 bytes from register 5
   i2c.close();
}

The code above will execute 2 I2C transactions. It will write 5 to the I2C slave device register pointer. It will then read 2 bytes from the slave device.

The following example doesn’t write the register pointer. It also shows how to customize the initialization.

int main(int argc, char * argv[]){
u16 data;
   I2C i2c(1);
   I2CPinAssignment pin_assignment;
   pin_assignment->sda = mcu_pin(0,10);
   pin_assignment->scl = mcu_pin(0,11);

   i2c.init(I2C::FLAG_SET_MASTER, 400000, pin_assignment);
   i2c.prepare(0x4C, FLAG_PREPARE_DATA); //prepare for data only transactions
   i2c.write(&data, sizeof(u16)); //write two bytes
   i2c.read(&data, sizeof(u16)); //read two bytes
i2c.close();
}

The I2C can also be configured using POSIX calls.

int main(int argc, char * argv[]){
   int fd;
   fd = open("/dev/i2c0", O_RDWR);
   if( fd < 0 ){
      perror("Failed to open I2C");
   } else {
      i2c_attr_t attr;
      attr.o_flags = I2C_FLAG_SET_MASTER;
      attr.freq = 100000;
      attr.pin_assignment.sda = mcu_pin(0,10);
      attr.pin_assignment.scl = mcu_pin(0,11);
      if( ioctl(fd, I_I2C_SETATTR, &attr) < 0 ){
         perror("Failed to set I2C attr");

         //Or the BSP defaults can be used
         if( ioctl(fd, I_I2C_SETATTR, 0) < 0 ){
            perror("No BSP defaults");
         }
      } else {

         attr.o_flags = I2C_FLAG_PREPARE_PTR_DATA;
         attr.slave_addr[0].addr8[0] = 0x4C;
         ioctl(fd, I_I2C_SETATTR, &attr);
         lseek(fd, 10, SEEK_SET);
         write(fd, &value, sizeof(u16)); //first writes 10 (from lseek) then writes 2 bytes (value)
      }
      close(fd);
   }

return 0;
}

See also: hal::I2CPinAssignment

See also: hal::I2CAttr

Summary

I2C(

port_t port
)

int clear(

const arg::Location & location,
int bit
)

int get_err() const

int get_error() const

int init(

u32 o_flags,
u32 freq,
const i2c_pin_assignment_t * pin_assignment
)

int prepare(

u8 slave_addr,
u32 o_flags
) const

int read(

const arg::Location & location,
u8 & reg
)

int reset() const

int set(

const arg::Location & location,
int bit,
bool high
)

int set_attr(

u32 o_flags,
u32 freq,
const i2c_pin_assignment_t * pin_assignment
)

int write(

const arg::Location & location,
u8 reg
)

enum i2c_error

enum i2c_flags

Members

I2C(

port_t port
)

Constructs an I2C object using the specified port.


int clear(

const arg::Location & location,
int bit
)

Clears the bit in a register on an I2C device.


int get_err() const

int get_error() const

Gets the last error.


int init(

u32 o_flags,
u32 freq,
const i2c_pin_assignment_t * pin_assignment
)


int prepare(

u8 slave_addr,
u32 o_flags
) const

Prepares an I2C transaction.

Parameters

  • slave_addr The slave address for the transaction

  • o_flags The flags for the transaction

Returns

Zero on success

The o_flags can be:

  • FLAG_PREPARE_PTR_DATA (default)
  • FLAG_PREPARE_PTR_16_DATA
  • FLAG_PREPARE_PTR
  • FLAG_PREPARE_DATA

int read(

const arg::Location & location,
u8 & reg
)

Reads the value of a register on an I2C device


int reset() const

Resets the I2C bus state.


int set(

const arg::Location & location,
int bit,
bool high
)

Sets (or clears) a specific bit in a a register on an I2C device.

Parameters

  • loc The register offset value

  • bit The bit to set (or clear)

  • high true to set the bit and false to clear it

Returns

Zero on success


int set_attr(

u32 o_flags,
u32 freq,
const i2c_pin_assignment_t * pin_assignment
)


int write(

const arg::Location & location,
u8 reg
)

Writes the value of a register on an I2C device


enum i2c_error
  • ERROR_NONE No errors
  • ERROR_START Error while starting
  • ERROR_WRITE Error while writing
  • ERROR_ACK Ack Error (most common error for a mis-wired hardware)
  • ERROR_STOP Error while stopping
  • ERROR_MASTER_ACK The master could not create an ACK
  • ERROR_BUS_BUSY The Bus is busy (happens with multi-masters on bus)
  • ERROR_LONG_SLEW
  • ERROR_ARBITRATION_LOST Arbitration lost on multi-master bus

enum i2c_flags
  • SET_MASTER Operate as a master I2C bus
  • SET_SLAVE Operate as a slave (ignored if master is set)
  • IS_SLAVE_ACK_GENERAL_CALL If slave operation, ack general call
  • IS_PULLUP Enable internal pullups if available (ignore otherwise)
  • PREPARE_PTR_DATA This prepares the driver to write the ptr then read/write data
  • IS_PTR_16 This prepares the driver to write a 16-bit ptr then read/write data
  • PREPARE_PTR This will write the ptr value only without writing or reading any data.
  • PREPARE_DATA This will read/write data without first writing the pointer information
  • IS_SLAVE_ADDR0 If hardware supports multiple slave addrs, use the first slot (default)
  • IS_SLAVE_ADDR1 If hardware supports multiple slave addrs, use the second slot
  • IS_SLAVE_ADDR2 If hardware supports multiple slave addrs, use the third slot
  • IS_SLAVE_ADDR3 If hardware supports multiple slave addrs, use the fourth slot
  • IS_SLAVE_PTR_8 Use a 8-bit address pointer when accessing data (default)
  • IS_SLAVE_PTR_16 Use a 16-bit address pointer when accessing data (set automatically is size > 255)
  • RESET Reset the state of the I2C
  • IS_STRETCH_CLOCK
  • IS_NO_STOP Don’t issue a stop condition when complete (use with I2C_FLAG_PREPARE_DATA)

X

Thanks for Coming!

Subscribe to news and updates