6
6
7
7
#include <zephyr/ztest.h>
8
8
#include <zephyr/kernel.h>
9
- #include <cmsis_os2.h>
9
+ #include <zephyr/portability/cmsis_os2.h>
10
+ #include <zephyr/portability/cmsis_types.h>
10
11
11
12
#include <zephyr/irq_offload.h>
12
13
#include <zephyr/kernel_structs.h>
13
14
14
- #define TIMEOUT_TICKS (10 )
15
+ #define TIMEOUT_TICKS (1000 )
15
16
#define FLAG1 (0x00000020)
16
17
#define FLAG2 (0x00000004)
18
+ #define FLAG3 (0x00000100)
17
19
#define FLAG (FLAG1 | FLAG2)
18
20
#define ISR_FLAG (0x50)
19
21
#define STACKSZ CONFIG_CMSIS_V2_THREAD_MAX_STACK_SIZE
@@ -34,6 +36,10 @@ static void thread1(void *arg)
34
36
flags = osThreadFlagsGet ();
35
37
zassert_equal (flags & FLAG1 , FLAG1 , "" );
36
38
39
+ /* We should be able to get the exact same flags again as they were not cleared */
40
+ flags = osThreadFlagsWait (FLAG1 , osFlagsWaitAny | osFlagsNoClear , 0 );
41
+ zassert_equal (flags & FLAG1 , FLAG1 , "" );
42
+
37
43
/* Clear the Flag explicitly */
38
44
flags = osThreadFlagsClear (FLAG1 );
39
45
zassert_not_equal (flags , osFlagsErrorParameter , "ThreadFlagsClear failed" );
@@ -161,4 +167,64 @@ ZTEST(cmsis_thread_flags, test_thread_flags_isr)
161
167
162
168
osDelay (TIMEOUT_TICKS );
163
169
}
170
+
171
+ static K_THREAD_STACK_DEFINE (test_stack4 , STACKSZ ) ;
172
+ static struct cmsis_rtos_thread_cb test_cb4 ;
173
+ static osThreadAttr_t thread4_attr = {
174
+ .name = "Thread4" ,
175
+ .cb_mem = & test_cb4 ,
176
+ .cb_size = sizeof (test_cb4 ),
177
+ .stack_mem = & test_stack4 ,
178
+ .stack_size = STACKSZ ,
179
+ .priority = osPriorityHigh ,
180
+ };
181
+ static bool m_thread_4_is_blocked ;
182
+
183
+ static void thread4 (void * arg )
184
+ {
185
+ uint32_t flags ;
186
+
187
+ /* Nothing will trigger FLAG1 to this thread, so the following should timeout */
188
+ flags = osThreadFlagsWait (FLAG1 , osFlagsWaitAny , 0 );
189
+ zassert_equal (flags , osFlagsErrorTimeout ,
190
+ "ThreadFlagsWait unexpected found 0x%x flags were set" );
191
+
192
+ flags = osThreadFlagsWait (FLAG1 , osFlagsWaitAll , TIMEOUT_TICKS / 10 );
193
+ zassert_equal (flags , osFlagsErrorTimeout ,
194
+ "ThreadFlagsWait unexpected found 0x%x flags were set" );
195
+
196
+ flags = osThreadFlagsWait (FLAG1 , osFlagsWaitAny | osFlagsNoClear , 0 );
197
+ zassert_equal (flags , osFlagsErrorTimeout ,
198
+ "ThreadFlagsWait unexpected found 0x%x flags were set" );
199
+
200
+ /* Nothing will trigger FLAG1 to this thread, so it should remain blocked here */
201
+ m_thread_4_is_blocked = true;
202
+ flags = osThreadFlagsWait (FLAG1 , osFlagsWaitAny , osWaitForever );
203
+ zassert_unreachable ();
204
+ }
205
+
206
+ ZTEST (cmsis_thread_flags , test_thread_flags_set_flags_not_waited_upon )
207
+ {
208
+ osThreadId_t id ;
209
+ uint32_t flags ;
210
+
211
+ m_thread_4_is_blocked = false;
212
+
213
+ id = osThreadNew (thread4 , NULL , & thread4_attr );
214
+ zassert_true (id != NULL , "Failed creating thread3" );
215
+
216
+ /* The thread will wait on any of FLAG1. Signal something it is not waiting for. */
217
+ flags = osThreadFlagsSet (id , FLAG3 );
218
+ zassert_equal (flags & FLAG3 , FLAG3 , "" );
219
+ osThreadYield ();
220
+
221
+ /* Wait a bit, but thread4 should remain blocked */
222
+ osDelay (TIMEOUT_TICKS );
223
+
224
+ zassert_true (m_thread_4_is_blocked , "Thread 4 did run till expected point" );
225
+
226
+ /* Kill the thread */
227
+ osThreadTerminate (id );
228
+ }
229
+
164
230
ZTEST_SUITE (cmsis_thread_flags , NULL , NULL , NULL , NULL , NULL );
0 commit comments