From b483e06195be666bcc6f138c39e90df8e6ea1045 Mon Sep 17 00:00:00 2001 From: Tom Burdick Date: Wed, 14 Aug 2024 09:03:11 -0500 Subject: [PATCH] samples: Add mctp host and client samples Samples work by sending MCTP encoded messages over a uart between two boards. Signed-off-by: Tom Burdick --- .../modules/mctp/mctp_endpoint/CMakeLists.txt | 7 ++ .../mctp_endpoint/boards/frdm_k64f.overlay | 5 ++ .../boards/mimxrt1010_evk.overlay | 5 ++ .../boards/mimxrt1060_evk.overlay | 10 +++ .../boards/nrf52840dk_nrf52840.overlay | 3 + .../boards/nucleo_f446re.overlay | 5 ++ samples/modules/mctp/mctp_endpoint/prj.conf | 8 +++ samples/modules/mctp/mctp_endpoint/src/main.c | 68 +++++++++++++++++++ samples/modules/mctp/mctp_host/CMakeLists.txt | 6 ++ .../mctp_host/boards/mimxrt1010_evk.overlay | 5 ++ .../boards/nrf52840dk_nrf52840.overlay | 3 + .../mctp_host/boards/nucleo_f303re.overlay | 7 ++ .../mctp_host/boards/nucleo_f446re.overlay | 5 ++ samples/modules/mctp/mctp_host/prj.conf | 8 +++ samples/modules/mctp/mctp_host/src/main.c | 63 +++++++++++++++++ 15 files changed, 208 insertions(+) create mode 100644 samples/modules/mctp/mctp_endpoint/CMakeLists.txt create mode 100644 samples/modules/mctp/mctp_endpoint/boards/frdm_k64f.overlay create mode 100644 samples/modules/mctp/mctp_endpoint/boards/mimxrt1010_evk.overlay create mode 100644 samples/modules/mctp/mctp_endpoint/boards/mimxrt1060_evk.overlay create mode 100644 samples/modules/mctp/mctp_endpoint/boards/nrf52840dk_nrf52840.overlay create mode 100644 samples/modules/mctp/mctp_endpoint/boards/nucleo_f446re.overlay create mode 100644 samples/modules/mctp/mctp_endpoint/prj.conf create mode 100644 samples/modules/mctp/mctp_endpoint/src/main.c create mode 100644 samples/modules/mctp/mctp_host/CMakeLists.txt create mode 100644 samples/modules/mctp/mctp_host/boards/mimxrt1010_evk.overlay create mode 100644 samples/modules/mctp/mctp_host/boards/nrf52840dk_nrf52840.overlay create mode 100644 samples/modules/mctp/mctp_host/boards/nucleo_f303re.overlay create mode 100644 samples/modules/mctp/mctp_host/boards/nucleo_f446re.overlay create mode 100644 samples/modules/mctp/mctp_host/prj.conf create mode 100644 samples/modules/mctp/mctp_host/src/main.c diff --git a/samples/modules/mctp/mctp_endpoint/CMakeLists.txt b/samples/modules/mctp/mctp_endpoint/CMakeLists.txt new file mode 100644 index 000000000000000..e3f4e04676ad1eb --- /dev/null +++ b/samples/modules/mctp/mctp_endpoint/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(mctp_endpoint) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/modules/mctp/mctp_endpoint/boards/frdm_k64f.overlay b/samples/modules/mctp/mctp_endpoint/boards/frdm_k64f.overlay new file mode 100644 index 000000000000000..43526cf829a8214 --- /dev/null +++ b/samples/modules/mctp/mctp_endpoint/boards/frdm_k64f.overlay @@ -0,0 +1,5 @@ +/ { + aliases { + i2c-target = &arduino_i2c; + }; +}; diff --git a/samples/modules/mctp/mctp_endpoint/boards/mimxrt1010_evk.overlay b/samples/modules/mctp/mctp_endpoint/boards/mimxrt1010_evk.overlay new file mode 100644 index 000000000000000..43526cf829a8214 --- /dev/null +++ b/samples/modules/mctp/mctp_endpoint/boards/mimxrt1010_evk.overlay @@ -0,0 +1,5 @@ +/ { + aliases { + i2c-target = &arduino_i2c; + }; +}; diff --git a/samples/modules/mctp/mctp_endpoint/boards/mimxrt1060_evk.overlay b/samples/modules/mctp/mctp_endpoint/boards/mimxrt1060_evk.overlay new file mode 100644 index 000000000000000..fc9192d341eb9ca --- /dev/null +++ b/samples/modules/mctp/mctp_endpoint/boards/mimxrt1060_evk.overlay @@ -0,0 +1,10 @@ +/ { + aliases { + i2c-target = &arduino_i2c; + }; +}; + +&arduino_serial { + status = "okay"; + current-speed = <115200>; +}; diff --git a/samples/modules/mctp/mctp_endpoint/boards/nrf52840dk_nrf52840.overlay b/samples/modules/mctp/mctp_endpoint/boards/nrf52840dk_nrf52840.overlay new file mode 100644 index 000000000000000..48c7f840dcab6b3 --- /dev/null +++ b/samples/modules/mctp/mctp_endpoint/boards/nrf52840dk_nrf52840.overlay @@ -0,0 +1,3 @@ +&arduino_serial{ + status = "okay"; +}; diff --git a/samples/modules/mctp/mctp_endpoint/boards/nucleo_f446re.overlay b/samples/modules/mctp/mctp_endpoint/boards/nucleo_f446re.overlay new file mode 100644 index 000000000000000..43526cf829a8214 --- /dev/null +++ b/samples/modules/mctp/mctp_endpoint/boards/nucleo_f446re.overlay @@ -0,0 +1,5 @@ +/ { + aliases { + i2c-target = &arduino_i2c; + }; +}; diff --git a/samples/modules/mctp/mctp_endpoint/prj.conf b/samples/modules/mctp/mctp_endpoint/prj.conf new file mode 100644 index 000000000000000..495c9ca12fa9b5c --- /dev/null +++ b/samples/modules/mctp/mctp_endpoint/prj.conf @@ -0,0 +1,8 @@ +CONFIG_SERIAL=y +CONFIG_UART_ASYNC_API=y +CONFIG_MCTP=y +CONFIG_MCTP_UART=y +CONFIG_LOG=y +CONFIG_LOG_BUFFER_SIZE=4096 +CONFIG_MCTP_LOG_LEVEL_DBG=y +CONFIG_ISR_STACK_SIZE=4096 diff --git a/samples/modules/mctp/mctp_endpoint/src/main.c b/samples/modules/mctp/mctp_endpoint/src/main.c new file mode 100644 index 000000000000000..430447ad2fe4682 --- /dev/null +++ b/samples/modules/mctp/mctp_endpoint/src/main.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2024 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +LOG_MODULE_REGISTER(mctp_endpoint); + +#include + +static struct mctp *mctp_ctx; + +#define LOCAL_HELLO_EID 10 + +#define REMOTE_HELLO_EID 20 + +K_SEM_DEFINE(mctp_rx, 0, 1); + +static void rx_message(uint8_t eid, bool tag_owner, uint8_t msg_tag, void *data, void *msg, + size_t len) +{ + switch (eid) { + case REMOTE_HELLO_EID: + LOG_INF("got mctp message %s for eid %d, replying to 5 with \"world\"", (char *)msg, + eid); + mctp_message_tx(mctp_ctx, LOCAL_HELLO_EID, false, 0, "world", sizeof("world")); + break; + default: + LOG_INF("Unknown endpoint %d", eid); + break; + } + + k_sem_give(&mctp_rx); +} + +MCTP_UART_DT_DEFINE(mctp_endpoint, DEVICE_DT_GET(DT_NODELABEL(arduino_serial))); + +#define RX_BUF_SZ 128 + +int main(void) +{ + LOG_INF("MCTP Endpoint EID:%d on %s\n", LOCAL_HELLO_EID, CONFIG_BOARD_TARGET); + + mctp_set_alloc_ops(malloc, free, realloc); + mctp_ctx = mctp_init(); + __ASSERT_NO_MSG(mctp_ctx != NULL); + mctp_register_bus(mctp_ctx, &mctp_endpoint.binding, LOCAL_HELLO_EID); + mctp_set_rx_all(mctp_ctx, rx_message, NULL); + + /* MCTP poll loop */ + while (true) { + mctp_uart_start_rx(&mctp_endpoint); + k_sem_take(&mctp_rx, K_FOREVER); + } + + LOG_INF("exiting"); + return 0; +} diff --git a/samples/modules/mctp/mctp_host/CMakeLists.txt b/samples/modules/mctp/mctp_host/CMakeLists.txt new file mode 100644 index 000000000000000..4b19d992eb2b422 --- /dev/null +++ b/samples/modules/mctp/mctp_host/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(mctp_host) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/modules/mctp/mctp_host/boards/mimxrt1010_evk.overlay b/samples/modules/mctp/mctp_host/boards/mimxrt1010_evk.overlay new file mode 100644 index 000000000000000..bf41a7896b38d33 --- /dev/null +++ b/samples/modules/mctp/mctp_host/boards/mimxrt1010_evk.overlay @@ -0,0 +1,5 @@ +/ { + aliases { + i2c-controller = &arduino_i2c; + }; +}; diff --git a/samples/modules/mctp/mctp_host/boards/nrf52840dk_nrf52840.overlay b/samples/modules/mctp/mctp_host/boards/nrf52840dk_nrf52840.overlay new file mode 100644 index 000000000000000..48c7f840dcab6b3 --- /dev/null +++ b/samples/modules/mctp/mctp_host/boards/nrf52840dk_nrf52840.overlay @@ -0,0 +1,3 @@ +&arduino_serial{ + status = "okay"; +}; diff --git a/samples/modules/mctp/mctp_host/boards/nucleo_f303re.overlay b/samples/modules/mctp/mctp_host/boards/nucleo_f303re.overlay new file mode 100644 index 000000000000000..f461ac882fe8ec3 --- /dev/null +++ b/samples/modules/mctp/mctp_host/boards/nucleo_f303re.overlay @@ -0,0 +1,7 @@ +/ { + aliases { + i2c-controller = &arduino_i2c; + }; +}; + +arduino_serial: &usart2 {}; diff --git a/samples/modules/mctp/mctp_host/boards/nucleo_f446re.overlay b/samples/modules/mctp/mctp_host/boards/nucleo_f446re.overlay new file mode 100644 index 000000000000000..bf41a7896b38d33 --- /dev/null +++ b/samples/modules/mctp/mctp_host/boards/nucleo_f446re.overlay @@ -0,0 +1,5 @@ +/ { + aliases { + i2c-controller = &arduino_i2c; + }; +}; diff --git a/samples/modules/mctp/mctp_host/prj.conf b/samples/modules/mctp/mctp_host/prj.conf new file mode 100644 index 000000000000000..341787ecbd3eb99 --- /dev/null +++ b/samples/modules/mctp/mctp_host/prj.conf @@ -0,0 +1,8 @@ +# nothing here +CONFIG_SERIAL=y +CONFIG_UART_ASYNC_API=y +CONFIG_MCTP=y +CONFIG_MCTP_UART=y +CONFIG_MCTP_LOG_LEVEL_DBG=y +CONFIG_LOG=y +CONFIG_LOG_BUFFER_SIZE=4096 diff --git a/samples/modules/mctp/mctp_host/src/main.c b/samples/modules/mctp/mctp_host/src/main.c new file mode 100644 index 000000000000000..7fa9423c24cde98 --- /dev/null +++ b/samples/modules/mctp/mctp_host/src/main.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2024 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +LOG_MODULE_REGISTER(mctp_host); + +#define LOCAL_HELLO_EID 20 + +#define REMOTE_HELLO_EID 10 + +K_SEM_DEFINE(mctp_rx, 0, 1); + +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 for endpoint %d, msg_tag %d, len %zu", (char *)msg, eid, + msg_tag, len); + k_sem_give(&mctp_rx); +} + +MCTP_UART_DT_DEFINE(mctp_host, DEVICE_DT_GET(DT_NODELABEL(arduino_serial))); + +int main(void) +{ + int rc; + struct mctp *mctp_ctx; + + LOG_INF("MCTP Host EID:%d on %s\n", LOCAL_HELLO_EID, CONFIG_BOARD_TARGET); + + mctp_set_alloc_ops(malloc, free, realloc); + mctp_ctx = mctp_init(); + __ASSERT_NO_MSG(mctp_ctx != NULL); + mctp_register_bus(mctp_ctx, &mctp_host.binding, LOCAL_HELLO_EID); + mctp_set_rx_all(mctp_ctx, rx_message, NULL); + mctp_uart_start_rx(&mctp_host); + + /* MCTP poll loop, send "hello" and get "world" back */ + while (true) { + rc = mctp_message_tx(mctp_ctx, REMOTE_HELLO_EID, false, 0, "hello", + sizeof("hello")); + if (rc != 0) { + LOG_WRN("Failed to send message, errno %d\n", rc); + k_msleep(1000); + } else { + k_sem_take(&mctp_rx, K_MSEC(10)); + } + rc = 0; + } + + return 0; +}