libi2cd  1.0.3
Linux kernel I2C character device library
Modules | Data Structures | Functions
Main API

Main data structures and functions. More...

Modules

 Register Access
 Register access functions.
 

Data Structures

struct  i2cd
 Handle to an I2C character device. More...
 

Functions

struct i2cdi2cd_open (const char *path)
 Open the I2C character device specified by path. More...
 
struct i2cdi2cd_open_by_name (const char *name)
 Open the I2C character device specified by name. More...
 
struct i2cdi2cd_open_by_number (unsigned int num)
 Open the I2C character device specified by num. More...
 
void i2cd_close (struct i2cd *dev)
 Close an I2C character device handle and free associated memory. More...
 
const char * i2cd_get_path (struct i2cd *dev)
 Get the path used to open the I2C character device handle. More...
 
int i2cd_set_retries (struct i2cd *dev, unsigned long retries)
 Set the number of times a slave address should be polled when not acknowledging. More...
 
int i2cd_set_timeout (struct i2cd *dev, unsigned long timeout)
 Set the timeout in units of 10ms. More...
 
int i2cd_get_functionality (struct i2cd *dev, unsigned long *funcs)
 Get the adapter functionality mask. More...
 
int i2cd_transfer (struct i2cd *dev, struct i2c_msg msgs[], size_t nmsgs)
 Transfer one or more low-level messages terminated with a single STOP condition. More...
 
int i2cd_read (struct i2cd *dev, uint16_t addr, void *buf, size_t len)
 Read bytes from a slave device. More...
 
int i2cd_write (struct i2cd *dev, uint16_t addr, const void *buf, size_t len)
 Write bytes to a slave device. More...
 
int i2cd_write_read (struct i2cd *dev, uint16_t addr, const void *write_buf, size_t write_len, void *read_buf, size_t read_len)
 Write and read bytes from a slave device using a repeated START condition. More...
 

Detailed Description

Main data structures and functions.

#include <i2cd.h>

Function Documentation

◆ i2cd_close()

void i2cd_close ( struct i2cd dev)

Close an I2C character device handle and free associated memory.

Parameters
devPointer to an I2C character device handle.

Once closed, dev is no longer valid for use.

◆ i2cd_get_functionality()

int i2cd_get_functionality ( struct i2cd dev,
unsigned long *  funcs 
)

Get the adapter functionality mask.

Parameters
devPointer to an I2C character device handle.
funcsPointer to a buffer to receive a mask.
Returns
0 on success, or -1 on error with errno set appropriately.

Adapter functionality can be determined by comparing the returned mask to values defined by the I2C_FUNC_* macros in linux/i2c.h.

This function corresponds to the I2C_FUNCS ioctl() request.

◆ i2cd_get_path()

const char* i2cd_get_path ( struct i2cd dev)

Get the path used to open the I2C character device handle.

Parameters
devPointer to an I2C character device handle.
Returns
The path used to open the I2C character device handle.

◆ i2cd_open()

struct i2cd* i2cd_open ( const char *  path)

Open the I2C character device specified by path.

Parameters
pathPath to the I2C character device to open.
Returns
Pointer to an I2C character device handle, or NULL on error with errno set appropriately.

◆ i2cd_open_by_name()

struct i2cd* i2cd_open_by_name ( const char *  name)

Open the I2C character device specified by name.

Parameters
nameName of the I2C character device to open.
Returns
Pointer to an I2C character device handle, or NULL on error with errno set appropriately.

This function is equivalent to calling:

char path[PATH_MAX];
snprintf(path, sizeof(path), "/dev/%s", name);
i2cd_open(path);

◆ i2cd_open_by_number()

struct i2cd* i2cd_open_by_number ( unsigned int  num)

Open the I2C character device specified by num.

Parameters
numNumber of the I2C character device to open.
Returns
Pointer to an I2C character device handle, or NULL on error with errno set appropriately.

This function is equivalent to calling:

char path[PATH_MAX];
snprintf(path, sizeof(path), "/dev/i2c-%u", num);
i2cd_open(path);

◆ i2cd_read()

int i2cd_read ( struct i2cd dev,
uint16_t  addr,
void *  buf,
size_t  len 
)

Read bytes from a slave device.

Parameters
devPointer to an I2C character device handle.
addrI2C slave address.
bufPointer to a buffer to receive bytes.
lenNumber of bytes to read.
Returns
Number of messages transferred on success, or -1 on error with errno set appropriately.

◆ i2cd_set_retries()

int i2cd_set_retries ( struct i2cd dev,
unsigned long  retries 
)

Set the number of times a slave address should be polled when not acknowledging.

Parameters
devPointer to an I2C character device handle.
retriesNumber of times to retry.
Returns
0 on success, or -1 on error with errno set appropriately.

This function corresponds to the I2C_RETRIES ioctl() request.

◆ i2cd_set_timeout()

int i2cd_set_timeout ( struct i2cd dev,
unsigned long  timeout 
)

Set the timeout in units of 10ms.

Parameters
devPointer to an I2C character device handle.
timeoutTimeout in units of 10ms.
Returns
0 on success, or -1 on error with errno set appropriately.

This function corresponds to the I2C_TIMEOUT ioctl() request.

◆ i2cd_transfer()

int i2cd_transfer ( struct i2cd dev,
struct i2c_msg  msgs[],
size_t  nmsgs 
)

Transfer one or more low-level messages terminated with a single STOP condition.

Parameters
devPointer to an I2C character device handle.
msgsArray of messages to transfer.
nmsgsNumber of messages to transfer.
Returns
Number of messages transferred on success, or -1 on error with errno set appropriately.

This function corresponds to the I2C_RDWR ioctl() request.

◆ i2cd_write()

int i2cd_write ( struct i2cd dev,
uint16_t  addr,
const void *  buf,
size_t  len 
)

Write bytes to a slave device.

Parameters
devPointer to an I2C character device handle.
addrI2C slave address.
bufPointer to a buffer to send bytes.
lenNumber of bytes to send.
Returns
Number of messages transferred on success, or -1 on error with errno set appropriately.

◆ i2cd_write_read()

int i2cd_write_read ( struct i2cd dev,
uint16_t  addr,
const void *  write_buf,
size_t  write_len,
void *  read_buf,
size_t  read_len 
)

Write and read bytes from a slave device using a repeated START condition.

Parameters
devPointer to an I2C character device handle.
addrI2C slave address.
write_bufPointer to a buffer to send bytes.
write_lenNumber of bytes to send.
read_bufPointer to a buffer to receive bytes.
read_lenNumber of bytes to receive.
Returns
Number of messages transferred on success, or -1 on error with errno set appropriately.
i2cd_open
struct i2cd * i2cd_open(const char *path)
Open the I2C character device specified by path.