Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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_target_callbacks
 Structure providing callbacks to be implemented for devices that supports the I2C target API. More...
 
struct  i2c_target_config
 Structure describing a device that supports the I2C target API. More...
 
struct  i2c_device_state
 I2C specific device state which allows for i2c device class specific additions. More...
 

Macros

#define I2C_SPEED_STANDARD   (0x1U)
 I2C Standard Speed: 100 kHz.
 
#define I2C_SPEED_FAST   (0x2U)
 I2C Fast Speed: 400 kHz.
 
#define I2C_SPEED_FAST_PLUS   (0x3U)
 I2C Fast Plus Speed: 1 MHz.
 
#define I2C_SPEED_HIGH   (0x4U)
 I2C High Speed: 3.4 MHz.
 
#define I2C_SPEED_ULTRA   (0x5U)
 I2C Ultra Fast Speed: 5 MHz.
 
#define I2C_SPEED_DT   (0x7U)
 Device Tree specified speed.
 
#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)
 Use 10-bit addressing.
 
#define I2C_MODE_CONTROLLER   BIT(4)
 Peripheral to act as Controller.
 
#define I2C_DT_SPEC_GET_ON_I3C(node_id)
 Structure initializer for i2c_dt_spec from devicetree (on I3C bus)
 
#define I2C_DT_SPEC_GET_ON_I2C(node_id)
 Structure initializer for i2c_dt_spec from devicetree (on I2C bus)
 
#define I2C_DT_SPEC_GET(node_id)
 Structure initializer for i2c_dt_spec from devicetree.
 
#define I2C_DT_SPEC_INST_GET(inst)    I2C_DT_SPEC_GET(DT_DRV_INST(inst))
 Structure initializer for i2c_dt_spec from devicetree instance.
 
#define I2C_MSG_WRITE   (0U << 0U)
 Write message to I2C bus.
 
#define I2C_MSG_READ   BIT(0)
 Read message from I2C bus.
 
#define I2C_MSG_STOP   BIT(1)
 Send STOP after this message.
 
#define I2C_MSG_RESTART   BIT(2)
 RESTART I2C transaction for this message.
 
#define I2C_MSG_ADDR_10_BITS   BIT(3)
 Use 10-bit addressing for this message.
 
#define I2C_TARGET_FLAGS_ADDR_10_BITS   BIT(0)
 Target device responds to 10-bit addressing.
 
#define I2C_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, prio, api, ...)
 Like DEVICE_DT_DEFINE() with I2C specifics.
 
#define I2C_DEVICE_DT_INST_DEFINE(inst, ...)    I2C_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
 Like I2C_DEVICE_DT_DEFINE() for an instance of a DT_DRV_COMPAT compatible.
 
#define I2C_DT_IODEV_DEFINE(name, node_id)
 Define an iodev for a given dt node on the bus.
 
#define I2C_IODEV_DEFINE(name, _bus, _addr)
 Define an iodev for a given i2c device on a bus.
 

Typedefs

typedef void(* i2c_callback_t) (const struct device *dev, int result, void *data)
 I2C callback for asynchronous transfer requests.
 
typedef int(* i2c_target_write_requested_cb_t) (struct i2c_target_config *config)
 Function called when a write to the device is initiated.
 
typedef int(* i2c_target_write_received_cb_t) (struct i2c_target_config *config, uint8_t val)
 Function called when a write to the device is continued.
 
typedef int(* i2c_target_read_requested_cb_t) (struct i2c_target_config *config, uint8_t *val)
 Function called when a read from the device is initiated.
 
typedef int(* i2c_target_read_processed_cb_t) (struct i2c_target_config *config, uint8_t *val)
 Function called when a read from the device is continued.
 
typedef int(* i2c_target_stop_cb_t) (struct i2c_target_config *config)
 Function called when a stop condition is observed after a start condition addressed to a particular device.
 

Functions

static bool i2c_is_ready_dt (const struct i2c_dt_spec *spec)
 Validate that I2C bus is ready.
 
static bool i2c_is_read_op (struct i2c_msg *msg)
 Check if the current message is a read operation.
 
void i2c_dump_msgs_rw (const struct device *dev, const struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr, bool dump_read)
 Dump out an I2C message.
 
static void i2c_dump_msgs (const struct device *dev, const struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr)
 Dump out an I2C message, before it is executed.
 
static void i2c_xfer_stats (const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs)
 Updates the i2c stats for i2c transfers.
 
int i2c_configure (const struct device *dev, uint32_t dev_config)
 Configure operation of a host controller.
 
int i2c_get_config (const struct device *dev, uint32_t *dev_config)
 Get configuration of a host controller.
 
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 controller mode.
 
static int i2c_transfer_cb (const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr, i2c_callback_t cb, void *userdata)
 Perform data transfer to another I2C device in controller mode.
 
static int i2c_transfer_cb_dt (const struct i2c_dt_spec *spec, struct i2c_msg *msgs, uint8_t num_msgs, i2c_callback_t cb, void *userdata)
 Perform data transfer to another I2C device in master mode asynchronously.
 
static int i2c_write_read_cb (const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr, const void *write_buf, size_t num_write, void *read_buf, size_t num_read, i2c_callback_t cb, void *userdata)
 Write then read data from an I2C device asynchronously.
 
static int i2c_write_read_cb_dt (const struct i2c_dt_spec *spec, struct i2c_msg *msgs, uint8_t num_msgs, const void *write_buf, size_t num_write, void *read_buf, size_t num_read, i2c_callback_t cb, void *userdata)
 Write then read data from an I2C device asynchronously.
 
static int i2c_transfer_signal (const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr, struct k_poll_signal *sig)
 Perform data transfer to another I2C device in controller mode.
 
static void i2c_iodev_submit (struct rtio_iodev_sqe *iodev_sqe)
 Submit request(s) to an I2C device with RTIO.
 
struct rtio_sqei2c_rtio_copy (struct rtio *r, struct rtio_iodev *iodev, const struct i2c_msg *msgs, uint8_t num_msgs)
 Copy the i2c_msgs into a set of RTIO requests.
 
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 controller mode.
 
int i2c_recover_bus (const struct device *dev)
 Recover the I2C bus.
 
static int i2c_target_register (const struct device *dev, struct i2c_target_config *cfg)
 Registers the provided config as Target device of a controller.
 
static int i2c_target_unregister (const struct device *dev, struct i2c_target_config *cfg)
 Unregisters the provided config as Target device.
 
int i2c_target_driver_register (const struct device *dev)
 Instructs the I2C Target device to register itself to the I2C Controller.
 
int i2c_target_driver_unregister (const struct device *dev)
 Instructs the I2C Target device to unregister itself from the I2C Controller.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 

Variables

const struct rtio_iodev_api i2c_iodev_api
 

Detailed Description

I2C Interface.

Since
1.0
Version
1.0.0

Macro Definition Documentation

◆ I2C_ADDR_10_BITS

#define I2C_ADDR_10_BITS   BIT(0)

#include <zephyr/drivers/i2c.h>

Use 10-bit addressing.

DEPRECATED - Use I2C_MSG_ADDR_10_BITS instead.

◆ I2C_DEVICE_DT_DEFINE

#define I2C_DEVICE_DT_DEFINE (   node_id,
  init_fn,
  pm,
  data,
  config,
  level,
  prio,
  api,
  ... 
)

#include <zephyr/drivers/i2c.h>

Value:
Z_I2C_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
Z_I2C_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
DEVICE_DT_NAME(node_id), \
&UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
pm, data, config, level, prio, api, \
&(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
__VA_ARGS__)
#define DEVICE_DT_NAME(node_id)
Return a string name for a devicetree node.
Definition: device.h:151
#define UTIL_CAT(a,...)
Definition: util_internal.h:104

Like DEVICE_DT_DEFINE() with I2C specifics.

Defines a device which implements the I2C API. May generate a custom device_state container struct and init_fn wrapper when needed depending on I2C CONFIG_I2C_STATS .

Parameters
node_idThe devicetree node identifier.
init_fnName of the init function of the driver. Can be NULL.
pmPM device resources reference (NULL if device does not use PM).
dataPointer to the device's private data.
configThe address to the structure containing the configuration information for this instance of the driver.
levelThe initialization level. See SYS_INIT() for details.
prioPriority within the selected initialization level. See SYS_INIT() for details.
apiProvides an initial pointer to the API function struct used by the driver. Can be NULL.

◆ I2C_DEVICE_DT_INST_DEFINE

#define I2C_DEVICE_DT_INST_DEFINE (   inst,
  ... 
)     I2C_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)

#include <zephyr/drivers/i2c.h>

Like I2C_DEVICE_DT_DEFINE() for an instance of a DT_DRV_COMPAT compatible.

Parameters
instinstance number. This is replaced by DT_DRV_COMPAT(inst) in the call to I2C_DEVICE_DT_DEFINE().
...other parameters as expected by I2C_DEVICE_DT_DEFINE().

◆ I2C_DT_IODEV_DEFINE

#define I2C_DT_IODEV_DEFINE (   name,
  node_id 
)

#include <zephyr/drivers/i2c.h>

Value:
const struct i2c_dt_spec _i2c_dt_spec_##name = \
I2C_DT_SPEC_GET(node_id); \
RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&_i2c_dt_spec_##name)
const struct rtio_iodev_api i2c_iodev_api
Complete I2C DT information.
Definition: i2c.h:77

Define an iodev for a given dt node on the bus.

These do not need to be shared globally but doing so will save a small amount of memory.

Parameters
nameSymbolic name of the iodev to define
node_idDevicetree node identifier

◆ I2C_DT_SPEC_GET

#define I2C_DT_SPEC_GET (   node_id)

#include <zephyr/drivers/i2c.h>

Value:
{ \
COND_CODE_1(DT_ON_BUS(node_id, i3c), \
(I2C_DT_SPEC_GET_ON_I3C(node_id)), \
(I2C_DT_SPEC_GET_ON_I2C(node_id))) \
}
#define DT_ON_BUS(node_id, bus)
Is a node on a bus of a given type?
Definition: devicetree.h:3414
#define I2C_DT_SPEC_GET_ON_I3C(node_id)
Structure initializer for i2c_dt_spec from devicetree (on I3C bus)
Definition: i2c.h:92
#define I2C_DT_SPEC_GET_ON_I2C(node_id)
Structure initializer for i2c_dt_spec from devicetree (on I2C bus)
Definition: i2c.h:106

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_GET_ON_I2C

#define I2C_DT_SPEC_GET_ON_I2C (   node_id)

#include <zephyr/drivers/i2c.h>

Value:
.bus = DEVICE_DT_GET(DT_BUS(node_id)), \
.addr = DT_REG_ADDR(node_id)
#define DEVICE_DT_GET(node_id)
Get a device reference from a devicetree node identifier.
Definition: device.h:245
#define DT_BUS(node_id)
Node's bus controller.
Definition: devicetree.h:3384
#define DT_REG_ADDR(node_id)
Get a node's (only) register block address.
Definition: devicetree.h:2235

Structure initializer for i2c_dt_spec from devicetree (on I2C bus)

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_GET_ON_I3C

#define I2C_DT_SPEC_GET_ON_I3C (   node_id)

#include <zephyr/drivers/i2c.h>

Value:
.bus = DEVICE_DT_GET(DT_BUS(node_id)), \
.addr = DT_PROP_BY_IDX(node_id, reg, 0)
#define DT_PROP_BY_IDX(node_id, prop, idx)
Get the value at index idx in an array type property.
Definition: devicetree.h:782

Structure initializer for i2c_dt_spec from devicetree (on I3C bus)

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 <zephyr/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_IODEV_DEFINE

#define I2C_IODEV_DEFINE (   name,
  _bus,
  _addr 
)

#include <zephyr/drivers/i2c.h>

Value:
const struct i2c_dt_spec _i2c_dt_spec_##name = { \
.bus = DEVICE_DT_GET(_bus), \
.addr = _addr, \
}; \
RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&_i2c_dt_spec_##name)
const struct device * bus
Definition: i2c.h:78

Define an iodev for a given i2c device on a bus.

These do not need to be shared globally but doing so will save a small amount of memory.

Parameters
nameSymbolic name of the iodev to define
_busNode ID for I2C bus
_addrI2C target address

◆ I2C_MODE_CONTROLLER

#define I2C_MODE_CONTROLLER   BIT(4)

#include <zephyr/drivers/i2c.h>

Peripheral to act as Controller.

◆ I2C_MSG_ADDR_10_BITS

#define I2C_MSG_ADDR_10_BITS   BIT(3)

#include <zephyr/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 <zephyr/drivers/i2c.h>

Read message from I2C bus.

◆ I2C_MSG_RESTART

#define I2C_MSG_RESTART   BIT(2)

#include <zephyr/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 <zephyr/drivers/i2c.h>

Send STOP after this message.

◆ I2C_MSG_WRITE

#define I2C_MSG_WRITE   (0U << 0U)

#include <zephyr/drivers/i2c.h>

Write message to I2C bus.

◆ I2C_SPEED_DT

#define I2C_SPEED_DT   (0x7U)

#include <zephyr/drivers/i2c.h>

Device Tree specified speed.

◆ I2C_SPEED_FAST

#define I2C_SPEED_FAST   (0x2U)

#include <zephyr/drivers/i2c.h>

I2C Fast Speed: 400 kHz.

◆ I2C_SPEED_FAST_PLUS

#define I2C_SPEED_FAST_PLUS   (0x3U)

#include <zephyr/drivers/i2c.h>

I2C Fast Plus Speed: 1 MHz.

◆ I2C_SPEED_GET

#define I2C_SPEED_GET (   cfg)

#include <zephyr/drivers/i2c.h>

Value:
(((cfg) & I2C_SPEED_MASK) \
#define I2C_SPEED_SHIFT
Definition: i2c.h:58
#define I2C_SPEED_MASK
Definition: i2c.h:61

◆ I2C_SPEED_HIGH

#define I2C_SPEED_HIGH   (0x4U)

#include <zephyr/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 <zephyr/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 <zephyr/drivers/i2c.h>

I2C Standard Speed: 100 kHz.

◆ I2C_SPEED_ULTRA

#define I2C_SPEED_ULTRA   (0x5U)

#include <zephyr/drivers/i2c.h>

I2C Ultra Fast Speed: 5 MHz.

◆ I2C_TARGET_FLAGS_ADDR_10_BITS

#define I2C_TARGET_FLAGS_ADDR_10_BITS   BIT(0)

#include <zephyr/drivers/i2c.h>

Target device responds to 10-bit addressing.

Typedef Documentation

◆ i2c_callback_t

typedef void(* i2c_callback_t) (const struct device *dev, int result, void *data)

#include <zephyr/drivers/i2c.h>

I2C callback for asynchronous transfer requests.

Parameters
devI2C device which is notifying of transfer completion or error
resultResult code of the transfer request. 0 is success, -errno for failure.
dataTransfer requester supplied data which is passed along to the callback.

◆ i2c_target_read_processed_cb_t

typedef int(* i2c_target_read_processed_cb_t) (struct i2c_target_config *config, uint8_t *val)

#include <zephyr/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_target_read_requested_cb_t

typedef int(* i2c_target_read_requested_cb_t) (struct i2c_target_config *config, uint8_t *val)

#include <zephyr/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_target_stop_cb_t

typedef int(* i2c_target_stop_cb_t) (struct i2c_target_config *config)

#include <zephyr/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_target_write_received_cb_t

typedef int(* i2c_target_write_received_cb_t) (struct i2c_target_config *config, uint8_t val)

#include <zephyr/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_target_write_requested_cb_t

typedef int(* i2c_target_write_requested_cb_t) (struct i2c_target_config *config)

#include <zephyr/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 <zephyr/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 controller 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 <zephyr/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 <zephyr/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 controller 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 <zephyr/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 <zephyr/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()

static void i2c_dump_msgs ( const struct device dev,
const struct i2c_msg msgs,
uint8_t  num_msgs,
uint16_t  addr 
)
inlinestatic

#include <zephyr/drivers/i2c.h>

Dump out an I2C message, before it is executed.

This is equivalent to:

i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, false);

The read messages' data isn't dumped.

Parameters
devTarget for the messages being sent. Its name will be printed in the log.
msgsArray of messages to dump.
num_msgsNumber of messages to dump.
addrAddress of the I2C target device.

◆ i2c_dump_msgs_rw()

void i2c_dump_msgs_rw ( const struct device dev,
const struct i2c_msg msgs,
uint8_t  num_msgs,
uint16_t  addr,
bool  dump_read 
)

#include <zephyr/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. Setting dump_read will dump the data for read messages too, which only makes sense when called after the messages have been processed.

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

D: I2C msg: testing, addr=56
D: W len=01: 06
D: W len=0e:
D: contents:
D: 00 01 02 03 04 05 06 07 |........
D: 08 09 0a 0b 0c 0d |......
D: W len=01: 0f
D: R len=01: 6c
irp nz macro MOVR cc d
Definition: asm-macro-32-bit-gnu.h:11
Parameters
devTarget for the messages being sent. Its name will be printed in the log.
msgsArray of messages to dump.
num_msgsNumber of messages to dump.
addrAddress of the I2C target device.
dump_readDump data from I2C reads, otherwise only writes have data dumped.

◆ i2c_get_config()

int i2c_get_config ( const struct device dev,
uint32_t dev_config 
)

#include <zephyr/drivers/i2c.h>

Get configuration of a host controller.

This routine provides a way to get current configuration. It is allowed to call the function before i2c_configure, because some I2C ports can be configured during init process. However, if the I2C port is not configured, i2c_get_config returns an error.

i2c_get_config can return cached config or probe hardware, but it has to be up to date with current configuration.

Parameters
devPointer to the device structure for the driver instance.
dev_configPointer to return bit-packed 32-bit value of the I2C controller configuration.
Return values
0If successful.
-EIOGeneral input / output error.
-ERANGEConfigured I2C frequency is invalid.
-ENOSYSIf get config is not implemented

◆ i2c_iodev_submit()

static void i2c_iodev_submit ( struct rtio_iodev_sqe iodev_sqe)
inlinestatic

#include <zephyr/drivers/i2c.h>

Submit request(s) to an I2C device with RTIO.

Parameters
iodev_sqePrepared submissions queue entry connected to an iodev defined by I2C_DT_IODEV_DEFINE.

◆ i2c_is_read_op()

static bool i2c_is_read_op ( struct i2c_msg msg)
inlinestatic

#include <zephyr/drivers/i2c.h>

Check if the current message is a read operation.

Parameters
msgThe message to check
Returns
true if the I2C message is sa read operation
false if the I2C message is a write operation

◆ i2c_is_ready_dt()

static bool i2c_is_ready_dt ( const struct i2c_dt_spec spec)
inlinestatic

#include <zephyr/drivers/i2c.h>

Validate that I2C bus is ready.

Parameters
specI2C specification from devicetree
Return values
trueif the I2C bus is ready for use.
falseif the I2C bus is not ready for use.

◆ i2c_read()

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

#include <zephyr/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 controller 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 <zephyr/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 <zephyr/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 controller 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 <zephyr/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 controller 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 <zephyr/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 <zephyr/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 controller 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 <zephyr/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 <zephyr/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 controller 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 <zephyr/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_rtio_copy()

struct rtio_sqe * i2c_rtio_copy ( struct rtio r,
struct rtio_iodev iodev,
const struct i2c_msg msgs,
uint8_t  num_msgs 
)

#include <zephyr/drivers/i2c.h>

Copy the i2c_msgs into a set of RTIO requests.

Parameters
rRTIO context
iodevRTIO IODev to target for the submissions
msgsArray of messages
num_msgsNumber of i2c msgs in array
Return values
sqeLast submission in the queue added
NULLNot enough memory in the context to copy the requests

◆ i2c_target_driver_register()

int i2c_target_driver_register ( const struct device dev)

#include <zephyr/drivers/i2c.h>

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

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

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

◆ i2c_target_driver_unregister()

int i2c_target_driver_unregister ( const struct device dev)

#include <zephyr/drivers/i2c.h>

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

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

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

◆ i2c_target_register()

static int i2c_target_register ( const struct device dev,
struct i2c_target_config cfg 
)
inlinestatic

#include <zephyr/drivers/i2c.h>

Registers the provided config as Target device of a controller.

Enable I2C target mode for the 'dev' I2C bus driver using the provided 'config' struct containing the functions and parameters to send bus events. The I2C target 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 target mode will be passed onto I2C target device driver via a set of callback functions provided in the 'callbacks' struct member.

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

Parameters
devPointer to the device structure for an I2C controller driver configured in target 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 target mode is not implemented

◆ i2c_target_unregister()

static int i2c_target_unregister ( const struct device dev,
struct i2c_target_config cfg 
)
inlinestatic

#include <zephyr/drivers/i2c.h>

Unregisters the provided config as Target device.

This routine disables I2C target 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 target 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 target 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 <zephyr/drivers/i2c.h>

Perform data transfer to another I2C device in controller 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.
The last message in the scatter/gather transaction implies a STOP whether or not it is explicitly set. This ensures the bus is in a good state for the next transaction which may be from a different call context.
Parameters
devPointer to the device structure for an I2C controller driver configured in controller 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_cb()

static int i2c_transfer_cb ( const struct device dev,
struct i2c_msg msgs,
uint8_t  num_msgs,
uint16_t  addr,
i2c_callback_t  cb,
void *  userdata 
)
inlinestatic

#include <zephyr/drivers/i2c.h>

Perform data transfer to another I2C device in controller mode.

This routine provides a generic interface to perform data transfer to another I2C device asynchronously with a callback completion.

See also
i2c_transfer()
Function properties (list may not be complete)
isr-ok
Parameters
devPointer to the device structure for an I2C controller driver configured in controller mode.
msgsArray of messages to transfer, must live until callback completes.
num_msgsNumber of messages to transfer.
addrAddress of the I2C target device.
cbFunction pointer for completion callback.
userdataUserdata passed to callback.
Return values
0If successful.
-EIOGeneral input / output error.
-ENOSYSIf transfer async is not implemented
-EWOULDBLOCKIf the device is temporarily busy doing another transfer

◆ i2c_transfer_cb_dt()

static int i2c_transfer_cb_dt ( const struct i2c_dt_spec spec,
struct i2c_msg msgs,
uint8_t  num_msgs,
i2c_callback_t  cb,
void *  userdata 
)
inlinestatic

#include <zephyr/drivers/i2c.h>

Perform data transfer to another I2C device in master mode asynchronously.

This is equivalent to:

i2c_transfer_cb(spec->bus, msgs, num_msgs, spec->addr, cb, userdata);
Parameters
specI2C specification from devicetree.
msgsArray of messages to transfer.
num_msgsNumber of messages to transfer.
cbFunction pointer for completion callback.
userdataUserdata passed to callback.
Returns
a value from i2c_transfer_cb()

◆ i2c_transfer_dt()

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

#include <zephyr/drivers/i2c.h>

Perform data transfer to another I2C device in controller 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_transfer_signal()

static int i2c_transfer_signal ( const struct device dev,
struct i2c_msg msgs,
uint8_t  num_msgs,
uint16_t  addr,
struct k_poll_signal sig 
)
inlinestatic

#include <zephyr/drivers/i2c.h>

Perform data transfer to another I2C device in controller mode.

This routine provides a generic interface to perform data transfer to another I2C device asynchronously with a k_poll_signal completion.

See also
i2c_transfer_cb()
Function properties (list may not be complete)
isr-ok
Parameters
devPointer to the device structure for an I2C controller driver configured in controller mode.
msgsArray of messages to transfer, must live until callback completes.
num_msgsNumber of messages to transfer.
addrAddress of the I2C target device.
sigSignal to notify of transfer completion.
Return values
0If successful.
-EIOGeneral input / output error.
-ENOSYSIf transfer async is not implemented
-EWOULDBLOCKIf the device is temporarily busy doing another transfer

◆ i2c_write()

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

#include <zephyr/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 controller 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 <zephyr/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 <zephyr/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 controller 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_cb()

static int i2c_write_read_cb ( const struct device dev,
struct i2c_msg msgs,
uint8_t  num_msgs,
uint16_t  addr,
const void *  write_buf,
size_t  num_write,
void *  read_buf,
size_t  num_read,
i2c_callback_t  cb,
void *  userdata 
)
inlinestatic

#include <zephyr/drivers/i2c.h>

Write then read data from an I2C device asynchronously.

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 but using i2c_transfer_cb. This helper function expects caller to pass a message pointer with 2 and only 2 size.

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 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
cbFunction pointer for completion callback.
userdataUserdata passed to callback.
Return values
0if successful
negativeon error.

◆ i2c_write_read_cb_dt()

static int i2c_write_read_cb_dt ( const struct i2c_dt_spec spec,
struct i2c_msg msgs,
uint8_t  num_msgs,
const void *  write_buf,
size_t  num_write,
void *  read_buf,
size_t  num_read,
i2c_callback_t  cb,
void *  userdata 
)
inlinestatic

#include <zephyr/drivers/i2c.h>

Write then read data from an I2C device asynchronously.

This is equivalent to:

i2c_write_read_cb(spec->bus, msgs, num_msgs,
               spec->addr, write_buf,
               num_write, read_buf, num_read);
Parameters
specI2C specification from devicetree.
msgsArray of messages to transfer.
num_msgsNumber of messages to transfer.
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
cbFunction pointer for completion callback.
userdataUserdata passed to callback.
Returns
a value from i2c_write_read_cb()

◆ 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 <zephyr/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()

◆ i2c_xfer_stats()

static void i2c_xfer_stats ( const struct device dev,
struct i2c_msg msgs,
uint8_t  num_msgs 
)
inlinestatic

#include <zephyr/drivers/i2c.h>

Updates the i2c stats for i2c transfers.

Parameters
devI2C device to update stats for
msgsArray of struct i2c_msg
num_msgsNumber of i2c_msgs

Variable Documentation

◆ i2c_iodev_api

const struct rtio_iodev_api i2c_iodev_api
extern