Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
22#include <errno.h>
23
24#include <zephyr/types.h>
25#include <zephyr/device.h>
26#include <zephyr/kernel.h>
27#include <zephyr/sys/slist.h>
28#include <zephyr/rtio/rtio.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/*
35 * The following #defines are used to configure the I2C controller.
36 */
37
39#define I2C_SPEED_STANDARD (0x1U)
40
42#define I2C_SPEED_FAST (0x2U)
43
45#define I2C_SPEED_FAST_PLUS (0x3U)
46
48#define I2C_SPEED_HIGH (0x4U)
49
51#define I2C_SPEED_ULTRA (0x5U)
52
54#define I2C_SPEED_DT (0x7U)
55
56#define I2C_SPEED_SHIFT (1U)
57#define I2C_SPEED_SET(speed) (((speed) << I2C_SPEED_SHIFT) \
58 & I2C_SPEED_MASK)
59#define I2C_SPEED_MASK (0x7U << I2C_SPEED_SHIFT) /* 3 bits */
60#define I2C_SPEED_GET(cfg) (((cfg) & I2C_SPEED_MASK) \
61 >> I2C_SPEED_SHIFT)
62
64#define I2C_ADDR_10_BITS BIT(0)
65
67#define I2C_MODE_CONTROLLER BIT(4)
68
76 const struct device *bus;
78};
79
90#define I2C_DT_SPEC_GET_ON_I3C(node_id) \
91 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
92 .addr = DT_PROP_BY_IDX(node_id, reg, 0)
93
104#define I2C_DT_SPEC_GET_ON_I2C(node_id) \
105 .bus = DEVICE_DT_GET(DT_BUS(node_id)), \
106 .addr = DT_REG_ADDR(node_id)
107
118#define I2C_DT_SPEC_GET(node_id) \
119 { \
120 COND_CODE_1(DT_ON_BUS(node_id, i3c), \
121 (I2C_DT_SPEC_GET_ON_I3C(node_id)), \
122 (I2C_DT_SPEC_GET_ON_I2C(node_id))) \
123 }
124
133#define I2C_DT_SPEC_INST_GET(inst) \
134 I2C_DT_SPEC_GET(DT_DRV_INST(inst))
135
136
137/*
138 * I2C_MSG_* are I2C Message flags.
139 */
140
142#define I2C_MSG_WRITE (0U << 0U)
143
145#define I2C_MSG_READ BIT(0)
146
148#define I2C_MSG_RW_MASK BIT(0)
152#define I2C_MSG_STOP BIT(1)
153
161#define I2C_MSG_RESTART BIT(2)
162
166#define I2C_MSG_ADDR_10_BITS BIT(3)
167
182struct i2c_msg {
185
188
191};
192
200typedef void (*i2c_callback_t)(const struct device *dev, int result, void *data);
201
208struct i2c_target_config;
209
210typedef int (*i2c_api_configure_t)(const struct device *dev,
211 uint32_t dev_config);
212typedef int (*i2c_api_get_config_t)(const struct device *dev,
213 uint32_t *dev_config);
214typedef int (*i2c_api_full_io_t)(const struct device *dev,
215 struct i2c_msg *msgs,
216 uint8_t num_msgs,
217 uint16_t addr);
218typedef int (*i2c_api_target_register_t)(const struct device *dev,
219 struct i2c_target_config *cfg);
220typedef int (*i2c_api_target_unregister_t)(const struct device *dev,
221 struct i2c_target_config *cfg);
222#ifdef CONFIG_I2C_CALLBACK
223typedef int (*i2c_api_transfer_cb_t)(const struct device *dev,
224 struct i2c_msg *msgs,
225 uint8_t num_msgs,
226 uint16_t addr,
228 void *userdata);
229#endif /* CONFIG_I2C_CALLBACK */
230#if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
231
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
260struct i2c_target_driver_api {
261 i2c_target_api_register_t driver_register;
262 i2c_target_api_unregister_t driver_unregister;
263};
264
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#ifdef CONFIG_I2C_TARGET_BUFFER_MODE
369typedef void (*i2c_target_buf_write_received_cb_t)(
370 struct i2c_target_config *config, uint8_t *ptr, uint32_t len);
371
394typedef int (*i2c_target_buf_read_requested_cb_t)(
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
425#ifdef CONFIG_I2C_TARGET_BUFFER_MODE
426 i2c_target_buf_write_received_cb_t buf_write_received;
427 i2c_target_buf_read_requested_cb_t buf_read_requested;
428#endif
430};
431
446
449
452
455};
456
465static inline bool i2c_is_ready_dt(const struct i2c_dt_spec *spec)
466{
467 /* Validate bus is ready */
468 return device_is_ready(spec->bus);
469}
470
497void i2c_dump_msgs_rw(const struct device *dev, const struct i2c_msg *msgs, uint8_t num_msgs,
498 uint16_t addr, bool dump_read);
499
514static inline void i2c_dump_msgs(const struct device *dev, const struct i2c_msg *msgs,
515 uint8_t num_msgs, uint16_t addr)
516{
517 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, false);
518}
519
520#if defined(CONFIG_I2C_STATS) || defined(__DOXYGEN__)
521
522#include <zephyr/stats/stats.h>
523
527STATS_SECT_ENTRY32(bytes_read)
528STATS_SECT_ENTRY32(bytes_written)
529STATS_SECT_ENTRY32(message_count)
530STATS_SECT_ENTRY32(transfer_call_count)
532
534STATS_NAME(i2c, bytes_read)
535STATS_NAME(i2c, bytes_written)
536STATS_NAME(i2c, message_count)
537STATS_NAME(i2c, transfer_call_count)
538STATS_NAME_END(i2c);
539
548 struct stats_i2c stats;
549};
550
558static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
559 uint8_t num_msgs)
560{
561 struct i2c_device_state *state =
563 uint32_t bytes_read = 0U;
564 uint32_t bytes_written = 0U;
565
566 STATS_INC(state->stats, transfer_call_count);
567 STATS_INCN(state->stats, message_count, num_msgs);
568 for (uint8_t i = 0U; i < num_msgs; i++) {
569 if (msgs[i].flags & I2C_MSG_READ) {
570 bytes_read += msgs[i].len;
571 } else {
572 bytes_written += msgs[i].len;
573 }
574 }
575 STATS_INCN(state->stats, bytes_read, bytes_read);
576 STATS_INCN(state->stats, bytes_written, bytes_written);
577}
578
584#define Z_I2C_DEVICE_STATE_DEFINE(dev_id) \
585 static struct i2c_device_state Z_DEVICE_STATE_NAME(dev_id) \
586 __attribute__((__section__(".z_devstate")))
587
594#define Z_I2C_INIT_FN(dev_id, init_fn) \
595 static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
596 { \
597 struct i2c_device_state *state = \
598 CONTAINER_OF(dev->state, struct i2c_device_state, devstate); \
599 stats_init(&state->stats.s_hdr, STATS_SIZE_32, 4, \
600 STATS_NAME_INIT_PARMS(i2c)); \
601 stats_register(dev->name, &(state->stats.s_hdr)); \
602 if (init_fn != NULL) { \
603 return init_fn(dev); \
604 } \
605 \
606 return 0; \
607 }
608
638#define I2C_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
639 prio, api, ...) \
640 Z_I2C_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
641 Z_I2C_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn) \
642 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
643 DEVICE_DT_NAME(node_id), \
644 &UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init), \
645 pm, data, config, level, prio, api, \
646 &(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
647 __VA_ARGS__)
648
649#else /* CONFIG_I2C_STATS */
650
651static inline void i2c_xfer_stats(const struct device *dev, struct i2c_msg *msgs,
652 uint8_t num_msgs)
653{
654 ARG_UNUSED(dev);
655 ARG_UNUSED(msgs);
656 ARG_UNUSED(num_msgs);
657}
658
659#define I2C_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
660 prio, api, ...) \
661 DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, \
662 prio, api, __VA_ARGS__)
663
664#endif /* CONFIG_I2C_STATS */
665
674#define I2C_DEVICE_DT_INST_DEFINE(inst, ...) \
675 I2C_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
676
677
688__syscall int i2c_configure(const struct device *dev, uint32_t dev_config);
689
690static inline int z_impl_i2c_configure(const struct device *dev,
691 uint32_t dev_config)
692{
693 const struct i2c_driver_api *api =
694 (const struct i2c_driver_api *)dev->api;
695
696 return api->configure(dev, dev_config);
697}
698
719__syscall int i2c_get_config(const struct device *dev, uint32_t *dev_config);
720
721static inline int z_impl_i2c_get_config(const struct device *dev, uint32_t *dev_config)
722{
723 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
724
725 if (api->get_config == NULL) {
726 return -ENOSYS;
727 }
728
729 return api->get_config(dev, dev_config);
730}
731
759__syscall int i2c_transfer(const struct device *dev,
760 struct i2c_msg *msgs, uint8_t num_msgs,
761 uint16_t addr);
762
763static inline int z_impl_i2c_transfer(const struct device *dev,
764 struct i2c_msg *msgs, uint8_t num_msgs,
765 uint16_t addr)
766{
767 const struct i2c_driver_api *api =
768 (const struct i2c_driver_api *)dev->api;
769
770 int res = api->transfer(dev, msgs, num_msgs, addr);
771
772 i2c_xfer_stats(dev, msgs, num_msgs);
773
774 if (IS_ENABLED(CONFIG_I2C_DUMP_MESSAGES)) {
775 i2c_dump_msgs_rw(dev, msgs, num_msgs, addr, true);
776 }
777
778 return res;
779}
780
781#if defined(CONFIG_I2C_CALLBACK) || defined(__DOXYGEN__)
782
805static inline int i2c_transfer_cb(const struct device *dev,
806 struct i2c_msg *msgs,
807 uint8_t num_msgs,
808 uint16_t addr,
810 void *userdata)
811{
812 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
813
814 if (api->transfer_cb == NULL) {
815 return -ENOSYS;
816 }
817
818 return api->transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
819}
820
836static inline int i2c_transfer_cb_dt(const struct i2c_dt_spec *spec,
837 struct i2c_msg *msgs,
838 uint8_t num_msgs,
840 void *userdata)
841{
842 return i2c_transfer_cb(spec->bus, msgs, num_msgs, spec->addr, cb, userdata);
843}
844
868static inline int i2c_write_read_cb(const struct device *dev, struct i2c_msg *msgs,
869 uint8_t num_msgs, uint16_t addr, const void *write_buf,
870 size_t num_write, void *read_buf, size_t num_read,
871 i2c_callback_t cb, void *userdata)
872{
873 if ((msgs == NULL) || (num_msgs != 2)) {
874 return -EINVAL;
875 }
876
877 msgs[0].buf = (uint8_t *)write_buf;
878 msgs[0].len = num_write;
879 msgs[0].flags = I2C_MSG_WRITE;
880
881 msgs[1].buf = (uint8_t *)read_buf;
882 msgs[1].len = num_read;
884
885 return i2c_transfer_cb(dev, msgs, num_msgs, addr, cb, userdata);
886}
887
909static inline int i2c_write_read_cb_dt(const struct i2c_dt_spec *spec, struct i2c_msg *msgs,
910 uint8_t num_msgs, const void *write_buf, size_t num_write,
911 void *read_buf, size_t num_read, i2c_callback_t cb,
912 void *userdata)
913{
914 return i2c_write_read_cb(spec->bus, msgs, num_msgs, spec->addr, write_buf, num_write,
915 read_buf, num_read, cb, userdata);
916}
917
918#if defined(CONFIG_POLL) || defined(__DOXYGEN__)
919
921void z_i2c_transfer_signal_cb(const struct device *dev, int result, void *userdata);
945static inline int i2c_transfer_signal(const struct device *dev,
946 struct i2c_msg *msgs,
947 uint8_t num_msgs,
948 uint16_t addr,
949 struct k_poll_signal *sig)
950{
951 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
952
953 if (api->transfer_cb == NULL) {
954 return -ENOSYS;
955 }
956
957 return api->transfer_cb(dev, msgs, num_msgs, addr, z_i2c_transfer_signal_cb, sig);
958}
959
960#endif /* CONFIG_POLL */
961
962#endif /* CONFIG_I2C_CALLBACK */
963
964
965#if defined(CONFIG_I2C_RTIO) || defined(__DOXYGEN__)
966
973static inline void i2c_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
974{
975 const struct i2c_dt_spec *dt_spec = iodev_sqe->sqe->iodev->data;
976 const struct device *dev = dt_spec->bus;
977 const struct i2c_driver_api *api = (const struct i2c_driver_api *)dev->api;
978
979 api->iodev_submit(dt_spec->bus, iodev_sqe);
980}
981
982extern const struct rtio_iodev_api i2c_iodev_api;
983
993#define I2C_DT_IODEV_DEFINE(name, node_id) \
994 const struct i2c_dt_spec _i2c_dt_spec_##name = \
995 I2C_DT_SPEC_GET(node_id); \
996 RTIO_IODEV_DEFINE(name, &i2c_iodev_api, (void *)&_i2c_dt_spec_##name)
997
1010 struct rtio_iodev *iodev,
1011 const struct i2c_msg *msgs,
1012 uint8_t num_msgs);
1013
1014#endif /* CONFIG_I2C_RTIO */
1015
1029static inline int i2c_transfer_dt(const struct i2c_dt_spec *spec,
1030 struct i2c_msg *msgs, uint8_t num_msgs)
1031{
1032 return i2c_transfer(spec->bus, msgs, num_msgs, spec->addr);
1033}
1034
1047__syscall int i2c_recover_bus(const struct device *dev);
1048
1049static inline int z_impl_i2c_recover_bus(const struct device *dev)
1050{
1051 const struct i2c_driver_api *api =
1052 (const struct i2c_driver_api *)dev->api;
1053
1054 if (api->recover_bus == NULL) {
1055 return -ENOSYS;
1056 }
1057
1058 return api->recover_bus(dev);
1059}
1060
1085static inline int i2c_target_register(const struct device *dev,
1086 struct i2c_target_config *cfg)
1087{
1088 const struct i2c_driver_api *api =
1089 (const struct i2c_driver_api *)dev->api;
1090
1091 if (api->target_register == NULL) {
1092 return -ENOSYS;
1093 }
1094
1095 return api->target_register(dev, cfg);
1096}
1097
1114static inline int i2c_target_unregister(const struct device *dev,
1115 struct i2c_target_config *cfg)
1116{
1117 const struct i2c_driver_api *api =
1118 (const struct i2c_driver_api *)dev->api;
1119
1120 if (api->target_unregister == NULL) {
1121 return -ENOSYS;
1122 }
1123
1124 return api->target_unregister(dev, cfg);
1125}
1126
1140__syscall int i2c_target_driver_register(const struct device *dev);
1141
1142static inline int z_impl_i2c_target_driver_register(const struct device *dev)
1143{
1144 const struct i2c_target_driver_api *api =
1145 (const struct i2c_target_driver_api *)dev->api;
1146
1147 return api->driver_register(dev);
1148}
1149
1163__syscall int i2c_target_driver_unregister(const struct device *dev);
1164
1165static inline int z_impl_i2c_target_driver_unregister(const struct device *dev)
1166{
1167 const struct i2c_target_driver_api *api =
1168 (const struct i2c_target_driver_api *)dev->api;
1169
1170 return api->driver_unregister(dev);
1171}
1172
1173/*
1174 * Derived i2c APIs -- all implemented in terms of i2c_transfer()
1175 */
1176
1191static inline int i2c_write(const struct device *dev, const uint8_t *buf,
1192 uint32_t num_bytes, uint16_t addr)
1193{
1194 struct i2c_msg msg;
1195
1196 msg.buf = (uint8_t *)buf;
1197 msg.len = num_bytes;
1199
1200 return i2c_transfer(dev, &msg, 1, addr);
1201}
1202
1216static inline int i2c_write_dt(const struct i2c_dt_spec *spec,
1217 const uint8_t *buf, uint32_t num_bytes)
1218{
1219 return i2c_write(spec->bus, buf, num_bytes, spec->addr);
1220}
1221
1236static inline int i2c_read(const struct device *dev, uint8_t *buf,
1237 uint32_t num_bytes, uint16_t addr)
1238{
1239 struct i2c_msg msg;
1240
1241 msg.buf = buf;
1242 msg.len = num_bytes;
1244
1245 return i2c_transfer(dev, &msg, 1, addr);
1246}
1247
1261static inline int i2c_read_dt(const struct i2c_dt_spec *spec,
1262 uint8_t *buf, uint32_t num_bytes)
1263{
1264 return i2c_read(spec->bus, buf, num_bytes, spec->addr);
1265}
1266
1285static inline int i2c_write_read(const struct device *dev, uint16_t addr,
1286 const void *write_buf, size_t num_write,
1287 void *read_buf, size_t num_read)
1288{
1289 struct i2c_msg msg[2];
1290
1291 msg[0].buf = (uint8_t *)write_buf;
1292 msg[0].len = num_write;
1293 msg[0].flags = I2C_MSG_WRITE;
1294
1295 msg[1].buf = (uint8_t *)read_buf;
1296 msg[1].len = num_read;
1298
1299 return i2c_transfer(dev, msg, 2, addr);
1300}
1301
1319static inline int i2c_write_read_dt(const struct i2c_dt_spec *spec,
1320 const void *write_buf, size_t num_write,
1321 void *read_buf, size_t num_read)
1322{
1323 return i2c_write_read(spec->bus, spec->addr,
1324 write_buf, num_write,
1325 read_buf, num_read);
1326}
1327
1346static inline int i2c_burst_read(const struct device *dev,
1347 uint16_t dev_addr,
1348 uint8_t start_addr,
1349 uint8_t *buf,
1350 uint32_t num_bytes)
1351{
1352 return i2c_write_read(dev, dev_addr,
1353 &start_addr, sizeof(start_addr),
1354 buf, num_bytes);
1355}
1356
1371static inline int i2c_burst_read_dt(const struct i2c_dt_spec *spec,
1372 uint8_t start_addr,
1373 uint8_t *buf,
1374 uint32_t num_bytes)
1375{
1376 return i2c_burst_read(spec->bus, spec->addr,
1377 start_addr, buf, num_bytes);
1378}
1379
1401static inline int i2c_burst_write(const struct device *dev,
1402 uint16_t dev_addr,
1403 uint8_t start_addr,
1404 const uint8_t *buf,
1405 uint32_t num_bytes)
1406{
1407 struct i2c_msg msg[2];
1408
1409 msg[0].buf = &start_addr;
1410 msg[0].len = 1U;
1411 msg[0].flags = I2C_MSG_WRITE;
1412
1413 msg[1].buf = (uint8_t *)buf;
1414 msg[1].len = num_bytes;
1415 msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
1416
1417 return i2c_transfer(dev, msg, 2, dev_addr);
1418}
1419
1434static inline int i2c_burst_write_dt(const struct i2c_dt_spec *spec,
1435 uint8_t start_addr,
1436 const uint8_t *buf,
1437 uint32_t num_bytes)
1438{
1439 return i2c_burst_write(spec->bus, spec->addr,
1440 start_addr, buf, num_bytes);
1441}
1442
1458static inline int i2c_reg_read_byte(const struct device *dev,
1459 uint16_t dev_addr,
1460 uint8_t reg_addr, uint8_t *value)
1461{
1462 return i2c_write_read(dev, dev_addr,
1463 &reg_addr, sizeof(reg_addr),
1464 value, sizeof(*value));
1465}
1466
1480static inline int i2c_reg_read_byte_dt(const struct i2c_dt_spec *spec,
1481 uint8_t reg_addr, uint8_t *value)
1482{
1483 return i2c_reg_read_byte(spec->bus, spec->addr, reg_addr, value);
1484}
1485
1504static inline int i2c_reg_write_byte(const struct device *dev,
1505 uint16_t dev_addr,
1506 uint8_t reg_addr, uint8_t value)
1507{
1508 uint8_t tx_buf[2] = {reg_addr, value};
1509
1510 return i2c_write(dev, tx_buf, 2, dev_addr);
1511}
1512
1526static inline int i2c_reg_write_byte_dt(const struct i2c_dt_spec *spec,
1527 uint8_t reg_addr, uint8_t value)
1528{
1529 return i2c_reg_write_byte(spec->bus, spec->addr, reg_addr, value);
1530}
1531
1551static inline int i2c_reg_update_byte(const struct device *dev,
1552 uint8_t dev_addr,
1553 uint8_t reg_addr, uint8_t mask,
1554 uint8_t value)
1555{
1556 uint8_t old_value, new_value;
1557 int rc;
1558
1559 rc = i2c_reg_read_byte(dev, dev_addr, reg_addr, &old_value);
1560 if (rc != 0) {
1561 return rc;
1562 }
1563
1564 new_value = (old_value & ~mask) | (value & mask);
1565 if (new_value == old_value) {
1566 return 0;
1567 }
1568
1569 return i2c_reg_write_byte(dev, dev_addr, reg_addr, new_value);
1570}
1571
1586static inline int i2c_reg_update_byte_dt(const struct i2c_dt_spec *spec,
1587 uint8_t reg_addr, uint8_t mask,
1588 uint8_t value)
1589{
1590 return i2c_reg_update_byte(spec->bus, spec->addr,
1591 reg_addr, mask, value);
1592}
1593
1594#ifdef __cplusplus
1595}
1596#endif
1597
1602#include <syscalls/i2c.h>
1603
1604#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:836
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:1434
static int i2c_target_unregister(const struct device *dev, struct i2c_target_config *cfg)
Unregisters the provided config as Target device.
Definition: i2c.h:1114
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:1285
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:973
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:1191
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:1216
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:1319
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:868
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 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:1346
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
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:1586
void(* i2c_callback_t)(const struct device *dev, int result, void *data)
I2C callback for asynchronous transfer requests.
Definition: i2c.h:200
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:1261
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:1526
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:1504
#define I2C_MSG_READ
Read message from I2C bus.
Definition: i2c.h:145
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:1480
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:909
#define I2C_MSG_RESTART
RESTART I2C transaction for this message.
Definition: i2c.h:161
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:1029
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:1236
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:1371
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:1085
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:805
#define I2C_MSG_STOP
Send STOP after this message.
Definition: i2c.h:152
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:558
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:514
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:945
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:1551
const struct rtio_iodev_api i2c_iodev_api
#define I2C_MSG_WRITE
Write message to I2C bus.
Definition: i2c.h:142
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:1458
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:1401
static bool i2c_is_ready_dt(const struct i2c_dt_spec *spec)
Validate that I2C bus is ready.
Definition: i2c.h:465
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:124
#define CONTAINER_OF(ptr, type, field)
Get a pointer to a structure containing the element.
Definition: util.h:247
#define EINVAL
Invalid argument.
Definition: errno.h:61
#define ENOSYS
Function not implemented.
Definition: errno.h:83
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:354
Runtime device structure (in ROM) per driver instance.
Definition: device.h:381
void * data
Address of the device instance private data.
Definition: device.h:391
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:387
struct device_state * state
Address of the common device state.
Definition: device.h:389
I2C specific device state which allows for i2c device class specific additions.
Definition: i2c.h:546
struct stats_i2c stats
Definition: i2c.h:548
struct device_state devstate
Definition: i2c.h:547
Complete I2C DT information.
Definition: i2c.h:75
const struct device * bus
Definition: i2c.h:76
uint16_t addr
Definition: i2c.h:77
One I2C Message.
Definition: i2c.h:182
uint8_t * buf
Data buffer in bytes.
Definition: i2c.h:184
uint32_t len
Length of buffer in bytes.
Definition: i2c.h:187
uint8_t flags
Flags for this message.
Definition: i2c.h:190
Structure providing callbacks to be implemented for devices that supports the I2C target API.
Definition: i2c.h:420
i2c_target_read_requested_cb_t read_requested
Definition: i2c.h:422
i2c_target_write_received_cb_t write_received
Definition: i2c.h:423
i2c_target_read_processed_cb_t read_processed
Definition: i2c.h:424
i2c_target_write_requested_cb_t write_requested
Definition: i2c.h:421
i2c_target_stop_cb_t stop
Definition: i2c.h:429
Structure describing a device that supports the I2C target API.
Definition: i2c.h:443
uint8_t flags
Flags for the target device defined by I2C_TARGET_FLAGS_* constants.
Definition: i2c.h:448
uint16_t address
Address for this target device.
Definition: i2c.h:451
sys_snode_t node
Private, do not modify.
Definition: i2c.h:445
const struct i2c_target_callbacks * callbacks
Callback functions.
Definition: i2c.h:454
Definition: kernel.h:5627
API that an RTIO IO device should implement.
Definition: rtio.h:429
Compute the mempool block index for a given pointer.
Definition: rtio.h:419
struct rtio_sqe sqe
Definition: rtio.h:420
An IO device with a function table for submitting requests.
Definition: rtio.h:444
void * data
Definition: rtio.h:452
A submission queue event.
Definition: rtio.h:230
const struct rtio_iodev * iodev
Device to operation on.
Definition: rtio.h:241
An RTIO context containing what can be viewed as a pair of queues.
Definition: rtio.h:323