Zephyr Project  2.7.99
A Scalable Open Source RTOS
I2C Interface

I2C Interface. More...

Data Structures

struct  i2c_dt_spec
 Complete I2C DT information. More...
 
struct  i2c_msg
 One I2C Message. More...
 
struct  i2c_slave_callbacks
 Structure providing callbacks to be implemented for devices that supports the I2C slave API. More...
 
struct  i2c_slave_config
 Structure describing a device that supports the I2C slave API. More...
 
struct  i2c_client_config
 

Macros

#define I2C_SPEED_STANDARD   (0x1U)
 
#define I2C_SPEED_FAST   (0x2U)
 
#define I2C_SPEED_FAST_PLUS   (0x3U)
 
#define I2C_SPEED_HIGH   (0x4U)
 
#define I2C_SPEED_ULTRA   (0x5U)
 
#define I2C_SPEED_SHIFT   (1U)
 
#define I2C_SPEED_SET(speed)
 
#define I2C_SPEED_MASK   (0x7U << I2C_SPEED_SHIFT) /* 3 bits */
 
#define I2C_SPEED_GET(cfg)
 
#define I2C_ADDR_10_BITS   BIT(0)
 
#define I2C_MODE_MASTER   BIT(4)
 
#define I2C_DT_SPEC_GET(node_id)
 Structure initializer for i2c_dt_spec from devicetree. More...
 
#define I2C_DT_SPEC_INST_GET(inst)    I2C_DT_SPEC_GET(DT_DRV_INST(inst))
 Structure initializer for i2c_dt_spec from devicetree instance. More...
 
#define I2C_MSG_WRITE   (0U << 0U)
 
#define I2C_MSG_READ   BIT(0)
 
#define I2C_MSG_STOP   BIT(1)
 
#define I2C_MSG_RESTART   BIT(2)
 
#define I2C_MSG_ADDR_10_BITS   BIT(3)
 
#define I2C_SLAVE_FLAGS_ADDR_10_BITS   BIT(0)
 
#define I2C_DECLARE_CLIENT_CONFIG   struct i2c_client_config i2c_client
 
#define I2C_CLIENT(_master, _addr)
 
#define I2C_GET_MASTER(_conf)   ((_conf)->i2c_client.i2c_master)
 
#define I2C_GET_ADDR(_conf)   ((_conf)->i2c_client.i2c_addr)
 

Typedefs

typedef int(* i2c_slave_write_requested_cb_t) (struct i2c_slave_config *config)
 Function called when a write to the device is initiated. More...
 
typedef int(* i2c_slave_write_received_cb_t) (struct i2c_slave_config *config, uint8_t val)
 Function called when a write to the device is continued. More...
 
typedef int(* i2c_slave_read_requested_cb_t) (struct i2c_slave_config *config, uint8_t *val)
 Function called when a read from the device is initiated. More...
 
typedef int(* i2c_slave_read_processed_cb_t) (struct i2c_slave_config *config, uint8_t *val)
 Function called when a read from the device is continued. More...
 
typedef int(* i2c_slave_stop_cb_t) (struct i2c_slave_config *config)
 Function called when a stop condition is observed after a start condition addressed to a particular device. More...
 

Functions

int i2c_configure (const struct device *dev, uint32_t dev_config)
 Configure operation of a host controller. More...
 
int i2c_transfer (const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr)
 Perform data transfer to another I2C device in master mode. More...
 
static int i2c_transfer_dt (const struct i2c_dt_spec *spec, struct i2c_msg *msgs, uint8_t num_msgs)
 Perform data transfer to another I2C device in master mode. More...
 
int i2c_recover_bus (const struct device *dev)
 Recover the I2C bus. More...
 
static int i2c_slave_register (const struct device *dev, struct i2c_slave_config *cfg)
 Registers the provided config as Slave device of a controller. More...
 
static int i2c_slave_unregister (const struct device *dev, struct i2c_slave_config *cfg)
 Unregisters the provided config as Slave device. More...
 
int i2c_slave_driver_register (const struct device *dev)
 Instructs the I2C Slave device to register itself to the I2C Controller. More...
 
int i2c_slave_driver_unregister (const struct device *dev)
 Instructs the I2C Slave device to unregister itself from the I2C Controller. More...
 
static int i2c_write (const struct device *dev, const uint8_t *buf, uint32_t num_bytes, uint16_t addr)
 Write a set amount of data to an I2C device. More...
 
static int i2c_write_dt (const struct i2c_dt_spec *spec, const uint8_t *buf, uint32_t num_bytes)
 Write a set amount of data to an I2C device. More...
 
static int i2c_read (const struct device *dev, uint8_t *buf, uint32_t num_bytes, uint16_t addr)
 Read a set amount of data from an I2C device. More...
 
static int i2c_read_dt (const struct i2c_dt_spec *spec, uint8_t *buf, uint32_t num_bytes)
 Read a set amount of data from an I2C device. More...
 
static int i2c_write_read (const struct device *dev, uint16_t addr, const void *write_buf, size_t num_write, void *read_buf, size_t num_read)
 Write then read data from an I2C device. More...
 
static int i2c_write_read_dt (const struct i2c_dt_spec *spec, const void *write_buf, size_t num_write, void *read_buf, size_t num_read)
 Write then read data from an I2C device. More...
 
static int i2c_burst_read (const struct device *dev, uint16_t dev_addr, uint8_t start_addr, uint8_t *buf, uint32_t num_bytes)
 Read multiple bytes from an internal address of an I2C device. More...
 
static int i2c_burst_read_dt (const struct i2c_dt_spec *spec, uint8_t start_addr, uint8_t *buf, uint32_t num_bytes)
 Read multiple bytes from an internal address of an I2C device. More...
 
static int i2c_burst_write (const struct device *dev, uint16_t dev_addr, uint8_t start_addr, const uint8_t *buf, uint32_t num_bytes)
 Write multiple bytes to an internal address of an I2C device. More...
 
static int i2c_burst_write_dt (const struct i2c_dt_spec *spec, uint8_t start_addr, const uint8_t *buf, uint32_t num_bytes)
 Write multiple bytes to an internal address of an I2C device. More...
 
static int i2c_reg_read_byte (const struct device *dev, uint16_t dev_addr, uint8_t reg_addr, uint8_t *value)
 Read internal register of an I2C device. More...
 
static int i2c_reg_read_byte_dt (const struct i2c_dt_spec *spec, uint8_t reg_addr, uint8_t *value)
 Read internal register of an I2C device. More...
 
static int i2c_reg_write_byte (const struct device *dev, uint16_t dev_addr, uint8_t reg_addr, uint8_t value)
 Write internal register of an I2C device. More...
 
static int i2c_reg_write_byte_dt (const struct i2c_dt_spec *spec, uint8_t reg_addr, uint8_t value)
 Write internal register of an I2C device. More...
 
static int i2c_reg_update_byte (const struct device *dev, uint8_t dev_addr, uint8_t reg_addr, uint8_t mask, uint8_t value)
 Update internal register of an I2C device. More...
 
static int i2c_reg_update_byte_dt (const struct i2c_dt_spec *spec, uint8_t reg_addr, uint8_t mask, uint8_t value)
 Update internal register of an I2C device. More...
 
void i2c_dump_msgs (const char *name, const struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr)
 Dump out an I2C message. More...
 

Detailed Description

I2C Interface.

Macro Definition Documentation

◆ I2C_ADDR_10_BITS

#define I2C_ADDR_10_BITS   BIT(0)

#include <include/drivers/i2c.h>

Use 10-bit addressing. DEPRECATED - Use I2C_MSG_ADDR_10_BITS instead.

◆ I2C_CLIENT

#define I2C_CLIENT (   _master,
  _addr 
)

#include <include/drivers/i2c.h>

Value:
.i2c_client = { \
.i2c_master = (_master), \
.i2c_addr = (_addr), \
}

◆ I2C_DECLARE_CLIENT_CONFIG

#define I2C_DECLARE_CLIENT_CONFIG   struct i2c_client_config i2c_client

◆ I2C_DT_SPEC_GET

#define I2C_DT_SPEC_GET (   node_id)

#include <include/drivers/i2c.h>

Value:
{ \
.bus = DEVICE_DT_GET(DT_BUS(node_id)), \
.addr = DT_REG_ADDR(node_id) \
}
#define DEVICE_DT_GET(node_id)
Obtain a pointer to a device object by node_id.
Definition: device.h:247
#define DT_BUS(node_id)
Node's bus controller.
Definition: devicetree.h:2022
#define DT_REG_ADDR(node_id)
Get a node's (only) register block address.
Definition: devicetree.h:1355

Structure initializer for i2c_dt_spec from devicetree.

This helper macro expands to a static initializer for a struct i2c_dt_spec by reading the relevant bus and address data from the devicetree.

Parameters
node_idDevicetree node identifier for the I2C device whose struct i2c_dt_spec to create an initializer for

◆ I2C_DT_SPEC_INST_GET

#define I2C_DT_SPEC_INST_GET (   inst)     I2C_DT_SPEC_GET(DT_DRV_INST(inst))

#include <include/drivers/i2c.h>

Structure initializer for i2c_dt_spec from devicetree instance.

This is equivalent to I2C_DT_SPEC_GET(DT_DRV_INST(inst)).

Parameters
instDevicetree instance number

◆ I2C_GET_ADDR

#define I2C_GET_ADDR (   _conf)    ((_conf)->i2c_client.i2c_addr)

◆ I2C_GET_MASTER

#define I2C_GET_MASTER (   _conf)    ((_conf)->i2c_client.i2c_master)

◆ I2C_MODE_MASTER

#define I2C_MODE_MASTER   BIT(4)

#include <include/drivers/i2c.h>

Controller to act as Master.

◆ I2C_MSG_ADDR_10_BITS

#define I2C_MSG_ADDR_10_BITS   BIT(3)

#include <include/drivers/i2c.h>

Use 10-bit addressing for this message.

Note
Not all SoC I2C implementations support this feature.

◆ I2C_MSG_READ

#define I2C_MSG_READ   BIT(0)

#include <include/drivers/i2c.h>

Read message from I2C bus.

◆ I2C_MSG_RESTART

#define I2C_MSG_RESTART   BIT(2)

#include <include/drivers/i2c.h>

RESTART I2C transaction for this message.

Note
Not all I2C drivers have or require explicit support for this feature. Some drivers require this be present on a read message that follows a write, or vice-versa. Some drivers will merge adjacent fragments into a single transaction using this flag; some will not.

◆ I2C_MSG_STOP

#define I2C_MSG_STOP   BIT(1)

#include <include/drivers/i2c.h>

Send STOP after this message.

◆ I2C_MSG_WRITE

#define I2C_MSG_WRITE   (0U << 0U)

#include <include/drivers/i2c.h>

Write message to I2C bus.

◆ I2C_SLAVE_FLAGS_ADDR_10_BITS

#define I2C_SLAVE_FLAGS_ADDR_10_BITS   BIT(0)

#include <include/drivers/i2c.h>

Slave device responds to 10-bit addressing.

◆ I2C_SPEED_FAST

#define I2C_SPEED_FAST   (0x2U)

#include <include/drivers/i2c.h>

I2C Fast Speed: 400 kHz

◆ I2C_SPEED_FAST_PLUS

#define I2C_SPEED_FAST_PLUS   (0x3U)

#include <include/drivers/i2c.h>

I2C Fast Plus Speed: 1 MHz

◆ I2C_SPEED_GET

#define I2C_SPEED_GET (   cfg)

#include <include/drivers/i2c.h>

Value:
(((cfg) & I2C_SPEED_MASK) \
#define I2C_SPEED_SHIFT
Definition: i2c.h:48
#define I2C_SPEED_MASK
Definition: i2c.h:51

◆ I2C_SPEED_HIGH

#define I2C_SPEED_HIGH   (0x4U)

#include <include/drivers/i2c.h>

I2C High Speed: 3.4 MHz

◆ I2C_SPEED_MASK

#define I2C_SPEED_MASK   (0x7U << I2C_SPEED_SHIFT) /* 3 bits */

◆ I2C_SPEED_SET

#define I2C_SPEED_SET (   speed)

#include <include/drivers/i2c.h>

Value:
(((speed) << I2C_SPEED_SHIFT) \

◆ I2C_SPEED_SHIFT

#define I2C_SPEED_SHIFT   (1U)

◆ I2C_SPEED_STANDARD

#define I2C_SPEED_STANDARD   (0x1U)

#include <include/drivers/i2c.h>

I2C Standard Speed: 100 kHz

◆ I2C_SPEED_ULTRA

#define I2C_SPEED_ULTRA   (0x5U)

#include <include/drivers/i2c.h>

I2C Ultra Fast Speed: 5 MHz

Typedef Documentation

◆ i2c_slave_read_processed_cb_t

typedef int(* i2c_slave_read_processed_cb_t) (struct i2c_slave_config *config, uint8_t *val)

#include <include/drivers/i2c.h>

Function called when a read from the device is continued.

This function is invoked by the controller when the bus is ready to provide additional data for a read operation from the address associated with the device device.

The value returned in *val will be transmitted. A success return shall cause the controller to react to additional read operations. An error return shall cause the controller to ignore bus operations until a new start condition is received.

Parameters
configthe configuration structure associated with the device to which the operation is addressed.
valpointer to storage for the next byte of data to return for the read request.
Returns
0 if data has been provided, or a negative error code.

◆ i2c_slave_read_requested_cb_t

typedef int(* i2c_slave_read_requested_cb_t) (struct i2c_slave_config *config, uint8_t *val)

#include <include/drivers/i2c.h>

Function called when a read from the device is initiated.

This function is invoked by the controller when the bus completes a start condition for a read operation from the address associated with a particular device.

The value returned in *val will be transmitted. A success return shall cause the controller to react to additional read operations. An error return shall cause the controller to ignore bus operations until a new start condition is received.

Parameters
configthe configuration structure associated with the device to which the operation is addressed.
valpointer to storage for the first byte of data to return for the read request.
Returns
0 if more data can be requested, or a negative error code.

◆ i2c_slave_stop_cb_t

typedef int(* i2c_slave_stop_cb_t) (struct i2c_slave_config *config)

#include <include/drivers/i2c.h>

Function called when a stop condition is observed after a start condition addressed to a particular device.

This function is invoked by the controller when the bus is ready to provide additional data for a read operation from the address associated with the device device. After the function returns the controller shall enter a state where it is ready to react to new start conditions.

Parameters
configthe configuration structure associated with the device to which the operation is addressed.
Returns
Ignored.

◆ i2c_slave_write_received_cb_t

typedef int(* i2c_slave_write_received_cb_t) (struct i2c_slave_config *config, uint8_t val)

#include <include/drivers/i2c.h>

Function called when a write to the device is continued.

This function is invoked by the controller when it completes reception of a byte of data in an ongoing write operation to the device.

A success return shall cause the controller to ACK the next byte received. An error return shall cause the controller to NACK the next byte received.

Parameters
configthe configuration structure associated with the device to which the operation is addressed.
valthe byte received by the controller.
Returns
0 if more data can be accepted, or a negative error code.

◆ i2c_slave_write_requested_cb_t

typedef int(* i2c_slave_write_requested_cb_t) (struct i2c_slave_config *config)

#include <include/drivers/i2c.h>

Function called when a write to the device is initiated.

This function is invoked by the controller when the bus completes a start condition for a write operation to the address associated with a particular device.

A success return shall cause the controller to ACK the next byte received. An error return shall cause the controller to NACK the next byte received.

Parameters
configthe configuration structure associated with the device to which the operation is addressed.
Returns
0 if the write is accepted, or a negative error code.

Function Documentation

◆ i2c_burst_read()

static int i2c_burst_read ( const struct device dev,
uint16_t  dev_addr,
uint8_t  start_addr,
uint8_t buf,
uint32_t  num_bytes 
)
inlinestatic

#include <include/drivers/i2c.h>

Read multiple bytes from an internal address of an I2C device.

This routine reads multiple bytes from an internal address of an I2C device synchronously.

Instances of this may be replaced by i2c_write_read().

Parameters
devPointer to the device structure for an I2C controller driver configured in master mode.
dev_addrAddress of the I2C device for reading.
start_addrInternal address from which the data is being read.
bufMemory pool that stores the retrieved data.
num_bytesNumber of bytes being read.
Return values
0If successful.
-EIOGeneral input / output error.

◆ i2c_burst_read_dt()

static int i2c_burst_read_dt ( const struct i2c_dt_spec spec,
uint8_t  start_addr,
uint8_t buf,
uint32_t  num_bytes 
)
inlinestatic

#include <include/drivers/i2c.h>

Read multiple bytes from an internal address of an I2C device.

This is equivalent to:

i2c_burst_read(spec->bus, spec->addr, start_addr, buf, num_bytes);
Parameters
specI2C specification from devicetree.
start_addrInternal address from which the data is being read.
bufMemory pool that stores the retrieved data.
num_bytesNumber of bytes to read.
Returns
a value from i2c_burst_read()

◆ i2c_burst_write()

static int i2c_burst_write ( const struct device dev,
uint16_t  dev_addr,
uint8_t  start_addr,
const uint8_t buf,
uint32_t  num_bytes 
)
inlinestatic

#include <include/drivers/i2c.h>

Write multiple bytes to an internal address of an I2C device.

This routine writes multiple bytes to an internal address of an I2C device synchronously.

Warning
The combined write synthesized by this API may not be supported on all I2C devices. Uses of this API may be made more portable by replacing them with calls to i2c_write() passing a buffer containing the combined address and data.
Parameters
devPointer to the device structure for an I2C controller driver configured in master mode.
dev_addrAddress of the I2C device for writing.
start_addrInternal address to which the data is being written.
bufMemory pool from which the data is transferred.
num_bytesNumber of bytes being written.
Return values
0If successful.
-EIOGeneral input / output error.

◆ i2c_burst_write_dt()

static int i2c_burst_write_dt ( const struct i2c_dt_spec spec,
uint8_t  start_addr,
const uint8_t buf,
uint32_t  num_bytes 
)
inlinestatic

#include <include/drivers/i2c.h>

Write multiple bytes to an internal address of an I2C device.

This is equivalent to:

i2c_burst_write(spec->bus, spec->addr, start_addr, buf, num_bytes);
Parameters
specI2C specification from devicetree.
start_addrInternal address to which the data is being written.
bufMemory pool from which the data is transferred.
num_bytesNumber of bytes being written.
Returns
a value from i2c_burst_write()

◆ i2c_configure()

int i2c_configure ( const struct device dev,
uint32_t  dev_config 
)

#include <include/drivers/i2c.h>

Configure operation of a host controller.

Parameters
devPointer to the device structure for the driver instance.
dev_configBit-packed 32-bit value to the device runtime configuration for the I2C controller.
Return values
0If successful.
-EIOGeneral input / output error, failed to configure device.

◆ i2c_dump_msgs()

void i2c_dump_msgs ( const char *  name,
const struct i2c_msg msgs,
uint8_t  num_msgs,
uint16_t  addr 
)

#include <include/drivers/i2c.h>

Dump out an I2C message.

Dumps out a list of I2C messages. For any that are writes (W), the data is displayed in hex.

It looks something like this (with name "testing"):

D: I2C msg: testing, addr=56 D: W len=01: D: contents: D: 06 |. D: W len=0e: D: contents: D: 00 01 02 03 04 05 06 07 |........ D: 08 09 0a 0b 0c 0d |......

Parameters
nameName of this dump, displayed at the top.
msgsArray of messages to dump.
num_msgsNumber of messages to dump.
addrAddress of the I2C target device.

◆ i2c_read()

static int i2c_read ( const struct device dev,
uint8_t buf,
uint32_t  num_bytes,
uint16_t  addr 
)
inlinestatic

#include <include/drivers/i2c.h>

Read a set amount of data from an I2C device.

This routine reads a set amount of data synchronously.

Parameters
devPointer to the device structure for an I2C controller driver configured in master mode.
bufMemory pool that stores the retrieved data.
num_bytesNumber of bytes to read.
addrAddress of the I2C device being read.
Return values
0If successful.
-EIOGeneral input / output error.

◆ i2c_read_dt()

static int i2c_read_dt ( const struct i2c_dt_spec spec,
uint8_t buf,
uint32_t  num_bytes 
)
inlinestatic

#include <include/drivers/i2c.h>

Read a set amount of data from an I2C device.

This is equivalent to:

i2c_read(spec->bus, buf, num_bytes, spec->addr);
Parameters
specI2C specification from devicetree.
bufMemory pool that stores the retrieved data.
num_bytesNumber of bytes to read.
Returns
a value from i2c_read()

◆ i2c_recover_bus()

int i2c_recover_bus ( const struct device dev)

#include <include/drivers/i2c.h>

Recover the I2C bus.

Attempt to recover the I2C bus.

Parameters
devPointer to the device structure for an I2C controller driver configured in master mode.
Return values
0If successful
-EBUSYIf bus is not clear after recovery attempt.
-EIOGeneral input / output error.
-ENOSYSIf bus recovery is not implemented

◆ i2c_reg_read_byte()

static int i2c_reg_read_byte ( const struct device dev,
uint16_t  dev_addr,
uint8_t  reg_addr,
uint8_t value 
)
inlinestatic

#include <include/drivers/i2c.h>

Read internal register of an I2C device.

This routine reads the value of an 8-bit internal register of an I2C device synchronously.

Parameters
devPointer to the device structure for an I2C controller driver configured in master mode.
dev_addrAddress of the I2C device for reading.
reg_addrAddress of the internal register being read.
valueMemory pool that stores the retrieved register value.
Return values
0If successful.
-EIOGeneral input / output error.

◆ i2c_reg_read_byte_dt()

static int i2c_reg_read_byte_dt ( const struct i2c_dt_spec spec,
uint8_t  reg_addr,
uint8_t value 
)
inlinestatic

#include <include/drivers/i2c.h>

Read internal register of an I2C device.

This is equivalent to:

i2c_reg_read_byte(spec->bus, spec->addr, reg_addr, value);
Parameters
specI2C specification from devicetree.
reg_addrAddress of the internal register being read.
valueMemory pool that stores the retrieved register value.
Returns
a value from i2c_reg_read_byte()

◆ i2c_reg_update_byte()

static int i2c_reg_update_byte ( const struct device dev,
uint8_t  dev_addr,
uint8_t  reg_addr,
uint8_t  mask,
uint8_t  value 
)
inlinestatic

#include <include/drivers/i2c.h>

Update internal register of an I2C device.

This routine updates the value of a set of bits from an 8-bit internal register of an I2C device synchronously.

Note
If the calculated new register value matches the value that was read this function will not generate a write operation.
Parameters
devPointer to the device structure for an I2C controller driver configured in master mode.
dev_addrAddress of the I2C device for updating.
reg_addrAddress of the internal register being updated.
maskBitmask for updating internal register.
valueValue for updating internal register.
Return values
0If successful.
-EIOGeneral input / output error.

◆ i2c_reg_update_byte_dt()

static int i2c_reg_update_byte_dt ( const struct i2c_dt_spec spec,
uint8_t  reg_addr,
uint8_t  mask,
uint8_t  value 
)
inlinestatic

#include <include/drivers/i2c.h>

Update internal register of an I2C device.

This is equivalent to:

i2c_reg_update_byte(spec->bus, spec->addr, reg_addr, mask, value);
Parameters
specI2C specification from devicetree.
reg_addrAddress of the internal register being updated.
maskBitmask for updating internal register.
valueValue for updating internal register.
Returns
a value from i2c_reg_update_byte()

◆ i2c_reg_write_byte()

static int i2c_reg_write_byte ( const struct device dev,
uint16_t  dev_addr,
uint8_t  reg_addr,
uint8_t  value 
)
inlinestatic

#include <include/drivers/i2c.h>

Write internal register of an I2C device.

This routine writes a value to an 8-bit internal register of an I2C device synchronously.

Note
This function internally combines the register and value into a single bus transaction.
Parameters
devPointer to the device structure for an I2C controller driver configured in master mode.
dev_addrAddress of the I2C device for writing.
reg_addrAddress of the internal register being written.
valueValue to be written to internal register.
Return values
0If successful.
-EIOGeneral input / output error.

◆ i2c_reg_write_byte_dt()

static int i2c_reg_write_byte_dt ( const struct i2c_dt_spec spec,
uint8_t  reg_addr,
uint8_t  value 
)
inlinestatic

#include <include/drivers/i2c.h>

Write internal register of an I2C device.

This is equivalent to:

i2c_reg_write_byte(spec->bus, spec->addr, reg_addr, value);
Parameters
specI2C specification from devicetree.
reg_addrAddress of the internal register being written.
valueValue to be written to internal register.
Returns
a value from i2c_reg_write_byte()

◆ i2c_slave_driver_register()

int i2c_slave_driver_register ( const struct device dev)

#include <include/drivers/i2c.h>

Instructs the I2C Slave device to register itself to the I2C Controller.

This routine instructs the I2C Slave device to register itself to the I2C Controller via its parent controller's i2c_slave_register() API.

Parameters
devPointer to the device structure for the I2C slave device (not itself an I2C controller).
Return values
0Is successful
-EINVALIf parameters are invalid
-EIOGeneral input / output error.

◆ i2c_slave_driver_unregister()

int i2c_slave_driver_unregister ( const struct device dev)

#include <include/drivers/i2c.h>

Instructs the I2C Slave device to unregister itself from the I2C Controller.

This routine instructs the I2C Slave device to unregister itself from the I2C Controller via its parent controller's i2c_slave_register() API.

Parameters
devPointer to the device structure for the I2C slave device (not itself an I2C controller).
Return values
0Is successful
-EINVALIf parameters are invalid

◆ i2c_slave_register()

static int i2c_slave_register ( const struct device dev,
struct i2c_slave_config cfg 
)
inlinestatic

#include <include/drivers/i2c.h>

Registers the provided config as Slave device of a controller.

Enable I2C slave mode for the 'dev' I2C bus driver using the provided 'config' struct containing the functions and parameters to send bus events. The I2C slave will be registered at the address provided as 'address' struct member. Addressing mode - 7 or 10 bit - depends on the 'flags' struct member. Any I2C bus events related to the slave mode will be passed onto I2C slave device driver via a set of callback functions provided in the 'callbacks' struct member.

Most of the existing hardware allows simultaneous support for master and slave mode. This is however not guaranteed.

Parameters
devPointer to the device structure for an I2C controller driver configured in slave mode.
cfgConfig struct with functions and parameters used by the I2C driver to send bus events
Return values
0Is successful
-EINVALIf parameters are invalid
-EIOGeneral input / output error.
-ENOSYSIf slave mode is not implemented

◆ i2c_slave_unregister()

static int i2c_slave_unregister ( const struct device dev,
struct i2c_slave_config cfg 
)
inlinestatic

#include <include/drivers/i2c.h>

Unregisters the provided config as Slave device.

This routine disables I2C slave mode for the 'dev' I2C bus driver using the provided 'config' struct containing the functions and parameters to send bus events.

Parameters
devPointer to the device structure for an I2C controller driver configured in slave mode.
cfgConfig struct with functions and parameters used by the I2C driver to send bus events
Return values
0Is successful
-EINVALIf parameters are invalid
-ENOSYSIf slave mode is not implemented

◆ i2c_transfer()

int i2c_transfer ( const struct device dev,
struct i2c_msg msgs,
uint8_t  num_msgs,
uint16_t  addr 
)

#include <include/drivers/i2c.h>

Perform data transfer to another I2C device in master mode.

This routine provides a generic interface to perform data transfer to another I2C device synchronously. Use i2c_read()/i2c_write() for simple read or write.

The array of message msgs must not be NULL. The number of message num_msgs may be zero,in which case no transfer occurs.

Note
Not all scatter/gather transactions can be supported by all drivers. As an example, a gather write (multiple consecutive i2c_msg buffers all configured for I2C_MSG_WRITE) may be packed into a single transaction by some drivers, but others may emit each fragment as a distinct write transaction, which will not produce the same behavior. See the documentation of struct i2c_msg for limitations on support for multi-message bus transactions.
Parameters
devPointer to the device structure for an I2C controller driver configured in master mode.
msgsArray of messages to transfer.
num_msgsNumber of messages to transfer.
addrAddress of the I2C target device.
Return values
0If successful.
-EIOGeneral input / output error.

◆ i2c_transfer_dt()

static int i2c_transfer_dt ( const struct i2c_dt_spec spec,
struct i2c_msg msgs,
uint8_t  num_msgs 
)
inlinestatic

#include <include/drivers/i2c.h>

Perform data transfer to another I2C device in master mode.

This is equivalent to:

i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
Parameters
specI2C specification from devicetree.
msgsArray of messages to transfer.
num_msgsNumber of messages to transfer.
Returns
a value from i2c_transfer()

◆ i2c_write()

static int i2c_write ( const struct device dev,
const uint8_t buf,
uint32_t  num_bytes,
uint16_t  addr 
)
inlinestatic

#include <include/drivers/i2c.h>

Write a set amount of data to an I2C device.

This routine writes a set amount of data synchronously.

Parameters
devPointer to the device structure for an I2C controller driver configured in master mode.
bufMemory pool from which the data is transferred.
num_bytesNumber of bytes to write.
addrAddress to the target I2C device for writing.
Return values
0If successful.
-EIOGeneral input / output error.

◆ i2c_write_dt()

static int i2c_write_dt ( const struct i2c_dt_spec spec,
const uint8_t buf,
uint32_t  num_bytes 
)
inlinestatic

#include <include/drivers/i2c.h>

Write a set amount of data to an I2C device.

This is equivalent to:

i2c_write(spec->bus, buf, num_bytes, spec->addr);
Parameters
specI2C specification from devicetree.
bufMemory pool from which the data is transferred.
num_bytesNumber of bytes to write.
Returns
a value from i2c_write()

◆ i2c_write_read()

static int i2c_write_read ( const struct device dev,
uint16_t  addr,
const void *  write_buf,
size_t  num_write,
void *  read_buf,
size_t  num_read 
)
inlinestatic

#include <include/drivers/i2c.h>

Write then read data from an I2C device.

This supports the common operation "this is what I want", "now give it to me" transaction pair through a combined write-then-read bus transaction.

Parameters
devPointer to the device structure for an I2C controller driver configured in master mode.
addrAddress of the I2C device
write_bufPointer to the data to be written
num_writeNumber of bytes to write
read_bufPointer to storage for read data
num_readNumber of bytes to read
Return values
0if successful
negativeon error.

◆ i2c_write_read_dt()

static int i2c_write_read_dt ( const struct i2c_dt_spec spec,
const void *  write_buf,
size_t  num_write,
void *  read_buf,
size_t  num_read 
)
inlinestatic

#include <include/drivers/i2c.h>

Write then read data from an I2C device.

This is equivalent to:

i2c_write_read(spec->bus, spec->addr,
               write_buf, num_write,
               read_buf, num_read);
Parameters
specI2C specification from devicetree.
write_bufPointer to the data to be written
num_writeNumber of bytes to write
read_bufPointer to storage for read data
num_readNumber of bytes to read
Returns
a value from i2c_write_read()