BeRTOS
Defines | Functions
I2C driver API
I2C driver

Defines

#define i2c_init(args...)   PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
 Initialize I2C module.
#define i2c_start_w(args...)   PP_CAT(i2c_start_w ## _, COUNT_PARMS(args)) (args)
 Start a write session.
#define i2c_start_r(args...)   PP_CAT(i2c_start_r ## _, COUNT_PARMS(args)) (args)
 Start a read session.

Functions

void i2c_hw_init (I2c *i2c, int dev, uint32_t clock)
 Initialize I2C module.
void i2c_hw_bitbangInit (I2c *i2c, int dev)
 Initialize i2c module.

I2C error flags

#define I2C_OK   0
 I2C no errors flag.
#define I2C_DATA_NACK   BV(4)
 I2C generic error.
#define I2C_ERR   BV(3)
 I2C generic error.
#define I2C_ARB_LOST   BV(2)
 I2C arbitration lost error.
#define I2C_START_TIMEOUT   BV(0)
 I2C timeout error on start.
#define I2C_NO_ACK   BV(1)
 I2C no ack for sla start.

I2C command flags

#define I2C_NOSTOP   0
 Do not program the stop for current transition.
#define I2C_STOP   BV(0)
 Program the stop for current transition.

I2C interface functions

void i2c_start_r_4 (I2c *i2c, uint16_t slave_addr, size_t size, int flags)
 Start a read session.
void i2c_start_w_4 (I2c *i2c, uint16_t slave_addr, size_t size, int flags)
 Start a write session.
uint8_t i2c_getc (I2c *i2c)
 Read a byte from I2C bus.
void i2c_putc (I2c *i2c, uint8_t data)
 Write the byte data into I2C port i2c.
void i2c_write (I2c *i2c, const void *_buf, size_t count)
 Write count bytes to port i2c, reading from _buf.
void i2c_read (I2c *i2c, void *_buf, size_t count)
 Read count bytes into buffer _buf from device i2c.
int i2c_error (I2c *i2c)
 Return the error condition of the bus and clear errors.
#define i2c_init_3(i2c, dev, clock)
 Initialize I2C context structure.

Define Documentation

#define i2c_init (   args...)    PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)

Initialize I2C module.

To initialize the module you can write this code:

 I2c ctx;
 i2c_init(&ctx, 0, CONFIG_I2C_FREQ);

This macro expands in two versions, depending on the number of parameters, to maintain compatibility with old API:

Do NOT use the above functions directly, use i2c_init().

Note:
Use the version with 3 parameters, the other one is only for legacy code.

Definition at line 140 of file i2c.h.

#define i2c_init_3 (   i2c,
  dev,
  clock 
)
Value:
((((dev) >= I2C_BITBANG0) | ((dev) == I2C_BITBANG_OLD)) ? \
                                        i2c_hw_bitbangInit((i2c), (dev)) : i2c_hw_init((i2c), (dev), (clock)))

Initialize I2C context structure.

Parameters:
i2cI2C context structure
devNumber of device to be initialized. You can use I2C_BITBANG0 and similar if you want to activate the bitbang driver.
clockPeripheral clock

Definition at line 438 of file i2c.h.

#define i2c_start_r (   args...)    PP_CAT(i2c_start_r ## _, COUNT_PARMS(args)) (args)

Start a read session.

To start a read session, use the following code:

 i2c_start_r(i2c, dev, bytes, I2C_STOP);

This macro expands in two versions, depending on the number of parameters:

Do NOT use the above functions directly, use i2c_start_r().

Note:
Use the version with 4 parameters, the others are only for legacy code

Definition at line 176 of file i2c.h.

#define i2c_start_w (   args...)    PP_CAT(i2c_start_w ## _, COUNT_PARMS(args)) (args)

Start a write session.

To start a write session, use the following code:

 i2c_start_w(i2c, dev, bytes, I2C_STOP);

This macro expands in two versions, depending on the number of parameters:

Do NOT use the above functions directly, use i2c_start_w().

Note:
Use the version with 4 parameters, the others are only for legacy code

Definition at line 158 of file i2c.h.


Function Documentation

uint8_t i2c_getc ( I2c *  i2c) [inline]

Read a byte from I2C bus.

Parameters:
i2cI2C context
Returns:
Byte read

Definition at line 333 of file i2c.h.

void i2c_putc ( I2c *  i2c,
uint8_t  data 
) [inline]

Write the byte data into I2C port i2c.

Parameters:
i2cI2C context
dataByte to be written

Definition at line 358 of file i2c.h.

void i2c_read ( I2c *  i2c,
void *  _buf,
size_t  count 
) [inline]

Read count bytes into buffer _buf from device i2c.

Parameters:
i2cContext structure
_bufBuffer to fill
countNumber of bytes to read

Definition at line 403 of file i2c.h.

void i2c_start_r_4 ( I2c *  i2c,
uint16_t  slave_addr,
size_t  size,
int  flags 
) [inline]

Start a read session.

Parameters:
i2cI2C context
slave_addrAddress of the slave device
sizeNumber of bytes to be read from device
flagsSession flags (I2C command flags)

Definition at line 307 of file i2c.h.

void i2c_start_w_4 ( I2c *  i2c,
uint16_t  slave_addr,
size_t  size,
int  flags 
) [inline]

Start a write session.

Parameters:
i2cI2C context
slave_addrAddress of the slave device
sizeSize to be transferred
flagsSession flags

Definition at line 321 of file i2c.h.

void i2c_write ( I2c *  i2c,
const void *  _buf,
size_t  count 
) [inline]

Write count bytes to port i2c, reading from _buf.

Parameters:
i2cI2C context
_bufUser buffer to read from
countNumber of bytes to write

Definition at line 381 of file i2c.h.