Skip to content

Commit 2c6d99d

Browse files
hubertmiscarlescufi
authored andcommitted
samples: ipc: RPMsg Service sample
This patch adds a new sample presenting usage of RPMsg Service subsystem. Signed-off-by: Hubert Miś <[email protected]>
1 parent 50767ae commit 2c6d99d

File tree

12 files changed

+439
-0
lines changed

12 files changed

+439
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
cmake_minimum_required(VERSION 3.13.1)
2+
# Copyright (c) 2019 Linaro Limited
3+
# Copyright (c) 2018-2021 Nordic Semiconductor ASA
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
8+
set(REMOTE_ZEPHYR_DIR ${CMAKE_CURRENT_BINARY_DIR}/rpmsg_service_remote-prefix/src/rpmsg_service_remote-build/zephyr)
9+
10+
if("${BOARD}" STREQUAL "nrf5340dk_nrf5340_cpuapp")
11+
set(BOARD_REMOTE "nrf5340dk_nrf5340_cpunet")
12+
elseif("${BOARD}" STREQUAL "lpcxpresso54114_m4")
13+
set(BOARD_REMOTE "lpcxpresso54114_m0")
14+
elseif("${BOARD}" STREQUAL "mps2_an521")
15+
set(QEMU_EXTRA_FLAGS "-device;loader,file=${REMOTE_ZEPHYR_DIR}/zephyr.elf")
16+
set(BOARD_REMOTE "mps2_an521_nonsecure")
17+
elseif("${BOARD}" STREQUAL "v2m_musca_b1")
18+
set(BOARD_REMOTE "v2m_musca_b1_nonsecure")
19+
else()
20+
message(FATAL_ERROR "${BOARD} was not supported for this sample")
21+
endif()
22+
23+
message(INFO " ${BOARD} compile as Master in this sample")
24+
25+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
26+
project(rpmsg_service)
27+
28+
enable_language(C ASM)
29+
30+
target_sources(app PRIVATE src/main.c)
31+
32+
include(ExternalProject)
33+
34+
ExternalProject_Add(
35+
rpmsg_service_remote
36+
SOURCE_DIR ${APPLICATION_SOURCE_DIR}/remote
37+
INSTALL_COMMAND "" # This particular build system has no install command
38+
CMAKE_CACHE_ARGS -DBOARD:STRING=${BOARD_REMOTE}
39+
CMAKE_CACHE_ARGS -DDTC_OVERLAY_FILE:STRING=${DTC_OVERLAY_FILE}
40+
BUILD_BYPRODUCTS "${REMOTE_ZEPHYR_DIR}/${KERNEL_BIN_NAME}"
41+
# NB: Do we need to pass on more CMake variables?
42+
BUILD_ALWAYS True
43+
)
44+
45+
if(("${BOARD}" STREQUAL "lpcxpresso54114_m4"))
46+
add_dependencies(core_m0_inc_target rpmsg_service_remote)
47+
endif()
48+
49+
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
.. _RPMsg_Service_sample:
2+
3+
RPMsg Service sample Application
4+
################################
5+
6+
Overview
7+
********
8+
9+
RPMsg Service is an abstraction created over OpenAMP that makes initialization
10+
and endpoints creation process easier.
11+
This application demonstrates how to use RPMsg Service in Zephyr. It is designed
12+
to demonstrate how to integrate RPMsg Service with Zephyr both from a build
13+
perspective and code.
14+
15+
Building the application for nrf5340dk_nrf5340_cpuapp
16+
*****************************************************
17+
18+
.. zephyr-app-commands::
19+
:zephyr-app: samples/subsys/ipc/rpmsg_service
20+
:board: nrf5340dk_nrf5340_cpuapp
21+
:goals: debug
22+
23+
Building the application for mps2_an521
24+
***************************************
25+
26+
.. zephyr-app-commands::
27+
:zephyr-app: samples/subsys/ipc/rpmsg_service
28+
:board: mps2_an521
29+
:goals: debug
30+
31+
Building the application for v2m_musca_b1
32+
*****************************************
33+
34+
.. zephyr-app-commands::
35+
:zephyr-app: samples/subsys/ipc/rpmsg_service
36+
:board: v2m_musca_b1
37+
:goals: debug
38+
39+
Open a serial terminal (minicom, putty, etc.) and connect the board with the
40+
following settings:
41+
42+
- Speed: 115200
43+
- Data: 8 bits
44+
- Parity: None
45+
- Stop bits: 1
46+
47+
Reset the board and the following message will appear on the corresponding
48+
serial port, one is master another is remote:
49+
50+
.. code-block:: console
51+
52+
**** Booting Zephyr OS build zephyr-v1.14.0-2064-g888fc98fddaa ****
53+
Starting application thread!
54+
55+
RPMsg Service [master] demo started
56+
Master core received a message: 1
57+
Master core received a message: 3
58+
Master core received a message: 5
59+
...
60+
Master core received a message: 99
61+
RPMsg Service demo ended.
62+
63+
64+
.. code-block:: console
65+
66+
**** Booting Zephyr OS build zephyr-v1.14.0-2064-g888fc98fddaa ****
67+
Starting application thread!
68+
69+
RPMsg Service [remote] demo started
70+
Remote core received a message: 0
71+
Remote core received a message: 2
72+
Remote core received a message: 4
73+
...
74+
Remote core received a message: 98
75+
RPMsg Service demo ended.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2019 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
chosen {
9+
/*
10+
* shared memory reserved for the inter-processor communication
11+
*/
12+
zephyr,ipc_shm = &sramx;
13+
zephyr,ipc = &mhu0;
14+
};
15+
16+
sramx: memory@28180000 {
17+
compatible = "mmio-sram";
18+
reg = <0x28180000 0x8000>;
19+
};
20+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_BOARD_ENABLE_CPUNET=y
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2019 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
chosen {
9+
/*
10+
* shared memory reserved for the inter-processor communication
11+
*/
12+
zephyr,ipc_shm = &sramx;
13+
zephyr,ipc = &mhu0;
14+
};
15+
16+
sramx: memory@20060000 {
17+
compatible = "mmio-sram";
18+
reg = <0x20060000 0x8000>;
19+
};
20+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_PRINTK=y
2+
CONFIG_IPM=y
3+
CONFIG_TIMESLICE_SIZE=1
4+
CONFIG_MAIN_STACK_SIZE=2048
5+
CONFIG_HEAP_MEM_POOL_SIZE=4096
6+
CONFIG_RPMSG_SERVICE=y
7+
CONFIG_RPMSG_SERVICE_MODE_MASTER=y
8+
CONFIG_OPENAMP_SLAVE=n
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.13.1)
2+
# Copyright (c) 2019 Linaro Limited
3+
# Copyright (c) 2018-2021 Nordic Semiconductor ASA
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
8+
if("${BOARD}" STREQUAL "nrf5340dk_nrf5340_cpunet"
9+
OR "${BOARD}" STREQUAL "lpcxpresso54114_m0"
10+
OR "${BOARD}" STREQUAL "mps2_an521_nonsecure"
11+
OR "${BOARD}" STREQUAL "v2m_musca_b1_nonsecure")
12+
message(INFO " ${BOARD} compile as slave in this sample")
13+
else()
14+
message(FATAL_ERROR "${BOARD} was not supported for this sample")
15+
endif()
16+
17+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
18+
project(rpmsg_service_remote)
19+
20+
target_sources(app PRIVATE src/main.c)
21+
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/..)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_IPM_MCUX=y
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_STDOUT_CONSOLE=n
2+
CONFIG_PRINTK=n
3+
CONFIG_IPM=y
4+
CONFIG_HEAP_MEM_POOL_SIZE=4096
5+
CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=4096
6+
CONFIG_RPMSG_SERVICE=y
7+
CONFIG_RPMSG_SERVICE_MODE_REMOTE=y
8+
CONFIG_OPENAMP_MASTER=n
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2018, NXP
3+
* Copyright (c) 2018-2019, Linaro Limited
4+
* Copyright (c) 2018-2021, Nordic Semiconductor ASA
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include <zephyr.h>
10+
#include <drivers/ipm.h>
11+
#include <sys/printk.h>
12+
#include <device.h>
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
#include <string.h>
16+
17+
#include <ipc/rpmsg_service.h>
18+
19+
#define APP_TASK_STACK_SIZE (1024)
20+
K_THREAD_STACK_DEFINE(thread_stack, APP_TASK_STACK_SIZE);
21+
static struct k_thread thread_data;
22+
23+
static volatile unsigned int received_data;
24+
25+
static K_SEM_DEFINE(data_sem, 0, 1);
26+
static K_SEM_DEFINE(data_rx_sem, 0, 1);
27+
28+
int endpoint_cb(struct rpmsg_endpoint *ept, void *data,
29+
size_t len, uint32_t src, void *priv)
30+
{
31+
received_data = *((unsigned int *) data);
32+
33+
k_sem_give(&data_rx_sem);
34+
35+
return RPMSG_SUCCESS;
36+
}
37+
38+
static int ep_id;
39+
40+
static unsigned int receive_message(void)
41+
{
42+
k_sem_take(&data_rx_sem, K_FOREVER);
43+
return received_data;
44+
}
45+
46+
static int send_message(unsigned int message)
47+
{
48+
return rpmsg_service_send(ep_id, &message, sizeof(message));
49+
}
50+
51+
void app_task(void *arg1, void *arg2, void *arg3)
52+
{
53+
ARG_UNUSED(arg1);
54+
ARG_UNUSED(arg2);
55+
ARG_UNUSED(arg3);
56+
int status = 0;
57+
unsigned int message = 0U;
58+
59+
printk("\r\nRPMsg Service [remote] demo started\r\n");
60+
61+
while (message < 99) {
62+
message = receive_message();
63+
printk("Remote core received a message: %d\n", message);
64+
65+
message++;
66+
status = send_message(message);
67+
if (status < 0) {
68+
printk("send_message(%d) failed with status %d\n",
69+
message, status);
70+
break;
71+
}
72+
}
73+
74+
printk("RPMsg Service demo ended.\n");
75+
}
76+
77+
void main(void)
78+
{
79+
printk("Starting application thread!\n");
80+
k_thread_create(&thread_data, thread_stack, APP_TASK_STACK_SIZE,
81+
(k_thread_entry_t)app_task,
82+
NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
83+
}
84+
85+
/* Make sure we register endpoint before RPMsg Service is initialized. */
86+
int register_endpoint(const struct device *arg)
87+
{
88+
int status;
89+
90+
status = rpmsg_service_register_endpoint("demo", endpoint_cb);
91+
92+
if (status < 0) {
93+
printk("rpmsg_create_ept failed %d\n", status);
94+
return status;
95+
}
96+
97+
ep_id = status;
98+
99+
return 0;
100+
}
101+
102+
SYS_INIT(register_endpoint, POST_KERNEL, CONFIG_RPMSG_SERVICE_EP_REG_PRIORITY);

0 commit comments

Comments
 (0)