Zephyr API Documentation 4.4.0-rc1
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
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/*
38 * The following #defines are used to configure the I2C controller.
39 */
40
42#define I2C_SPEED_STANDARD (0x1U)
43
45#define I2C_SPEED_FAST (0x2U)
46
48#define I2C_SPEED_FAST_PLUS (0x3U)
49
51#define I2C_SPEED_HIGH (0x4U)
52
54#define I2C_SPEED_ULTRA (0x5U)
55
57#define I2C_SPEED_DT (0x7U)
58
59#define I2C_SPEED_SHIFT (1U)
60#define I2C_SPEED_SET(speed) (((speed) << I2C_SPEED_SHIFT) \
61 & I2C_SPEED_MASK)
62#define I2C_SPEED_MASK (0x7U << I2C_SPEED_SHIFT) /* 3 bits */
63#define I2C_SPEED_GET(cfg) (((cfg) & I2C_SPEED_MASK) \
64 >> I2C_SPEED_SHIFT)
65
67#define I2C_ADDR_10_BITS BIT(0)
68
70#define I2C_MODE_CONTROLLER BIT(4)
71
79 const struct device *bus;
81};
82
93#define I2C_DT_SPEC_GET_ON_I3C(node_id) \
94 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
95 .addr = DT_PROP_BY_IDX(node_id, reg, 0)
96
107#define I2C_DT_SPEC_GET_ON_I2C(node_id) \
108 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
109 .addr = DT_REG_ADDR(node_id)
110
121#define I2C_DT_SPEC_GET(node_id) \
122 { \
123 COND_CODE_1(DT_ON_BUS(node_id, i3c), \
124 (I2C_DT_SPEC_GET_ON_I3C(node_id)), \
125 (I2C_DT_SPEC_GET_ON_I2C(node_id))) \
126 }
127
136#define I2C_DT_SPEC_INST_GET(inst) \
137 I2C_DT_SPEC_GET(DT_DRV_INST(inst))
138
139
140/*
141 * I2C_MSG_* are I2C Message flags.
142 */
143
145#define I2C_MSG_WRITE (0U << 0U)
146
148#define I2C_MSG_READ BIT(0)
149
151#define I2C_MSG_RW_MASK BIT(0)
153
155#define I2C_MSG_STOP BIT(1)
156
164#define I2C_MSG_RESTART BIT(2)
165
169#define I2C_MSG_ADDR_10_BITS BIT(3)
170
195
203typedef void (*i2c_callback_t)(const struct device *dev, int result, void *data);
204
211struct i2c_target_config;
212
213typedef int (*i2c_api_configure_t)(const struct device *dev,
214 uint32_t dev_config);
215typedef int (*i2c_api_get_config_t)(const struct device *dev,
216 uint32_t *dev_config);
217typedef int (*i2c_api_full_io_t)(const struct device *dev,
218 struct i2c_msg *msgs,
219 uint8_t num_msgs,
220 uint16_t addr);
221typedef int (*i2c_api_target_register_t)(const struct device *dev,
222 struct i2c_target_config *cfg);
223typedef int (*i2c_api_target_unregister_t)(const struct device *dev,
224 struct i2c_target_config *cfg);
225
226#ifdef CONFIG_I2C_CALLBACK
227typedef int (*i2c_api_transfer_cb_t)(const struct device *dev,
228 struct i2c_msg *msgs,
229 uint8_t num_msgs,
230 uint16_t addr,
232 void *userdata);
233#endif /* CONFIG_I2C_CALLBACK */
234
235#ifdef CONFIG_I2C_RTIO
236typedef void (*i2c_api_iodev_submit)(const struct device *dev,
237 struct rtio_iodev_sqe *iodev_sqe);
238#endif /* CONFIG_I2C_RTIO */
239
240typedef int (*i2c_api_recover_bus_t)(const struct device *dev);
241
242__subsystem struct i2c_driver_api {
243 i2c_api_configure_t configure;
244 i2c_api_get_config_t get_config;
245 i2c_api_full_io_t transfer;
246 i2c_api_target_register_t target_register;
247 i2c_api_target_unregister_t target_unregister;
248#ifdef CONFIG_I2C_CALLBACK
249 i2c_api_transfer_cb_t transfer_cb;
250#endif
251#ifdef CONFIG_I2C_RTIO
252 i2c_api_iodev_submit iodev_submit;
253#endif
254 i2c_api_recover_bus_t recover_bus;
255};
256
257typedef int (*i2c_target_api_register_t)(const struct device *dev);
258typedef int (*i2c_target_api_unregister_t)(const struct device *dev);
259
260__subsystem struct i2c_target_driver_api {
261 i2c_target_api_register_t driver_register;
262 i2c_target_api_unregister_t driver_unregister;
263};
264
268
270#define I2C_TARGET_FLAGS_ADDR_10_BITS BIT(0)
271
288 struct i2c_target_config *config);
289
309 struct i2c_target_config *config, uint8_t val);
310
331 struct i2c_target_config *config, uint8_t *val);
332
353 struct i2c_target_config *config, uint8_t *val);
354
355#if defined(CONFIG_I2C_TARGET_BUFFER_MODE) || defined(__DOXYGEN__)
370 struct i2c_target_config *config, uint8_t *ptr, uint32_t len);
371
395 struct i2c_target_config *config, uint8_t **ptr, uint32_t *len);
396#endif
397
412typedef int (*i2c_target_stop_cb_t)(struct i2c_target_config *config);
413
421 I2C_ERROR_TIMEOUT = 0, /* Timeout error */
422 I2C_ERROR_ARBITRATION, /* Bus arbitration size */
423 I2C_ERROR_SIZE, /* Bad frame size */
424 I2C_ERROR_DMA, /* DMA transfer error */
425 I2C_ERROR_GENERIC, /* Any other bus error */
426};
427
439typedef void (*i2c_target_error_cb_t)(struct i2c_target_config *config,
440 enum i2c_error_reason error_code);
441
474
499
508static inline bool i2c_is_ready_dt(const struct i2c_dt_spec *spec)
509{
510 /* Validate bus is ready */
511 return device_is_ready(spec->bus);
512}
513
521static inline bool i2c_is_read_op(const struct i2c_msg *msg)
522{
523 return (msg->flags & I2C_MSG_READ) == I2C_MSG_READ;
524}
525
533static inline bool i2c_is_stop_op(const struct i2c_msg *msg)
534{
535 return (msg->flags & I2C_MSG_STOP) == I2C_MSG_STOP;
536}
537
564void i2c_dump_msgs_rw(const struct device *dev, const struct i2c_msg *msgs, uint8_t num_msgs,
565 uint16_t addr, bool dump_read);
566
581static inline void i2c_dump_msgs(const struct device *dev, const struct i2c_msg *msgs,
582 uint8_t num_msgs, uint16_t addr)
583{
584 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, false);
585}
586
587#if defined(CONFIG_I2C_STATS) || defined(__DOXYGEN__)
588
589#include <zephyr/stats/stats.h>
590
592
594STATS_SECT_ENTRY32(bytes_read)
595STATS_SECT_ENTRY32(bytes_written)
596STATS_SECT_ENTRY32(message_count)
597STATS_SECT_ENTRY32(transfer_call_count)
599
601STATS_NAME(i2c, bytes_read)
602STATS_NAME(i2c, bytes_written)
603STATS_NAME(i2c, message_count)
604STATS_NAME(i2c, transfer_call_count)
605STATS_NAME_END(i2c);
606
608
609
615 struct stats_i2c stats;
616};
617
625static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
626 uint8_t num_msgs)
627{
628 struct i2c_device_state *state =
630 uint32_t bytes_read = 0U;
631 uint32_t bytes_written = 0U;
632
633 STATS_INC(state->stats, transfer_call_count);
634 STATS_INCN(state->stats, message_count, num_msgs);
635 for (uint8_t i = 0U; i < num_msgs; i++) {
636 if (msgs[i].flags & I2C_MSG_READ) {
637 bytes_read += msgs[i].len;
638 } else {
639 bytes_written += msgs[i].len;
640 }
641 }
642 STATS_INCN(state->stats, bytes_read, bytes_read);
643 STATS_INCN(state->stats, bytes_written, bytes_written);
644}
645
647
651#define Z_I2C_DEVICE_STATE_DEFINE(dev_id) \
652 static struct i2c_device_state Z_DEVICE_STATE_NAME(dev_id) \
653 __attribute__((__section__(".z_devstate")))
654
661#define Z_I2C_INIT_FN(dev_id, init_fn) \
662 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
663 { \
664 struct i2c_device_state *state = \
665 CONTAINER_OF(dev->state, struct i2c_device_state, devstate); \
666 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 4, \
667 STATS_NAME_INIT_PARMS(i2c)); \
668 stats_register(dev->name, &(state->stats.s_hdr)); \
669 if (!is_null_no_warn(init_fn)) { \
670 return init_fn(dev); \
671 } \
672 \
673 return 0; \
674 }
675
677
707#define I2C_DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, \
708 data, config, level, prio, api, ...)\
709 Z_I2C_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
710 Z_I2C_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
711 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
712 DEVICE_DT_NAME(node_id), \
713 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
714 deinit_fn, Z_DEVICE_DT_FLAGS(node_id), pm, data,\
715 config, level, prio, api, \
716 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
717 __VA_ARGS__)
718
719#else /* CONFIG_I2C_STATS */
720
721static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
722 uint8_t num_msgs)
723{
724 ARG_UNUSED(dev);
725 ARG_UNUSED(msgs);
726 ARG_UNUSED(num_msgs);
727}
728
729#define I2C_DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, \
730 data, config, level, prio, api, ...)\
731 DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, data, \
732 config, level, prio, api, __VA_ARGS__)
733
734#endif /* CONFIG_I2C_STATS */
735
739#define I2C_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
740 prio, api, ...) \
741 I2C_DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, NULL, pm, data, \
742 config, level, prio, api, \
743 __VA_ARGS__)
744
753#define I2C_DEVICE_DT_INST_DEINIT_DEFINE(inst, ...) \
754 I2C_DEVICE_DT_DEINIT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
755
764#define I2C_DEVICE_DT_INST_DEFINE(inst, ...) \
765 I2C_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
766
777__syscall int i2c_configure(const struct device *dev, uint32_t dev_config);
778
779static inline int z_impl_i2c_configure(const struct device *dev,
780 uint32_t dev_config)
781{
782 const struct i2c_driver_api *api =
783 (const struct i2c_driver_api *)dev->api;
784
785 return api->configure(dev, dev_config);
786}
787
801static inline int i2c_configure_dt(const struct i2c_dt_spec *spec,
802 uint32_t dev_config)
803{
804 return i2c_configure(spec->bus, dev_config);
805}
806
827__syscall int i2c_get_config(const struct device *dev, uint32_t *dev_config);
828
829static inline int z_impl_i2c_get_config(const struct device *dev, uint32_t *dev_config)
830{
831 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
832
833 if (api->get_config == NULL) {
834 return -ENOSYS;
835 }
836
837 return api->get_config(dev, dev_config);
838}
839
871__syscall int i2c_transfer(const struct device *dev,
872 struct i2c_msg *msgs, uint8_t num_msgs,
873 uint16_t addr);
874
875static inline int z_impl_i2c_transfer(const struct device *dev,
876 struct i2c_msg *msgs, uint8_t num_msgs,
877 uint16_t addr)
878{
879 const struct i2c_driver_api *api =
880 (const struct i2c_driver_api *)dev->api;
881
882 if (!num_msgs) {
883 return 0;
884 }
885
886 if (!IS_ENABLED(CONFIG_I2C_ALLOW_NO_STOP_TRANSACTIONS)) {
887 msgs[num_msgs - 1].flags |= I2C_MSG_STOP;
888 }
889
890 int res = api->transfer(dev, msgs, num_msgs, addr);
891
892 i2c_xfer_stats(dev, msgs, num_msgs);
893
894 if (IS_ENABLED(CONFIG_I2C_DUMP_MESSAGES)) {
895 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, true);
896 }
897
898 return res;
899}
900
901#if defined(CONFIG_I2C_CALLBACK) || defined(__DOXYGEN__)
902
925static inline int i2c_transfer_cb(const struct device *dev,
926 struct i2c_msg *msgs,
927 uint8_t num_msgs,
928 uint16_t addr,
930 void *userdata)
931{
932 const struct i2c_driver_api *api =
933 (const struct i2c_driver_api *)dev->api;
934
935 if (api->transfer_cb == NULL) {
936 return -ENOSYS;
937 }
938
939 if (!num_msgs) {
940 cb(dev, 0, userdata);
941 return 0;
942 }
943
944 if (!IS_ENABLED(CONFIG_I2C_ALLOW_NO_STOP_TRANSACTIONS)) {
945 msgs[num_msgs - 1].flags |= I2C_MSG_STOP;
946 }
947
948 return api->transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
949}
950
966static inline int i2c_transfer_cb_dt(const struct i2c_dt_spec *spec,
967 struct i2c_msg *msgs,
968 uint8_t num_msgs,
970 void *userdata)
971{
972 return i2c_transfer_cb(spec->bus, msgs, num_msgs, spec->addr, cb, userdata);
973}
974
998static inline int i2c_write_read_cb(const struct device *dev, struct i2c_msg *msgs,
999 uint8_t num_msgs, uint16_t addr, const void *write_buf,
1000 size_t num_write, void *read_buf, size_t num_read,
1001 i2c_callback_t cb, void *userdata)
1002{
1003 if ((msgs == NULL) || (num_msgs != 2)) {
1004 return -EINVAL;
1005 }
1006
1007 msgs[0].buf = (uint8_t *)write_buf;
1008 msgs[0].len = num_write;
1009 msgs[0].flags = I2C_MSG_WRITE;
1010
1011 msgs[1].buf = (uint8_t *)read_buf;
1012 msgs[1].len = num_read;
1014
1015 return i2c_transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
1016}
1017
1039static inline int i2c_write_read_cb_dt(const struct i2c_dt_spec *spec, struct i2c_msg *msgs,
1040 uint8_t num_msgs, const void *write_buf, size_t num_write,
1041 void *read_buf, size_t num_read, i2c_callback_t cb,
1042 void *userdata)
1043{
1044 return i2c_write_read_cb(spec->bus, msgs, num_msgs, spec->addr, write_buf, num_write,
1045 read_buf, num_read, cb, userdata);
1046}
1047
1048#if defined(CONFIG_POLL) || defined(__DOXYGEN__)
1049
1051void z_i2c_transfer_signal_cb(const struct device *dev, int result, void *userdata);
1053
1075static inline int i2c_transfer_signal(const struct device *dev,
1076 struct i2c_msg *msgs,
1077 uint8_t num_msgs,
1078 uint16_t addr,
1079 struct k_poll_signal *sig)
1080{
1081 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
1082
1083 if (api->transfer_cb == NULL) {
1084 return -ENOSYS;
1085 }
1086
1087 return api->transfer_cb(dev, msgs, num_msgs, addr, z_i2c_transfer_signal_cb, sig);
1088}
1089
1090#endif /* CONFIG_POLL */
1091
1092#endif /* CONFIG_I2C_CALLBACK */
1093
1094
1095#if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
1096
1107void i2c_iodev_submit_fallback(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe);
1108
1115static inline void i2c_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
1116{
1117 const struct i2c_dt_spec *dt_spec = (const struct i2c_dt_spec *)iodev_sqe->sqe.iodev->data;
1118 const struct device *dev = dt_spec->bus;
1119 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
1120
1121 if (api->iodev_submit == NULL) {
1122 rtio_iodev_sqe_err(iodev_sqe, -ENOSYS);
1123 return;
1124 }
1125 api->iodev_submit(dt_spec->bus, iodev_sqe);
1126}
1127
1128extern const struct rtio_iodev_api i2c_iodev_api;
1129
1130#define I2C_CAT2(x, y) x ## y
1131
1141#define I2C_DT_IODEV_DEFINE(name, node_id) \
1142 const struct i2c_dt_spec _i2c_dt_spec_##name = \
1143 I2C_DT_SPEC_GET(node_id); \
1144 RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&_i2c_dt_spec_##name)
1145
1155#define I2C_DT_INST_IODEV_DEFINE(name, inst) \
1156 I2C_DT_IODEV_DEFINE(name, DT_DRV_INST(inst))
1157
1168#define I2C_IODEV_DEFINE(name, _bus, _addr) \
1169 const struct i2c_dt_spec I2C_CAT2(_i2c_dt_spec_, name) = { \
1170 .bus = DEVICE_DT_GET(_bus), \
1171 .addr = _addr, \
1172 }; \
1173 RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&I2C_CAT2(_i2c_dt_spec_, name))
1174
1183static inline bool i2c_is_ready_iodev(const struct rtio_iodev *i2c_iodev)
1184{
1185 struct i2c_dt_spec *spec = (struct i2c_dt_spec *)i2c_iodev->data;
1186
1187 return i2c_is_ready_dt(spec);
1188}
1189
1202 struct rtio_iodev *iodev,
1203 const struct i2c_msg *msgs,
1204 uint8_t num_msgs);
1205
1218 uint8_t reg_addr, uint8_t data);
1219
1233 uint8_t start_addr, void *buf, size_t num_bytes);
1234
1235#endif /* CONFIG_I2C_RTIO */
1236
1250static inline int i2c_transfer_dt(const struct i2c_dt_spec *spec,
1251 struct i2c_msg *msgs, uint8_t num_msgs)
1252{
1253 return i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
1254}
1255
1268__syscall int i2c_recover_bus(const struct device *dev);
1269
1270static inline int z_impl_i2c_recover_bus(const struct device *dev)
1271{
1272 const struct i2c_driver_api *api =
1273 (const struct i2c_driver_api *)dev->api;
1274
1275 if (api->recover_bus == NULL) {
1276 return -ENOSYS;
1277 }
1278
1279 return api->recover_bus(dev);
1280}
1281
1306static inline int i2c_target_register(const struct device *dev,
1307 struct i2c_target_config *cfg)
1308{
1309 const struct i2c_driver_api *api =
1310 (const struct i2c_driver_api *)dev->api;
1311
1312 if (api->target_register == NULL) {
1313 return -ENOSYS;
1314 }
1315
1316 return api->target_register(dev, cfg);
1317}
1318
1335static inline int i2c_target_unregister(const struct device *dev,
1336 struct i2c_target_config *cfg)
1337{
1338 const struct i2c_driver_api *api =
1339 (const struct i2c_driver_api *)dev->api;
1340
1341 if (api->target_unregister == NULL) {
1342 return -ENOSYS;
1343 }
1344
1345 return api->target_unregister(dev, cfg);
1346}
1347
1361__syscall int i2c_target_driver_register(const struct device *dev);
1362
1363static inline int z_impl_i2c_target_driver_register(const struct device *dev)
1364{
1365 const struct i2c_target_driver_api *api =
1366 (const struct i2c_target_driver_api *)dev->api;
1367
1368 return api->driver_register(dev);
1369}
1370
1384__syscall int i2c_target_driver_unregister(const struct device *dev);
1385
1386static inline int z_impl_i2c_target_driver_unregister(const struct device *dev)
1387{
1388 const struct i2c_target_driver_api *api =
1389 (const struct i2c_target_driver_api *)dev->api;
1390
1391 return api->driver_unregister(dev);
1392}
1393
1394/*
1395 * Derived i2c APIs -- all implemented in terms of i2c_transfer()
1396 */
1397
1412static inline int i2c_write(const struct device *dev, const uint8_t *buf,
1413 uint32_t num_bytes, uint16_t addr)
1414{
1415 struct i2c_msg msg;
1416
1417 msg.buf = (uint8_t *)buf;
1418 msg.len = num_bytes;
1420
1421 return i2c_transfer(dev, &msg, 1, addr);
1422}
1423
1437static inline int i2c_write_dt(const struct i2c_dt_spec *spec,
1438 const uint8_t *buf, uint32_t num_bytes)
1439{
1440 return i2c_write(spec->bus, buf, num_bytes, spec->addr);
1441}
1442
1457static inline int i2c_read(const struct device *dev, uint8_t *buf,
1458 uint32_t num_bytes, uint16_t addr)
1459{
1460 struct i2c_msg msg;
1461
1462 msg.buf = buf;
1463 msg.len = num_bytes;
1465
1466 return i2c_transfer(dev, &msg, 1, addr);
1467}
1468
1482static inline int i2c_read_dt(const struct i2c_dt_spec *spec,
1483 uint8_t *buf, uint32_t num_bytes)
1484{
1485 return i2c_read(spec->bus, buf, num_bytes, spec->addr);
1486}
1487
1506static inline int i2c_write_read(const struct device *dev, uint16_t addr,
1507 const void *write_buf, size_t num_write,
1508 void *read_buf, size_t num_read)
1509{
1510 struct i2c_msg msg[2];
1511
1512 msg[0].buf = (uint8_t *)write_buf;
1513 msg[0].len = num_write;
1514 msg[0].flags = I2C_MSG_WRITE;
1515
1516 msg[1].buf = (uint8_t *)read_buf;
1517 msg[1].len = num_read;
1519
1520 return i2c_transfer(dev, msg, 2, addr);
1521}
1522
1540static inline int i2c_write_read_dt(const struct i2c_dt_spec *spec,
1541 const void *write_buf, size_t num_write,
1542 void *read_buf, size_t num_read)
1543{
1544 return i2c_write_read(spec->bus, spec->addr,
1545 write_buf, num_write,
1546 read_buf, num_read);
1547}
1548
1567static inline int i2c_burst_read(const struct device *dev,
1568 uint16_t dev_addr,
1569 uint8_t start_addr,
1570 uint8_t *buf,
1571 uint32_t num_bytes)
1572{
1573 return i2c_write_read(dev, dev_addr,
1574 &start_addr, sizeof(start_addr),
1575 buf, num_bytes);
1576}
1577
1592static inline int i2c_burst_read_dt(const struct i2c_dt_spec *spec,
1593 uint8_t start_addr,
1594 uint8_t *buf,
1595 uint32_t num_bytes)
1596{
1597 return i2c_burst_read(spec->bus, spec->addr,
1598 start_addr, buf, num_bytes);
1599}
1600
1622static inline int i2c_burst_write(const struct device *dev,
1623 uint16_t dev_addr,
1624 uint8_t start_addr,
1625 const uint8_t *buf,
1626 uint32_t num_bytes)
1627{
1628 struct i2c_msg msg[2];
1629
1630 msg[0].buf = &start_addr;
1631 msg[0].len = 1U;
1632 msg[0].flags = I2C_MSG_WRITE;
1633
1634 msg[1].buf = (uint8_t *)buf;
1635 msg[1].len = num_bytes;
1636 msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
1637
1638 return i2c_transfer(dev, msg, 2, dev_addr);
1639}
1640
1655static inline int i2c_burst_write_dt(const struct i2c_dt_spec *spec,
1656 uint8_t start_addr,
1657 const uint8_t *buf,
1658 uint32_t num_bytes)
1659{
1660 return i2c_burst_write(spec->bus, spec->addr,
1661 start_addr, buf, num_bytes);
1662}
1663
1679static inline int i2c_reg_read_byte(const struct device *dev,
1680 uint16_t dev_addr,
1681 uint8_t reg_addr, uint8_t *value)
1682{
1683 return i2c_write_read(dev, dev_addr,
1684 &reg_addr, sizeof(reg_addr),
1685 value, sizeof(*value));
1686}
1687
1701static inline int i2c_reg_read_byte_dt(const struct i2c_dt_spec *spec,
1702 uint8_t reg_addr, uint8_t *value)
1703{
1704 return i2c_reg_read_byte(spec->bus, spec->addr, reg_addr, value);
1705}
1706
1725static inline int i2c_reg_write_byte(const struct device *dev,
1726 uint16_t dev_addr,
1727 uint8_t reg_addr, uint8_t value)
1728{
1729 uint8_t tx_buf[2] = {reg_addr, value};
1730
1731 return i2c_write(dev, tx_buf, 2, dev_addr);
1732}
1733
1747static inline int i2c_reg_write_byte_dt(const struct i2c_dt_spec *spec,
1748 uint8_t reg_addr, uint8_t value)
1749{
1750 return i2c_reg_write_byte(spec->bus, spec->addr, reg_addr, value);
1751}
1752
1772static inline int i2c_reg_update_byte(const struct device *dev,
1773 uint8_t dev_addr,
1774 uint8_t reg_addr, uint8_t mask,
1775 uint8_t value)
1776{
1777 uint8_t old_value, new_value;
1778 int rc;
1779
1780 rc = i2c_reg_read_byte(dev, dev_addr, reg_addr, &old_value);
1781 if (rc != 0) {
1782 return rc;
1783 }
1784
1785 new_value = (old_value & ~mask) | (value & mask);
1786 if (new_value == old_value) {
1787 return 0;
1788 }
1789
1790 return i2c_reg_write_byte(dev, dev_addr, reg_addr, new_value);
1791}
1792
1807static inline int i2c_reg_update_byte_dt(const struct i2c_dt_spec *spec,
1808 uint8_t reg_addr, uint8_t mask,
1809 uint8_t value)
1810{
1811 return i2c_reg_update_byte(spec->bus, spec->addr,
1812 reg_addr, mask, value);
1813}
1814
1815#ifdef __cplusplus
1816}
1817#endif
1818
1822
1823#include <zephyr/syscalls/i2c.h>
1824
1825#endif /* ZEPHYR_INCLUDE_DRIVERS_I2C_H_ */
workaround assembler barfing for ST r
Definition asm-macro-32-bit-gnu.h:24
System error numbers.
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
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:966
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:1655
static int i2c_target_unregister(const struct device *dev, struct i2c_target_config *cfg)
Unregisters the provided config as Target device.
Definition i2c.h:1335
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:1506
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:1115
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:330
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:1412
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:1437
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:1540
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:439
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:998
static bool i2c_is_stop_op(const struct i2c_msg *msg)
Check if the current message includes a stop.
Definition i2c.h:533
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:412
static bool i2c_is_ready_iodev(const struct rtio_iodev *i2c_iodev)
Validate that I2C bus is ready.
Definition i2c.h:1183
i2c_error_reason
I2C error reasons.
Definition i2c.h:420
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:1567
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:352
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:287
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:1807
void(* i2c_callback_t)(const struct device *dev, int result, void *data)
I2C callback for asynchronous transfer requests.
Definition i2c.h:203
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:1482
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:308
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:1747
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:1725
#define I2C_MSG_READ
Read message from I2C bus.
Definition i2c.h:148
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:1701
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:801
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:1039
#define I2C_MSG_RESTART
RESTART I2C transaction for this message.
Definition i2c.h:164
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:1250
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:1457
static bool i2c_is_read_op(const struct i2c_msg *msg)
Check if the current message is a read operation.
Definition i2c.h:521
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:1592
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:1306
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:394
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:925
#define I2C_MSG_STOP
Send STOP after this message.
Definition i2c.h:155
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:625
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:581
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:1075
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:369
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:1772
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:145
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:1679
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:1622
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:508
@ I2C_ERROR_DMA
Definition i2c.h:424
@ I2C_ERROR_SIZE
Definition i2c.h:423
@ I2C_ERROR_GENERIC
Definition i2c.h:425
@ I2C_ERROR_TIMEOUT
Definition i2c.h:421
@ I2C_ERROR_ARBITRATION
Definition i2c.h:422
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:662
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
#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:60
#define ENOSYS
Function not implemented.
Definition errno.h:82
#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.
Statistics.
#define STATS_NAME_END(name__)
Definition stats.h:391
#define STATS_NAME(name__, entry__)
Definition stats.h:390
#define STATS_SECT_END
Ends a stats group struct definition.
Definition stats.h:89
#define STATS_SECT_ENTRY32(var__)
Definition stats.h:359
#define STATS_INC(group__, var__)
Definition stats.h:364
#define STATS_NAME_START(name__)
Definition stats.h:389
#define STATS_INCN(group__, var__, n__)
Definition stats.h:363
#define STATS_SECT_START(group__)
Definition stats.h:354
__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
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:519
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:613
struct stats_i2c stats
Definition i2c.h:615
struct device_state devstate
Definition i2c.h:614
Complete I2C DT information.
Definition i2c.h:78
const struct device * bus
Definition i2c.h:79
uint16_t addr
Definition i2c.h:80
One I2C Message.
Definition i2c.h:185
uint8_t * buf
Data buffer in bytes.
Definition i2c.h:187
uint32_t len
Length of buffer in bytes.
Definition i2c.h:190
uint8_t flags
Flags for this message.
Definition i2c.h:193
Structure providing callbacks to be implemented for devices that supports the I2C target API.
Definition i2c.h:448
i2c_target_read_requested_cb_t read_requested
Function called when a read from the device is initiated.
Definition i2c.h:452
i2c_target_write_received_cb_t write_received
Function called when a write to the device is continued.
Definition i2c.h:454
i2c_target_read_processed_cb_t read_processed
Function called when a read from the device is continued.
Definition i2c.h:456
i2c_target_write_requested_cb_t write_requested
Function called when a write to the device is initiated.
Definition i2c.h:450
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:470
i2c_target_buf_read_requested_cb_t buf_read_requested
Definition i2c.h:467
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:472
i2c_target_buf_write_received_cb_t buf_write_received
Definition i2c.h:462
Structure describing a device that supports the I2C target API.
Definition i2c.h:486
uint8_t flags
Flags for the target device defined by I2C_TARGET_FLAGS_* constants.
Definition i2c.h:491
uint16_t address
Address for this target device.
Definition i2c.h:494
sys_snode_t node
Private, do not modify.
Definition i2c.h:488
const struct i2c_target_callbacks * callbacks
Callback functions.
Definition i2c.h:497
Definition kernel.h:6566
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:71