1 /*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/kernel.h>
9 #include <cmsis_os.h>
10
11 #define MESSAGE1 512
12 #define MESSAGE2 123456
13
14 #define TIMEOUT 500
15 #define Q_LEN 5
16
17 osMessageQDef(message, Q_LEN, uint32_t);
18 osMessageQId message_id;
19
send_msg_thread(void const * argument)20 void send_msg_thread(void const *argument)
21 {
22 int i;
23 osStatus status;
24 uint32_t data = MESSAGE1;
25
26 /* Wait for message_recv to complete initial checks */
27 osDelay(TIMEOUT);
28
29 /* Prepare and send the 1st message */
30 status = osMessagePut(message_id, data, osWaitForever);
31 zassert_true(status == osOK, "osMessagePut failure for Message1");
32
33 /* Fill the queue with blocks of messages */
34 for (i = 0; i < Q_LEN; i++) {
35 data = i;
36 status = osMessagePut(message_id, data, osWaitForever);
37 zassert_true(status == osOK,
38 "osMessagePut failure for message!");
39 }
40
41 /* Try putting message to a full queue immediately
42 * before it is emptied out and assert failure
43 */
44 data = MESSAGE2;
45 status = osMessagePut(message_id, data, 0);
46 zassert_true(status == osErrorResource,
47 "Something's wrong with osMessagePut!");
48
49 /* Try putting message to a full queue within a duration
50 * less than TIMEOUT, before the queue is emptied out
51 */
52 data = MESSAGE2;
53 status = osMessagePut(message_id, data, TIMEOUT/2);
54 zassert_true(status == osErrorTimeoutResource,
55 "Something's wrong with osMessagePut!");
56
57 /* Send another message after the queue is emptied */
58 data = MESSAGE2;
59 status = osMessagePut(message_id, data, TIMEOUT*2);
60 zassert_true(status == osOK,
61 "osMessagePut failure for message!");
62 }
63
message_recv(void)64 void message_recv(void)
65 {
66 int i;
67 osEvent evt;
68 uint32_t data;
69
70 /* Try getting message immediately before the queue is populated */
71 evt = osMessageGet(message_id, 0);
72 zassert_true(evt.status == osOK,
73 "Something's wrong with osMessageGet!");
74
75 /* Try receiving message within a duration of TIMEOUT */
76 evt = osMessageGet(message_id, TIMEOUT);
77 zassert_true(evt.status == osEventTimeout,
78 "Something's wrong with osMessageGet!");
79
80 /* Receive 1st message */
81 evt = osMessageGet(message_id, osWaitForever);
82 zassert_true(evt.status == osEventMessage, "osMessageGet failure");
83
84 data = evt.value.v;
85 zassert_equal(data, MESSAGE1);
86
87 /* Wait for queue to get filled */
88 osDelay(TIMEOUT);
89
90 /* Empty the queue */
91 for (i = 0; i < Q_LEN; i++) {
92 evt = osMessageGet(message_id, osWaitForever);
93 zassert_true(evt.status == osEventMessage,
94 "osMessageGet failure");
95
96 data = evt.value.v;
97 zassert_equal(data, i);
98 }
99
100 /* Receive the next message */
101 evt = osMessageGet(message_id, osWaitForever);
102 zassert_true(evt.status == osEventMessage, "osMessageGet failure");
103
104 data = evt.value.v;
105 zassert_equal(data, MESSAGE2);
106 }
107
108 osThreadDef(send_msg_thread, osPriorityNormal, 1, 0);
109
ZTEST(cmsis_msgq,test_messageq)110 ZTEST(cmsis_msgq, test_messageq)
111 {
112 osThreadId tid;
113
114 message_id = osMessageCreate(osMessageQ(message), NULL);
115 zassert_true(message_id != NULL, "Message creation failed");
116
117 tid = osThreadCreate(osThread(send_msg_thread), NULL);
118 zassert_true(tid != NULL, "Thread creation failed");
119
120 message_recv();
121 }
122 ZTEST_SUITE(cmsis_msgq, NULL, NULL, NULL, NULL, NULL);
123