Skip to content

Commit e25ca9f

Browse files
Emil Obalskicarlescufi
authored andcommitted
samples: ipc: icsmg: Clean up the sample
Make the sample exchange the data between cores for predefined time. Clean up logging messages and do not print transfer speed on each side. This sample is now used to demonstrate functionalities of ipc service with icmsg backend. Signed-off-by: Emil Obalski <[email protected]>
1 parent 45f0fb1 commit e25ca9f

File tree

7 files changed

+139
-141
lines changed

7 files changed

+139
-141
lines changed

samples/subsys/ipc/ipc_service/icmsg/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ endif()
1717
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
1818
project(ipc_service_host)
1919

20+
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/common)
21+
2022
target_sources(app PRIVATE src/main.c)
2123

2224
include(ExternalProject)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef __COMMON_H__
8+
#define __COMMON_H__
9+
10+
#define PACKET_SIZE_START (40)
11+
#define DATA_SIZE (100)
12+
#define SENDING_TIME_MS (1000)
13+
14+
struct data_packet {
15+
unsigned long cnt;
16+
unsigned long size;
17+
unsigned char data[DATA_SIZE];
18+
};
19+
20+
#endif /* __COMMON_H__ */

samples/subsys/ipc/ipc_service/icmsg/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ CONFIG_IPC_SERVICE_BACKEND_ICMSG=y
55
CONFIG_MBOX=y
66

77
CONFIG_LOG=y
8+
CONFIG_ASSERT=y

samples/subsys/ipc/ipc_service/icmsg/remote/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ cmake_minimum_required(VERSION 3.20.0)
99
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
1010
project(ipc_service_remote)
1111

12+
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../common)
13+
1214
target_sources(app PRIVATE src/main.c)

samples/subsys/ipc/ipc_service/icmsg/remote/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ CONFIG_IPC_SERVICE_BACKEND_ICMSG=y
55
CONFIG_MBOX=y
66

77
CONFIG_LOG=y
8+
CONFIG_ASSERT=y

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

Lines changed: 57 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,11 @@
99

1010
#include <zephyr/ipc/ipc_service.h>
1111

12+
#include "common.h"
13+
1214
#include <zephyr/logging/log.h>
1315
LOG_MODULE_REGISTER(remote, LOG_LEVEL_INF);
1416

15-
#define MLEN_0 40
16-
17-
struct data_packet {
18-
unsigned char message;
19-
unsigned char data[100];
20-
};
21-
2217

2318
K_SEM_DEFINE(bound_sem, 0, 1);
2419

@@ -30,51 +25,66 @@ static void ep_bound(void *priv)
3025

3126
static void ep_recv(const void *data, size_t len, void *priv)
3227
{
33-
char received_data = *((char *)data);
34-
static char expected_message = 'a';
35-
static uint16_t expected_len = MLEN_0;
28+
struct data_packet *packet = (struct data_packet *)data;
29+
static unsigned char expected_message = 'a';
30+
static size_t expected_len = PACKET_SIZE_START;
3631

37-
static unsigned long long cnt;
38-
static unsigned int stats_every;
39-
static uint32_t start;
40-
static unsigned int err;
32+
__ASSERT(packet->data[0] == expected_message, "Unexpected message. Expected %c, got %c",
33+
expected_message, packet->data[0]);
34+
__ASSERT(len == expected_len, "Unexpected length. Expected %zu, got %zu",
35+
expected_len, len);
4136

42-
if (start == 0) {
43-
start = k_uptime_get_32();
44-
}
37+
expected_message++;
38+
expected_len++;
4539

46-
if (received_data != expected_message) {
47-
err++;
40+
if (expected_message > 'z') {
41+
expected_message = 'a';
4842
}
4943

50-
if (len != expected_len) {
51-
err++;
44+
if (expected_len > sizeof(struct data_packet)) {
45+
expected_len = PACKET_SIZE_START;
5246
}
47+
}
5348

54-
expected_message++;
55-
expected_len++;
49+
static int send_for_time(struct ipc_ept *ep, const int64_t sending_time_ms)
50+
{
51+
struct data_packet msg = {.data[0] = 'A'};
52+
size_t mlen = PACKET_SIZE_START;
53+
size_t bytes_sent = 0;
54+
int ret = 0;
5655

57-
cnt += len;
56+
LOG_INF("Perform sends for %lld [ms]", sending_time_ms);
5857

59-
if (expected_message > 'z') {
60-
if (stats_every++ > 50) {
61-
/* Print throuhput [Bytes/s]. Use printk not to overload CPU with logger.
62-
* Sample never reaches lower priority thread because of high throughput
63-
* (100% cpu load) so logging would not be able to handle messages in
64-
* deferred mode (immediate mode would be heavier than printk).
65-
*/
66-
printk("%llu\n", (1000*cnt)/(k_uptime_get_32() - start));
67-
stats_every = 0;
58+
int64_t start = k_uptime_get();
59+
60+
while ((k_uptime_get() - start) < sending_time_ms) {
61+
ret = ipc_service_send(ep, &msg, mlen);
62+
if (ret == -ENOMEM) {
63+
/* No space in the buffer. Retry. */
64+
continue;
65+
} else if (ret < 0) {
66+
LOG_ERR("Failed to send (%c) failed with ret %d", msg.data[0], ret);
67+
break;
6868
}
69-
if (err) {
70-
printk("Unexpected message\n");
69+
70+
msg.data[0]++;
71+
if (msg.data[0] > 'Z') {
72+
msg.data[0] = 'A';
7173
}
72-
expected_message = 'a';
73-
}
7474

75-
if (expected_len > sizeof(struct data_packet)) {
76-
expected_len = MLEN_0;
75+
bytes_sent += mlen;
76+
mlen++;
77+
78+
if (mlen > sizeof(struct data_packet)) {
79+
mlen = PACKET_SIZE_START;
80+
}
81+
82+
k_usleep(1);
7783
}
84+
85+
LOG_INF("Sent %zu [Bytes] over %lld [ms]", bytes_sent, sending_time_ms);
86+
87+
return ret;
7888
}
7989

8090
static struct ipc_ept_cfg ep_cfg = {
@@ -87,7 +97,6 @@ static struct ipc_ept_cfg ep_cfg = {
8797
int main(void)
8898
{
8999
const struct device *ipc0_instance;
90-
struct data_packet msg = {.message = 'A'};
91100
struct ipc_ept ep;
92101
int ret;
93102

@@ -97,48 +106,25 @@ int main(void)
97106

98107
ret = ipc_service_open_instance(ipc0_instance);
99108
if ((ret < 0) && (ret != -EALREADY)) {
100-
LOG_INF("ipc_service_open_instance() failure");
109+
LOG_ERR("ipc_service_open_instance() failure");
101110
return ret;
102111
}
103112

104113
ret = ipc_service_register_endpoint(ipc0_instance, &ep, &ep_cfg);
105-
if (ret < 0) {
106-
printf("ipc_service_register_endpoint() failure");
114+
if (ret != 0) {
115+
LOG_ERR("ipc_service_register_endpoint() failure");
107116
return ret;
108117
}
109118

110119
k_sem_take(&bound_sem, K_FOREVER);
111120

112-
uint16_t mlen = MLEN_0;
113-
114-
while (true) {
115-
ret = ipc_service_send(&ep, &msg, mlen);
116-
if (ret == -ENOMEM) {
117-
/* No space in the buffer. Retry. */
118-
continue;
119-
} else if (ret < 0) {
120-
LOG_ERR("send_message(%d) failed with ret %d", msg.message, ret);
121-
break;
122-
}
123-
124-
msg.message++;
125-
if (msg.message > 'Z') {
126-
msg.message = 'A';
127-
}
128-
129-
mlen++;
130-
if (mlen > sizeof(struct data_packet)) {
131-
mlen = MLEN_0;
132-
}
133-
134-
/* Quasi minimal busy wait time which allows to continuosly send
135-
* data without -ENOMEM error code. The purpose is to test max
136-
* throughput. Determined experimentally.
137-
*/
138-
k_busy_wait(50);
121+
ret = send_for_time(&ep, SENDING_TIME_MS);
122+
if (ret < 0) {
123+
LOG_ERR("send_for_time() failure");
124+
return ret;
139125
}
140126

141-
LOG_INF("IPC-service REMOTE demo ended.");
127+
LOG_INF("IPC-service REMOTE demo ended");
142128

143129
return 0;
144130
}

0 commit comments

Comments
 (0)