Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
i2c.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_I2C_H_
14#define ZEPHYR_INCLUDE_DRIVERS_I2C_H_
15
24
25#include <errno.h>
26
27#include <zephyr/types.h>
28#include <zephyr/device.h>
29#include <zephyr/kernel.h>
30#include <zephyr/sys/slist.h>
31#include <zephyr/rtio/rtio.h>
32#include <zephyr/toolchain.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/*
39 * The following #defines are used to configure the I2C controller.
40 */
41
43#define I2C_SPEED_STANDARD (0x1U)
44
46#define I2C_SPEED_FAST (0x2U)
47
49#define I2C_SPEED_FAST_PLUS (0x3U)
50
52#define I2C_SPEED_HIGH (0x4U)
53
55#define I2C_SPEED_ULTRA (0x5U)
56
58#define I2C_SPEED_DT (0x7U)
59
60#define I2C_SPEED_SHIFT (1U)
61#define I2C_SPEED_SET(speed) (((speed) << I2C_SPEED_SHIFT) \
62 & I2C_SPEED_MASK)
63#define I2C_SPEED_MASK (0x7U << I2C_SPEED_SHIFT) /* 3 bits */
64#define I2C_SPEED_GET(cfg) (((cfg) & I2C_SPEED_MASK) \
65 >> I2C_SPEED_SHIFT)
66
68#define I2C_ADDR_10_BITS BIT(0)
69
71#define I2C_MODE_CONTROLLER BIT(4)
72
73#if CONFIG_I2C_TRANSFER_TIMEOUT_SUPPORTED
74
76#if CONFIG_I2C_TRANSFER_TIMEOUT_MS
77#define I2C_TRANSFER_TIMEOUT K_MSEC(CONFIG_I2C_TRANSFER_TIMEOUT_MS)
78#else
79#define I2C_TRANSFER_TIMEOUT K_FOREVER
80#endif
81
91#define I2C_DT_INST_TRANSFER_TIMEOUT_MS(inst) \
92 DT_INST_PROP_OR(inst, zephyr_transfer_timeout_ms, CONFIG_I2C_TRANSFER_TIMEOUT_MS)
93
106#define I2C_DT_INST_TRANSFER_TIMEOUT(inst) \
107 SYS_TIMEOUT_MS_INIT(((I2C_DT_INST_TRANSFER_TIMEOUT_MS(inst) != 0) \
108 ? I2C_DT_INST_TRANSFER_TIMEOUT_MS(inst) \
109 : SYS_FOREVER_MS))
110
112#define BUILD_ASSERT_INVALID_I2C_TRANSFER_TIMEOUT() \
113 BUILD_ASSERT(CONFIG_I2C_TRANSFER_TIMEOUT_MS != 0, \
114 "infinite i2c transfer timeout not unsupported")
115
116#endif /* CONFIG_I2C_TRANSFER_TIMEOUT_SUPPORTED */
117
123 const struct device *bus;
126};
127
138#define I2C_DT_SPEC_GET_ON_I3C(node_id) \
139 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
140 .addr = DT_PROP_BY_IDX(node_id, reg, 0)
141
152#define I2C_DT_SPEC_GET_ON_I2C(node_id) \
153 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
154 .addr = DT_REG_ADDR(node_id)
155
166#define I2C_DT_SPEC_GET(node_id) \
167 { \
168 COND_CODE_1(DT_ON_BUS(node_id, i3c), \
169 (I2C_DT_SPEC_GET_ON_I3C(node_id)), \
170 (I2C_DT_SPEC_GET_ON_I2C(node_id))) \
171 }
172
181#define I2C_DT_SPEC_INST_GET(inst) \
182 I2C_DT_SPEC_GET(DT_DRV_INST(inst))
183
184
185/*
186 * I2C_MSG_* are I2C Message flags.
187 */
188
190#define I2C_MSG_WRITE (0U << 0U)
191
193#define I2C_MSG_READ BIT(0)
194
196#define I2C_MSG_RW_MASK BIT(0)
198
200#define I2C_MSG_STOP BIT(1)
201
209#define I2C_MSG_RESTART BIT(2)
210
214#define I2C_MSG_ADDR_10_BITS BIT(3)
215
240
248typedef void (*i2c_callback_t)(const struct device *dev, int result, void *data);
249
250struct i2c_target_config;
251
256
261typedef int (*i2c_api_configure_t)(const struct device *dev,
262 uint32_t dev_config);
263
268typedef int (*i2c_api_get_config_t)(const struct device *dev,
269 uint32_t *dev_config);
270
275typedef int (*i2c_api_full_io_t)(const struct device *dev,
276 struct i2c_msg *msgs,
277 uint8_t num_msgs,
278 uint16_t addr);
279
284typedef int (*i2c_api_target_register_t)(const struct device *dev,
285 struct i2c_target_config *cfg);
286
291typedef int (*i2c_api_target_unregister_t)(const struct device *dev,
292 struct i2c_target_config *cfg);
293
294#if defined(CONFIG_I2C_CALLBACK) || defined(__DOXYGEN__)
299typedef int (*i2c_api_transfer_cb_t)(const struct device *dev,
300 struct i2c_msg *msgs,
301 uint8_t num_msgs,
302 uint16_t addr,
304 void *userdata);
305#endif /* CONFIG_I2C_CALLBACK */
306
307#if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
312typedef void (*i2c_api_iodev_submit)(const struct device *dev,
313 struct rtio_iodev_sqe *iodev_sqe);
314#endif /* CONFIG_I2C_RTIO */
315
320typedef int (*i2c_api_recover_bus_t)(const struct device *dev);
321
353
358typedef int (*i2c_target_api_register_t)(const struct device *dev);
359
364typedef int (*i2c_target_api_unregister_t)(const struct device *dev);
365
375
377
379#define I2C_TARGET_FLAGS_ADDR_10_BITS BIT(0)
380
397 struct i2c_target_config *config);
398
417 struct i2c_target_config *config, uint8_t val);
418
439 struct i2c_target_config *config, uint8_t *val);
440
461 struct i2c_target_config *config, uint8_t *val);
462
463#if defined(CONFIG_I2C_TARGET_BUFFER_MODE) || defined(__DOXYGEN__)
478 struct i2c_target_config *config, uint8_t *ptr, uint32_t len);
479
503 struct i2c_target_config *config, uint8_t **ptr, uint32_t *len);
504#endif
505
520typedef int (*i2c_target_stop_cb_t)(struct i2c_target_config *config);
521
529 I2C_ERROR_TIMEOUT = 0, /* Timeout error */
530 I2C_ERROR_ARBITRATION, /* Bus arbitration size */
531 I2C_ERROR_SIZE, /* Bad frame size */
532 I2C_ERROR_DMA, /* DMA transfer error */
533 I2C_ERROR_GENERIC, /* Any other bus error */
534};
535
547typedef void (*i2c_target_error_cb_t)(struct i2c_target_config *config,
548 enum i2c_error_reason error_code);
549
582
607
616static inline bool i2c_is_ready_dt(const struct i2c_dt_spec *spec)
617{
618 /* Validate bus is ready */
619 return device_is_ready(spec->bus);
620}
621
629static inline bool i2c_is_read_op(const struct i2c_msg *msg)
630{
631 return (msg->flags & I2C_MSG_READ) == I2C_MSG_READ;
632}
633
641static inline bool i2c_is_stop_op(const struct i2c_msg *msg)
642{
643 return (msg->flags & I2C_MSG_STOP) == I2C_MSG_STOP;
644}
645
653static inline bool i2c_is_restart_op(const struct i2c_msg *msg)
654{
655 return (msg->flags & I2C_MSG_RESTART) == I2C_MSG_RESTART;
656}
657
684void i2c_dump_msgs_rw(const struct device *dev, const struct i2c_msg *msgs, uint8_t num_msgs,
685 uint16_t addr, bool dump_read);
686
701static inline void i2c_dump_msgs(const struct device *dev, const struct i2c_msg *msgs,
702 uint8_t num_msgs, uint16_t addr)
703{
704 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, false);
705}
706
707#if defined(CONFIG_I2C_STATS) || defined(__DOXYGEN__)
708
709#include <zephyr/stats/stats.h>
710
712
714STATS_SECT_ENTRY32(bytes_read)
715STATS_SECT_ENTRY32(bytes_written)
716STATS_SECT_ENTRY32(message_count)
717STATS_SECT_ENTRY32(transfer_call_count)
719
721STATS_NAME(i2c, bytes_read)
722STATS_NAME(i2c, bytes_written)
723STATS_NAME(i2c, message_count)
724STATS_NAME(i2c, transfer_call_count)
725STATS_NAME_END(i2c);
726
728
729
735 struct stats_i2c stats;
736};
737
745static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
746 uint8_t num_msgs)
747{
748 struct i2c_device_state *state =
750 uint32_t bytes_read = 0U;
751 uint32_t bytes_written = 0U;
752
753 STATS_INC(state->stats, transfer_call_count);
754 STATS_INCN(state->stats, message_count, num_msgs);
755 for (uint8_t i = 0U; i < num_msgs; i++) {
756 if (msgs[i].flags & I2C_MSG_READ) {
757 bytes_read += msgs[i].len;
758 } else {
759 bytes_written += msgs[i].len;
760 }
761 }
762 STATS_INCN(state->stats, bytes_read, bytes_read);
763 STATS_INCN(state->stats, bytes_written, bytes_written);
764}
765
767
771#define Z_I2C_DEVICE_STATE_DEFINE(dev_id) \
772 static struct i2c_device_state Z_DEVICE_STATE_NAME(dev_id) \
773 __attribute__((__section__(".z_devstate")))
774
781#define Z_I2C_INIT_FN(dev_id, init_fn) \
782 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
783 { \
784 struct i2c_device_state *state = \
785 CONTAINER_OF(dev->state, struct i2c_device_state, devstate); \
786 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 4, \
787 STATS_NAME_INIT_PARMS(i2c)); \
788 stats_register(dev->name, &(state->stats.s_hdr)); \
789 if (!is_null_no_warn(init_fn)) { \
790 return init_fn(dev); \
791 } \
792 \
793 return 0; \
794 }
795
797
827#define I2C_DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, \
828 data, config, level, prio, api, ...)\
829 Z_I2C_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
830 Z_I2C_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
831 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
832 DEVICE_DT_NAME(node_id), \
833 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
834 deinit_fn, Z_DEVICE_DT_FLAGS(node_id), pm, data,\
835 config, level, prio, api, \
836 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
837 __VA_ARGS__)
838
839#else /* CONFIG_I2C_STATS */
840
841static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
842 uint8_t num_msgs)
843{
844 ARG_UNUSED(dev);
845 ARG_UNUSED(msgs);
846 ARG_UNUSED(num_msgs);
847}
848
849#define I2C_DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, \
850 data, config, level, prio, api, ...)\
851 DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, data, \
852 config, level, prio, api, __VA_ARGS__)
853
854#endif /* CONFIG_I2C_STATS */
855
859#define I2C_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
860 prio, api, ...) \
861 I2C_DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, NULL, pm, data, \
862 config, level, prio, api, \
863 __VA_ARGS__)
864
873#define I2C_DEVICE_DT_INST_DEINIT_DEFINE(inst, ...) \
874 I2C_DEVICE_DT_DEINIT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
875
884#define I2C_DEVICE_DT_INST_DEFINE(inst, ...) \
885 I2C_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
886
897__syscall int i2c_configure(const struct device *dev, uint32_t dev_config);
898
899static inline int z_impl_i2c_configure(const struct device *dev,
900 uint32_t dev_config)
901{
902 return DEVICE_API_GET(i2c, dev)->configure(dev, dev_config);
903}
904
918static inline int i2c_configure_dt(const struct i2c_dt_spec *spec,
919 uint32_t dev_config)
920{
921 return i2c_configure(spec->bus, dev_config);
922}
923
944__syscall int i2c_get_config(const struct device *dev, uint32_t *dev_config);
945
946static inline int z_impl_i2c_get_config(const struct device *dev, uint32_t *dev_config)
947{
948 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
949
950 if (api->get_config == NULL) {
951 return -ENOSYS;
952 }
953
954 return api->get_config(dev, dev_config);
955}
956
988__syscall int i2c_transfer(const struct device *dev,
989 struct i2c_msg *msgs, uint8_t num_msgs,
990 uint16_t addr);
991
992static inline int z_impl_i2c_transfer(const struct device *dev,
993 struct i2c_msg *msgs, uint8_t num_msgs,
994 uint16_t addr)
995{
996 if (!num_msgs) {
997 return 0;
998 }
999
1000 if (!IS_ENABLED(CONFIG_I2C_ALLOW_NO_STOP_TRANSACTIONS)) {
1001 msgs[num_msgs - 1].flags |= I2C_MSG_STOP;
1002 }
1003
1004 int res = DEVICE_API_GET(i2c, dev)->transfer(dev, msgs, num_msgs, addr);
1005
1006 i2c_xfer_stats(dev, msgs, num_msgs);
1007
1008 if (IS_ENABLED(CONFIG_I2C_DUMP_MESSAGES)) {
1009 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, true);
1010 }
1011
1012 return res;
1013}
1014
1015#if defined(CONFIG_I2C_CALLBACK) || defined(__DOXYGEN__)
1016
1039static inline int i2c_transfer_cb(const struct device *dev,
1040 struct i2c_msg *msgs,
1041 uint8_t num_msgs,
1042 uint16_t addr,
1043 i2c_callback_t cb,
1044 void *userdata)
1045{
1046 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1047
1048 if (api->transfer_cb == NULL) {
1049 return -ENOSYS;
1050 }
1051
1052 if (!num_msgs) {
1053 cb(dev, 0, userdata);
1054 return 0;
1055 }
1056
1057 if (!IS_ENABLED(CONFIG_I2C_ALLOW_NO_STOP_TRANSACTIONS)) {
1058 msgs[num_msgs - 1].flags |= I2C_MSG_STOP;
1059 }
1060
1061 return api->transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
1062}
1063
1079static inline int i2c_transfer_cb_dt(const struct i2c_dt_spec *spec,
1080 struct i2c_msg *msgs,
1081 uint8_t num_msgs,
1082 i2c_callback_t cb,
1083 void *userdata)
1084{
1085 return i2c_transfer_cb(spec->bus, msgs, num_msgs, spec->addr, cb, userdata);
1086}
1087
1110static inline int i2c_write_read_cb(const struct device *dev, struct i2c_msg *msgs,
1111 uint8_t num_msgs, uint16_t addr, const void *write_buf,
1112 size_t num_write, void *read_buf, size_t num_read,
1113 i2c_callback_t cb, void *userdata)
1114{
1115 if ((msgs == NULL) || (num_msgs != 2)) {
1116 return -EINVAL;
1117 }
1118
1119 msgs[0].buf = (uint8_t *)write_buf;
1120 msgs[0].len = num_write;
1121 msgs[0].flags = I2C_MSG_WRITE;
1122
1123 msgs[1].buf = (uint8_t *)read_buf;
1124 msgs[1].len = num_read;
1126
1127 return i2c_transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
1128}
1129
1151static inline int i2c_write_read_cb_dt(const struct i2c_dt_spec *spec, struct i2c_msg *msgs,
1152 uint8_t num_msgs, const void *write_buf, size_t num_write,
1153 void *read_buf, size_t num_read, i2c_callback_t cb,
1154 void *userdata)
1155{
1156 return i2c_write_read_cb(spec->bus, msgs, num_msgs, spec->addr, write_buf, num_write,
1157 read_buf, num_read, cb, userdata);
1158}
1159
1160#if defined(CONFIG_POLL) || defined(__DOXYGEN__)
1161
1163void z_i2c_transfer_signal_cb(const struct device *dev, int result, void *userdata);
1165
1187static inline int i2c_transfer_signal(const struct device *dev,
1188 struct i2c_msg *msgs,
1189 uint8_t num_msgs,
1190 uint16_t addr,
1191 struct k_poll_signal *sig)
1192{
1193 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1194
1195 if (api->transfer_cb == NULL) {
1196 return -ENOSYS;
1197 }
1198
1199 return api->transfer_cb(dev, msgs, num_msgs, addr, z_i2c_transfer_signal_cb, sig);
1200}
1201
1202#endif /* CONFIG_POLL */
1203
1204#endif /* CONFIG_I2C_CALLBACK */
1205
1206
1207#if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
1208
1219void i2c_iodev_submit_fallback(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe);
1220
1227static inline void i2c_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
1228{
1229 const struct i2c_dt_spec *dt_spec = (const struct i2c_dt_spec *)iodev_sqe->sqe.iodev->data;
1230 const struct device *dev = dt_spec->bus;
1231 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1232
1233 if (api->iodev_submit == NULL) {
1234 rtio_iodev_sqe_err(iodev_sqe, -ENOSYS);
1235 return;
1236 }
1237 api->iodev_submit(dt_spec->bus, iodev_sqe);
1238}
1239
1240extern const struct rtio_iodev_api i2c_iodev_api;
1241
1242#define I2C_CAT2(x, y) x ## y
1243
1253#define I2C_DT_IODEV_DEFINE(name, node_id) \
1254 const struct i2c_dt_spec _i2c_dt_spec_##name = \
1255 I2C_DT_SPEC_GET(node_id); \
1256 RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&_i2c_dt_spec_##name)
1257
1267#define I2C_DT_INST_IODEV_DEFINE(name, inst) \
1268 I2C_DT_IODEV_DEFINE(name, DT_DRV_INST(inst))
1269
1280#define I2C_IODEV_DEFINE(name, _bus, _addr) \
1281 const struct i2c_dt_spec I2C_CAT2(_i2c_dt_spec_, name) = { \
1282 .bus = DEVICE_DT_GET(_bus), \
1283 .addr = _addr, \
1284 }; \
1285 RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&I2C_CAT2(_i2c_dt_spec_, name))
1286
1295static inline bool i2c_is_ready_iodev(const struct rtio_iodev *i2c_iodev)
1296{
1297 struct i2c_dt_spec *spec = (struct i2c_dt_spec *)i2c_iodev->data;
1298
1299 return i2c_is_ready_dt(spec);
1300}
1301
1314 struct rtio_iodev *iodev,
1315 const struct i2c_msg *msgs,
1316 uint8_t num_msgs);
1317
1330 uint8_t reg_addr, uint8_t data);
1331
1345 uint8_t start_addr, void *buf, size_t num_bytes);
1346
1347#endif /* CONFIG_I2C_RTIO */
1348
1362static inline int i2c_transfer_dt(const struct i2c_dt_spec *spec,
1363 struct i2c_msg *msgs, uint8_t num_msgs)
1364{
1365 return i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
1366}
1367
1380__syscall int i2c_recover_bus(const struct device *dev);
1381
1382static inline int z_impl_i2c_recover_bus(const struct device *dev)
1383{
1384 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1385
1386 if (api->recover_bus == NULL) {
1387 return -ENOSYS;
1388 }
1389
1390 return api->recover_bus(dev);
1391}
1392
1417static inline int i2c_target_register(const struct device *dev,
1418 struct i2c_target_config *cfg)
1419{
1420 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1421
1422 if (api->target_register == NULL) {
1423 return -ENOSYS;
1424 }
1425
1426 return api->target_register(dev, cfg);
1427}
1428
1445static inline int i2c_target_unregister(const struct device *dev,
1446 struct i2c_target_config *cfg)
1447{
1448 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1449
1450 if (api->target_unregister == NULL) {
1451 return -ENOSYS;
1452 }
1453
1454 return api->target_unregister(dev, cfg);
1455}
1456
1470__syscall int i2c_target_driver_register(const struct device *dev);
1471
1472static inline int z_impl_i2c_target_driver_register(const struct device *dev)
1473{
1474 return DEVICE_API_GET(i2c_target, dev)->driver_register(dev);
1475}
1476
1490__syscall int i2c_target_driver_unregister(const struct device *dev);
1491
1492static inline int z_impl_i2c_target_driver_unregister(const struct device *dev)
1493{
1494 return DEVICE_API_GET(i2c_target, dev)->driver_unregister(dev);
1495}
1496
1497/*
1498 * Derived i2c APIs -- all implemented in terms of i2c_transfer()
1499 */
1500
1515static inline int i2c_write(const struct device *dev, const uint8_t *buf,
1516 uint32_t num_bytes, uint16_t addr)
1517{
1518 struct i2c_msg msg;
1519
1520 msg.buf = (uint8_t *)buf;
1521 msg.len = num_bytes;
1523
1524 return i2c_transfer(dev, &msg, 1, addr);
1525}
1526
1540static inline int i2c_write_dt(const struct i2c_dt_spec *spec,
1541 const uint8_t *buf, uint32_t num_bytes)
1542{
1543 return i2c_write(spec->bus, buf, num_bytes, spec->addr);
1544}
1545
1560static inline int i2c_read(const struct device *dev, uint8_t *buf,
1561 uint32_t num_bytes, uint16_t addr)
1562{
1563 struct i2c_msg msg;
1564
1565 msg.buf = buf;
1566 msg.len = num_bytes;
1568
1569 return i2c_transfer(dev, &msg, 1, addr);
1570}
1571
1585static inline int i2c_read_dt(const struct i2c_dt_spec *spec,
1586 uint8_t *buf, uint32_t num_bytes)
1587{
1588 return i2c_read(spec->bus, buf, num_bytes, spec->addr);
1589}
1590
1608static inline int i2c_write_read(const struct device *dev, uint16_t addr,
1609 const void *write_buf, size_t num_write,
1610 void *read_buf, size_t num_read)
1611{
1612 struct i2c_msg msg[2];
1613
1614 msg[0].buf = (uint8_t *)write_buf;
1615 msg[0].len = num_write;
1616 msg[0].flags = I2C_MSG_WRITE;
1617
1618 msg[1].buf = (uint8_t *)read_buf;
1619 msg[1].len = num_read;
1621
1622 return i2c_transfer(dev, msg, 2, addr);
1623}
1624
1642static inline int i2c_write_read_dt(const struct i2c_dt_spec *spec,
1643 const void *write_buf, size_t num_write,
1644 void *read_buf, size_t num_read)
1645{
1646 return i2c_write_read(spec->bus, spec->addr,
1647 write_buf, num_write,
1648 read_buf, num_read);
1649}
1650
1669static inline int i2c_burst_read(const struct device *dev,
1670 uint16_t dev_addr,
1671 uint8_t start_addr,
1672 uint8_t *buf,
1673 uint32_t num_bytes)
1674{
1675 return i2c_write_read(dev, dev_addr,
1676 &start_addr, sizeof(start_addr),
1677 buf, num_bytes);
1678}
1679
1694static inline int i2c_burst_read_dt(const struct i2c_dt_spec *spec,
1695 uint8_t start_addr,
1696 uint8_t *buf,
1697 uint32_t num_bytes)
1698{
1699 return i2c_burst_read(spec->bus, spec->addr,
1700 start_addr, buf, num_bytes);
1701}
1702
1724static inline int i2c_burst_write(const struct device *dev,
1725 uint16_t dev_addr,
1726 uint8_t start_addr,
1727 const uint8_t *buf,
1728 uint32_t num_bytes)
1729{
1730 struct i2c_msg msg[2];
1731
1732 msg[0].buf = &start_addr;
1733 msg[0].len = 1U;
1734 msg[0].flags = I2C_MSG_WRITE;
1735
1736 msg[1].buf = (uint8_t *)buf;
1737 msg[1].len = num_bytes;
1738 msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
1739
1740 return i2c_transfer(dev, msg, 2, dev_addr);
1741}
1742
1757static inline int i2c_burst_write_dt(const struct i2c_dt_spec *spec,
1758 uint8_t start_addr,
1759 const uint8_t *buf,
1760 uint32_t num_bytes)
1761{
1762 return i2c_burst_write(spec->bus, spec->addr,
1763 start_addr, buf, num_bytes);
1764}
1765
1781static inline int i2c_reg_read_byte(const struct device *dev,
1782 uint16_t dev_addr,
1783 uint8_t reg_addr, uint8_t *value)
1784{
1785 return i2c_write_read(dev, dev_addr,
1786 &reg_addr, sizeof(reg_addr),
1787 value, sizeof(*value));
1788}
1789
1803static inline int i2c_reg_read_byte_dt(const struct i2c_dt_spec *spec,
1804 uint8_t reg_addr, uint8_t *value)
1805{
1806 return i2c_reg_read_byte(spec->bus, spec->addr, reg_addr, value);
1807}
1808
1827static inline int i2c_reg_write_byte(const struct device *dev,
1828 uint16_t dev_addr,
1829 uint8_t reg_addr, uint8_t value)
1830{
1831 uint8_t tx_buf[2] = {reg_addr, value};
1832
1833 return i2c_write(dev, tx_buf, 2, dev_addr);
1834}
1835
1849static inline int i2c_reg_write_byte_dt(const struct i2c_dt_spec *spec,
1850 uint8_t reg_addr, uint8_t value)
1851{
1852 return i2c_reg_write_byte(spec->bus, spec->addr, reg_addr, value);
1853}
1854
1874static inline int i2c_reg_update_byte(const struct device *dev,
1875 uint8_t dev_addr,
1876 uint8_t reg_addr, uint8_t mask,
1877 uint8_t value)
1878{
1879 uint8_t old_value, new_value;
1880 int rc;
1881
1882 rc = i2c_reg_read_byte(dev, dev_addr, reg_addr, &old_value);
1883 if (rc != 0) {
1884 return rc;
1885 }
1886
1887 new_value = (old_value & ~mask) | (value & mask);
1888 if (new_value == old_value) {
1889 return 0;
1890 }
1891
1892 return i2c_reg_write_byte(dev, dev_addr, reg_addr, new_value);
1893}
1894
1909static inline int i2c_reg_update_byte_dt(const struct i2c_dt_spec *spec,
1910 uint8_t reg_addr, uint8_t mask,
1911 uint8_t value)
1912{
1913 return i2c_reg_update_byte(spec->bus, spec->addr,
1914 reg_addr, mask, value);
1915}
1916
1917#ifdef __cplusplus
1918}
1919#endif
1920
1924
1925#include <zephyr/syscalls/i2c.h>
1926
1927#endif /* ZEPHYR_INCLUDE_DRIVERS_I2C_H_ */
workaround assembler barfing for ST r
Definition asm-macro-32-bit-gnu.h:27
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1449
System error numbers.
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
void(* i2c_api_iodev_submit)(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
Callback API to submit an RTIO request to the I2C controller.
Definition i2c.h:312
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.
Definition i2c.h:1079
int i2c_target_driver_unregister(const struct device *dev)
Instructs the I2C Target device to unregister itself from the I2C Controller.
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:1757
static int i2c_target_unregister(const struct device *dev, struct i2c_target_config *cfg)
Unregisters the provided config as Target device.
Definition i2c.h:1445
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:1608
static bool i2c_is_restart_op(const struct i2c_msg *msg)
Check if the current message includes a restart.
Definition i2c.h:653
int i2c_target_driver_register(const struct device *dev)
Instructs the I2C Target device to register itself to the I2C Controller.
static void i2c_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
Submit request(s) to an I2C device with RTIO.
Definition i2c.h:1227
int(* i2c_api_configure_t)(const struct device *dev, uint32_t dev_config)
@def_driverbackendgroup{I2C,i2c_interface}
Definition i2c.h:261
struct rtio_sqe * i2c_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.
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.
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.
Definition i2c.h:438
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_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:1515
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:1540
int(* i2c_api_full_io_t)(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr)
Callback API to transfer messages on the I2C bus in controller mode.
Definition i2c.h:275
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:1642
int(* i2c_api_transfer_cb_t)(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr, i2c_callback_t cb, void *userdata)
Callback API to transfer messages on the I2C bus asynchronously.
Definition i2c.h:299
void(* i2c_target_error_cb_t)(struct i2c_target_config *config, enum i2c_error_reason error_code)
Function called when an error is detected on the I2C bus while acting as a target.
Definition i2c.h:547
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.
Definition i2c.h:1110
static bool i2c_is_stop_op(const struct i2c_msg *msg)
Check if the current message includes a stop.
Definition i2c.h:641
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 d...
Definition i2c.h:520
static bool i2c_is_ready_iodev(const struct rtio_iodev *i2c_iodev)
Validate that I2C bus is ready.
Definition i2c.h:1295
i2c_error_reason
I2C error reasons.
Definition i2c.h:528
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:1669
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.
Definition i2c.h:460
int(* i2c_target_write_requested_cb_t)(struct i2c_target_config *config)
Function called when a write to the device is initiated.
Definition i2c.h:396
void i2c_iodev_submit_fallback(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
Fallback submit implementation.
int(* i2c_target_api_unregister_t)(const struct device *dev)
Callback API to instruct the I2C target device to unregister itself.
Definition i2c.h:364
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:1909
void(* i2c_callback_t)(const struct device *dev, int result, void *data)
I2C callback for asynchronous transfer requests.
Definition i2c.h:248
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:1585
int(* i2c_api_target_unregister_t)(const struct device *dev, struct i2c_target_config *cfg)
Callback API to unregister a target device from the I2C controller.
Definition i2c.h:291
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.
Definition i2c.h:416
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:1849
int i2c_get_config(const struct device *dev, uint32_t *dev_config)
Get configuration of a host controller.
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:1827
#define I2C_MSG_READ
Read message from I2C bus.
Definition i2c.h:193
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:1803
int i2c_configure(const struct device *dev, uint32_t dev_config)
Configure operation of a host controller.
static int i2c_configure_dt(const struct i2c_dt_spec *spec, uint32_t dev_config)
Configure operation of a host controller.
Definition i2c.h:918
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.
Definition i2c.h:1151
int(* i2c_target_api_register_t)(const struct device *dev)
Callback API to instruct the I2C target device to register itself.
Definition i2c.h:358
#define I2C_MSG_RESTART
RESTART I2C transaction for this message.
Definition i2c.h:209
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.
Definition i2c.h:1362
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:1560
static bool i2c_is_read_op(const struct i2c_msg *msg)
Check if the current message is a read operation.
Definition i2c.h:629
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:1694
static int i2c_target_register(const struct device *dev, struct i2c_target_config *cfg)
Registers the provided config as Target device of a controller.
Definition i2c.h:1417
int(* i2c_target_buf_read_requested_cb_t)(struct i2c_target_config *config, uint8_t **ptr, uint32_t *len)
Function called when a read from the device is initiated.
Definition i2c.h:502
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.
Definition i2c.h:1039
#define I2C_MSG_STOP
Send STOP after this message.
Definition i2c.h:200
int(* i2c_api_target_register_t)(const struct device *dev, struct i2c_target_config *cfg)
Callback API to register a target device on the I2C controller.
Definition i2c.h:284
static void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs)
Updates the i2c stats for i2c transfers.
Definition i2c.h:745
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.
Definition i2c.h:701
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.
Definition i2c.h:1187
void(* i2c_target_buf_write_received_cb_t)(struct i2c_target_config *config, uint8_t *ptr, uint32_t len)
Function called when a write to the device is completed.
Definition i2c.h:477
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:1874
int(* i2c_api_recover_bus_t)(const struct device *dev)
Callback API to recover the I2C bus.
Definition i2c.h:320
int(* i2c_api_get_config_t)(const struct device *dev, uint32_t *dev_config)
Callback API to get the current configuration of the I2C controller.
Definition i2c.h:268
const struct rtio_iodev_api i2c_iodev_api
struct rtio_sqe * i2c_rtio_copy_reg_write_byte(struct rtio *r, struct rtio_iodev *iodev, uint8_t reg_addr, uint8_t data)
Copy the register address and data to a SQE.
#define I2C_MSG_WRITE
Write message to I2C bus.
Definition i2c.h:190
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:1781
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:1724
struct rtio_sqe * i2c_rtio_copy_reg_burst_read(struct rtio *r, struct rtio_iodev *iodev, uint8_t start_addr, void *buf, size_t num_bytes)
acquire and configure a i2c burst read transmission
static bool i2c_is_ready_dt(const struct i2c_dt_spec *spec)
Validate that I2C bus is ready.
Definition i2c.h:616
@ I2C_ERROR_DMA
Definition i2c.h:532
@ I2C_ERROR_SIZE
Definition i2c.h:531
@ I2C_ERROR_GENERIC
Definition i2c.h:533
@ I2C_ERROR_TIMEOUT
Definition i2c.h:529
@ I2C_ERROR_ARBITRATION
Definition i2c.h:530
static void rtio_iodev_sqe_err(struct rtio_iodev_sqe *iodev_sqe, int result)
Inform the executor of a submissions completion with error.
Definition rtio.h:673
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:44
#define STATS_SECT_END
Ends a stats group struct definition.
Definition stats.h:169
#define STATS_NAME(sectname__, entry__)
Add an entry name to a statistics entry name map.
Definition stats.h:519
#define STATS_SECT_ENTRY32(var__)
Declares a 32-bit stat entry inside a group struct.
Definition stats.h:204
#define STATS_NAME_START(sectname__)
Begin a statistics entry name map.
Definition stats.h:510
#define STATS_NAME_END(sectname__)
End a statistics entry name map.
Definition stats.h:527
#define STATS_INC(group__, var__)
Increments a statistic entry.
Definition stats.h:255
#define STATS_INCN(group__, var__, n__)
Increases a statistic entry by the specified amount.
Definition stats.h:243
#define STATS_SECT_START(group__)
Begins a stats group struct definition.
Definition stats.h:181
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:154
#define CONTAINER_OF(ptr, type, field)
Get a pointer to a structure containing the element.
Definition util.h:281
#define EINVAL
Invalid argument.
Definition errno.h:61
#define ENOSYS
Function not implemented.
Definition errno.h:83
#define NULL
Definition iar_missing_defs.h:20
Public kernel APIs.
flags
Definition parser.h:97
state
Definition parser_state.h:29
Real-Time IO device API for moving bytes with low effort.
Header file for the single-linked list API.
Statistics.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device dynamic structure (in RAM) per driver instance.
Definition device.h:458
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
void * data
Address of the device instance private data.
Definition device.h:523
struct device_state * state
Address of the common device state.
Definition device.h:521
I2C specific device state which allows for i2c device class specific additions.
Definition i2c.h:733
struct stats_i2c stats
Definition i2c.h:735
struct device_state devstate
Definition i2c.h:734
@driver_ops{I2C}
Definition i2c.h:325
i2c_api_target_unregister_t target_unregister
@driver_ops_optional Unregisters the provided config as Target device.
Definition i2c.h:335
i2c_api_recover_bus_t recover_bus
@driver_ops_optional Recover the I2C bus.
Definition i2c.h:351
i2c_api_full_io_t transfer
@driver_ops_mandatory Perform data transfer to another I2C device in controller mode.
Definition i2c.h:331
i2c_api_iodev_submit iodev_submit
@driver_ops_optional Submit request(s) to an I2C device with RTIO.
Definition i2c.h:348
i2c_api_get_config_t get_config
@driver_ops_optional Get configuration of a host controller.
Definition i2c.h:329
i2c_api_configure_t configure
@driver_ops_mandatory Configure operation of a host controller.
Definition i2c.h:327
i2c_api_target_register_t target_register
@driver_ops_optional Registers the provided config as Target device of a controller.
Definition i2c.h:333
i2c_api_transfer_cb_t transfer_cb
@driver_ops_optional Perform data transfer to another I2C device in controller mode.
Definition i2c.h:341
Complete I2C DT information.
Definition i2c.h:121
const struct device * bus
I2C bus.
Definition i2c.h:123
uint16_t addr
Target address.
Definition i2c.h:125
One I2C Message.
Definition i2c.h:230
uint8_t * buf
Data buffer in bytes.
Definition i2c.h:232
uint32_t len
Length of buffer in bytes.
Definition i2c.h:235
uint8_t flags
Flags for this message.
Definition i2c.h:238
Structure providing callbacks to be implemented for devices that supports the I2C target API.
Definition i2c.h:556
i2c_target_read_requested_cb_t read_requested
Function called when a read from the device is initiated.
Definition i2c.h:560
i2c_target_write_received_cb_t write_received
Function called when a write to the device is continued.
Definition i2c.h:562
i2c_target_read_processed_cb_t read_processed
Function called when a read from the device is continued.
Definition i2c.h:564
i2c_target_write_requested_cb_t write_requested
Function called when a write to the device is initiated.
Definition i2c.h:558
i2c_target_stop_cb_t stop
Function called when a stop condition is observed after a start condition addressed to a particular d...
Definition i2c.h:578
i2c_target_buf_read_requested_cb_t buf_read_requested
Definition i2c.h:575
i2c_target_error_cb_t error
Function called when an error is detected on the I2C bus while acting as a target.
Definition i2c.h:580
i2c_target_buf_write_received_cb_t buf_write_received
Definition i2c.h:570
Structure describing a device that supports the I2C target API.
Definition i2c.h:594
uint8_t flags
Flags for the target device defined by I2C_TARGET_FLAGS_* constants.
Definition i2c.h:599
uint16_t address
Address for this target device.
Definition i2c.h:602
sys_snode_t node
Private, do not modify.
Definition i2c.h:596
const struct i2c_target_callbacks * callbacks
Callback functions.
Definition i2c.h:605
@driver_ops{I2C Target}
Definition i2c.h:369
i2c_target_api_unregister_t driver_unregister
@driver_ops_mandatory Instructs the I2C Target device to unregister itself from the I2C Controller.
Definition i2c.h:373
i2c_target_api_register_t driver_register
@driver_ops_mandatory Instructs the I2C Target device to register itself to the I2C Controller.
Definition i2c.h:371
Poll signal object.
Definition kernel.h:6862
API that an RTIO IO device should implement.
Definition iodev.h:33
IO device submission queue entry.
Definition sqe.h:394
struct rtio_sqe sqe
Definition sqe.h:395
An IO device with a function table for submitting requests.
Definition iodev.h:48
void * data
Definition iodev.h:53
A submission queue event.
Definition sqe.h:304
const struct rtio_iodev * iodev
Device to operation on.
Definition sqe.h:313
const uint8_t * buf
Buffer to write from.
Definition sqe.h:329
An RTIO context containing what can be viewed as a pair of queues.
Definition rtio.h:70
Macros to abstract toolchain specific capabilities.