12#ifndef ZEPHYR_INCLUDE_DRIVERS_I2C_H_
13#define ZEPHYR_INCLUDE_DRIVERS_I2C_H_
34#define I2C_SPEED_STANDARD (0x1U)
37#define I2C_SPEED_FAST (0x2U)
40#define I2C_SPEED_FAST_PLUS (0x3U)
43#define I2C_SPEED_HIGH (0x4U)
46#define I2C_SPEED_ULTRA (0x5U)
48#define I2C_SPEED_SHIFT (1U)
49#define I2C_SPEED_SET(speed) (((speed) << I2C_SPEED_SHIFT) \
51#define I2C_SPEED_MASK (0x7U << I2C_SPEED_SHIFT)
52#define I2C_SPEED_GET(cfg) (((cfg) & I2C_SPEED_MASK) \
56#define I2C_ADDR_10_BITS BIT(0)
59#define I2C_MODE_MASTER BIT(4)
82#define I2C_DT_SPEC_GET(node_id) \
84 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
85 .addr = DT_REG_ADDR(node_id) \
96#define I2C_DT_SPEC_INST_GET(inst) \
97 I2C_DT_SPEC_GET(DT_DRV_INST(inst))
105#define I2C_MSG_WRITE (0U << 0U)
108#define I2C_MSG_READ BIT(0)
111#define I2C_MSG_RW_MASK BIT(0)
115#define I2C_MSG_STOP BIT(1)
124#define I2C_MSG_RESTART BIT(2)
129#define I2C_MSG_ADDR_10_BITS BIT(3)
164typedef int (*i2c_api_configure_t)(
const struct device *dev,
166typedef int (*i2c_api_full_io_t)(
const struct device *dev,
170typedef int (*i2c_api_slave_register_t)(
const struct device *dev,
172typedef int (*i2c_api_slave_unregister_t)(
const struct device *dev,
174typedef int (*i2c_api_recover_bus_t)(
const struct device *dev);
176__subsystem
struct i2c_driver_api {
177 i2c_api_configure_t configure;
178 i2c_api_full_io_t transfer;
179 i2c_api_slave_register_t slave_register;
180 i2c_api_slave_unregister_t slave_unregister;
181 i2c_api_recover_bus_t recover_bus;
184typedef int (*i2c_slave_api_register_t)(
const struct device *dev);
185typedef int (*i2c_slave_api_unregister_t)(
const struct device *dev);
187struct i2c_slave_driver_api {
188 i2c_slave_api_register_t driver_register;
189 i2c_slave_api_unregister_t driver_unregister;
197#define I2C_SLAVE_FLAGS_ADDR_10_BITS BIT(0)
349static inline int z_impl_i2c_configure(
const struct device *dev,
352 const struct i2c_driver_api *api =
353 (
const struct i2c_driver_api *)dev->
api;
355 return api->configure(dev, dev_config);
389static inline int z_impl_i2c_transfer(
const struct device *dev,
393 const struct i2c_driver_api *api =
394 (
const struct i2c_driver_api *)dev->
api;
396 return api->transfer(dev, msgs, num_msgs, addr);
432static inline int z_impl_i2c_recover_bus(
const struct device *dev)
434 const struct i2c_driver_api *api =
435 (
const struct i2c_driver_api *)dev->
api;
437 if (api->recover_bus == NULL) {
441 return api->recover_bus(dev);
471 const struct i2c_driver_api *api =
472 (
const struct i2c_driver_api *)dev->
api;
474 if (api->slave_register == NULL) {
478 return api->slave_register(dev, cfg);
500 const struct i2c_driver_api *api =
501 (
const struct i2c_driver_api *)dev->
api;
503 if (api->slave_unregister == NULL) {
507 return api->slave_unregister(dev, cfg);
525static inline int z_impl_i2c_slave_driver_register(
const struct device *dev)
527 const struct i2c_slave_driver_api *api =
528 (
const struct i2c_slave_driver_api *)dev->
api;
530 return api->driver_register(dev);
548static inline int z_impl_i2c_slave_driver_unregister(
const struct device *dev)
550 const struct i2c_slave_driver_api *api =
551 (
const struct i2c_slave_driver_api *)dev->
api;
553 return api->driver_unregister(dev);
669 const void *write_buf,
size_t num_write,
670 void *read_buf,
size_t num_read)
675 msg[0].
len = num_write;
679 msg[1].
len = num_read;
703 const void *write_buf,
size_t num_write,
704 void *read_buf,
size_t num_read)
707 write_buf, num_write,
736 &start_addr,
sizeof(start_addr),
760 start_addr,
buf, num_bytes);
792 msg[0].
buf = &start_addr;
797 msg[1].
len = num_bytes;
823 start_addr,
buf, num_bytes);
846 ®_addr,
sizeof(reg_addr),
847 value,
sizeof(*value));
891 uint8_t tx_buf[2] = {reg_addr, value};
893 return i2c_write(dev, tx_buf, 2, dev_addr);
947 new_value = (old_value & ~mask) | (value & mask);
948 if (new_value == old_value) {
974 reg_addr, mask, value);
1007#define I2C_DECLARE_CLIENT_CONFIG struct i2c_client_config i2c_client
1009#define I2C_CLIENT(_master, _addr) \
1011 .i2c_master = (_master), \
1012 .i2c_addr = (_addr), \
1015#define I2C_GET_MASTER(_conf) ((_conf)->i2c_client.i2c_master)
1016#define I2C_GET_ADDR(_conf) ((_conf)->i2c_client.i2c_addr)
1026#include <syscalls/i2c.h>
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.
Definition: i2c.h:817
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.
Definition: i2c.h:668
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.
static int i2c_slave_register(const struct device *dev, struct i2c_slave_config *cfg)
Registers the provided config as Slave device of a controller.
Definition: i2c.h:468
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.
Definition: i2c.h:574
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.
Definition: i2c.h:599
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.
Definition: i2c.h:257
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.
Definition: i2c.h:702
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.
Definition: i2c.h:729
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.
Definition: i2c.h:969
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.
Definition: i2c.h:644
int i2c_slave_driver_unregister(const struct device *dev)
Instructs the I2C Slave device to unregister itself from the I2C Controller.
int(* i2c_slave_write_requested_cb_t)(struct i2c_slave_config *config)
Function called when a write to the device is initiated.
Definition: i2c.h:214
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.
Definition: i2c.h:909
void i2c_dump_msgs(const char *name, const struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr)
Dump out an I2C message.
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.
Definition: i2c.h:887
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.
Definition: i2c.h:279
#define I2C_MSG_READ
Definition: i2c.h:108
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.
Definition: i2c.h:863
int i2c_configure(const struct device *dev, uint32_t dev_config)
Configure operation of a host controller.
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 d...
Definition: i2c.h:296
#define I2C_MSG_RESTART
Definition: i2c.h:124
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.
Definition: i2c.h:412
int i2c_recover_bus(const struct device *dev)
Recover the I2C bus.
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.
Definition: i2c.h:619
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.
Definition: i2c.h:754
#define I2C_MSG_STOP
Definition: i2c.h:115
int i2c_slave_driver_register(const struct device *dev)
Instructs the I2C Slave device to register itself to the I2C Controller.
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.
Definition: i2c.h:235
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.
Definition: i2c.h:934
static int i2c_slave_unregister(const struct device *dev, struct i2c_slave_config *cfg)
Unregisters the provided config as Slave device.
Definition: i2c.h:497
#define I2C_MSG_WRITE
Definition: i2c.h:105
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.
Definition: i2c.h:841
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.
Definition: i2c.h:784
#define ENOSYS
Definition: errno.h:83
struct _snode sys_snode_t
Definition: slist.h:33
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
Runtime device structure (in ROM) per driver instance.
Definition: device.h:367
const void * api
Definition: device.h:373
uint16_t i2c_addr
Definition: i2c.h:1004
char * i2c_master
Definition: i2c.h:1003
Complete I2C DT information.
Definition: i2c.h:67
const struct device * bus
Definition: i2c.h:68
uint16_t addr
Definition: i2c.h:69
One I2C Message.
Definition: i2c.h:145
uint8_t * buf
Definition: i2c.h:147
uint32_t len
Definition: i2c.h:150
uint8_t flags
Definition: i2c.h:153
Structure providing callbacks to be implemented for devices that supports the I2C slave API.
Definition: i2c.h:304
i2c_slave_write_requested_cb_t write_requested
Definition: i2c.h:305
i2c_slave_write_received_cb_t write_received
Definition: i2c.h:307
i2c_slave_read_processed_cb_t read_processed
Definition: i2c.h:308
i2c_slave_read_requested_cb_t read_requested
Definition: i2c.h:306
i2c_slave_stop_cb_t stop
Definition: i2c.h:309
Structure describing a device that supports the I2C slave API.
Definition: i2c.h:323
uint8_t flags
Definition: i2c.h:328
const struct i2c_slave_callbacks * callbacks
Definition: i2c.h:334
sys_snode_t node
Definition: i2c.h:325
uint16_t address
Definition: i2c.h:331