14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
- #if defined(MBED_RTOS_SINGLE_THREAD) || !defined(MBED_CONF_RTOS_PRESENT)
18
- #error [NOT_SUPPORTED] Event flags test cases require RTOS with multithread to run
19
- #else
20
17
21
18
#include " mbed.h"
22
19
#include " greentea-client/test_env.h"
@@ -29,11 +26,13 @@ using utest::v1::Case;
29
26
#error [NOT_SUPPORTED] UsTicker need to be enabled for this test.
30
27
#else
31
28
29
+ #if defined(MBED_CONF_RTOS_PRESENT)
32
30
#if defined(__CORTEX_M23) || defined(__CORTEX_M33)
33
31
#define THREAD_STACK_SIZE 512
34
32
#else
35
33
#define THREAD_STACK_SIZE 320 /* 512B stack on GCC_ARM compiler cause out of memory on some 16kB RAM boards e.g. NUCLEO_F070RB */
36
34
#endif
35
+ #endif
37
36
38
37
#define MAX_FLAG_POS 30
39
38
#define PROHIBITED_FLAG_POS 31
@@ -45,20 +44,22 @@ using utest::v1::Case;
45
44
#define PROHIBITED_FLAG 0x80000000 /* 10000000000000000000000000000000 */
46
45
#define NO_FLAGS 0x0
47
46
48
- Semaphore sync_sem (0 , 1 );
49
-
50
- template <uint32_t flags, uint32_t wait_ms>
51
- void send_thread (EventFlags *ef)
47
+ void send_thread (EventFlags *ef, uint32_t flags, uint32_t wait_ms)
52
48
{
53
49
for (uint32_t i = 0 ; i <= MAX_FLAG_POS; i++) {
54
50
const uint32_t flag = flags & (1 << i);
55
51
if (flag) {
56
52
ef->set (flag);
57
- ThisThread::sleep_for (wait_ms);
53
+ if (wait_ms != 0 ) {
54
+ ThisThread::sleep_for (wait_ms);
55
+ }
58
56
}
59
57
}
60
58
}
61
59
60
+ #if defined(MBED_CONF_RTOS_PRESENT)
61
+ Semaphore sync_sem (0 , 1 );
62
+
62
63
template <uint32_t flags, uint32_t wait_ms>
63
64
void send_thread_sync (EventFlags *ef)
64
65
{
@@ -81,6 +82,7 @@ void wait_thread_all(EventFlags *ef)
81
82
TEST_ASSERT (flags | ret);
82
83
TEST_ASSERT (flags | ~flags_after_clear);
83
84
}
85
+ #endif
84
86
85
87
86
88
/* * Test if get on empty EventFlags object return NO_FLAGS
@@ -203,6 +205,7 @@ void test_set_get_clear_full_flag_range(void)
203
205
}
204
206
}
205
207
208
+ #if defined(MBED_CONF_RTOS_PRESENT)
206
209
/* * Test if multi-threaded flag set cause wait_all to return
207
210
208
211
Given a EventFlags object and three threads are started in parallel
@@ -215,9 +218,9 @@ void test_multi_thread_all(void)
215
218
Thread thread1 (osPriorityNormal, THREAD_STACK_SIZE);
216
219
Thread thread2 (osPriorityNormal, THREAD_STACK_SIZE);
217
220
Thread thread3 (osPriorityNormal, THREAD_STACK_SIZE);
218
- thread1.start (callback ( send_thread<FLAG01, 1 >, &ef) );
219
- thread2.start (callback ( send_thread<FLAG02, 2 >, &ef) );
220
- thread3.start (callback ( send_thread<FLAG03, 3 >, &ef) );
221
+ thread1.start ([&] { send_thread (&ef, FLAG01, 1 ); } );
222
+ thread2.start ([&] { send_thread (&ef, FLAG02, 2 ); } );
223
+ thread3.start ([&] { send_thread (&ef, FLAG03, 3 ); } );
221
224
222
225
uint32_t ret = ef.wait_all (FLAG01 | FLAG02 | FLAG03);
223
226
TEST_ASSERT_EQUAL (FLAG01 | FLAG02 | FLAG03, ret);
@@ -236,9 +239,9 @@ void test_multi_thread_any(void)
236
239
Thread thread1 (osPriorityNormal, THREAD_STACK_SIZE);
237
240
Thread thread2 (osPriorityNormal, THREAD_STACK_SIZE);
238
241
Thread thread3 (osPriorityNormal, THREAD_STACK_SIZE);
239
- thread1.start (callback ( send_thread<FLAG01, 1 >, &ef) );
240
- thread2.start (callback ( send_thread<FLAG02, 1 >, &ef) );
241
- thread3.start (callback ( send_thread<FLAG03, 1 >, &ef) );
242
+ thread1.start ([&] { send_thread (&ef, FLAG01, 1 ); } );
243
+ thread2.start ([&] { send_thread (&ef, FLAG02, 1 ); } );
244
+ thread3.start ([&] { send_thread (&ef, FLAG03, 1 ); } );
242
245
243
246
for (int i = 0 ; i <= MAX_FLAG_POS; i++) {
244
247
uint32_t flag = 1 << i;
@@ -291,9 +294,9 @@ void test_multi_thread_any_no_clear(void)
291
294
Thread thread1 (osPriorityNormal, THREAD_STACK_SIZE);
292
295
Thread thread2 (osPriorityNormal, THREAD_STACK_SIZE);
293
296
Thread thread3 (osPriorityNormal, THREAD_STACK_SIZE);
294
- thread1.start (callback ( send_thread<FLAG01, 1 >, &ef) );
295
- thread2.start (callback ( send_thread<FLAG02, 1 >, &ef) );
296
- thread3.start (callback ( send_thread<FLAG03, 1 >, &ef) );
297
+ thread1.start ([&] { send_thread (&ef, FLAG01, 1 ); } );
298
+ thread2.start ([&] { send_thread (&ef, FLAG02, 1 ); } );
299
+ thread3.start ([&] { send_thread (&ef, FLAG03, 1 ); } );
297
300
298
301
for (int i = 0 ; i <= MAX_FLAG_POS; i++) {
299
302
uint32_t flag = 1 << i;
@@ -347,6 +350,74 @@ void test_multi_thread_all_many_wait(void)
347
350
TEST_ASSERT_EQUAL (NO_FLAGS, ef.get ());
348
351
}
349
352
}
353
+ #endif
354
+
355
+ /* * Test if multi-event flag set cause wait_all to return
356
+
357
+ Given a EventFlags object and three ticker instance with the callback registered
358
+ When callbacks set specified flags
359
+ Then main thread waits until receive all of them
360
+ */
361
+ void test_multi_eventflags_all (void )
362
+ {
363
+ EventFlags ef;
364
+ Ticker t1, t2, t3;
365
+ t1.attach_us ([&] { send_thread (&ef, FLAG01, 0 ); }, 3000 );
366
+ t2.attach_us ([&] { send_thread (&ef, FLAG02, 0 ); }, 4000 );
367
+ t3.attach_us ([&] { send_thread (&ef, FLAG03, 0 ); }, 5000 );
368
+ uint32_t ret = ef.wait_all (FLAG01 | FLAG02 | FLAG03, 20 , false );
369
+ TEST_ASSERT_EQUAL (FLAG01 | FLAG02 | FLAG03, ret);
370
+ }
371
+
372
+ /* * Test if multi-event flag set cause wait_any to return
373
+
374
+ Given a EventFlags object and three ticker instance with the callback registered
375
+ When callbacks set specified flags
376
+ Then main thread waits until receive all of them
377
+ */
378
+ void test_multi_eventflags_any (void )
379
+ {
380
+ EventFlags ef;
381
+ uint32_t ret;
382
+ Ticker t1, t2, t3;
383
+ t1.attach_us ([&] { send_thread (&ef, FLAG01, 0 ); }, 3000 );
384
+ t2.attach_us ([&] { send_thread (&ef, FLAG02, 0 ); }, 4000 );
385
+ t3.attach_us ([&] { send_thread (&ef, FLAG03, 0 ); }, 5000 );
386
+
387
+ for (int i = 0 ; i <= MAX_FLAG_POS; i++) {
388
+ uint32_t flag = 1 << i;
389
+ ret = ef.wait_any (flag);
390
+ TEST_ASSERT (flag | ret);
391
+ }
392
+ ret = ef.get ();
393
+ TEST_ASSERT_EQUAL (NO_FLAGS, ret);
394
+ }
395
+
396
+ /* * Test if multi-event flag set cause wait_any(without clear) to return
397
+
398
+ Given a EventFlags object and three ticker instance with the callback registered
399
+ When callbacks set specified flags
400
+ Then main thread waits until receive all of them
401
+ */
402
+ void test_multi_eventflags_any_no_clear (void )
403
+ {
404
+ EventFlags ef;
405
+ uint32_t ret;
406
+ Ticker t1, t2, t3;
407
+ t1.attach_us ([&] { send_thread (&ef, FLAG01, 0 ); }, 3000 );
408
+ t2.attach_us ([&] { send_thread (&ef, FLAG02, 0 ); }, 4000 );
409
+ t3.attach_us ([&] { send_thread (&ef, FLAG03, 0 ); }, 5000 );
410
+
411
+ for (int i = 0 ; i <= MAX_FLAG_POS; i++) {
412
+ uint32_t flag = 1 << i;
413
+ ret = ef.wait_any (flag, osWaitForever, false );
414
+ TEST_ASSERT (flag | ret);
415
+ ret = ef.clear (flag);
416
+ TEST_ASSERT (ret < osFlagsError);
417
+ }
418
+ ret = ef.get ();
419
+ TEST_ASSERT_EQUAL (NO_FLAGS, ret);
420
+ }
350
421
351
422
utest::v1::status_t test_setup (const size_t number_of_cases)
352
423
{
@@ -360,11 +431,16 @@ Case cases[] = {
360
431
Case (" Test empty set" , test_empty_set),
361
432
Case (" Test clear/set with prohibited flag" , test_prohibited),
362
433
Case (" Test set/get/clear for full flag range" , test_set_get_clear_full_flag_range),
434
+ #if defined(MBED_CONF_RTOS_PRESENT)
363
435
Case (" Test multi-threaded wait_all" , test_multi_thread_all),
364
436
Case (" Test multi-threaded wait_any" , test_multi_thread_any),
365
437
Case (" Test multi-threaded wait_all many wait" , test_multi_thread_all_many_wait),
366
438
Case (" Test multi-threaded wait_any timeout" , test_multi_thread_any_timeout),
367
- Case (" Test multi-threaded wait_any no clear" , test_multi_thread_any_no_clear)
439
+ Case (" Test multi-threaded wait_any no clear" , test_multi_thread_any_no_clear),
440
+ #endif
441
+ Case (" Test multi-eventflags wait_all" , test_multi_eventflags_all),
442
+ Case (" Test multi-eventflags wait_any" , test_multi_eventflags_any),
443
+ Case (" Test multi-eventflags wait_any no clear" , test_multi_eventflags_any_no_clear)
368
444
};
369
445
370
446
utest::v1::Specification specification (test_setup, cases);
@@ -375,4 +451,3 @@ int main()
375
451
}
376
452
377
453
#endif // !DEVICE_USTICKER
378
- #endif // defined(MBED_RTOS_SINGLE_THREAD) || !defined(MBED_CONF_RTOS_PRESENT)
0 commit comments