From 7d9fffaeb920f76dc695b61561e6a43761a31ce5 Mon Sep 17 00:00:00 2001 From: Tom Burdick Date: Tue, 29 Apr 2025 09:11:06 -0500 Subject: [PATCH] samples: pmci: mctp: Add i2c+gpio endpoint sample Adds sample showing i2c+gpio endpoint bindings in use. Provides an overlay for the frdm-mcxn947 development board using JP8 pins for i2c and gpio connectivity. The sample periodically sends a "ping" message to the bus controller and prints out any messages it receives itself. Signed-off-by: Tom Burdick --- .../mctp/i2c_gpio_bus_endpoint/CMakeLists.txt | 6 ++ .../mctp/i2c_gpio_bus_endpoint/README.rst | 32 +++++++++++ .../boards/frdm_mcxn947_mcxn947_cpu0.overlay | 36 ++++++++++++ .../pmci/mctp/i2c_gpio_bus_endpoint/prj.conf | 8 +++ .../mctp/i2c_gpio_bus_endpoint/src/main.c | 56 +++++++++++++++++++ 5 files changed, 138 insertions(+) create mode 100644 samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/CMakeLists.txt create mode 100644 samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/README.rst create mode 100644 samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/boards/frdm_mcxn947_mcxn947_cpu0.overlay create mode 100644 samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/prj.conf create mode 100644 samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/src/main.c diff --git a/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/CMakeLists.txt b/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/CMakeLists.txt new file mode 100644 index 00000000000..48798575275 --- /dev/null +++ b/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(mctp_i2c_gpio_bus_endpoint) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/README.rst b/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/README.rst new file mode 100644 index 00000000000..1457729f320 --- /dev/null +++ b/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/README.rst @@ -0,0 +1,32 @@ +.. zephyr:code-sample:: mctp_i2c_bus_endpoint + :name: MCTP I2C GPIO Endpoint Node Sample + + Create an MCTP endpoint node controlling I2C with GPIO signaling. + +Overview +******** +Sets up an MCTP endpoint node that may talk to a top node and peer to other MCTP endpoints. + +Requirements +************ +Board with an I2C and a GPIO used for signaling the top node to read the target device. + +Wiring +****** +A common I2C bus SDA/SCL pair connected to any number of boards (setup in devicetree) +along with a GPIO for signaling endpoint write requests. + +Building and Running +******************** + +.. zephyr-app-commands:: + :zephyr-app: samples/modules/pmci/mctp/i2c_gpio_bus_endpoint + :host-os: unix + :board: frdm_mcxn947_mcxn947_cpu0 + :goals: run + :compact: + +References +********** + +`MCTP Base Specification 2019 `_ diff --git a/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/boards/frdm_mcxn947_mcxn947_cpu0.overlay b/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/boards/frdm_mcxn947_mcxn947_cpu0.overlay new file mode 100644 index 00000000000..348370b719d --- /dev/null +++ b/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/boards/frdm_mcxn947_mcxn947_cpu0.overlay @@ -0,0 +1,36 @@ +#include + +/* Sets up I2C and a GPIO on JP8 to use as our i2c bus for MCTP */ + + +&nxp_lcd_8080_connector { + status = "disabled"; +}; + +&flexio0 { + status = "disabled"; +}; + +&gpio0 { + status = "okay"; +}; + +&flexcomm2 { + status = "okay"; +}; + +&flexcomm2_lpi2c2 { + status = "okay"; + clock-frequency = ; +}; + + +/ { + mctp_i2c: mctp_i2c { + compatible = "zephyr,mctp-i2c-gpio-target"; + i2c = <&flexcomm2_lpi2c2>; + i2c-addr = <70>; + endpoint-id = <11>; + endpoint-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>; + }; +}; diff --git a/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/prj.conf b/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/prj.conf new file mode 100644 index 00000000000..77886465f53 --- /dev/null +++ b/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/prj.conf @@ -0,0 +1,8 @@ +CONFIG_I2C=y +CONFIG_I2C_TARGET=y +CONFIG_MCTP=y +CONFIG_MCTP_I2C_GPIO_TARGET=y +CONFIG_MCTP_LOG_LEVEL_DBG=y +CONFIG_LOG=y +#CONFIG_LOG_MODE_IMMEDIATE=y +CONFIG_LOG_BUFFER_SIZE=4096 diff --git a/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/src/main.c b/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/src/main.c new file mode 100644 index 00000000000..6e726309575 --- /dev/null +++ b/samples/modules/pmci/mctp/i2c_gpio_bus_endpoint/src/main.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +LOG_MODULE_REGISTER(mctp_i2c_gpio_bus_endpoint); + +MCTP_I2C_GPIO_TARGET_DT_DEFINE(mctp_i2c_ctrl, DT_NODELABEL(mctp_i2c)); + +static void rx_message(uint8_t eid, bool tag_owner, uint8_t msg_tag, void *data, void *msg, + size_t len) +{ + LOG_INF("received message \"%s\" from endpoint %d, msg_tag %d, len %zu", (char *)msg, eid, + msg_tag, len); +} + +#define BUS_OWNER_ID 20 + +int main(void) +{ + int rc; + struct mctp *mctp_ctx; + + LOG_INF("MCTP Host EID:%d on %s\n", mctp_i2c_ctrl.endpoint_id, CONFIG_BOARD_TARGET); + + mctp_ctx = mctp_init(); + __ASSERT_NO_MSG(mctp_ctx != NULL); + mctp_register_bus(mctp_ctx, &mctp_i2c_ctrl.binding, mctp_i2c_ctrl.endpoint_id); + mctp_set_rx_all(mctp_ctx, rx_message, NULL); + + /* + * 1. MCTP poll loop, send "ping" to each endpoint and get "pong" back + * 2. Then send a broadcast "hello" to each endpoint and get a "from endoint %d" back + */ + while (true) { + rc = mctp_message_tx(mctp_ctx, BUS_OWNER_ID, false, + 0, "ping", sizeof("ping")); + if (rc != 0) { + LOG_WRN("Failed to send message \"ping\", errno %d\n", rc); + } + k_msleep(500); + } + + return 0; +}