Zephyr API Documentation 4.0.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
i2c.h
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2015 Intel Corporation
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12#ifndef ZEPHYR_INCLUDE_DRIVERS_I2C_H_
13#define ZEPHYR_INCLUDE_DRIVERS_I2C_H_
14
24#include <errno.h>
25
26#include <zephyr/types.h>
27#include <zephyr/device.h>
28#include <zephyr/kernel.h>
29#include <zephyr/sys/slist.h>
30#include <zephyr/rtio/rtio.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/*
37 * The following #defines are used to configure the I2C controller.
38 */
39
41#define I2C_SPEED_STANDARD (0x1U)
42
44#define I2C_SPEED_FAST (0x2U)
45
47#define I2C_SPEED_FAST_PLUS (0x3U)
48
50#define I2C_SPEED_HIGH (0x4U)
51
53#define I2C_SPEED_ULTRA (0x5U)
54
56#define I2C_SPEED_DT (0x7U)
57
58#define I2C_SPEED_SHIFT (1U)
59#define I2C_SPEED_SET(speed) (((speed) << I2C_SPEED_SHIFT) \
60 & I2C_SPEED_MASK)
61#define I2C_SPEED_MASK (0x7U << I2C_SPEED_SHIFT) /* 3 bits */
62#define I2C_SPEED_GET(cfg) (((cfg) & I2C_SPEED_MASK) \
63 >> I2C_SPEED_SHIFT)
64
66#define I2C_ADDR_10_BITS BIT(0)
67
69#define I2C_MODE_CONTROLLER BIT(4)
70
78 const struct device *bus;
80};
81
92#define I2C_DT_SPEC_GET_ON_I3C(node_id) \
93 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
94 .addr = DT_PROP_BY_IDX(node_id, reg, 0)
95
106#define I2C_DT_SPEC_GET_ON_I2C(node_id) \
107 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
108 .addr = DT_REG_ADDR(node_id)
109
120#define I2C_DT_SPEC_GET(node_id) \
121 { \
122 COND_CODE_1(DT_ON_BUS(node_id, i3c), \
123 (I2C_DT_SPEC_GET_ON_I3C(node_id)), \
124 (I2C_DT_SPEC_GET_ON_I2C(node_id))) \
125 }
126
135#define I2C_DT_SPEC_INST_GET(inst) \
136 I2C_DT_SPEC_GET(DT_DRV_INST(inst))
137
138
139/*
140 * I2C_MSG_* are I2C Message flags.
141 */
142
144#define I2C_MSG_WRITE (0U << 0U)
145
147#define I2C_MSG_READ BIT(0)
148
150#define I2C_MSG_RW_MASK BIT(0)
154#define I2C_MSG_STOP BIT(1)
155
163#define I2C_MSG_RESTART BIT(2)
164
168#define I2C_MSG_ADDR_10_BITS BIT(3)
169
194
202typedef void (*i2c_callback_t)(const struct device *dev, int result, void *data);
203
210struct i2c_target_config;
211
212typedef int (*i2c_api_configure_t)(const struct device *dev,
213 uint32_t dev_config);
214typedef int (*i2c_api_get_config_t)(const struct device *dev,
215 uint32_t *dev_config);
216typedef int (*i2c_api_full_io_t)(const struct device *dev,
217 struct i2c_msg *msgs,
218 uint8_t num_msgs,
219 uint16_t addr);
220typedef int (*i2c_api_target_register_t)(const struct device *dev,
221 struct i2c_target_config *cfg);
222typedef int (*i2c_api_target_unregister_t)(const struct device *dev,
223 struct i2c_target_config *cfg);
224#ifdef CONFIG_I2C_CALLBACK
225typedef int (*i2c_api_transfer_cb_t)(const struct device *dev,
226 struct i2c_msg *msgs,
227 uint8_t num_msgs,
228 uint16_t addr,
230 void *userdata);
231#endif /* CONFIG_I2C_CALLBACK */
232#if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
233
238typedef void (*i2c_api_iodev_submit)(const struct device *dev,
239 struct rtio_iodev_sqe *iodev_sqe);
240#endif /* CONFIG_I2C_RTIO */
241
242typedef int (*i2c_api_recover_bus_t)(const struct device *dev);
243
244__subsystem struct i2c_driver_api {
245 i2c_api_configure_t configure;
246 i2c_api_get_config_t get_config;
247 i2c_api_full_io_t transfer;
248 i2c_api_target_register_t target_register;
249 i2c_api_target_unregister_t target_unregister;
250#ifdef CONFIG_I2C_CALLBACK
251 i2c_api_transfer_cb_t transfer_cb;
252#endif
253#ifdef CONFIG_I2C_RTIO
254 i2c_api_iodev_submit iodev_submit;
255#endif
256 i2c_api_recover_bus_t recover_bus;
257};
258
259typedef int (*i2c_target_api_register_t)(const struct device *dev);
260typedef int (*i2c_target_api_unregister_t)(const struct device *dev);
261
262__subsystem struct i2c_target_driver_api {
263 i2c_target_api_register_t driver_register;
264 i2c_target_api_unregister_t driver_unregister;
265};
266
272#define I2C_TARGET_FLAGS_ADDR_10_BITS BIT(0)
273
290 struct i2c_target_config *config);
291
311 struct i2c_target_config *config, uint8_t val);
312
333 struct i2c_target_config *config, uint8_t *val);
334
355 struct i2c_target_config *config, uint8_t *val);
356
357#ifdef CONFIG_I2C_TARGET_BUFFER_MODE
371typedef void (*i2c_target_buf_write_received_cb_t)(
372 struct i2c_target_config *config, uint8_t *ptr, uint32_t len);
373
396typedef int (*i2c_target_buf_read_requested_cb_t)(
397 struct i2c_target_config *config, uint8_t **ptr, uint32_t *len);
398#endif
399
414typedef int (*i2c_target_stop_cb_t)(struct i2c_target_config *config);
415
427#ifdef CONFIG_I2C_TARGET_BUFFER_MODE
428 i2c_target_buf_write_received_cb_t buf_write_received;
429 i2c_target_buf_read_requested_cb_t buf_read_requested;
430#endif
432};
433
458
467static inline bool i2c_is_ready_dt(const struct i2c_dt_spec *spec)
468{
469 /* Validate bus is ready */
470 return device_is_ready(spec->bus);
471}
472
480static inline bool i2c_is_read_op(const struct i2c_msg *msg)
481{
482 return (msg->flags & I2C_MSG_READ) == I2C_MSG_READ;
483}
484
492static inline bool i2c_is_stop_op(const struct i2c_msg *msg)
493{
494 return (msg->flags & I2C_MSG_STOP) == I2C_MSG_STOP;
495}
496
523void i2c_dump_msgs_rw(const struct device *dev, const struct i2c_msg *msgs, uint8_t num_msgs,
524 uint16_t addr, bool dump_read);
525
540static inline void i2c_dump_msgs(const struct device *dev, const struct i2c_msg *msgs,
541 uint8_t num_msgs, uint16_t addr)
542{
543 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, false);
544}
545
546#if defined(CONFIG_I2C_STATS) || defined(__DOXYGEN__)
547
548#include <zephyr/stats/stats.h>
549
553STATS_SECT_ENTRY32(bytes_read)
554STATS_SECT_ENTRY32(bytes_written)
555STATS_SECT_ENTRY32(message_count)
556STATS_SECT_ENTRY32(transfer_call_count)
558
560STATS_NAME(i2c, bytes_read)
561STATS_NAME(i2c, bytes_written)
562STATS_NAME(i2c, message_count)
563STATS_NAME(i2c, transfer_call_count)
564STATS_NAME_END(i2c);
565
574 struct stats_i2c stats;
575};
576
584static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
585 uint8_t num_msgs)
586{
587 struct i2c_device_state *state =
589 uint32_t bytes_read = 0U;
590 uint32_t bytes_written = 0U;
591
592 STATS_INC(state->stats, transfer_call_count);
593 STATS_INCN(state->stats, message_count, num_msgs);
594 for (uint8_t i = 0U; i < num_msgs; i++) {
595 if (msgs[i].flags & I2C_MSG_READ) {
596 bytes_read += msgs[i].len;
597 } else {
598 bytes_written += msgs[i].len;
599 }
600 }
601 STATS_INCN(state->stats, bytes_read, bytes_read);
602 STATS_INCN(state->stats, bytes_written, bytes_written);
603}
604
610#define Z_I2C_DEVICE_STATE_DEFINE(dev_id) \
611 static struct i2c_device_state Z_DEVICE_STATE_NAME(dev_id) \
612 __attribute__((__section__(".z_devstate")))
613
620#define Z_I2C_INIT_FN(dev_id, init_fn) \
621 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
622 { \
623 struct i2c_device_state *state = \
624 CONTAINER_OF(dev->state, struct i2c_device_state, devstate); \
625 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 4, \
626 STATS_NAME_INIT_PARMS(i2c)); \
627 stats_register(dev->name, &(state->stats.s_hdr)); \
628 if (!is_null_no_warn(init_fn)) { \
629 return init_fn(dev); \
630 } \
631 \
632 return 0; \
633 }
634
664#define I2C_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
665 prio, api, ...) \
666 Z_I2C_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
667 Z_I2C_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
668 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
669 DEVICE_DT_NAME(node_id), \
670 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
671 pm, data, config, level, prio, api, \
672 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
673 __VA_ARGS__)
674
675#else /* CONFIG_I2C_STATS */
676
677static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
678 uint8_t num_msgs)
679{
680 ARG_UNUSED(dev);
681 ARG_UNUSED(msgs);
682 ARG_UNUSED(num_msgs);
683}
684
685#define I2C_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
686 prio, api, ...) \
687 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
688 prio, api, __VA_ARGS__)
689
690#endif /* CONFIG_I2C_STATS */
691
700#define I2C_DEVICE_DT_INST_DEFINE(inst, ...) \
701 I2C_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
702
703
714__syscall int i2c_configure(const struct device *dev, uint32_t dev_config);
715
716static inline int z_impl_i2c_configure(const struct device *dev,
717 uint32_t dev_config)
718{
719 const struct i2c_driver_api *api =
720 (const struct i2c_driver_api *)dev->api;
721
722 return api->configure(dev, dev_config);
723}
724
745__syscall int i2c_get_config(const struct device *dev, uint32_t *dev_config);
746
747static inline int z_impl_i2c_get_config(const struct device *dev, uint32_t *dev_config)
748{
749 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
750
751 if (api->get_config == NULL) {
752 return -ENOSYS;
753 }
754
755 return api->get_config(dev, dev_config);
756}
757
789__syscall int i2c_transfer(const struct device *dev,
790 struct i2c_msg *msgs, uint8_t num_msgs,
791 uint16_t addr);
792
793static inline int z_impl_i2c_transfer(const struct device *dev,
794 struct i2c_msg *msgs, uint8_t num_msgs,
795 uint16_t addr)
796{
797 const struct i2c_driver_api *api =
798 (const struct i2c_driver_api *)dev->api;
799
800 if (!num_msgs) {
801 return 0;
802 }
803
804 if (!IS_ENABLED(CONFIG_I2C_ALLOW_NO_STOP_TRANSACTIONS)) {
805 msgs[num_msgs - 1].flags |= I2C_MSG_STOP;
806 }
807
808 int res = api->transfer(dev, msgs, num_msgs, addr);
809
810 i2c_xfer_stats(dev, msgs, num_msgs);
811
812 if (IS_ENABLED(CONFIG_I2C_DUMP_MESSAGES)) {
813 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, true);
814 }
815
816 return res;
817}
818
819#if defined(CONFIG_I2C_CALLBACK) || defined(__DOXYGEN__)
820
843static inline int i2c_transfer_cb(const struct device *dev,
844 struct i2c_msg *msgs,
845 uint8_t num_msgs,
846 uint16_t addr,
848 void *userdata)
849{
850 const struct i2c_driver_api *api =
851 (const struct i2c_driver_api *)dev->api;
852
853 if (api->transfer_cb == NULL) {
854 return -ENOSYS;
855 }
856
857 if (!num_msgs) {
858 cb(dev, 0, userdata);
859 return 0;
860 }
861
862 if (!IS_ENABLED(CONFIG_I2C_ALLOW_NO_STOP_TRANSACTIONS)) {
863 msgs[num_msgs - 1].flags |= I2C_MSG_STOP;
864 }
865
866 return api->transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
867}
868
884static inline int i2c_transfer_cb_dt(const struct i2c_dt_spec *spec,
885 struct i2c_msg *msgs,
886 uint8_t num_msgs,
888 void *userdata)
889{
890 return i2c_transfer_cb(spec->bus, msgs, num_msgs, spec->addr, cb, userdata);
891}
892
916static inline int i2c_write_read_cb(const struct device *dev, struct i2c_msg *msgs,
917 uint8_t num_msgs, uint16_t addr, const void *write_buf,
918 size_t num_write, void *read_buf, size_t num_read,
919 i2c_callback_t cb, void *userdata)
920{
921 if ((msgs == NULL) || (num_msgs != 2)) {
922 return -EINVAL;
923 }
924
925 msgs[0].buf = (uint8_t *)write_buf;
926 msgs[0].len = num_write;
927 msgs[0].flags = I2C_MSG_WRITE;
928
929 msgs[1].buf = (uint8_t *)read_buf;
930 msgs[1].len = num_read;
932
933 return i2c_transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
934}
935
957static inline int i2c_write_read_cb_dt(const struct i2c_dt_spec *spec, struct i2c_msg *msgs,
958 uint8_t num_msgs, const void *write_buf, size_t num_write,
959 void *read_buf, size_t num_read, i2c_callback_t cb,
960 void *userdata)
961{
962 return i2c_write_read_cb(spec->bus, msgs, num_msgs, spec->addr, write_buf, num_write,
963 read_buf, num_read, cb, userdata);
964}
965
966#if defined(CONFIG_POLL) || defined(__DOXYGEN__)
967
969void z_i2c_transfer_signal_cb(const struct device *dev, int result, void *userdata);
993static inline int i2c_transfer_signal(const struct device *dev,
994 struct i2c_msg *msgs,
995 uint8_t num_msgs,
996 uint16_t addr,
997 struct k_poll_signal *sig)
998{
999 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
1000
1001 if (api->transfer_cb == NULL) {
1002 return -ENOSYS;
1003 }
1004
1005 return api->transfer_cb(dev, msgs, num_msgs, addr, z_i2c_transfer_signal_cb, sig);
1006}
1007
1008#endif /* CONFIG_POLL */
1009
1010#endif /* CONFIG_I2C_CALLBACK */
1011
1012
1013#if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
1014
1025void i2c_iodev_submit_fallback(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe);
1026
1033static inline void i2c_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
1034{
1035 const struct i2c_dt_spec *dt_spec = (const struct i2c_dt_spec *)iodev_sqe->sqe.iodev->data;
1036 const struct device *dev = dt_spec->bus;
1037 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
1038
1039 if (api->iodev_submit == NULL) {
1040 rtio_iodev_sqe_err(iodev_sqe, -ENOSYS);
1041 return;
1042 }
1043 api->iodev_submit(dt_spec->bus, iodev_sqe);
1044}
1045
1046extern const struct rtio_iodev_api i2c_iodev_api;
1047
1057#define I2C_DT_IODEV_DEFINE(name, node_id) \
1058 const struct i2c_dt_spec _i2c_dt_spec_##name = \
1059 I2C_DT_SPEC_GET(node_id); \
1060 RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&_i2c_dt_spec_##name)
1061
1072#define I2C_IODEV_DEFINE(name, _bus, _addr) \
1073 const struct i2c_dt_spec _i2c_dt_spec_##name = { \
1074 .bus = DEVICE_DT_GET(_bus), \
1075 .addr = _addr, \
1076 }; \
1077 RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&_i2c_dt_spec_##name)
1078
1091 struct rtio_iodev *iodev,
1092 const struct i2c_msg *msgs,
1093 uint8_t num_msgs);
1094
1095#endif /* CONFIG_I2C_RTIO */
1096
1110static inline int i2c_transfer_dt(const struct i2c_dt_spec *spec,
1111 struct i2c_msg *msgs, uint8_t num_msgs)
1112{
1113 return i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
1114}
1115
1128__syscall int i2c_recover_bus(const struct device *dev);
1129
1130static inline int z_impl_i2c_recover_bus(const struct device *dev)
1131{
1132 const struct i2c_driver_api *api =
1133 (const struct i2c_driver_api *)dev->api;
1134
1135 if (api->recover_bus == NULL) {
1136 return -ENOSYS;
1137 }
1138
1139 return api->recover_bus(dev);
1140}
1141
1166static inline int i2c_target_register(const struct device *dev,
1167 struct i2c_target_config *cfg)
1168{
1169 const struct i2c_driver_api *api =
1170 (const struct i2c_driver_api *)dev->api;
1171
1172 if (api->target_register == NULL) {
1173 return -ENOSYS;
1174 }
1175
1176 return api->target_register(dev, cfg);
1177}
1178
1195static inline int i2c_target_unregister(const struct device *dev,
1196 struct i2c_target_config *cfg)
1197{
1198 const struct i2c_driver_api *api =
1199 (const struct i2c_driver_api *)dev->api;
1200
1201 if (api->target_unregister == NULL) {
1202 return -ENOSYS;
1203 }
1204
1205 return api->target_unregister(dev, cfg);
1206}
1207
1221__syscall int i2c_target_driver_register(const struct device *dev);
1222
1223static inline int z_impl_i2c_target_driver_register(const struct device *dev)
1224{
1225 const struct i2c_target_driver_api *api =
1226 (const struct i2c_target_driver_api *)dev->api;
1227
1228 return api->driver_register(dev);
1229}
1230
1244__syscall int i2c_target_driver_unregister(const struct device *dev);
1245
1246static inline int z_impl_i2c_target_driver_unregister(const struct device *dev)
1247{
1248 const struct i2c_target_driver_api *api =
1249 (const struct i2c_target_driver_api *)dev->api;
1250
1251 return api->driver_unregister(dev);
1252}
1253
1254/*
1255 * Derived i2c APIs -- all implemented in terms of i2c_transfer()
1256 */
1257
1272static inline int i2c_write(const struct device *dev, const uint8_t *buf,
1273 uint32_t num_bytes, uint16_t addr)
1274{
1275 struct i2c_msg msg;
1276
1277 msg.buf = (uint8_t *)buf;
1278 msg.len = num_bytes;
1280
1281 return i2c_transfer(dev, &msg, 1, addr);
1282}
1283
1297static inline int i2c_write_dt(const struct i2c_dt_spec *spec,
1298 const uint8_t *buf, uint32_t num_bytes)
1299{
1300 return i2c_write(spec->bus, buf, num_bytes, spec->addr);
1301}
1302
1317static inline int i2c_read(const struct device *dev, uint8_t *buf,
1318 uint32_t num_bytes, uint16_t addr)
1319{
1320 struct i2c_msg msg;
1321
1322 msg.buf = buf;
1323 msg.len = num_bytes;
1325
1326 return i2c_transfer(dev, &msg, 1, addr);
1327}
1328
1342static inline int i2c_read_dt(const struct i2c_dt_spec *spec,
1343 uint8_t *buf, uint32_t num_bytes)
1344{
1345 return i2c_read(spec->bus, buf, num_bytes, spec->addr);
1346}
1347
1366static inline int i2c_write_read(const struct device *dev, uint16_t addr,
1367 const void *write_buf, size_t num_write,
1368 void *read_buf, size_t num_read)
1369{
1370 struct i2c_msg msg[2];
1371
1372 msg[0].buf = (uint8_t *)write_buf;
1373 msg[0].len = num_write;
1374 msg[0].flags = I2C_MSG_WRITE;
1375
1376 msg[1].buf = (uint8_t *)read_buf;
1377 msg[1].len = num_read;
1379
1380 return i2c_transfer(dev, msg, 2, addr);
1381}
1382
1400static inline int i2c_write_read_dt(const struct i2c_dt_spec *spec,
1401 const void *write_buf, size_t num_write,
1402 void *read_buf, size_t num_read)
1403{
1404 return i2c_write_read(spec->bus, spec->addr,
1405 write_buf, num_write,
1406 read_buf, num_read);
1407}
1408
1427static inline int i2c_burst_read(const struct device *dev,
1428 uint16_t dev_addr,
1429 uint8_t start_addr,
1430 uint8_t *buf,
1431 uint32_t num_bytes)
1432{
1433 return i2c_write_read(dev, dev_addr,
1434 &start_addr, sizeof(start_addr),
1435 buf, num_bytes);
1436}
1437
1452static inline int i2c_burst_read_dt(const struct i2c_dt_spec *spec,
1453 uint8_t start_addr,
1454 uint8_t *buf,
1455 uint32_t num_bytes)
1456{
1457 return i2c_burst_read(spec->bus, spec->addr,
1458 start_addr, buf, num_bytes);
1459}
1460
1482static inline int i2c_burst_write(const struct device *dev,
1483 uint16_t dev_addr,
1484 uint8_t start_addr,
1485 const uint8_t *buf,
1486 uint32_t num_bytes)
1487{
1488 struct i2c_msg msg[2];
1489
1490 msg[0].buf = &start_addr;
1491 msg[0].len = 1U;
1492 msg[0].flags = I2C_MSG_WRITE;
1493
1494 msg[1].buf = (uint8_t *)buf;
1495 msg[1].len = num_bytes;
1496 msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
1497
1498 return i2c_transfer(dev, msg, 2, dev_addr);
1499}
1500
1515static inline int i2c_burst_write_dt(const struct i2c_dt_spec *spec,
1516 uint8_t start_addr,
1517 const uint8_t *buf,
1518 uint32_t num_bytes)
1519{
1520 return i2c_burst_write(spec->bus, spec->addr,
1521 start_addr, buf, num_bytes);
1522}
1523
1539static inline int i2c_reg_read_byte(const struct device *dev,
1540 uint16_t dev_addr,
1541 uint8_t reg_addr, uint8_t *value)
1542{
1543 return i2c_write_read(dev, dev_addr,
1544 &reg_addr, sizeof(reg_addr),
1545 value, sizeof(*value));
1546}
1547
1561static inline int i2c_reg_read_byte_dt(const struct i2c_dt_spec *spec,
1562 uint8_t reg_addr, uint8_t *value)
1563{
1564 return i2c_reg_read_byte(spec->bus, spec->addr, reg_addr, value);
1565}
1566
1585static inline int i2c_reg_write_byte(const struct device *dev,
1586 uint16_t dev_addr,
1587 uint8_t reg_addr, uint8_t value)
1588{
1589 uint8_t tx_buf[2] = {reg_addr, value};
1590
1591 return i2c_write(dev, tx_buf, 2, dev_addr);
1592}
1593
1607static inline int i2c_reg_write_byte_dt(const struct i2c_dt_spec *spec,
1608 uint8_t reg_addr, uint8_t value)
1609{
1610 return i2c_reg_write_byte(spec->bus, spec->addr, reg_addr, value);
1611}
1612
1632static inline int i2c_reg_update_byte(const struct device *dev,
1633 uint8_t dev_addr,
1634 uint8_t reg_addr, uint8_t mask,
1635 uint8_t value)
1636{
1637 uint8_t old_value, new_value;
1638 int rc;
1639
1640 rc = i2c_reg_read_byte(dev, dev_addr, reg_addr, &old_value);
1641 if (rc != 0) {
1642 return rc;
1643 }
1644
1645 new_value = (old_value & ~mask) | (value & mask);
1646 if (new_value == old_value) {
1647 return 0;
1648 }
1649
1650 return i2c_reg_write_byte(dev, dev_addr, reg_addr, new_value);
1651}
1652
1667static inline int i2c_reg_update_byte_dt(const struct i2c_dt_spec *spec,
1668 uint8_t reg_addr, uint8_t mask,
1669 uint8_t value)
1670{
1671 return i2c_reg_update_byte(spec->bus, spec->addr,
1672 reg_addr, mask, value);
1673}
1674
1675#ifdef __cplusplus
1676}
1677#endif
1678
1683#include <zephyr/syscalls/i2c.h>
1684
1685#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:884
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:1515
static int i2c_target_unregister(const struct device *dev, struct i2c_target_config *cfg)
Unregisters the provided config as Target device.
Definition i2c.h:1195
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:1366
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:1033
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:332
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:1272
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:1297
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:1400
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:916
static bool i2c_is_stop_op(const struct i2c_msg *msg)
Check if the current message includes a stop.
Definition i2c.h:492
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:414
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:1427
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:354
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:289
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:1667
void(* i2c_callback_t)(const struct device *dev, int result, void *data)
I2C callback for asynchronous transfer requests.
Definition i2c.h:202
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:1342
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:310
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:1607
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:1585
#define I2C_MSG_READ
Read message from I2C bus.
Definition i2c.h:147
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:1561
int i2c_configure(const struct device *dev, uint32_t dev_config)
Configure operation of a host controller.
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:957
#define I2C_MSG_RESTART
RESTART I2C transaction for this message.
Definition i2c.h:163
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:1110
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:1317
static bool i2c_is_read_op(const struct i2c_msg *msg)
Check if the current message is a read operation.
Definition i2c.h:480
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:1452
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:1166
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:843
#define I2C_MSG_STOP
Send STOP after this message.
Definition i2c.h:154
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:584
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:540
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:993
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:1632
const struct rtio_iodev_api i2c_iodev_api
#define I2C_MSG_WRITE
Write message to I2C bus.
Definition i2c.h:144
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:1539
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:1482
static bool i2c_is_ready_dt(const struct i2c_dt_spec *spec)
Validate that I2C bus is ready.
Definition i2c.h:467
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:1151
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:140
#define CONTAINER_OF(ptr, type, field)
Get a pointer to a structure containing the element.
Definition util.h:284
#define EINVAL
Invalid argument.
Definition errno.h:60
#define ENOSYS
Function not implemented.
Definition errno.h:82
Public kernel APIs.
flags
Definition parser.h:96
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:380
Runtime device structure (in ROM) per driver instance.
Definition device.h:412
void * data
Address of the device instance private data.
Definition device.h:422
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:418
struct device_state * state
Address of the common device state.
Definition device.h:420
I2C specific device state which allows for i2c device class specific additions.
Definition i2c.h:572
struct stats_i2c stats
Definition i2c.h:574
struct device_state devstate
Definition i2c.h:573
Complete I2C DT information.
Definition i2c.h:77
const struct device * bus
Definition i2c.h:78
uint16_t addr
Definition i2c.h:79
One I2C Message.
Definition i2c.h:184
uint8_t * buf
Data buffer in bytes.
Definition i2c.h:186
uint32_t len
Length of buffer in bytes.
Definition i2c.h:189
uint8_t flags
Flags for this message.
Definition i2c.h:192
Structure providing callbacks to be implemented for devices that supports the I2C target API.
Definition i2c.h:422
i2c_target_read_requested_cb_t read_requested
Definition i2c.h:424
i2c_target_write_received_cb_t write_received
Definition i2c.h:425
i2c_target_read_processed_cb_t read_processed
Definition i2c.h:426
i2c_target_write_requested_cb_t write_requested
Definition i2c.h:423
i2c_target_stop_cb_t stop
Definition i2c.h:431
Structure describing a device that supports the I2C target API.
Definition i2c.h:445
uint8_t flags
Flags for the target device defined by I2C_TARGET_FLAGS_* constants.
Definition i2c.h:450
uint16_t address
Address for this target device.
Definition i2c.h:453
sys_snode_t node
Private, do not modify.
Definition i2c.h:447
const struct i2c_target_callbacks * callbacks
Callback functions.
Definition i2c.h:456
Definition kernel.h:5768
API that an RTIO IO device should implement.
Definition rtio.h:439
Compute the mempool block index for a given pointer.
Definition rtio.h:429
struct rtio_sqe sqe
Definition rtio.h:430
An IO device with a function table for submitting requests.
Definition rtio.h:454
void * data
Definition rtio.h:459
A submission queue event.
Definition rtio.h:232
const struct rtio_iodev * iodev
Device to operation on.
Definition rtio.h:243
An RTIO context containing what can be viewed as a pair of queues.
Definition rtio.h:333