1 /* mailbox_b.c */
2 
3 /*
4  * Copyright (c) 1997-2010, 2013-2014 Wind River Systems, Inc.
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include "master.h"
10 
11 static struct k_mbox_msg message;
12 
13 #define PRINT_HEADER()                                                       \
14 	(PRINT_STRING                                                        \
15 	   ("|   size(B) |       time/packet (nsec)       |          KB/sec" \
16 	    "                |\n"))
17 
18 #define PRINT_ONE_RESULT()                                                   \
19 	PRINT_F("|%11u|%32u|%32u|\n", putsize, puttime,	                     \
20 		(uint32_t)                                                   \
21 		(((uint64_t)putsize * 1000000U) / SAFE_DIVISOR(puttime)))
22 
23 #define PRINT_OVERHEAD()                                                     \
24 	PRINT_F("| message overhead:  %10u     nsec/packet                 " \
25 	     "              |\n", empty_msg_put_time)
26 
27 #define PRINT_XFER_RATE()                                                    \
28 	PRINT_F("| raw transfer rate:     %10u KB/sec (without"              \
29 		" overhead)                 |\n",                            \
30 		(uint32_t)((uint64_t)(putsize >> 1) * 1000000U /             \
31 			   SAFE_DIVISOR(puttime - empty_msg_put_time)))
32 
33 /*
34  * Function prototypes.
35  */
36 void mailbox_put(uint32_t size, int count, uint32_t *time);
37 
38 /*
39  * Function declarations.
40  */
41 
42 /**
43  * @brief Mailbox transfer speed test
44  */
mailbox_test(void)45 void mailbox_test(void)
46 {
47 	uint32_t putsize;
48 	uint32_t puttime;
49 	int putcount;
50 	unsigned int empty_msg_put_time;
51 	struct getinfo getinfo;
52 
53 	PRINT_STRING(dashline);
54 	PRINT_STRING("|                "
55 		     "M A I L B O X   M E A S U R E M E N T S"
56 		     "                      |\n");
57 	PRINT_STRING(dashline);
58 	PRINT_STRING("| Send mailbox message to waiting high "
59 		     "priority task and wait                 |\n");
60 	PRINT_F("| repeat for %4d times and take the "
61 		"average                                  |\n",
62 		NR_OF_MBOX_RUNS);
63 	PRINT_STRING(dashline);
64 	PRINT_HEADER();
65 	PRINT_STRING(dashline);
66 	k_sem_reset(&SEM0);
67 	k_sem_give(&STARTRCV);
68 
69 	putcount = NR_OF_MBOX_RUNS;
70 
71 	putsize = 0U;
72 	mailbox_put(putsize, putcount, &puttime);
73 	/* waiting for ack */
74 	k_msgq_get(&MB_COMM, &getinfo, K_FOREVER);
75 	PRINT_ONE_RESULT();
76 	empty_msg_put_time = puttime;
77 	for (putsize = 8U; putsize <= MESSAGE_SIZE; putsize <<= 1) {
78 		mailbox_put(putsize, putcount, &puttime);
79 		/* waiting for ack */
80 		k_msgq_get(&MB_COMM, &getinfo, K_FOREVER);
81 		PRINT_ONE_RESULT();
82 	}
83 	PRINT_STRING(dashline);
84 	PRINT_OVERHEAD();
85 	PRINT_XFER_RATE();
86 }
87 
88 
89 /**
90  * @brief Write the number of data chunks into the mailbox
91  *
92  * @param size    The size of the data chunk.
93  * @param count   Number of data chunks.
94  * @param time    The total time.
95  */
mailbox_put(uint32_t size,int count,uint32_t * time)96 void mailbox_put(uint32_t size, int count, uint32_t *time)
97 {
98 	int i;
99 	unsigned int t;
100 	timing_t  start;
101 	timing_t  end;
102 
103 	message.rx_source_thread = K_ANY;
104 	message.tx_target_thread = K_ANY;
105 
106 	/* first sync with the receiver */
107 	k_sem_give(&SEM0);
108 	start = timing_timestamp_get();
109 	for (i = 0; i < count; i++) {
110 		k_mbox_put(&MAILB1, &message, K_FOREVER);
111 	}
112 	end = timing_timestamp_get();
113 	t = (unsigned int)timing_cycles_get(&start, &end);
114 	*time = SYS_CLOCK_HW_CYCLES_TO_NS_AVG(t, count);
115 }
116