From 00ffad91027873d8edbfb3bbef1706c8a86e0144 Mon Sep 17 00:00:00 2001 From: Bram Vlerick Date: Mon, 29 Apr 2024 16:20:49 +0200 Subject: [PATCH] samples: drivers: i2c: add i2c target api sample Add simple sample which demonstrates how to setup a custom i2c target. Signed-off-by: Bram Vlerick --- .../drivers/i2c/custom_target/CMakeLists.txt | 8 ++ samples/drivers/i2c/custom_target/README.rst | 31 ++++++ .../lpcxpresso55s69_lpc55s69_cpu0.overlay | 9 ++ samples/drivers/i2c/custom_target/prj.conf | 2 + samples/drivers/i2c/custom_target/sample.yaml | 8 ++ samples/drivers/i2c/custom_target/src/main.c | 100 ++++++++++++++++++ 6 files changed, 158 insertions(+) create mode 100644 samples/drivers/i2c/custom_target/CMakeLists.txt create mode 100644 samples/drivers/i2c/custom_target/README.rst create mode 100644 samples/drivers/i2c/custom_target/boards/lpcxpresso55s69_lpc55s69_cpu0.overlay create mode 100644 samples/drivers/i2c/custom_target/prj.conf create mode 100644 samples/drivers/i2c/custom_target/sample.yaml create mode 100644 samples/drivers/i2c/custom_target/src/main.c diff --git a/samples/drivers/i2c/custom_target/CMakeLists.txt b/samples/drivers/i2c/custom_target/CMakeLists.txt new file mode 100644 index 00000000000..1a1702c4216 --- /dev/null +++ b/samples/drivers/i2c/custom_target/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(i2c_custom_target) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/drivers/i2c/custom_target/README.rst b/samples/drivers/i2c/custom_target/README.rst new file mode 100644 index 00000000000..a3d86482888 --- /dev/null +++ b/samples/drivers/i2c/custom_target/README.rst @@ -0,0 +1,31 @@ +.. zephyr:code-sample:: i2c-custom-target + :name: I2C Custom Target + :relevant-api: i2c_interface + + Setup a custom I2C target on the I2C interface. + +Overview +******** + +This sample demonstrates how to setup an I2C custom target on the I2C interface +using the :ref:`i2c-target-api`. + +Requirements +************ + +This sample requires an I2C peripheral which is capable of acting as a target. + +This sample has been tested on :ref:`lpcxpresso55s69`. + +Building and Running +******************** + +The code for this sample can be found in :zephyr_file:`samples/drivers/i2c_target`. + +To build and flash the application: + +.. zephyr-app-commands:: + :zephyr-app: samples/drivers/i2c_target + :board: lpcxpresso55s69/lpc55s69/cpu0 + :goals: flash + :compact: diff --git a/samples/drivers/i2c/custom_target/boards/lpcxpresso55s69_lpc55s69_cpu0.overlay b/samples/drivers/i2c/custom_target/boards/lpcxpresso55s69_lpc55s69_cpu0.overlay new file mode 100644 index 00000000000..8826b80c552 --- /dev/null +++ b/samples/drivers/i2c/custom_target/boards/lpcxpresso55s69_lpc55s69_cpu0.overlay @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2024 Open Pixel Systems + * + * SPDX-License-Identifier: Apache-2.0 + */ + +&flexcomm4 { + status = "okay"; +}; diff --git a/samples/drivers/i2c/custom_target/prj.conf b/samples/drivers/i2c/custom_target/prj.conf new file mode 100644 index 00000000000..f3d64527b85 --- /dev/null +++ b/samples/drivers/i2c/custom_target/prj.conf @@ -0,0 +1,2 @@ +CONFIG_I2C=y +CONFIG_I2C_TARGET=y diff --git a/samples/drivers/i2c/custom_target/sample.yaml b/samples/drivers/i2c/custom_target/sample.yaml new file mode 100644 index 00000000000..dd8cc903dbb --- /dev/null +++ b/samples/drivers/i2c/custom_target/sample.yaml @@ -0,0 +1,8 @@ +sample: + name: I2C custom target sample +tests: + sample.drivers.i2c.custom_target: + tags: i2c_target + platform_allow: + - lpcxpresso55s69/lpc55s69/cpu0 + harness: TBD diff --git a/samples/drivers/i2c/custom_target/src/main.c b/samples/drivers/i2c/custom_target/src/main.c new file mode 100644 index 00000000000..83a0003cb34 --- /dev/null +++ b/samples/drivers/i2c/custom_target/src/main.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2024 Open Pixel Systems + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +static const struct device *bus = DEVICE_DT_GET(DT_NODELABEL(flexcomm4)); +static char last_byte; + +/* + * @brief Callback which is called when a write request is received from the master. + * @param config Pointer to the target configuration. + */ +int sample_target_write_requested_cb(struct i2c_target_config *config) +{ + printk("sample target write requested\n"); + return 0; +} + +/* + * @brief Callback which is called when a write is received from the master. + * @param config Pointer to the target configuration. + * @param val The byte received from the master. + */ +int sample_target_write_received_cb(struct i2c_target_config *config, uint8_t val) +{ + printk("sample target write received: 0x%02x\n", val); + last_byte = val; + return 0; +} + +/* + * @brief Callback which is called when a read request is received from the master. + * @param config Pointer to the target configuration. + * @param val Pointer to the byte to be sent to the master. + */ +int sample_target_read_requested_cb(struct i2c_target_config *config, uint8_t *val) +{ + printk("sample target read request: 0x%02x\n", *val); + *val = 0x42; + return 0; +} + +/* + * @brief Callback which is called when a read is processed from the master. + * @param config Pointer to the target configuration. + * @param val Pointer to the next byte to be sent to the master. + */ +int sample_target_read_processed_cb(struct i2c_target_config *config, uint8_t *val) +{ + printk("sample target read processed: 0x%02x\n", *val); + *val = 0x43; + return 0; +} + +/* + * @brief Callback which is called when the master sends a stop condition. + * @param config Pointer to the target configuration. + */ +int sample_target_stop_cb(struct i2c_target_config *config) +{ + printk("sample target stop callback\n"); + return 0; +} + +static struct i2c_target_callbacks sample_target_callbacks = { + .write_requested = sample_target_write_requested_cb, + .write_received = sample_target_write_received_cb, + .read_requested = sample_target_read_requested_cb, + .read_processed = sample_target_read_processed_cb, + .stop = sample_target_stop_cb, +}; + +int main(void) +{ + struct i2c_target_config target_cfg = { + .address = 0x60, + .callbacks = &sample_target_callbacks, + }; + + printk("i2c custom target sample\n"); + + if (i2c_target_register(bus, &target_cfg) < 0) { + printk("Failed to register target\n"); + return -1; + } + + k_msleep(5000); + + if (i2c_target_unregister(bus, &target_cfg) < 0) { + printk("Failed to unregister target\n"); + return -1; + } + + return 0; +}