m2mb API docs  25.30.003
m2mb API sets documentation
m2mb_i2c.h File Reference

I2C library implementation. More...

Go to the source code of this file.

Data Structures

struct  M2MB_I2C_MSG
 Structure to be used as container for a write or read operation in I2C Combined mode (M2MB_I2C_IOCTL_RDWR call) More...
 
struct  M2MB_I2C_RDWR_IOCTL_DATA
 Structure to be used with the M2MB_I2C_IOCTL_RDWR option in m2mb_i2c_ioctl call. More...
 
struct  M2MB_I2C_CFG_T
 I2C device configuration structure. More...
 

Macros

#define NULL   0
 
#define I2C_M_WR   0x0000
 
#define I2C_M_RD   0x0001
 

Enumerations

enum  M2MB_I2C_IOCTL_REQUEST { M2MB_I2C_IOCTL_SET_CFG, M2MB_I2C_IOCTL_GET_CFG, M2MB_I2C_IOCTL_RDWR }
 enum used to access I2C configuration More...
 

Functions

INT32 m2mb_i2c_open (const CHAR *path, INT32 flags,...)
 open an I2C device More...
 
INT32 m2mb_i2c_close (INT32 fd)
 Close an I2C device. More...
 
INT32 m2mb_i2c_ioctl (INT32 fd, INT32 request,...)
 configure, read from and/or write to an I2C device More...
 
SSIZE_T m2mb_i2c_read (INT32 fd, void *buf, SIZE_T nbyte)
 Read data from a connected I2C device. More...
 
SSIZE_T m2mb_i2c_write (INT32 fd, const void *buf, SIZE_T nbyte)
 Write data to a connected I2C device. More...
 

Detailed Description

I2C library implementation.

m2m/m2m_generic/common/m2mb_inc/m2mb_i2c.h

The following functions are implemented: m2mb_i2c_ioctl m2mb_i2c_open m2mb_i2c_read m2mb_i2c_write m2mb_i2c_close

@notes Dependencies: m2mb_types.h

Author
Morgan Deidda

@changes Enrico Perini

Date
31/08/2017

Macro Definition Documentation

◆ I2C_M_RD

#define I2C_M_RD   0x0001

read data, from slave to master

◆ I2C_M_WR

#define I2C_M_WR   0x0000

write data, from master to slave

Enumeration Type Documentation

◆ M2MB_I2C_IOCTL_REQUEST

enum used to access I2C configuration

Enumerator
M2MB_I2C_IOCTL_SET_CFG 

set i2c device configuration

M2MB_I2C_IOCTL_GET_CFG 

get i2c device configuration

M2MB_I2C_IOCTL_RDWR 

i2c combined format

Function Documentation

◆ m2mb_i2c_close()

INT32 m2mb_i2c_close ( INT32  fd)

Close an I2C device.

This function closes an opened file descriptor.

Parameters
[in]fdFile descriptor returned by m2mb_i2c_open
Returns
0 on SUCCESS, -1 on FAILURE
#define DEVICEADDR 0x0F //e.g. the device address reported by device vendor on datasheet
CHAR path[32];
INT32 fd;
sprintf(path, "/dev/I2C-%d", DEVICEADDR << 1);
fd = m2mb_i2c_open( path, 0 );
if (-1 == fd)
{
LOG("cannot open I2C channel");
return -1;
}

◆ m2mb_i2c_ioctl()

INT32 m2mb_i2c_ioctl ( INT32  fd,
INT32  request,
  ... 
)

configure, read from and/or write to an I2C device

Depending on request parameter, this function can configure an I2C device, or can send and/or receive data through I2C combined format

Parameters
[in]fdfile descriptor returned by m2mb_i2c_open
[in]requestrequired operation (see M2MB_I2C_IOCTL_REQUEST)
[in]cfgpointer to the configuration struct M2MB_I2C_CFG_T casted to void* The parameter depend on request code: if request == M2MB_I2C_IOCTL_SET_CFG: in struct M2MB_I2C_CFG_T sdaPin, sclPin and registerId are mandatory SDA and SCL pins are mapped on GPIOs, the slave device register Id from which to read (or to which to write) is set if request == M2MB_I2C_IOCTL_GET_CFG: a pointer to the structure M2MB_I2C_CFG_T previously set is got if request == M2MB_I2C_IOCTL_RDWR: in struct M2MB_I2C_CFG_T, sdaPin, sclPin, rw_param are mandatory SDA and SCL pins are mapped on GPIOs, rw_param will contain the buffers and parameters required to read and write through I2C combined format
Returns
0 on SUCCESS if request == M2MB_I2C_IOCTL_SET_CFG or request == M2MB_I2C_IOCTL_GET_CFG number of written Bytes on SUCCESS if request == M2MB_I2C_IOCTL_RDWR and the function is used only for writing number of read Bytes on SUCCESS if request == M2MB_I2C_IOCTL_RDWR and the function is used only for reading or both for writing and reading

Example of config

M2MB_I2C_CFG_T i2c_data;
i2c_data.sdaPin = 2; //SDA pin on GPIO_02
i2c_data.sclPin = 3; //SCL pin on GPIO_03
i2c_data.registerId = 4; //register to write to or to read from: 0x04
Ret = m2mb_i2c_ioctl( fd, M2MB_I2C_IOCTL_SET_CFG, (void *)&i2c_data );
if ( Ret )
{
LOG(" m2mb_i2c_ioctl() - I2C FAIL" );
testErrs = 1;
}

Example of Combined mode write access

//write 2 bytes starting from register 0x04
M2MB_I2C_CFG_T i2c_data;
INT32 IICres;
i2c_data.registerId = 0x4;
memset( iicbuf_wr, 0x00, 256 );
memset( iicbuf_rd, 0x00, 256 );
iicbuf_wr[0] = i2c_data.registerId; //first send register id
iicbuf_wr[1] = 0xaa; //data to write in register 0x04
iicbuf_wr[2] = 0xbb; //data to write to register 0x05
msgs[0].buf = iicbuf_wr;
msgs[0].flags = I2C_M_WR;
msgs[0].len = 3; //send 1 data to external device
msgs[1].flags = I2C_M_RD;
msgs[1].len = 0; //read 10 bytes starting fom 0x4 register
msgs[1].buf = iicbuf_rd;
i2c_data.rw_param->msgs = &msgs[0];
i2c_data.rw_param->nmsgs = 1; //both writing and reading
IICres = m2mb_i2c_ioctl(fd, M2MB_I2C_IOCTL_RDWR, (void *)&i2c_data);

Example of Combined mode read access

//read 2 bytes starting fom 0x4 register
M2MB_I2C_CFG_T i2c_data;
INT32 IICres;
i2c_data.registerId = 0x4;
memset( iicbuf_wr, 0x00, 256 );
memset( iicbuf_rd, 0x00, 256 );
iicbuf_wr[0] = i2c_data.registerId; //first send register id
msgs[0].buf = iicbuf_wr;
msgs[0].flags = I2C_M_WR;
msgs[0].len = 1; //send 1 data to external device
msgs[1].flags = I2C_M_RD;
msgs[1].len = 2;
msgs[1].buf = iicbuf_rd;
i2c_data.rw_param->msgs = &msgs[0];
i2c_data.rw_param->nmsgs = 2; //both writing and reading
IICres = m2mb_i2c_ioctl(fd, M2MB_I2C_IOCTL_RDWR, (void *)&i2c_data);

◆ m2mb_i2c_open()

INT32 m2mb_i2c_open ( const CHAR *  path,
INT32  flags,
  ... 
)

open an I2C device

This function opens a file descriptor to communicate with an I2C device, given its address.

Parameters
[in]pathPath for the device address, in the format "/dev/I2C-#" where # is in decimal format I2C address is in 8 bit format, where last bit is the R/W flag
[in]flagscurrently unused
Returns
file descriptor on SUCCESS, -1 on FAILURE
#define DEVICEADDR 0x0F //e.g. the device address reported by device vendor on datasheet
CHAR path[32];
INT32 fd;
sprintf(path, "/dev/I2C-%d", DEVICEADDR << 1);
fd = m2mb_i2c_open( path, 0 );
if (-1 == fd)
{
LOG("cannot open I2C channel");
return -1;
}

◆ m2mb_i2c_read()

SSIZE_T m2mb_i2c_read ( INT32  fd,
void *  buf,
SIZE_T  nbyte 
)

Read data from a connected I2C device.

This function will try to read the requested amount of data from the specified I2C device. Read is performed with a write request for the register on the I2C bus, followed by a second Start Sequence and the read request. The register address for the operation must be previously set with m2mb_i2c_ioctl.

Parameters
[in]fdFile descriptor, returned by m2mb_i2c_open
[in]bufdestination buffer, previously allocated
[in]nbytelength of destination buffer in Bytes
Returns
number of read Bytes on SUCCESS, -1 on FAILURE
#define DEVICEADDR 0x0F //e.g. the device address reported by device vendor on datasheet
CHAR path[32];
INT32 fd;
UINT8 buffer[10];
SSIZE_T i2c_res;
sprintf(path, "/dev/I2C-%d", DEVICEADDR << 1);
fd = m2mb_i2c_open( path, 0 );
if (-1 == fd)
{
LOG("cannot open I2C channel");
return -1;
}
config.sclPin = 3; //use GPIO3 for Clock
config.sdaPin = 2; //use GPIO2 for Data
config.RegisterId=0x1F; //Slave Device Register address to be accessed (either in read or write operations)
//do communication with I2C device
i2c_res = m2mb_i2c_read(fd,buffer,5); //try to read 5 bytes from the slave device.

◆ m2mb_i2c_write()

SSIZE_T m2mb_i2c_write ( INT32  fd,
const void *  buf,
SIZE_T  nbyte 
)

Write data to a connected I2C device.

This function will try to write the requested amount of data to the specified I2C device. The register address for the operation must be previously set with m2mb_i2c_ioctl.

Parameters
[in]fdFile descriptor, returned by m2mb_i2c_open
[in]bufinput buffer, previously allocated
[in]nbytenumber of bytes to be written, in Bytes.
Returns
number of written Bytes on SUCCESS, -1 on FAILURE
#define DEVICEADDR 0x0F //e.g. the device address reported by device vendor on datasheet
CHAR path[32];
INT32 fd;
UINT8 buffer[10];
SSIZE_T i2c_res;
sprintf(path, "/dev/I2C-%d", DEVICEADDR << 1);
fd = m2mb_i2c_open( path, 0 );
if (-1 == fd)
{
LOG("cannot open I2C channel");
return -1;
}
config.sclPin = 3; //use GPIO3 for Clock
config.sdaPin = 2; //use GPIO2 for Data
config.RegisterId=0x1F; //Slave Device Register address to be accessed (either in read or write operations)
//do communication with I2C device
buffer[0] = 0xF2; //set some data
i2c_res = m2mb_i2c_write(fd,buffer,1); //try to write 1 byte on the slave device register.
m2mb_i2c_write
SSIZE_T m2mb_i2c_write(INT32 fd, const void *buf, SIZE_T nbyte)
Write data to a connected I2C device.
M2MB_I2C_CFG_T::sdaPin
UINT8 sdaPin
Definition: m2mb_i2c.h:86
m2mb_i2c_close
INT32 m2mb_i2c_close(INT32 fd)
Close an I2C device.
M2MB_I2C_RDWR_IOCTL_DATA::nmsgs
UINT32 nmsgs
Definition: m2mb_i2c.h:78
M2MB_I2C_IOCTL_SET_CFG
Definition: m2mb_i2c.h:51
M2MB_I2C_RDWR_IOCTL_DATA::msgs
M2MB_I2C_MSG * msgs
Definition: m2mb_i2c.h:77
I2C_M_RD
#define I2C_M_RD
Definition: m2mb_i2c.h:42
M2MB_I2C_IOCTL_RDWR
Definition: m2mb_i2c.h:53
m2mb_os_malloc
void * m2mb_os_malloc(UINT32 size)
Allocates bytes of memory.
M2MB_I2C_CFG_T::sclPin
UINT8 sclPin
Definition: m2mb_i2c.h:87
M2MB_I2C_RDWR_IOCTL_DATA
Structure to be used with the M2MB_I2C_IOCTL_RDWR option in m2mb_i2c_ioctl call.
Definition: m2mb_i2c.h:75
I2C_M_WR
#define I2C_M_WR
Definition: m2mb_i2c.h:41
m2mb_i2c_open
INT32 m2mb_i2c_open(const CHAR *path, INT32 flags,...)
open an I2C device
M2MB_I2C_CFG_T::registerId
UINT8 registerId
Definition: m2mb_i2c.h:88
M2MB_I2C_CFG_T::rw_param
M2MB_I2C_RDWR_IOCTL_DATA * rw_param
Definition: m2mb_i2c.h:90
m2mb_i2c_ioctl
INT32 m2mb_i2c_ioctl(INT32 fd, INT32 request,...)
configure, read from and/or write to an I2C device
M2MB_I2C_CFG_T
I2C device configuration structure.
Definition: m2mb_i2c.h:84
m2mb_i2c_read
SSIZE_T m2mb_i2c_read(INT32 fd, void *buf, SIZE_T nbyte)
Read data from a connected I2C device.