Line data Source code
1 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 : * @ingroup i2c_interface
27 : * @{
28 : */
29 :
30 : #ifdef __cplusplus
31 : extern "C" {
32 : #endif
33 :
34 : struct i2c_msg;
35 : struct i2c_emul_api;
36 :
37 : /** Node in a linked list of emulators for I2C devices */
38 1 : struct i2c_emul {
39 0 : sys_snode_t node;
40 :
41 : /** Target emulator - REQUIRED for all emulated bus nodes of any type */
42 1 : const struct emul *target;
43 :
44 : /* API provided for this device */
45 0 : const struct i2c_emul_api *api;
46 :
47 : /**
48 : * A mock API that if not NULL will take precedence over the actual API. If set, a return
49 : * value of -ENOSYS will revert back to the default api.
50 : */
51 1 : struct i2c_emul_api *mock_api;
52 :
53 : /* I2C address of the emulated device */
54 0 : uint16_t addr;
55 : };
56 :
57 : /**
58 : * Passes I2C messages to the emulator. The emulator updates the data with what
59 : * was read back.
60 : *
61 : * @param target The device Emulator instance.
62 : * @param msgs Array of messages to transfer. For 'read' messages, this function
63 : * updates the 'buf' member with the data that was read.
64 : * @param num_msgs Number of messages to transfer.
65 : * @param addr Address of the I2C target device.
66 : *
67 : * @retval 0 If successful.
68 : * @retval -EIO General input / output error.
69 : */
70 1 : typedef int (*i2c_emul_transfer_t)(const struct emul *target, struct i2c_msg *msgs, int num_msgs,
71 : int addr);
72 :
73 : /**
74 : * Register an emulated device on the controller
75 : *
76 : * @param dev Device that will use the emulator
77 : * @param emul I2C emulator to use
78 : * @return 0 indicating success (always)
79 : */
80 1 : int i2c_emul_register(const struct device *dev, struct i2c_emul *emul);
81 :
82 : /** Definition of the emulator API */
83 1 : struct i2c_emul_api {
84 0 : i2c_emul_transfer_t transfer;
85 : };
86 :
87 : #ifdef __cplusplus
88 : }
89 : #endif
90 :
91 : /**
92 : * @}
93 : */
94 :
95 : #endif /* ZEPHYR_INCLUDE_DRIVERS_I2C_I2C_EMUL_H_ */
|