Skip to content

Commit be4a3d9

Browse files
teburdhenrikbrixandersen
authored andcommitted
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 <[email protected]>
1 parent 4148512 commit be4a3d9

File tree

11 files changed

+252
-0
lines changed

11 files changed

+252
-0
lines changed

samples/modules/mctp/mctp.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. zephyr:code-sample-category:: mctp
2+
:name: MCTP
3+
:show-listing:
4+
5+
These samples demonstrate how to build communicating firmwares using MCTP in Zephyr.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
4+
5+
project(mctp_endpoint)
6+
7+
target_sources(app PRIVATE src/main.c)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.. zephyr:code-sample:: mctp_endpoint_sample
2+
:name: MCTP Endpoint Sample
3+
4+
Create an MCTP endpoint over UART.
5+
6+
Overview
7+
********
8+
Sets up an MCTP node that listens on a UART for messages targeting a particular
9+
MCTP endpoint id with the message "hello". Responds to this "hello" message with
10+
"world".
11+
12+
Requirements
13+
************
14+
A board and SoC that provide access to a UART and a driver that implements the
15+
UART async API.
16+
17+
Wiring
18+
******
19+
The listening UART pins should be wired to a board which will run the MCTP host
20+
sample such that this board's UART tx pin connects to the host board's rx pin,
21+
and this board's UART rx pin connects to the host board's tx pin. The boards'
22+
grounds should also be wired together.
23+
24+
Optionally a logic analyzer can be wired up and listening to the UART to inspect
25+
the data flowing.
26+
27+
Building and Running
28+
********************
29+
30+
31+
.. zephyr-app-commands::
32+
:zephyr-app: samples/modules/mctp/mctp_endpoint
33+
:host-os: unix
34+
:board: nrf52840_nrf52840dk
35+
:goals: run
36+
:compact:
37+
38+
References
39+
**********
40+
41+
`MCTP Base Specification 2019 <https://www.dmtf.org/sites/default/files/standards/documents/DSP0236_1.3.1.pdf>`_
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
&arduino_serial{
2+
status = "okay";
3+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_SERIAL=y
2+
CONFIG_UART_ASYNC_API=y
3+
CONFIG_MCTP=y
4+
CONFIG_MCTP_UART=y
5+
CONFIG_LOG=y
6+
CONFIG_LOG_BUFFER_SIZE=4096
7+
CONFIG_MCTP_LOG_LEVEL_DBG=y
8+
CONFIG_ISR_STACK_SIZE=4096
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2024 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdlib.h>
8+
#include <stdio.h>
9+
#include <assert.h>
10+
#include <unistd.h>
11+
#include <zephyr/kernel.h>
12+
#include <zephyr/types.h>
13+
#include <zephyr/mctp/mctp_uart.h>
14+
#include <libmctp.h>
15+
16+
#include <zephyr/logging/log.h>
17+
LOG_MODULE_REGISTER(mctp_endpoint);
18+
19+
#include <zephyr/drivers/uart.h>
20+
21+
static struct mctp *mctp_ctx;
22+
23+
#define LOCAL_HELLO_EID 10
24+
25+
#define REMOTE_HELLO_EID 20
26+
27+
K_SEM_DEFINE(mctp_rx, 0, 1);
28+
29+
static void rx_message(uint8_t eid, bool tag_owner, uint8_t msg_tag, void *data, void *msg,
30+
size_t len)
31+
{
32+
switch (eid) {
33+
case REMOTE_HELLO_EID:
34+
LOG_INF("got mctp message %s for eid %d, replying to 5 with \"world\"", (char *)msg,
35+
eid);
36+
mctp_message_tx(mctp_ctx, LOCAL_HELLO_EID, false, 0, "world", sizeof("world"));
37+
break;
38+
default:
39+
LOG_INF("Unknown endpoint %d", eid);
40+
break;
41+
}
42+
43+
k_sem_give(&mctp_rx);
44+
}
45+
46+
MCTP_UART_DT_DEFINE(mctp_endpoint, DEVICE_DT_GET(DT_NODELABEL(arduino_serial)));
47+
48+
#define RX_BUF_SZ 128
49+
50+
int main(void)
51+
{
52+
LOG_INF("MCTP Endpoint EID:%d on %s\n", LOCAL_HELLO_EID, CONFIG_BOARD_TARGET);
53+
54+
mctp_set_alloc_ops(malloc, free, realloc);
55+
mctp_ctx = mctp_init();
56+
__ASSERT_NO_MSG(mctp_ctx != NULL);
57+
mctp_register_bus(mctp_ctx, &mctp_endpoint.binding, LOCAL_HELLO_EID);
58+
mctp_set_rx_all(mctp_ctx, rx_message, NULL);
59+
60+
/* MCTP poll loop */
61+
while (true) {
62+
mctp_uart_start_rx(&mctp_endpoint);
63+
k_sem_take(&mctp_rx, K_FOREVER);
64+
}
65+
66+
LOG_INF("exiting");
67+
return 0;
68+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
4+
project(mctp_host)
5+
6+
target_sources(app PRIVATE src/main.c)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
.. zephyr:code-sample:: mctp_host_sample
2+
:name: MCTP Host Sample
3+
4+
Create an MCTP host over UART.
5+
6+
Overview
7+
********
8+
Sets up an MCTP node that sends a request on a UART targeting a particular MCTP
9+
endpoint id with the message "hello". Expects and waits for a response to this
10+
"hello" message containing "world".
11+
12+
Requirements
13+
************
14+
A board and SoC that provide access to a UART and a driver that implements the
15+
UART async API.
16+
17+
Wiring
18+
******
19+
The UART pins should be wired to a board which will run the MCTP endpoint
20+
sample such that this board's UART tx pin connects to the endpoint board's rx
21+
pin, and this board's UART rx pin connects to the endpoint board's tx pin. The
22+
boards' grounds should also be wired together.
23+
24+
Optionally a logic analyzer can be wired up and listening to the UART to inspect
25+
the data flowing.
26+
27+
Building and Running
28+
********************
29+
30+
.. zephyr-app-commands::
31+
:zephyr-app: samples/modules/mctp/mctp_host
32+
:host-os: unix
33+
:board: nrf52840_nrf52840dk
34+
:goals: run
35+
:compact:
36+
37+
References
38+
**********
39+
40+
`MCTP Base Specification 2019 <https://www.dmtf.org/sites/default/files/standards/documents/DSP0236_1.3.1.pdf>`_
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
&arduino_serial{
2+
status = "okay";
3+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# nothing here
2+
CONFIG_SERIAL=y
3+
CONFIG_UART_ASYNC_API=y
4+
CONFIG_MCTP=y
5+
CONFIG_MCTP_UART=y
6+
CONFIG_MCTP_LOG_LEVEL_DBG=y
7+
CONFIG_LOG=y
8+
CONFIG_LOG_BUFFER_SIZE=4096

0 commit comments

Comments
 (0)