1 /*
2 * Copyright (c) 2024 Open Pixel Systems
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 #include <zephyr/drivers/i2c.h>
10
11 static const struct device *bus = DEVICE_DT_GET(DT_NODELABEL(flexcomm4));
12 static char last_byte;
13
14 /*
15 * @brief Callback which is called when a write request is received from the master.
16 * @param config Pointer to the target configuration.
17 */
sample_target_write_requested_cb(struct i2c_target_config * config)18 int sample_target_write_requested_cb(struct i2c_target_config *config)
19 {
20 printk("sample target write requested\n");
21 return 0;
22 }
23
24 /*
25 * @brief Callback which is called when a write is received from the master.
26 * @param config Pointer to the target configuration.
27 * @param val The byte received from the master.
28 */
sample_target_write_received_cb(struct i2c_target_config * config,uint8_t val)29 int sample_target_write_received_cb(struct i2c_target_config *config, uint8_t val)
30 {
31 printk("sample target write received: 0x%02x\n", val);
32 last_byte = val;
33 return 0;
34 }
35
36 /*
37 * @brief Callback which is called when a read request is received from the master.
38 * @param config Pointer to the target configuration.
39 * @param val Pointer to the byte to be sent to the master.
40 */
sample_target_read_requested_cb(struct i2c_target_config * config,uint8_t * val)41 int sample_target_read_requested_cb(struct i2c_target_config *config, uint8_t *val)
42 {
43 printk("sample target read request: 0x%02x\n", *val);
44 *val = 0x42;
45 return 0;
46 }
47
48 /*
49 * @brief Callback which is called when a read is processed from the master.
50 * @param config Pointer to the target configuration.
51 * @param val Pointer to the next byte to be sent to the master.
52 */
sample_target_read_processed_cb(struct i2c_target_config * config,uint8_t * val)53 int sample_target_read_processed_cb(struct i2c_target_config *config, uint8_t *val)
54 {
55 printk("sample target read processed: 0x%02x\n", *val);
56 *val = 0x43;
57 return 0;
58 }
59
60 /*
61 * @brief Callback which is called when the master sends a stop condition.
62 * @param config Pointer to the target configuration.
63 */
sample_target_stop_cb(struct i2c_target_config * config)64 int sample_target_stop_cb(struct i2c_target_config *config)
65 {
66 printk("sample target stop callback\n");
67 return 0;
68 }
69
70 static struct i2c_target_callbacks sample_target_callbacks = {
71 .write_requested = sample_target_write_requested_cb,
72 .write_received = sample_target_write_received_cb,
73 .read_requested = sample_target_read_requested_cb,
74 .read_processed = sample_target_read_processed_cb,
75 .stop = sample_target_stop_cb,
76 };
77
main(void)78 int main(void)
79 {
80 struct i2c_target_config target_cfg = {
81 .address = 0x60,
82 .callbacks = &sample_target_callbacks,
83 };
84
85 printk("i2c custom target sample\n");
86
87 if (i2c_target_register(bus, &target_cfg) < 0) {
88 printk("Failed to register target\n");
89 return -1;
90 }
91
92 k_msleep(5000);
93
94 if (i2c_target_unregister(bus, &target_cfg) < 0) {
95 printk("Failed to unregister target\n");
96 return -1;
97 }
98
99 return 0;
100 }
101