1 /**
2  * @file
3  *
4  * @brief Public APIs for the I2C emulation drivers.
5  */
6 
7 /*
8  * Copyright 2020 Google LLC
9  * Copyright (c) 2020 Nordic Semiconductor ASA
10  *
11  * SPDX-License-Identifier: Apache-2.0
12  */
13 #ifndef ZEPHYR_INCLUDE_DRIVERS_I2C_I2C_EMUL_H_
14 #define ZEPHYR_INCLUDE_DRIVERS_I2C_I2C_EMUL_H_
15 
16 #include <zephyr/device.h>
17 #include <zephyr/drivers/emul.h>
18 #include <zephyr/drivers/i2c.h>
19 #include <zephyr/sys/slist.h>
20 #include <zephyr/types.h>
21 
22 /**
23  * @brief I2C Emulation Interface
24  * @defgroup i2c_emul_interface I2C Emulation Interface
25  * @ingroup io_emulators
26  * @{
27  */
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 struct i2c_msg;
34 struct i2c_emul_api;
35 
36 /** Node in a linked list of emulators for I2C devices */
37 struct i2c_emul {
38 	sys_snode_t node;
39 
40 	/** Target emulator - REQUIRED for all emulated bus nodes of any type */
41 	const struct emul *target;
42 
43 	/* API provided for this device */
44 	const struct i2c_emul_api *api;
45 
46 	/**
47 	 * A mock API that if not NULL will take precedence over the actual API. If set, a return
48 	 * value of -ENOSYS will revert back to the default api.
49 	 */
50 	struct i2c_emul_api *mock_api;
51 
52 	/* I2C address of the emulated device */
53 	uint16_t addr;
54 };
55 
56 /**
57  * Passes I2C messages to the emulator. The emulator updates the data with what
58  * was read back.
59  *
60  * @param target The device Emulator instance.
61  * @param msgs Array of messages to transfer. For 'read' messages, this function
62  *	updates the 'buf' member with the data that was read.
63  * @param num_msgs Number of messages to transfer.
64  * @param addr Address of the I2C target device.
65  *
66  * @retval 0 If successful.
67  * @retval -EIO General input / output error.
68  */
69 typedef int (*i2c_emul_transfer_t)(const struct emul *target, struct i2c_msg *msgs, int num_msgs,
70 				   int addr);
71 
72 /**
73  * Register an emulated device on the controller
74  *
75  * @param dev Device that will use the emulator
76  * @param emul I2C emulator to use
77  * @return 0 indicating success (always)
78  */
79 int i2c_emul_register(const struct device *dev, struct i2c_emul *emul);
80 
81 /** Definition of the emulator API */
82 struct i2c_emul_api {
83 	i2c_emul_transfer_t transfer;
84 };
85 
86 #ifdef __cplusplus
87 }
88 #endif
89 
90 /**
91  * @}
92  */
93 
94 #endif /* ZEPHYR_INCLUDE_DRIVERS_I2C_I2C_EMUL_H_ */
95