Skip to content

Commit 464e59e

Browse files
committed
devicetree: add macros to deal with endpoints
Add macros to access the port/endpoint constructs from drivers and application. Devicetrees currently use "port { endpoint { ... }; };" to describe interconnection between devices. With introduction of the "remote-endpoint-label = string" syntax, it is now possible to describe circular dependencies until #57708 is addressed. Signed-off-by: Josuah Demangeon <me@josuah.net>
1 parent 829c03b commit 464e59e

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

dts/bindings/test/vnd,video.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2024 tinyVision.ai Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Test Video device
5+
6+
compatible: "vnd,video"
7+
8+
child-binding:
9+
child-binding:
10+
include: video-interfaces.yaml

include/zephyr/devicetree.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,70 @@
37873787
*/
37883788
#define DT_ON_BUS(node_id, bus) IS_ENABLED(DT_CAT3(node_id, _BUS_, bus))
37893789

3790+
/**
3791+
* @}
3792+
*/
3793+
3794+
/**
3795+
* @defgroup devicetree-generic-endpoint Endpoint helpers
3796+
* @ingroup devicetree
3797+
* @{
3798+
*/
3799+
3800+
/**
3801+
* @brief Get the remote endpoint node connected to a local endpoint.
3802+
*
3803+
* Devices can be interconnected through ports and endpoints.
3804+
*
3805+
* Example devicetree overlay:
3806+
*
3807+
* @code{.dts}
3808+
* &source {
3809+
* port {
3810+
* source_out: endpoint {
3811+
* remote-endpoint-label = "sink_in";
3812+
* source-property = <1>;
3813+
* };
3814+
* };
3815+
* };
3816+
*
3817+
* &sink {
3818+
* port {
3819+
* sink_in: endpoint {
3820+
* remote-endpoint-label = "source_out";
3821+
* sink-property = <2>;
3822+
* };
3823+
* };
3824+
* };
3825+
* @endcode
3826+
*
3827+
* @c DT_REMOTE_ENDPOINT(endpoint_node) permits to access the other endpoint
3828+
* connected to @c endpoint_node.
3829+
*
3830+
* Example usage: starting from the sink, access the source endpoint connected
3831+
* to the @c sink_in endpoint:
3832+
*
3833+
* @code{.c}
3834+
* DT_PROP(DT_REMOTE_ENDPOINT(DT_NODELABEL(sink_in)), source_property)
3835+
* @endcode
3836+
*
3837+
* Example usage, starting from the source, to access the sink endpoint
3838+
* connected to the @c source_out endpoint:
3839+
*
3840+
* @code{.c}
3841+
* DT_PROP(DT_REMOTE_ENDPOINT(DT_NODELABEL(source_out)), sink_property)
3842+
* @endcode
3843+
*
3844+
* @note Once circular phandle references are supported in Zephyr devicetree,
3845+
* @c remote-endpoint-label (string) may be changed into
3846+
* @c remote-endpoint (phandle).
3847+
*
3848+
* @param node The local endpoint node to use.
3849+
*
3850+
* @return The remote endpoint node connected to @p node.
3851+
*/
3852+
#define DT_REMOTE_ENDPOINT(node) DT_NODELABEL(DT_STRING_TOKEN(node, remote_endpoint_label))
3853+
37903854
/**
37913855
* @}
37923856
*/

tests/lib/devicetree/api/app.overlay

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,26 @@
506506
status = "okay";
507507
};
508508

509+
test_video_source: video_source {
510+
compatible = "vnd,video";
511+
512+
port {
513+
test_video_source_out: endpoint {
514+
remote-endpoint-label = "test_video_sink_in";
515+
};
516+
};
517+
};
518+
519+
test_video_sink: video_sink {
520+
compatible = "vnd,video";
521+
522+
port {
523+
test_video_sink_in: endpoint {
524+
remote-endpoint-label = "test_video_source_out";
525+
};
526+
};
527+
};
528+
509529
test_pwm1: pwm@55551111 {
510530
compatible = "vnd,pwm";
511531
#pwm-cells = <3>;

tests/lib/devicetree/api/src/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
#define TEST_DMA_CTLR_1 DT_NODELABEL(test_dma1)
7777
#define TEST_DMA_CTLR_2 DT_NODELABEL(test_dma2)
7878

79+
#define TEST_VIDEO_SOURCE_OUT DT_NODELABEL(test_video_source_out)
80+
#define TEST_VIDEO_SINK_IN DT_NODELABEL(test_video_sink_in)
81+
7982
#define TEST_IO_CHANNEL_CTLR_1 DT_NODELABEL(test_adc_1)
8083
#define TEST_IO_CHANNEL_CTLR_2 DT_NODELABEL(test_adc_2)
8184

@@ -1278,6 +1281,19 @@ ZTEST(devicetree_api, test_dma)
12781281
zassert_false(DT_INST_DMAS_HAS_IDX(0, 2), "");
12791282
}
12801283

1284+
#undef DT_DRV_COMPAT
1285+
#define DT_DRV_COMPAT vnd_video_device
1286+
ZTEST(devicetree_api, test_video)
1287+
{
1288+
/* DT_REMOTE_ENDPOINT(source) */
1289+
zassert_true(DT_SAME_NODE(DT_REMOTE_ENDPOINT(TEST_VIDEO_SOURCE_OUT),
1290+
TEST_VIDEO_SINK_IN), "");
1291+
1292+
/* DT_REMOTE_ENDPOINT(sink) */
1293+
zassert_true(DT_SAME_NODE(DT_REMOTE_ENDPOINT(TEST_VIDEO_SINK_IN),
1294+
TEST_VIDEO_SOURCE_OUT), "");
1295+
}
1296+
12811297
#undef DT_DRV_COMPAT
12821298
#define DT_DRV_COMPAT vnd_phandle_holder
12831299
ZTEST(devicetree_api, test_pwms)

0 commit comments

Comments
 (0)