Zephyr API Documentation 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
61#define I2C_SPEED_SHIFT (1U)
62#define I2C_SPEED_MASK (0x7U << I2C_SPEED_SHIFT) /* 3 bits */
64
70#define I2C_SPEED_SET(speed) (((speed) << I2C_SPEED_SHIFT) \
71 & I2C_SPEED_MASK)
72
78#define I2C_SPEED_GET(cfg) (((cfg) & I2C_SPEED_MASK) \
79 >> I2C_SPEED_SHIFT)
80
82#define I2C_ADDR_10_BITS BIT(0)
83
85#define I2C_MODE_CONTROLLER BIT(4)
86
87#if CONFIG_I2C_TRANSFER_TIMEOUT_SUPPORTED
88
90#if CONFIG_I2C_TRANSFER_TIMEOUT_MS
91#define I2C_TRANSFER_TIMEOUT K_MSEC(CONFIG_I2C_TRANSFER_TIMEOUT_MS)
92#else
93#define I2C_TRANSFER_TIMEOUT K_FOREVER
94#endif
95
105#define I2C_DT_INST_TRANSFER_TIMEOUT_MS(inst) \
106 DT_INST_PROP_OR(inst, zephyr_transfer_timeout_ms, CONFIG_I2C_TRANSFER_TIMEOUT_MS)
107
120#define I2C_DT_INST_TRANSFER_TIMEOUT(inst) \
121 SYS_TIMEOUT_MS_INIT(((I2C_DT_INST_TRANSFER_TIMEOUT_MS(inst) != 0) \
122 ? I2C_DT_INST_TRANSFER_TIMEOUT_MS(inst) \
123 : SYS_FOREVER_MS))
124
126#define BUILD_ASSERT_INVALID_I2C_TRANSFER_TIMEOUT() \
127 BUILD_ASSERT(CONFIG_I2C_TRANSFER_TIMEOUT_MS != 0, \
128 "infinite i2c transfer timeout not unsupported")
129
130#endif /* CONFIG_I2C_TRANSFER_TIMEOUT_SUPPORTED */
131
137 const struct device *bus;
140};
141
152#define I2C_DT_SPEC_GET_ON_I3C(node_id) \
153 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
154 .addr = DT_PROP_BY_IDX(node_id, reg, 0)
155
166#define I2C_DT_SPEC_GET_ON_I2C(node_id) \
167 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
168 .addr = DT_REG_ADDR(node_id)
169
180#define I2C_DT_SPEC_GET(node_id) \
181 { \
182 COND_CODE_1(DT_ON_BUS(node_id, i3c), \
183 (I2C_DT_SPEC_GET_ON_I3C(node_id)), \
184 (I2C_DT_SPEC_GET_ON_I2C(node_id))) \
185 }
186
195#define I2C_DT_SPEC_INST_GET(inst) \
196 I2C_DT_SPEC_GET(DT_DRV_INST(inst))
197
198
199/*
200 * I2C_MSG_* are I2C Message flags.
201 */
202
204#define I2C_MSG_WRITE (0U << 0U)
205
207#define I2C_MSG_READ BIT(0)
208
210#define I2C_MSG_RW_MASK BIT(0)
212
214#define I2C_MSG_STOP BIT(1)
215
223#define I2C_MSG_RESTART BIT(2)
224
228#define I2C_MSG_ADDR_10_BITS BIT(3)
229
254
262typedef void (*i2c_callback_t)(const struct device *dev, int result, void *data);
263
264struct i2c_target_config;
265
270
275typedef int (*i2c_api_configure_t)(const struct device *dev,
276 uint32_t dev_config);
277
282typedef int (*i2c_api_get_config_t)(const struct device *dev,
283 uint32_t *dev_config);
284
289typedef int (*i2c_api_full_io_t)(const struct device *dev,
290 struct i2c_msg *msgs,
291 uint8_t num_msgs,
292 uint16_t addr);
293
298typedef int (*i2c_api_target_register_t)(const struct device *dev,
299 struct i2c_target_config *cfg);
300
305typedef int (*i2c_api_target_unregister_t)(const struct device *dev,
306 struct i2c_target_config *cfg);
307
308#if defined(CONFIG_I2C_CALLBACK) || defined(__DOXYGEN__)
313typedef int (*i2c_api_transfer_cb_t)(const struct device *dev,
314 struct i2c_msg *msgs,
315 uint8_t num_msgs,
316 uint16_t addr,
318 void *userdata);
319#endif /* CONFIG_I2C_CALLBACK */
320
321#if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
326typedef void (*i2c_api_iodev_submit)(const struct device *dev,
327 struct rtio_iodev_sqe *iodev_sqe);
328#endif /* CONFIG_I2C_RTIO */
329
334typedef int (*i2c_api_recover_bus_t)(const struct device *dev);
335
367
372typedef int (*i2c_target_api_register_t)(const struct device *dev);
373
378typedef int (*i2c_target_api_unregister_t)(const struct device *dev);
379
389
391
393#define I2C_TARGET_FLAGS_ADDR_10_BITS BIT(0)
394
411 struct i2c_target_config *config);
412
431 struct i2c_target_config *config, uint8_t val);
432
453 struct i2c_target_config *config, uint8_t *val);
454
475 struct i2c_target_config *config, uint8_t *val);
476
477#if defined(CONFIG_I2C_TARGET_BUFFER_MODE) || defined(__DOXYGEN__)
492 struct i2c_target_config *config, uint8_t *ptr, uint32_t len);
493
517 struct i2c_target_config *config, uint8_t **ptr, uint32_t *len);
518#endif
519
534typedef int (*i2c_target_stop_cb_t)(struct i2c_target_config *config);
535
549
561typedef void (*i2c_target_error_cb_t)(struct i2c_target_config *config,
562 enum i2c_error_reason error_code);
563
596
621
630static inline bool i2c_is_ready_dt(const struct i2c_dt_spec *spec)
631{
632 /* Validate bus is ready */
633 return device_is_ready(spec->bus);
634}
635
643static inline bool i2c_is_read_op(const struct i2c_msg *msg)
644{
645 return (msg->flags & I2C_MSG_READ) == I2C_MSG_READ;
646}
647
655static inline bool i2c_is_stop_op(const struct i2c_msg *msg)
656{
657 return (msg->flags & I2C_MSG_STOP) == I2C_MSG_STOP;
658}
659
667static inline bool i2c_is_restart_op(const struct i2c_msg *msg)
668{
669 return (msg->flags & I2C_MSG_RESTART) == I2C_MSG_RESTART;
670}
671
698void i2c_dump_msgs_rw(const struct device *dev, const struct i2c_msg *msgs, uint8_t num_msgs,
699 uint16_t addr, bool dump_read);
700
715static inline void i2c_dump_msgs(const struct device *dev, const struct i2c_msg *msgs,
716 uint8_t num_msgs, uint16_t addr)
717{
718 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, false);
719}
720
721#if defined(CONFIG_I2C_STATS) || defined(__DOXYGEN__)
722
723#include <zephyr/stats/stats.h>
724
726
728STATS_SECT_ENTRY32(bytes_read)
729STATS_SECT_ENTRY32(bytes_written)
730STATS_SECT_ENTRY32(message_count)
731STATS_SECT_ENTRY32(transfer_call_count)
733
735STATS_NAME(i2c, bytes_read)
736STATS_NAME(i2c, bytes_written)
737STATS_NAME(i2c, message_count)
738STATS_NAME(i2c, transfer_call_count)
739STATS_NAME_END(i2c);
740
742
743
751 struct stats_i2c stats;
752};
753
761static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
762 uint8_t num_msgs)
763{
764 struct i2c_device_state *state =
766 uint32_t bytes_read = 0U;
767 uint32_t bytes_written = 0U;
768
769 STATS_INC(state->stats, transfer_call_count);
770 STATS_INCN(state->stats, message_count, num_msgs);
771 for (uint8_t i = 0U; i < num_msgs; i++) {
772 if (msgs[i].flags & I2C_MSG_READ) {
773 bytes_read += msgs[i].len;
774 } else {
775 bytes_written += msgs[i].len;
776 }
777 }
778 STATS_INCN(state->stats, bytes_read, bytes_read);
779 STATS_INCN(state->stats, bytes_written, bytes_written);
780}
781
783
787#define Z_I2C_DEVICE_STATE_DEFINE(dev_id) \
788 static struct i2c_device_state Z_DEVICE_STATE_NAME(dev_id) \
789 __attribute__((__section__(".z_devstate")))
790
797#define Z_I2C_INIT_FN(dev_id, init_fn) \
798 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
799 { \
800 struct i2c_device_state *state = \
801 CONTAINER_OF(dev->state, struct i2c_device_state, devstate); \
802 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 4, \
803 STATS_NAME_INIT_PARMS(i2c)); \
804 stats_register(dev->name, &(state->stats.s_hdr)); \
805 if (!is_null_no_warn(init_fn)) { \
806 return init_fn(dev); \
807 } \
808 \
809 return 0; \
810 }
811
813
843#define I2C_DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, \
844 data, config, level, prio, api, ...)\
845 Z_I2C_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
846 Z_I2C_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
847 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
848 DEVICE_DT_NAME(node_id), \
849 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
850 deinit_fn, Z_DEVICE_DT_FLAGS(node_id), pm, data,\
851 config, level, prio, api, \
852 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
853 __VA_ARGS__)
854
855#else /* CONFIG_I2C_STATS */
856
857static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
858 uint8_t num_msgs)
859{
860 ARG_UNUSED(dev);
861 ARG_UNUSED(msgs);
862 ARG_UNUSED(num_msgs);
863}
864
865#define I2C_DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, \
866 data, config, level, prio, api, ...)\
867 DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, data, \
868 config, level, prio, api, __VA_ARGS__)
869
870#endif /* CONFIG_I2C_STATS */
871
875#define I2C_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
876 prio, api, ...) \
877 I2C_DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, NULL, pm, data, \
878 config, level, prio, api, \
879 __VA_ARGS__)
880
889#define I2C_DEVICE_DT_INST_DEINIT_DEFINE(inst, ...) \
890 I2C_DEVICE_DT_DEINIT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
891
900#define I2C_DEVICE_DT_INST_DEFINE(inst, ...) \
901 I2C_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
902
913__syscall int i2c_configure(const struct device *dev, uint32_t dev_config);
914
915static inline int z_impl_i2c_configure(const struct device *dev,
916 uint32_t dev_config)
917{
918 return DEVICE_API_GET(i2c, dev)->configure(dev, dev_config);
919}
920
934static inline int i2c_configure_dt(const struct i2c_dt_spec *spec,
935 uint32_t dev_config)
936{
937 return i2c_configure(spec->bus, dev_config);
938}
939
960__syscall int i2c_get_config(const struct device *dev, uint32_t *dev_config);
961
962static inline int z_impl_i2c_get_config(const struct device *dev, uint32_t *dev_config)
963{
964 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
965
966 if (api->get_config == NULL) {
967 return -ENOSYS;
968 }
969
970 return api->get_config(dev, dev_config);
971}
972
1004__syscall int i2c_transfer(const struct device *dev,
1005 struct i2c_msg *msgs, uint8_t num_msgs,
1006 uint16_t addr);
1007
1008static inline int z_impl_i2c_transfer(const struct device *dev,
1009 struct i2c_msg *msgs, uint8_t num_msgs,
1010 uint16_t addr)
1011{
1012 if (!num_msgs) {
1013 return 0;
1014 }
1015
1016 if (!IS_ENABLED(CONFIG_I2C_ALLOW_NO_STOP_TRANSACTIONS)) {
1017 msgs[num_msgs - 1].flags |= I2C_MSG_STOP;
1018 }
1019
1020 int res = DEVICE_API_GET(i2c, dev)->transfer(dev, msgs, num_msgs, addr);
1021
1022 i2c_xfer_stats(dev, msgs, num_msgs);
1023
1024 if (IS_ENABLED(CONFIG_I2C_DUMP_MESSAGES)) {
1025 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, true);
1026 }
1027
1028 return res;
1029}
1030
1031#if defined(CONFIG_I2C_CALLBACK) || defined(__DOXYGEN__)
1032
1055static inline int i2c_transfer_cb(const struct device *dev,
1056 struct i2c_msg *msgs,
1057 uint8_t num_msgs,
1058 uint16_t addr,
1059 i2c_callback_t cb,
1060 void *userdata)
1061{
1062 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1063
1064 if (api->transfer_cb == NULL) {
1065 return -ENOSYS;
1066 }
1067
1068 if (!num_msgs) {
1069 cb(dev, 0, userdata);
1070 return 0;
1071 }
1072
1073 if (!IS_ENABLED(CONFIG_I2C_ALLOW_NO_STOP_TRANSACTIONS)) {
1074 msgs[num_msgs - 1].flags |= I2C_MSG_STOP;
1075 }
1076
1077 return api->transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
1078}
1079
1095static inline int i2c_transfer_cb_dt(const struct i2c_dt_spec *spec,
1096 struct i2c_msg *msgs,
1097 uint8_t num_msgs,
1098 i2c_callback_t cb,
1099 void *userdata)
1100{
1101 return i2c_transfer_cb(spec->bus, msgs, num_msgs, spec->addr, cb, userdata);
1102}
1103
1126static inline int i2c_write_read_cb(const struct device *dev, struct i2c_msg *msgs,
1127 uint8_t num_msgs, uint16_t addr, const void *write_buf,
1128 size_t num_write, void *read_buf, size_t num_read,
1129 i2c_callback_t cb, void *userdata)
1130{
1131 if ((msgs == NULL) || (num_msgs != 2)) {
1132 return -EINVAL;
1133 }
1134
1135 msgs[0].buf = (uint8_t *)write_buf;
1136 msgs[0].len = num_write;
1137 msgs[0].flags = I2C_MSG_WRITE;
1138
1139 msgs[1].buf = (uint8_t *)read_buf;
1140 msgs[1].len = num_read;
1142
1143 return i2c_transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
1144}
1145
1167static inline int i2c_write_read_cb_dt(const struct i2c_dt_spec *spec, struct i2c_msg *msgs,
1168 uint8_t num_msgs, const void *write_buf, size_t num_write,
1169 void *read_buf, size_t num_read, i2c_callback_t cb,
1170 void *userdata)
1171{
1172 return i2c_write_read_cb(spec->bus, msgs, num_msgs, spec->addr, write_buf, num_write,
1173 read_buf, num_read, cb, userdata);
1174}
1175
1176#if defined(CONFIG_POLL) || defined(__DOXYGEN__)
1177
1179void z_i2c_transfer_signal_cb(const struct device *dev, int result, void *userdata);
1181
1203static inline int i2c_transfer_signal(const struct device *dev,
1204 struct i2c_msg *msgs,
1205 uint8_t num_msgs,
1206 uint16_t addr,
1207 struct k_poll_signal *sig)
1208{
1209 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1210
1211 if (api->transfer_cb == NULL) {
1212 return -ENOSYS;
1213 }
1214
1215 return api->transfer_cb(dev, msgs, num_msgs, addr, z_i2c_transfer_signal_cb, sig);
1216}
1217
1218#endif /* CONFIG_POLL */
1219
1220#endif /* CONFIG_I2C_CALLBACK */
1221
1222
1223#if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
1224
1235void i2c_iodev_submit_fallback(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe);
1236
1243static inline void i2c_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
1244{
1245 const struct i2c_dt_spec *dt_spec = (const struct i2c_dt_spec *)iodev_sqe->sqe.iodev->data;
1246 const struct device *dev = dt_spec->bus;
1247 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1248
1249 if (api->iodev_submit == NULL) {
1250 rtio_iodev_sqe_err(iodev_sqe, -ENOSYS);
1251 return;
1252 }
1253 api->iodev_submit(dt_spec->bus, iodev_sqe);
1254}
1255
1257
1258extern const struct rtio_iodev_api i2c_iodev_api;
1259
1260#define I2C_CAT2(x, y) x ## y
1261
1263
1273#define I2C_DT_IODEV_DEFINE(name, node_id) \
1274 const struct i2c_dt_spec _i2c_dt_spec_##name = \
1275 I2C_DT_SPEC_GET(node_id); \
1276 RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&_i2c_dt_spec_##name)
1277
1287#define I2C_DT_INST_IODEV_DEFINE(name, inst) \
1288 I2C_DT_IODEV_DEFINE(name, DT_DRV_INST(inst))
1289
1300#define I2C_IODEV_DEFINE(name, _bus, _addr) \
1301 const struct i2c_dt_spec I2C_CAT2(_i2c_dt_spec_, name) = { \
1302 .bus = DEVICE_DT_GET(_bus), \
1303 .addr = _addr, \
1304 }; \
1305 RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&I2C_CAT2(_i2c_dt_spec_, name))
1306
1315static inline bool i2c_is_ready_iodev(const struct rtio_iodev *i2c_iodev)
1316{
1317 struct i2c_dt_spec *spec = (struct i2c_dt_spec *)i2c_iodev->data;
1318
1319 return i2c_is_ready_dt(spec);
1320}
1321
1333struct rtio_sqe *i2c_rtio_copy(struct rtio *r,
1334 struct rtio_iodev *iodev,
1335 const struct i2c_msg *msgs,
1336 uint8_t num_msgs);
1337
1350 uint8_t reg_addr, uint8_t data);
1351
1365 uint8_t start_addr, void *buf, size_t num_bytes);
1366
1367#endif /* CONFIG_I2C_RTIO */
1368
1382static inline int i2c_transfer_dt(const struct i2c_dt_spec *spec,
1383 struct i2c_msg *msgs, uint8_t num_msgs)
1384{
1385 return i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
1386}
1387
1400__syscall int i2c_recover_bus(const struct device *dev);
1401
1402static inline int z_impl_i2c_recover_bus(const struct device *dev)
1403{
1404 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1405
1406 if (api->recover_bus == NULL) {
1407 return -ENOSYS;
1408 }
1409
1410 return api->recover_bus(dev);
1411}
1412
1437static inline int i2c_target_register(const struct device *dev,
1438 struct i2c_target_config *cfg)
1439{
1440 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1441
1442 if (api->target_register == NULL) {
1443 return -ENOSYS;
1444 }
1445
1446 return api->target_register(dev, cfg);
1447}
1448
1465static inline int i2c_target_unregister(const struct device *dev,
1466 struct i2c_target_config *cfg)
1467{
1468 const struct i2c_driver_api *api = DEVICE_API_GET(i2c, dev);
1469
1470 if (api->target_unregister == NULL) {
1471 return -ENOSYS;
1472 }
1473
1474 return api->target_unregister(dev, cfg);
1475}
1476
1490__syscall int i2c_target_driver_register(const struct device *dev);
1491
1492static inline int z_impl_i2c_target_driver_register(const struct device *dev)
1493{
1494 return DEVICE_API_GET(i2c_target, dev)->driver_register(dev);
1495}
1496
1510__syscall int i2c_target_driver_unregister(const struct device *dev);
1511
1512static inline int z_impl_i2c_target_driver_unregister(const struct device *dev)
1513{
1514 return DEVICE_API_GET(i2c_target, dev)->driver_unregister(dev);
1515}
1516
1517/*
1518 * Derived i2c APIs -- all implemented in terms of i2c_transfer()
1519 */
1520
1535static inline int i2c_write(const struct device *dev, const uint8_t *buf,
1536 uint32_t num_bytes, uint16_t addr)
1537{
1538 struct i2c_msg msg;
1539
1540 msg.buf = (uint8_t *)buf;
1541 msg.len = num_bytes;
1543
1544 return i2c_transfer(dev, &msg, 1, addr);
1545}
1546
1560static inline int i2c_write_dt(const struct i2c_dt_spec *spec,
1561 const uint8_t *buf, uint32_t num_bytes)
1562{
1563 return i2c_write(spec->bus, buf, num_bytes, spec->addr);
1564}
1565
1580static inline int i2c_read(const struct device *dev, uint8_t *buf,
1581 uint32_t num_bytes, uint16_t addr)
1582{
1583 struct i2c_msg msg;
1584
1585 msg.buf = buf;
1586 msg.len = num_bytes;
1588
1589 return i2c_transfer(dev, &msg, 1, addr);
1590}
1591
1605static inline int i2c_read_dt(const struct i2c_dt_spec *spec,
1606 uint8_t *buf, uint32_t num_bytes)
1607{
1608 return i2c_read(spec->bus, buf, num_bytes, spec->addr);
1609}
1610
1628static inline int i2c_write_read(const struct device *dev, uint16_t addr,
1629 const void *write_buf, size_t num_write,
1630 void *read_buf, size_t num_read)
1631{
1632 struct i2c_msg msg[2];
1633
1634 msg[0].buf = (uint8_t *)write_buf;
1635 msg[0].len = num_write;
1636 msg[0].flags = I2C_MSG_WRITE;
1637
1638 msg[1].buf = (uint8_t *)read_buf;
1639 msg[1].len = num_read;
1641
1642 return i2c_transfer(dev, msg, 2, addr);
1643}
1644
1662static inline int i2c_write_read_dt(const struct i2c_dt_spec *spec,
1663 const void *write_buf, size_t num_write,
1664 void *read_buf, size_t num_read)
1665{
1666 return i2c_write_read(spec->bus, spec->addr,
1667 write_buf, num_write,
1668 read_buf, num_read);
1669}
1670
1689static inline int i2c_burst_read(const struct device *dev,
1690 uint16_t dev_addr,
1691 uint8_t start_addr,
1692 uint8_t *buf,
1693 uint32_t num_bytes)
1694{
1695 return i2c_write_read(dev, dev_addr,
1696 &start_addr, sizeof(start_addr),
1697 buf, num_bytes);
1698}
1699
1714static inline int i2c_burst_read_dt(const struct i2c_dt_spec *spec,
1715 uint8_t start_addr,
1716 uint8_t *buf,
1717 uint32_t num_bytes)
1718{
1719 return i2c_burst_read(spec->bus, spec->addr,
1720 start_addr, buf, num_bytes);
1721}
1722
1744static inline int i2c_burst_write(const struct device *dev,
1745 uint16_t dev_addr,
1746 uint8_t start_addr,
1747 const uint8_t *buf,
1748 uint32_t num_bytes)
1749{
1750 struct i2c_msg msg[2];
1751
1752 msg[0].buf = &start_addr;
1753 msg[0].len = 1U;
1754 msg[0].flags = I2C_MSG_WRITE;
1755
1756 msg[1].buf = (uint8_t *)buf;
1757 msg[1].len = num_bytes;
1758 msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
1759
1760 return i2c_transfer(dev, msg, 2, dev_addr);
1761}
1762
1777static inline int i2c_burst_write_dt(const struct i2c_dt_spec *spec,
1778 uint8_t start_addr,
1779 const uint8_t *buf,
1780 uint32_t num_bytes)
1781{
1782 return i2c_burst_write(spec->bus, spec->addr,
1783 start_addr, buf, num_bytes);
1784}
1785
1801static inline int i2c_reg_read_byte(const struct device *dev,
1802 uint16_t dev_addr,
1803 uint8_t reg_addr, uint8_t *value)
1804{
1805 return i2c_write_read(dev, dev_addr,
1806 &reg_addr, sizeof(reg_addr),
1807 value, sizeof(*value));
1808}
1809
1823static inline int i2c_reg_read_byte_dt(const struct i2c_dt_spec *spec,
1824 uint8_t reg_addr, uint8_t *value)
1825{
1826 return i2c_reg_read_byte(spec->bus, spec->addr, reg_addr, value);
1827}
1828
1847static inline int i2c_reg_write_byte(const struct device *dev,
1848 uint16_t dev_addr,
1849 uint8_t reg_addr, uint8_t value)
1850{
1851 uint8_t tx_buf[2] = {reg_addr, value};
1852
1853 return i2c_write(dev, tx_buf, 2, dev_addr);
1854}
1855
1869static inline int i2c_reg_write_byte_dt(const struct i2c_dt_spec *spec,
1870 uint8_t reg_addr, uint8_t value)
1871{
1872 return i2c_reg_write_byte(spec->bus, spec->addr, reg_addr, value);
1873}
1874
1894static inline int i2c_reg_update_byte(const struct device *dev,
1895 uint8_t dev_addr,
1896 uint8_t reg_addr, uint8_t mask,
1897 uint8_t value)
1898{
1899 uint8_t old_value, new_value;
1900 int rc;
1901
1902 rc = i2c_reg_read_byte(dev, dev_addr, reg_addr, &old_value);
1903 if (rc != 0) {
1904 return rc;
1905 }
1906
1907 new_value = (old_value & ~mask) | (value & mask);
1908 if (new_value == old_value) {
1909 return 0;
1910 }
1911
1912 return i2c_reg_write_byte(dev, dev_addr, reg_addr, new_value);
1913}
1914
1929static inline int i2c_reg_update_byte_dt(const struct i2c_dt_spec *spec,
1930 uint8_t reg_addr, uint8_t mask,
1931 uint8_t value)
1932{
1933 return i2c_reg_update_byte(spec->bus, spec->addr,
1934 reg_addr, mask, value);
1935}
1936
1937#ifdef __cplusplus
1938}
1939#endif
1940
1944
1945#include <zephyr/syscalls/i2c.h>
1946
1947#endif /* ZEPHYR_INCLUDE_DRIVERS_I2C_H_ */
#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:326
int(* i2c_api_configure_t)(const struct device *dev, uint32_t dev_config)
Callback API to configure the I2C controller.
Definition i2c.h:275
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:289
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:313
int(* i2c_target_api_unregister_t)(const struct device *dev)
Callback API to instruct the I2C target device to unregister itself.
Definition i2c.h:378
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:305
int(* i2c_target_api_register_t)(const struct device *dev)
Callback API to instruct the I2C target device to register itself.
Definition i2c.h:372
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:298
int(* i2c_api_recover_bus_t)(const struct device *dev)
Callback API to recover the I2C bus.
Definition i2c.h:334
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:282
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:1095
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:1777
static int i2c_target_unregister(const struct device *dev, struct i2c_target_config *cfg)
Unregisters the provided config as Target device.
Definition i2c.h:1465
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:1628
static bool i2c_is_restart_op(const struct i2c_msg *msg)
Check if the current message includes a restart.
Definition i2c.h:667
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:1243
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:452
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:1535
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:1560
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:1662
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:561
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:1126
static bool i2c_is_stop_op(const struct i2c_msg *msg)
Check if the current message includes a stop.
Definition i2c.h:655
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:534
static bool i2c_is_ready_iodev(const struct rtio_iodev *i2c_iodev)
Validate that I2C bus is ready.
Definition i2c.h:1315
i2c_error_reason
I2C error reasons.
Definition i2c.h:542
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:1689
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:474
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:410
void i2c_iodev_submit_fallback(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
Fallback submit implementation.
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:1929
void(* i2c_callback_t)(const struct device *dev, int result, void *data)
I2C callback for asynchronous transfer requests.
Definition i2c.h:262
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:1605
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:430
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:1869
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:1847
#define I2C_MSG_READ
Read message from I2C bus.
Definition i2c.h:207
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:1823
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:934
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:1167
#define I2C_MSG_RESTART
RESTART I2C transaction for this message.
Definition i2c.h:223
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:1382
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:1580
static bool i2c_is_read_op(const struct i2c_msg *msg)
Check if the current message is a read operation.
Definition i2c.h:643
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:1714
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:1437
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:516
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:1055
#define I2C_MSG_STOP
Send STOP after this message.
Definition i2c.h:214
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:761
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:715
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:1203
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:491
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:1894
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:204
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:1801
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:1744
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:630
@ I2C_ERROR_DMA
DMA transfer error.
Definition i2c.h:546
@ I2C_ERROR_SIZE
Bad frame size.
Definition i2c.h:545
@ I2C_ERROR_GENERIC
Any other bus error.
Definition i2c.h:547
@ I2C_ERROR_TIMEOUT
Timeout error.
Definition i2c.h:543
@ I2C_ERROR_ARBITRATION
Bus arbitration lost.
Definition i2c.h:544
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:680
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
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:747
struct stats_i2c stats
I2C statistics.
Definition i2c.h:751
struct device_state devstate
Common device state.
Definition i2c.h:749
<span class="mlabel">Driver Operations</span> I2C driver operations
Definition i2c.h:339
i2c_api_target_unregister_t target_unregister
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition i2c.h:349
i2c_api_recover_bus_t recover_bus
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition i2c.h:365
i2c_api_full_io_t transfer
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition i2c.h:345
i2c_api_iodev_submit iodev_submit
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition i2c.h:362
i2c_api_get_config_t get_config
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition i2c.h:343
i2c_api_configure_t configure
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition i2c.h:341
i2c_api_target_register_t target_register
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition i2c.h:347
i2c_api_transfer_cb_t transfer_cb
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition i2c.h:355
Complete I2C DT information.
Definition i2c.h:135
const struct device * bus
I2C bus.
Definition i2c.h:137
uint16_t addr
Target address.
Definition i2c.h:139
One I2C Message.
Definition i2c.h:244
uint8_t * buf
Data buffer in bytes.
Definition i2c.h:246
uint32_t len
Length of buffer in bytes.
Definition i2c.h:249
uint8_t flags
Flags for this message.
Definition i2c.h:252
Structure providing callbacks to be implemented for devices that supports the I2C target API.
Definition i2c.h:570
i2c_target_read_requested_cb_t read_requested
Function called when a read from the device is initiated.
Definition i2c.h:574
i2c_target_write_received_cb_t write_received
Function called when a write to the device is continued.
Definition i2c.h:576
i2c_target_read_processed_cb_t read_processed
Function called when a read from the device is continued.
Definition i2c.h:578
i2c_target_write_requested_cb_t write_requested
Function called when a write to the device is initiated.
Definition i2c.h:572
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:592
i2c_target_buf_read_requested_cb_t buf_read_requested
Definition i2c.h:589
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:594
i2c_target_buf_write_received_cb_t buf_write_received
Definition i2c.h:584
Structure describing a device that supports the I2C target API.
Definition i2c.h:608
uint8_t flags
Flags for the target device defined by I2C_TARGET_FLAGS_* constants.
Definition i2c.h:613
uint16_t address
Address for this target device.
Definition i2c.h:616
sys_snode_t node
Private, do not modify.
Definition i2c.h:610
const struct i2c_target_callbacks * callbacks
Callback functions.
Definition i2c.h:619
<span class="mlabel">Driver Operations</span> I2C Target driver operations
Definition i2c.h:383
i2c_target_api_unregister_t driver_unregister
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition i2c.h:387
i2c_target_api_register_t driver_register
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition i2c.h:385
Poll signal object.
Definition kernel.h:6821
API that an RTIO IO device should implement.
Definition iodev.h:34
IO device submission queue entry.
Definition sqe.h:398
struct rtio_sqe sqe
Submission this entry carries.
Definition sqe.h:399
An IO device with a function table for submitting requests.
Definition iodev.h:49
void * data
Data associated with this iodev.
Definition iodev.h:54
A submission queue event.
Definition sqe.h:305
const struct rtio_iodev * iodev
Device to operation on.
Definition sqe.h:314
const uint8_t * buf
Buffer to write from.
Definition sqe.h:331
An RTIO context containing what can be viewed as a pair of queues.
Definition rtio.h:71
Macros to abstract toolchain specific capabilities.