Skip to content

Commit 7624fa7

Browse files
committed
samples: i2c: add i2c_shell sample
Add i2c_shell sample to check i2c slave devices Signed-off-by: Biwen Li <[email protected]>
1 parent 2fc22b7 commit 7624fa7

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed

samples/i2c/i2c.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. zephyr:code-sample-category:: i2c
2+
:name: I2c
3+
:show-listing:
4+
5+
These samples demonstrate how to use i2c supported by Zephyr.

samples/i2c/i2c_shell/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(i2c_shell)
7+
8+
target_sources(app PRIVATE src/main.c)

samples/i2c/i2c_shell/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright 2025 NXP
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
source "Kconfig.zephyr"

samples/i2c/i2c_shell/README.rst

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
.. zephyr:code-sample:: i2c_shell
2+
:name: I2c shell
3+
:relevant-api: i2c_interface
4+
5+
Interact with i2c using the shell module.
6+
7+
Overview
8+
********
9+
This is a simple shell module to test i2c slave devices via i2c commands in the system.
10+
11+
Building and Running
12+
********************
13+
14+
Build the sample app by choosing the target board that has i2c driver
15+
enabled, for example:
16+
17+
.. zephyr-app-commands::
18+
:zephyr-app: samples/i2c/i2c_shell
19+
:board: imx943_evk/mimx94398/m33
20+
:goals: build flash
21+
22+
Shell Module Command Help
23+
=========================
24+
25+
.. code-block:: console
26+
27+
i2c - I2c commands
28+
Subcommands:
29+
scan :Scan I2C devices.
30+
recover :Recover I2C bus.
31+
read :Read bytes from an I2C device.
32+
read_byte :Read a byte from an I2C device.
33+
direct_read :Read byte stream directly from an I2C device without
34+
writing a register address first.
35+
write :Write bytes to an I2C device.
36+
write_byte :Write a byte to an I2C device.
37+
speed :Configure I2C bus speed.
38+
39+
Output example (imx943_evk/mimx94398/m33):
40+
41+
.. code-block:: console
42+
43+
uart:~$ i2c
44+
i2c - I2C commands
45+
Subcommands:
46+
scan : Scan I2C devices
47+
Usage: scan <device>
48+
recover : Recover I2C bus
49+
Usage: recover <device>
50+
read : Read bytes from an I2C device
51+
Usage: read <device> <addr> <reg> [<bytes>]
52+
read_byte : Read a byte from an I2C device
53+
Usage: read_byte <device> <addr> <reg>
54+
direct_read : Read byte stream directly from an I2C device without writing a
55+
register address first
56+
Usage: direct_read <device> <addr> [<bytes>]
57+
write : Write bytes to an I2C device
58+
Usage: write <device> <addr> <reg> [<byte1>, ...]
59+
write_byte : Write a byte to an I2C device
60+
Usage: write_byte <device> <addr> <reg> <value>
61+
speed : Configure I2C bus speed
62+
Usage: speed <device> <speed>
63+
64+
uart:~$ i2c scan
65+
scan: wrong parameter count
66+
scan - Scan I2C devices
67+
Usage: scan <device>
68+
Subcommands:
69+
i2c@426c0000
70+
uart:~$ i2c scan i2c@426c0000
71+
0 1 2 3 4 5 6 7 8 9 a b c d e f
72+
00: -- -- -- -- -- -- -- -- -- -- -- --
73+
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
74+
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
75+
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
76+
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
77+
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
78+
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
79+
70: -- -- -- -- -- -- -- 77
80+
1 devices found on i2c@426c0000
81+
82+
uart:~$ i2c speed i2c@426c0000 1
83+
84+
uart:~$ i2c write_byte i2c@426c0000 0x77 0 0x1
85+
uart:~$ i2c read_byte i2c@426c0000 0x77 0
86+
Output: 0x1

samples/i2c/i2c_shell/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_SHELL=y
2+
CONFIG_I2C=y
3+
CONFIG_I2C_SHELL=y

samples/i2c/i2c_shell/sample.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
sample:
2+
name: Shell I2c Module Sample
3+
common:
4+
tags:
5+
- i2c
6+
- shell
7+
8+
tests:
9+
sample.i2c.shell:
10+
integration_platforms:
11+
- imx943_evk/mimx94398/m33
12+
filter: ( CONFIG_UART_CONSOLE and CONFIG_SERIAL_SUPPORT_INTERRUPT )
13+
harness: keyboard
14+
min_ram: 20
15+
min_flash: 33

samples/i2c/i2c_shell/src/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* This sample app launches a shell. Interact with it using the `i2c` command. See
9+
* `drivers/i2c/i2c_shell.c`. There is nothing to do in the main thread.
10+
*/
11+
12+
int main(void)
13+
{
14+
return 0;
15+
}

0 commit comments

Comments
 (0)