Skip to content

Commit 4148512

Browse files
teburdhenrikbrixandersen
authored andcommitted
mctp: Add mctp subsystem with uart binding
libmctp provides interfaces for wiring up a MCTP bus it calls bus bindings. The bindings provided in libmctp however are not directly useful to Zephyr without some work. Provide an initial uart binding that directly uses Zephyr's async uart interface. Signed-off-by: Tom Burdick <[email protected]>
1 parent bb22e3e commit 4148512

File tree

6 files changed

+437
-0
lines changed

6 files changed

+437
-0
lines changed

include/zephyr/mctp/mctp_uart.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2024 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
*/
7+
8+
#ifndef ZEPHYR_MCTP_UART_H_
9+
#define ZEPHYR_MCTP_UART_H_
10+
11+
#include <stdint.h>
12+
#include <zephyr/kernel.h>
13+
#include <zephyr/device.h>
14+
#include <libmctp.h>
15+
16+
/**
17+
* @brief An MCTP binding for Zephyr's asynchronous UART interface
18+
*/
19+
struct mctp_binding_uart {
20+
/** @cond INTERNAL_HIDDEN */
21+
struct mctp_binding binding;
22+
const struct device *dev;
23+
24+
/* receive buffers and state */
25+
uint8_t rx_buf[2][256];
26+
bool rx_buf_used[2];
27+
struct mctp_pktbuf *rx_pkt;
28+
uint8_t rx_exp_len;
29+
uint16_t rx_fcs;
30+
uint16_t rx_fcs_calc;
31+
enum {
32+
STATE_WAIT_SYNC_START,
33+
STATE_WAIT_REVISION,
34+
STATE_WAIT_LEN,
35+
STATE_DATA,
36+
STATE_DATA_ESCAPED,
37+
STATE_WAIT_FCS1,
38+
STATE_WAIT_FCS2,
39+
STATE_WAIT_SYNC_END,
40+
} rx_state;
41+
int rx_res;
42+
43+
/* staging buffer for tx */
44+
uint8_t tx_buf[256];
45+
int tx_res;
46+
47+
/** @endcond INTERNAL_HIDDEN */
48+
};
49+
50+
/**
51+
* @brief Start the receive of a single mctp message
52+
*
53+
* Will read a single mctp message from the uart.
54+
*
55+
* @param uart MCTP UART binding
56+
*/
57+
void mctp_uart_start_rx(struct mctp_binding_uart *uart);
58+
59+
/** @cond INTERNAL_HIDDEN */
60+
int mctp_uart_start(struct mctp_binding *binding);
61+
int mctp_uart_tx(struct mctp_binding *binding, struct mctp_pktbuf *pkt);
62+
/** @endcond INTERNAL_HIDDEN */
63+
64+
/**
65+
* @brief Statically define a MCTP bus binding for a UART
66+
*
67+
* @param _name Symbolic name of the bus binding variable
68+
* @param _dev UART device
69+
*/
70+
#define MCTP_UART_DT_DEFINE(_name, _dev) \
71+
struct mctp_binding_uart _name = { \
72+
.binding = \
73+
{ \
74+
.name = STRINGIFY(_name), .version = 1, \
75+
.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU), \
76+
.pkt_header = 0, .pkt_trailer = 0, \
77+
.start = mctp_uart_start, .tx = mctp_uart_tx, \
78+
}, \
79+
.dev = _dev, \
80+
.rx_state = STATE_WAIT_SYNC_START, \
81+
.rx_pkt = NULL, \
82+
.rx_res = 0, \
83+
.tx_res = 0, \
84+
};
85+
86+
#endif /* ZEPHYR_MCTP_UART_H_ */

subsys/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ add_subdirectory_ifdef(CONFIG_IMG_MANAGER dfu)
4646
add_subdirectory_ifdef(CONFIG_INPUT input)
4747
add_subdirectory_ifdef(CONFIG_JWT jwt)
4848
add_subdirectory_ifdef(CONFIG_LLEXT llext)
49+
add_subdirectory_ifdef(CONFIG_MCTP mctp)
4950
add_subdirectory_ifdef(CONFIG_MODEM_MODULES modem)
5051
add_subdirectory_ifdef(CONFIG_NETWORKING net)
5152
add_subdirectory_ifdef(CONFIG_PROFILING profiling)

subsys/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ source "subsys/jwt/Kconfig"
2626
source "subsys/llext/Kconfig"
2727
source "subsys/logging/Kconfig"
2828
source "subsys/lorawan/Kconfig"
29+
source "subsys/mctp/Kconfig"
2930
source "subsys/mem_mgmt/Kconfig"
3031
source "subsys/mgmt/Kconfig"
3132
source "subsys/modbus/Kconfig"

subsys/mctp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
zephyr_library()
2+
zephyr_library_sources_ifdef(CONFIG_MCTP_UART mctp_uart.c)

subsys/mctp/Kconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
menuconfig MCTP
5+
bool "Management Component Transport Protocol"
6+
help
7+
Enable the MCTP Subsystem and Module Usage
8+
9+
if MCTP
10+
11+
config MCTP_UART
12+
bool "MCTP UART Binding"
13+
depends on UART_ASYNC_API
14+
help
15+
Build the MCTP UART binding to use MCTP over Zephyr's async UART
16+
interface.
17+
18+
module = MCTP
19+
module-str = MCTP
20+
source "subsys/logging/Kconfig.template.log_config"
21+
22+
endif

0 commit comments

Comments
 (0)