View on Github

I2C

I2C driver support for hardware abstraction layer

Minimalist code

uint16_t value, i2cPeriphAddr, i2cRegAddr;
rt_dev_t i2c_bus;

// init
i2c_bus = i2c_getFreeDevice();
i2c_setBaudSpeed(i2c_bus, I2C_BAUD_400K);
i2c_setAddressWidth(i2c_bus, 7);
i2c_enable(i2c_bus);

// send and receive data
i2cPeriphAddr = 0x2A;
i2cRegAddr = 0x12;
value = i2c_readreg(i2c_bus, i2cPeriphAddr, i2cRegAddr, I2C_REG8 | I2C_REGADDR8);

value = 10;
i2c_writereg(i2c_bus, i2cPeriphAddr, i2cRegAddr, value, I2C_REG8 | I2C_REGADDR8);

API

Device assignation

i2c_getFreeDevice

rt_dev_t i2c_getFreeDevice();

Gives a free i2c bus device number

i2c_open

int i2c_open(rt_dev_t device);

Open an i2c bus

i2c_close

int i2c_close(rt_dev_t device);

Close an i2c bus

Device enable/disable

i2c_enable

int i2c_enable(rt_dev_t device);

Enable the specified i2c bus device

i2c_disable

int i2c_disable(rt_dev_t device);

Disable the specified i2c bus device

Device settings

i2c_setBaudSpeed

int i2c_setBaudSpeed(rt_dev_t device, uint32_t baudSpeed);

Sets the speed of receive and transmit

i2c_baudSpeed

uint32_t i2c_baudSpeed(rt_dev_t device);

Gets the true baud speed

i2c_effectiveBaudSpeed

uint32_t i2c_effectiveBaudSpeed(rt_dev_t device);

Gets the effective baud speed

i2c_setAddressWidth

int i2c_setAddressWidth(rt_dev_t device, uint8_t addressWidth);

Sets the address width of slaves

i2c_addressWidth

uint8_t i2c_addressWidth(rt_dev_t device);

Gets the address width of slaves

Device low level interface

i2c_start

int i2c_start(rt_dev_t device);

Sends a start condition

i2c_restart

int i2c_restart(rt_dev_t device);

Sends a restart condition

i2c_stop

int i2c_stop(rt_dev_t device);

Sends a stop condition

i2c_idle

int i2c_idle(rt_dev_t device);

Sends a idle condition

i2c_ack

int i2c_ack(rt_dev_t device);

Sends a ack condition

i2c_nack

int i2c_nack(rt_dev_t device);

Sends a nack condition

i2c_putc

int i2c_putc(rt_dev_t device, const char data);

Puts a byte

i2c_getc

uint8_t i2c_getc(rt_dev_t device);

Gets a byte

High level functions, registers level

i2c_readreg

uint16_t i2c_readreg(rt_dev_t device, uint16_t address, uint16_t reg, uint8_t flags);

Read a register at address 'reg' in i2c chip with address 'address'

i2c_readregs

ssize_t i2c_readregs(rt_dev_t device, uint16_t address, uint16_t reg, uint8_t regs[], size_t size, uint8_t flags);

Read 'size' registers beginning at address 'reg' in i2c chip with address 'address'

i2c_writereg

int i2c_writereg(rt_dev_t device, uint16_t address, uint16_t reg, uint16_t value, uint8_t flags);

Write 'value' in register at address 'reg' in i2c chip with address 'address'

i2c_writeregs

int i2c_writeregs(rt_dev_t device, uint16_t address, uint16_t reg, uint8_t regs[], size_t size, uint8_t flags);

Write 'regs' in registers that start at address 'reg' in i2c chip

flags parameter should be use with the following flag :

This flag should be used with (|) pipes/logic or. It is not necessary to precise it if you use default value

Development status

Device assignation, configuration, send and read data fully functional

TODO

Supported architectures

Header file : i2c.h

High level implementation file : i2c.c