66
77#include <zephyr/kernel.h>
88#include <zephyr/device.h>
9+ #include <string.h>
10+
11+ #include <zephyr/logging/log.h>
912
1013#include <zephyr/ipc/ipc_service.h>
1114
12- #include <zephyr/logging/log.h>
13- LOG_MODULE_REGISTER (host , LOG_LEVEL_INF );
15+ #define STACKSIZE (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
1416
15- #define BASE_MESSAGE_LEN 40
16- #define MESSAGE_LEN 100
17+ K_THREAD_STACK_DEFINE (ipc0_stack , STACKSIZE );
1718
18- struct data_packet {
19- uint8_t message ;
20- uint8_t data [MESSAGE_LEN ];
19+ LOG_MODULE_REGISTER (host , LOG_LEVEL_INF );
20+
21+ struct payload {
22+ unsigned long cnt ;
23+ unsigned long size ;
24+ uint8_t data [];
2125};
2226
27+ struct payload * p_payload ;
2328
2429static K_SEM_DEFINE (bound_sem , 0 , 1 ) ;
2530
@@ -32,38 +37,15 @@ static void ep_recv(const void *data, size_t len, void *priv)
3237{
3338 uint8_t received_val = * ((uint8_t * )data );
3439 static uint8_t expected_val ;
35- static uint16_t expected_len = BASE_MESSAGE_LEN ;
3640
37- static unsigned long long cnt ;
38- static unsigned int stats_every ;
39- static uint32_t start ;
40-
41- if (start == 0 ) {
42- start = k_uptime_get_32 ();
43- }
4441
45- if ((received_val != expected_val ) || (len != expected_len )) {
46- printk ("Unexpected message\n" );
42+ if ((received_val != expected_val ) || (len != CONFIG_APP_IPC_SERVICE_MESSAGE_LEN )) {
43+ printk ("Unexpected message received_val: %d , expected_val: %d\n" ,
44+ received_val ,
45+ expected_val );
4746 }
4847
4948 expected_val ++ ;
50- expected_len ++ ;
51-
52- cnt += len ;
53-
54- if (stats_every ++ > 5000 ) {
55- /* Print throuhput [Bytes/s]. Use printk not to overload CPU with logger.
56- * Sample never reaches lower priority thread because of high throughput
57- * (100% cpu load) so logging would not be able to handle messages in
58- * deferred mode (immediate mode would be heavier than printk).
59- */
60- printk ("%llu\n" , (1000 * cnt )/(k_uptime_get_32 () - start ));
61- stats_every = 0 ;
62- }
63-
64- if (expected_len > sizeof (struct data_packet )) {
65- expected_len = BASE_MESSAGE_LEN ;
66- }
6749}
6850
6951static struct ipc_ept_cfg ep_cfg = {
@@ -77,10 +59,22 @@ static struct ipc_ept_cfg ep_cfg = {
7759int main (void )
7860{
7961 const struct device * ipc0_instance ;
80- struct data_packet msg = {.message = 0 };
8162 struct ipc_ept ep ;
8263 int ret ;
8364
65+ p_payload = (struct payload * ) k_malloc (CONFIG_APP_IPC_SERVICE_MESSAGE_LEN );
66+ if (!p_payload ) {
67+ printk ("k_malloc() failure\n" );
68+ return - ENOMEM ;
69+ }
70+
71+ memset (p_payload -> data , 0xA5 , CONFIG_APP_IPC_SERVICE_MESSAGE_LEN - sizeof (struct payload ));
72+
73+ p_payload -> size = CONFIG_APP_IPC_SERVICE_MESSAGE_LEN ;
74+ p_payload -> cnt = 0 ;
75+
76+ printk ("IPC-service %s demo started\n" , CONFIG_BOARD );
77+
8478 ipc0_instance = DEVICE_DT_GET (DT_NODELABEL (ipc0 ));
8579
8680 ret = ipc_service_open_instance (ipc0_instance );
@@ -97,24 +91,18 @@ int main(void)
9791
9892 k_sem_take (& bound_sem , K_FOREVER );
9993
100- uint16_t mlen = BASE_MESSAGE_LEN ;
101-
10294 while (true) {
103- ret = ipc_service_send (& ep , & msg , mlen );
95+ ret = ipc_service_send (& ep , p_payload , CONFIG_APP_IPC_SERVICE_MESSAGE_LEN );
10496 if (ret == - ENOMEM ) {
10597 /* No space in the buffer. Retry. */
10698 continue ;
10799 } else if (ret < 0 ) {
108- LOG_ERR ("send_message(%d ) failed with ret %d" , msg . message , ret );
100+ printk ("send_message(%ld ) failed with ret %d\n " , p_payload -> cnt , ret );
109101 break ;
110102 }
111103
112- msg . message ++ ;
104+ p_payload -> cnt ++ ;
113105
114- mlen ++ ;
115- if (mlen > sizeof (struct data_packet )) {
116- mlen = BASE_MESSAGE_LEN ;
117- }
118106
119107 /* Quasi minimal busy wait time which allows to continuosly send
120108 * data without -ENOMEM error code. The purpose is to test max
@@ -129,3 +117,26 @@ int main(void)
129117
130118 return 0 ;
131119}
120+
121+ static void check_task (void * arg1 , void * arg2 , void * arg3 )
122+ {
123+ ARG_UNUSED (arg1 );
124+ ARG_UNUSED (arg2 );
125+ ARG_UNUSED (arg3 );
126+
127+ unsigned long last_cnt = p_payload -> cnt ;
128+ unsigned long delta ;
129+
130+ while (1 ) {
131+ k_sleep (K_MSEC (1000 ));
132+
133+ delta = p_payload -> cnt - last_cnt ;
134+
135+ printk ("Δpkt: %ld (%ld B/pkt) | throughput: %ld bit/s\n" ,
136+ delta , p_payload -> size , delta * CONFIG_APP_IPC_SERVICE_MESSAGE_LEN * 8 );
137+
138+ last_cnt = p_payload -> cnt ;
139+ }
140+ }
141+ K_THREAD_DEFINE (thread_check_id , STACKSIZE , check_task , NULL , NULL , NULL ,
142+ K_PRIO_COOP (1 ), 0 , 100 );
0 commit comments