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 <bram.vlerick@openpixelsystems.org>
This commit is contained in:
Bram Vlerick 2024-04-29 16:20:49 +02:00 committed by Fabio Baltieri
commit 00ffad9102
6 changed files with 158 additions and 0 deletions

View file

@ -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)

View file

@ -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:

View file

@ -0,0 +1,9 @@
/*
* Copyright (c) 2024 Open Pixel Systems
*
* SPDX-License-Identifier: Apache-2.0
*/
&flexcomm4 {
status = "okay";
};

View file

@ -0,0 +1,2 @@
CONFIG_I2C=y
CONFIG_I2C_TARGET=y

View file

@ -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

View file

@ -0,0 +1,100 @@
/*
* Copyright (c) 2024 Open Pixel Systems
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/drivers/i2c.h>
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;
}