Skip to content

Commit 2230857

Browse files
Emil Obalskicarlescufi
authored andcommitted
samples: ipc: icmsg: Add usage of endpoint deregister
Expand the sample to show deregister functionality for IPC service with icmsg backend. Signed-off-by: Emil Obalski <[email protected]>
1 parent e25ca9f commit 2230857

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. _ipc_icmsg_sample:
2+
3+
IPC Service - icmsg - Sample Application
4+
########################################
5+
6+
Overview
7+
********
8+
9+
This application demonstrates how to use IPC Service and the icmsg backend with
10+
Zephyr. It is designed to demonstrate how to integrate it with Zephyr both
11+
from a build perspective and code.
12+
13+
Building the application for nrf5340dk_nrf5340_cpuapp
14+
*****************************************************
15+
16+
.. zephyr-app-commands::
17+
:zephyr-app: samples/subsys/ipc/ipc_service/icmsg
18+
:board: nrf5340dk_nrf5340_cpuapp
19+
:goals: debug
20+
21+
Open a serial terminal (minicom, putty, etc.) and connect the board with the
22+
following settings:
23+
24+
- Speed: 115200
25+
- Data: 8 bits
26+
- Parity: None
27+
- Stop bits: 1
28+
29+
Reset the board and the following message will appear on the corresponding
30+
serial port, one is host another is remote:
31+
32+
.. code-block:: console
33+
34+
*** Booting Zephyr OS build zephyr-v3.2.0-323-g468d4ae383c1 ***
35+
[00:00:00.415,985] <inf> host: IPC-service HOST demo started
36+
[00:00:00.417,816] <inf> host: Ep bounded
37+
[00:00:00.417,877] <inf> host: Perform sends for 1000 [ms]
38+
[00:00:01.417,114] <inf> host: Sent 488385 [Bytes] over 1000 [ms]
39+
[00:00:01.417,175] <inf> host: Wait 500ms. Let net core finish its sends
40+
[00:00:01.917,266] <inf> host: Stop network core
41+
[00:00:01.917,297] <inf> host: Reset IPC service
42+
[00:00:01.917,327] <inf> host: Run network core
43+
[00:00:01.924,865] <inf> host: Ep bounded
44+
[00:00:01.924,896] <inf> host: Perform sends for 1000 [ms]
45+
[00:00:02.924,194] <inf> host: Sent 489340 [Bytes] over 1000 [ms]
46+
[00:00:02.924,224] <inf> host: IPC-service HOST demo ended
47+
48+
49+
.. code-block:: console
50+
51+
*** Booting Zephyr OS build zephyr-v3.2.0-323-g468d4ae383c1 ***
52+
[00:00:00.006,256] <inf> remote: IPC-service REMOTE demo started
53+
[00:00:00.006,378] <inf> remote: Ep bounded
54+
[00:00:00.006,439] <inf> remote: Perform sends for 1000 [ms]
55+
[00:00:00.833,160] <inf> sync_rtc: Updated timestamp to synchronized RTC by 1)
56+
[00:00:01.417,572] <inf> remote: Sent 235527 [Bytes] over 1000 [ms]
57+
[00:00:01.417,602] <inf> remote: IPC-service REMOTE demo ended
58+
*** Booting Zephyr OS build zephyr-v3.2.0-323-g468d4ae383c1 ***
59+
[00:00:00.006,256] <inf> remote: IPC-service REMOTE demo started
60+
[00:00:00.006,347] <inf> remote: Ep bounded
61+
[00:00:00.006,378] <inf> remote: Perform sends for 1000 [ms]
62+
[00:00:01.006,164] <inf> remote: Sent 236797 [Bytes] over 1000 [ms]
63+
[00:00:01.006,195] <inf> remote: IPC-service REMOTE demo ended

samples/subsys/ipc/ipc_service/icmsg/src/main.c

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
#include <zephyr/device.h>
99

1010
#include <zephyr/ipc/ipc_service.h>
11+
#include <hal/nrf_reset.h>
12+
#include <string.h>
1113

1214
#include "common.h"
1315

1416
#include <zephyr/logging/log.h>
1517
LOG_MODULE_REGISTER(host, LOG_LEVEL_INF);
1618

1719

20+
#define DT_DRV_COMPAT zephyr_ipc_icmsg
21+
1822
K_SEM_DEFINE(bound_sem, 0, 1);
23+
static unsigned char expected_message = 'A';
24+
static size_t expected_len = PACKET_SIZE_START;
1925

2026
static void ep_bound(void *priv)
2127
{
@@ -26,8 +32,6 @@ static void ep_bound(void *priv)
2632
static void ep_recv(const void *data, size_t len, void *priv)
2733
{
2834
struct data_packet *packet = (struct data_packet *)data;
29-
static unsigned char expected_message = 'A';
30-
static size_t expected_len = PACKET_SIZE_START;
3135

3236
__ASSERT(packet->data[0] == expected_message, "Unexpected message. Expected %c, got %c",
3337
expected_message, packet->data[0]);
@@ -124,6 +128,58 @@ int main(void)
124128
return ret;
125129
}
126130

131+
LOG_INF("Wait 500ms. Let net core finish its sends");
132+
k_msleep(500);
133+
134+
LOG_INF("Stop network core");
135+
nrf_reset_network_force_off(NRF_RESET, true);
136+
137+
LOG_INF("Reset IPC service");
138+
139+
ret = ipc_service_deregister_endpoint(&ep);
140+
if (ret != 0) {
141+
LOG_ERR("ipc_service_register_endpoint() failure");
142+
return ret;
143+
}
144+
145+
/* Reset message and expected message value and len. */
146+
expected_message = 'A';
147+
expected_len = PACKET_SIZE_START;
148+
149+
/* Reset bound sem. */
150+
ret = k_sem_init(&bound_sem, 0, 1);
151+
if (ret != 0) {
152+
LOG_ERR("k_sem_init() failure");
153+
return ret;
154+
}
155+
156+
uintptr_t tx_shm_size = DT_REG_SIZE(DT_INST_PHANDLE(0, tx_region));
157+
uintptr_t tx_shm_addr = DT_REG_ADDR(DT_INST_PHANDLE(0, tx_region));
158+
uintptr_t rx_shm_size = DT_REG_SIZE(DT_INST_PHANDLE(0, rx_region));
159+
uintptr_t rx_shm_addr = DT_REG_ADDR(DT_INST_PHANDLE(0, rx_region));
160+
161+
LOG_INF("Clean shared memory");
162+
163+
memset((void *)tx_shm_addr, 0, tx_shm_size);
164+
memset((void *)rx_shm_addr, 0, rx_shm_size);
165+
166+
ret = ipc_service_register_endpoint(ipc0_instance, &ep, &ep_cfg);
167+
if (ret != 0) {
168+
LOG_INF("ipc_service_register_endpoint() failure");
169+
return ret;
170+
}
171+
172+
LOG_INF("Run network core");
173+
nrf_reset_network_force_off(NRF_RESET, false);
174+
175+
k_sem_take(&bound_sem, K_FOREVER);
176+
177+
ret = send_for_time(&ep, SENDING_TIME_MS);
178+
if (ret < 0) {
179+
LOG_ERR("send_for_time() failure");
180+
return ret;
181+
}
182+
127183
LOG_INF("IPC-service HOST demo ended");
128184

129185
return 0;

0 commit comments

Comments
 (0)