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/sys/slist.h>
19 #include <zephyr/types.h>
20 
21 /**
22  * @brief I2C Emulation Interface
23  * @defgroup i2c_emul_interface I2C Emulation Interface
24  * @ingroup io_emulators
25  * @{
26  */
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 struct i2c_msg;
33 struct i2c_emul_api;
34 
35 /** Node in a linked list of emulators for I2C devices */
36 struct i2c_emul {
37 	sys_snode_t node;
38 
39 	/** Target emulator - REQUIRED for all emulated bus nodes of any type */
40 	const struct emul *target;
41 
42 	/* API provided for this device */
43 	const struct i2c_emul_api *api;
44 
45 	/* I2C address of the emulated device */
46 	uint16_t addr;
47 };
48 
49 /**
50  * Passes I2C messages to the emulator. The emulator updates the data with what
51  * was read back.
52  *
53  * @param target The device Emulator instance.
54  * @param msgs Array of messages to transfer. For 'read' messages, this function
55  *	updates the 'buf' member with the data that was read.
56  * @param num_msgs Number of messages to transfer.
57  * @param addr Address of the I2C target device.
58  *
59  * @retval 0 If successful.
60  * @retval -EIO General input / output error.
61  */
62 typedef int (*i2c_emul_transfer_t)(const struct emul *target, struct i2c_msg *msgs, int num_msgs,
63 				   int addr);
64 
65 /**
66  * Register an emulated device on the controller
67  *
68  * @param dev Device that will use the emulator
69  * @param emul I2C emulator to use
70  * @return 0 indicating success (always)
71  */
72 int i2c_emul_register(const struct device *dev, struct i2c_emul *emul);
73 
74 /** Definition of the emulator API */
75 struct i2c_emul_api {
76 	i2c_emul_transfer_t transfer;
77 };
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 
83 /**
84  * @}
85  */
86 
87 #endif /* ZEPHYR_INCLUDE_DRIVERS_I2C_I2C_EMUL_H_ */
88