19
19
#error [NOT_SUPPORTED] usb device tests not enabled
20
20
#else
21
21
22
- #if !defined(MBED_CONF_RTOS_PRESENT)
23
- #error [NOT_SUPPORTED] USB stack and test cases require RTOS to run.
24
- #else
25
-
26
22
#if !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
27
23
#error [NOT_SUPPORTED] USB Device not supported for this target
28
24
#else
78
74
// The solution is to wait for the first DTR spike, ignore it, and wait for
79
75
// the correct DTR signal again.
80
76
#define LINUX_HOST_DTR_FIX 1
81
- #define LINUX_HOST_DTR_FIX_DELAY_MS 1
77
+ #define LINUX_HOST_DTR_FIX_DELAY_MS 1ms
82
78
83
79
#define CDC_LOOPBACK_REPS 1200
84
80
#define SERIAL_LOOPBACK_REPS 100
85
- #define USB_RECONNECT_DELAY_MS 1
81
+ #define USB_RECONNECT_DELAY_MS 1ms
86
82
87
83
#define LINE_CODING_SEP (' ,' )
88
84
#define LINE_CODING_DELIM (' ;' )
@@ -153,8 +149,7 @@ line_coding_t test_codings[] = {
153
149
{ 9600 , 8 , 0 , 0 },
154
150
{ 57600 , 8 , 0 , 0 },
155
151
};
156
-
157
- Mail<line_coding_t , 8 > lc_mail;
152
+ static CircularBuffer<line_coding_t , 8 > lc_data;
158
153
159
154
#define EF_SEND (1ul << 0 )
160
155
EventFlags event_flags;
@@ -297,7 +292,7 @@ void test_cdc_usb_reconnect()
297
292
usb_cdc.connect ();
298
293
// Wait for the USB enumeration to complete.
299
294
while (!usb_cdc.configured ()) {
300
- ThisThread::sleep_for (1 );
295
+ ThisThread::sleep_for (1ms );
301
296
}
302
297
TEST_ASSERT_TRUE (usb_cdc.configured ());
303
298
TEST_ASSERT_FALSE (usb_cdc.ready ());
@@ -322,7 +317,7 @@ void test_cdc_usb_reconnect()
322
317
usb_cdc.connect ();
323
318
// Wait for the USB enumeration to complete.
324
319
while (!usb_cdc.configured ()) {
325
- ThisThread::sleep_for (1 );
320
+ ThisThread::sleep_for (1ms );
326
321
}
327
322
TEST_ASSERT_TRUE (usb_cdc.configured ());
328
323
TEST_ASSERT_FALSE (usb_cdc.ready ());
@@ -370,7 +365,7 @@ void test_cdc_rx_single_bytes()
370
365
}
371
366
// Wait for the host to close its port.
372
367
while (usb_cdc.ready ()) {
373
- ThisThread::sleep_for (1 );
368
+ ThisThread::sleep_for (1ms );
374
369
}
375
370
usb_cdc.disconnect ();
376
371
}
@@ -381,14 +376,25 @@ void tx_thread_fun(USBCDC *usb_cdc)
381
376
uint8_t buff[TX_BUFF_SIZE] = { 0 };
382
377
while (event_flags.get () & EF_SEND) {
383
378
if (!usb_cdc->send (buff, TX_BUFF_SIZE)) {
384
- ThisThread::sleep_for (1 );
379
+ ThisThread::sleep_for (1ms );
385
380
continue ;
386
381
}
387
382
buff_val++;
388
383
memset (buff, buff_val, TX_BUFF_SIZE);
389
384
}
390
385
}
391
386
387
+ void tx_ticker_fun (USBCDC *usb_cdc, uint8_t start_val, uint8_t size = TX_BUFF_SIZE)
388
+ {
389
+ static uint8_t buff_val = start_val;
390
+ uint32_t actual_tx = 0 ;
391
+ uint8_t buff[TX_BUFF_SIZE] = { 0 };
392
+ memset (buff, buff_val, size);
393
+ usb_cdc->send_nb (buff, size, &actual_tx);
394
+ TEST_ASSERT_EQUAL_UINT8 (size, actual_tx);
395
+ buff_val++;
396
+ }
397
+
392
398
/* * Test CDC receive single bytes concurrently
393
399
*
394
400
* Given the USB CDC device connected to a host
@@ -406,9 +412,15 @@ void test_cdc_rx_single_bytes_concurrent()
406
412
ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
407
413
#endif
408
414
usb_cdc.wait_ready ();
415
+ #if defined(MBED_CONF_RTOS_PRESENT)
409
416
Thread tx_thread;
410
417
event_flags.set (EF_SEND);
411
418
tx_thread.start (mbed::callback (tx_thread_fun, &usb_cdc));
419
+ #else
420
+ Ticker t;
421
+ t.attach ([&] { tx_ticker_fun (&usb_cdc, 0 , 1 ); }, 2ms);
422
+ #endif
423
+
412
424
uint8_t buff = 0x01 ;
413
425
for (int expected = 0xff ; expected >= 0 ; expected--) {
414
426
TEST_ASSERT (usb_cdc.receive (&buff, 1 , NULL ));
@@ -418,11 +430,16 @@ void test_cdc_rx_single_bytes_concurrent()
418
430
TEST_ASSERT (usb_cdc.receive (&buff, 1 , NULL ));
419
431
TEST_ASSERT_EQUAL_UINT8 (expected, buff);
420
432
}
433
+
434
+ #if defined(MBED_CONF_RTOS_PRESENT)
421
435
event_flags.clear (EF_SEND);
422
436
tx_thread.join ();
437
+ #else
438
+ t.detach ();
439
+ #endif
423
440
// Wait for the host to close its port.
424
441
while (usb_cdc.ready ()) {
425
- ThisThread::sleep_for (1 );
442
+ ThisThread::sleep_for (1ms );
426
443
}
427
444
usb_cdc.disconnect ();
428
445
}
@@ -461,7 +478,7 @@ void test_cdc_rx_multiple_bytes()
461
478
}
462
479
// Wait for the host to close its port.
463
480
while (usb_cdc.ready ()) {
464
- ThisThread::sleep_for (1 );
481
+ ThisThread::sleep_for (1ms );
465
482
}
466
483
usb_cdc.disconnect ();
467
484
}
@@ -483,9 +500,14 @@ void test_cdc_rx_multiple_bytes_concurrent()
483
500
ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
484
501
#endif
485
502
usb_cdc.wait_ready ();
503
+ #if defined(MBED_CONF_RTOS_PRESENT)
486
504
Thread tx_thread;
487
505
event_flags.set (EF_SEND);
488
506
tx_thread.start (mbed::callback (tx_thread_fun, &usb_cdc));
507
+ #else
508
+ Ticker t;
509
+ t.attach ([&] { tx_ticker_fun (&usb_cdc, 0 ); }, 3ms);
510
+ #endif
489
511
uint8_t buff[RX_BUFF_SIZE] = { 0 };
490
512
uint8_t expected_buff[RX_BUFF_SIZE] = { 0 };
491
513
for (int expected = 0xff ; expected >= 0 ; expected--) {
@@ -502,11 +524,15 @@ void test_cdc_rx_multiple_bytes_concurrent()
502
524
TEST_ASSERT_EQUAL_UINT8_ARRAY (expected_buff, buff, RX_BUFF_SIZE);
503
525
}
504
526
}
527
+ #if defined(MBED_CONF_RTOS_PRESENT)
505
528
event_flags.clear (EF_SEND);
506
529
tx_thread.join ();
530
+ #else
531
+ t.detach ();
532
+ #endif
507
533
// Wait for the host to close its port.
508
534
while (usb_cdc.ready ()) {
509
- ThisThread::sleep_for (1 );
535
+ ThisThread::sleep_for (1ms );
510
536
}
511
537
usb_cdc.disconnect ();
512
538
}
@@ -538,7 +564,7 @@ void test_cdc_loopback()
538
564
}
539
565
// Wait for the host to close its port.
540
566
while (usb_cdc.ready ()) {
541
- ThisThread::sleep_for (1 );
567
+ ThisThread::sleep_for (1ms );
542
568
}
543
569
usb_cdc.disconnect ();
544
570
}
@@ -560,7 +586,7 @@ void test_serial_usb_reconnect()
560
586
usb_serial.connect ();
561
587
// Wait for the USB enumeration to complete.
562
588
while (!usb_serial.configured ()) {
563
- ThisThread::sleep_for (1 );
589
+ ThisThread::sleep_for (1ms );
564
590
}
565
591
TEST_ASSERT_TRUE (usb_serial.configured ());
566
592
TEST_ASSERT_FALSE (usb_serial.connected ());
@@ -573,7 +599,7 @@ void test_serial_usb_reconnect()
573
599
ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
574
600
#endif
575
601
while (!usb_serial.connected ()) {
576
- ThisThread::sleep_for (1 );
602
+ ThisThread::sleep_for (1ms );
577
603
}
578
604
TEST_ASSERT_TRUE (usb_serial.configured ());
579
605
TEST_ASSERT_TRUE (usb_serial.connected ());
@@ -590,7 +616,7 @@ void test_serial_usb_reconnect()
590
616
usb_serial.connect ();
591
617
// Wait for the USB enumeration to complete.
592
618
while (!usb_serial.configured ()) {
593
- ThisThread::sleep_for (1 );
619
+ ThisThread::sleep_for (1ms );
594
620
}
595
621
TEST_ASSERT_TRUE (usb_serial.configured ());
596
622
TEST_ASSERT_FALSE (usb_serial.connected ());
@@ -603,7 +629,7 @@ void test_serial_usb_reconnect()
603
629
ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
604
630
#endif
605
631
while (!usb_serial.connected ()) {
606
- ThisThread::sleep_for (1 );
632
+ ThisThread::sleep_for (1ms );
607
633
}
608
634
TEST_ASSERT_TRUE (usb_serial.configured ());
609
635
TEST_ASSERT_TRUE (usb_serial.connected ());
@@ -633,7 +659,7 @@ void test_serial_term_reopen()
633
659
ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
634
660
#endif
635
661
while (!usb_serial.connected ()) {
636
- ThisThread::sleep_for (1 );
662
+ ThisThread::sleep_for (1ms );
637
663
}
638
664
TEST_ASSERT_TRUE (usb_serial.configured ());
639
665
TEST_ASSERT_TRUE (usb_serial.ready ());
@@ -642,7 +668,7 @@ void test_serial_term_reopen()
642
668
643
669
// Wait for the host to close the terminal.
644
670
while (usb_serial.ready ()) {
645
- ThisThread::sleep_for (1 );
671
+ ThisThread::sleep_for (1ms );
646
672
}
647
673
TEST_ASSERT_TRUE (usb_serial.configured ());
648
674
TEST_ASSERT_FALSE (usb_serial.ready ());
@@ -656,7 +682,7 @@ void test_serial_term_reopen()
656
682
ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
657
683
#endif
658
684
while (!usb_serial.connected ()) {
659
- ThisThread::sleep_for (1 );
685
+ ThisThread::sleep_for (1ms );
660
686
}
661
687
TEST_ASSERT_TRUE (usb_serial.configured ());
662
688
TEST_ASSERT_TRUE (usb_serial.ready ());
@@ -665,7 +691,7 @@ void test_serial_term_reopen()
665
691
666
692
// Wait for the host to close the terminal again.
667
693
while (usb_serial.ready ()) {
668
- ThisThread::sleep_for (1 );
694
+ ThisThread::sleep_for (1ms );
669
695
}
670
696
TEST_ASSERT_TRUE (usb_serial.configured ());
671
697
TEST_ASSERT_FALSE (usb_serial.ready ());
@@ -699,7 +725,7 @@ void test_serial_getc()
699
725
}
700
726
// Wait for the host to close its port.
701
727
while (usb_serial.ready ()) {
702
- ThisThread::sleep_for (1 );
728
+ ThisThread::sleep_for (1ms );
703
729
}
704
730
usb_serial.disconnect ();
705
731
}
@@ -736,19 +762,21 @@ void test_serial_printf_scanf()
736
762
}
737
763
// Wait for the host to close its port.
738
764
while (usb_serial.ready ()) {
739
- ThisThread::sleep_for (1 );
765
+ ThisThread::sleep_for (1ms );
740
766
}
741
767
usb_serial.disconnect ();
742
768
}
743
769
744
770
void line_coding_changed_cb (int baud, int bits, int parity, int stop)
745
771
{
746
- line_coding_t *lc = lc_mail.alloc ();
747
- lc->baud = baud;
748
- lc->bits = bits;
749
- lc->parity = parity;
750
- lc->stop = stop;
751
- lc_mail.put (lc);
772
+ line_coding_t lc = {
773
+ .baud = baud,
774
+ .bits = bits,
775
+ .parity = parity,
776
+ .stop = stop
777
+ };
778
+ lc_data.push (lc);
779
+ event_flags.set (EF_SEND);
752
780
}
753
781
754
782
/* * Test Serial / CDC line coding change
@@ -772,9 +800,9 @@ void test_serial_line_coding_change()
772
800
size_t num_line_codings = sizeof test_codings / sizeof test_codings[0 ];
773
801
line_coding_t *lc_prev = &default_lc;
774
802
line_coding_t *lc_expected = NULL ;
775
- line_coding_t *lc_actual = NULL ;
776
803
int num_expected_callbacks, rc;
777
804
for (size_t i = 0 ; i < num_line_codings; i++) {
805
+ line_coding_t lc_actual = {0 };
778
806
lc_expected = &(test_codings[i]);
779
807
num_expected_callbacks = lc_prev->get_num_diffs (*lc_expected);
780
808
rc = usb_serial.printf (" %06i,%02i,%01i,%01i%c" , lc_expected->baud , lc_expected->bits , lc_expected->parity ,
@@ -786,27 +814,21 @@ void test_serial_line_coding_change()
786
814
// calls to line_coding_changed callback on the device.
787
815
while (num_expected_callbacks > 0 ) {
788
816
num_expected_callbacks--;
789
- osEvent event = lc_mail.get ();
790
- TEST_ASSERT_EQUAL_UINT32 (osEventMail, event.status );
791
- lc_actual = (line_coding_t *) event.value .p ;
792
- if (lc_expected->get_num_diffs (*lc_actual) == 0 ) {
817
+ event_flags.wait_all (EF_SEND);
818
+ lc_data.pop (lc_actual);
819
+ if (lc_expected->get_num_diffs (lc_actual) == 0 ) {
793
820
break ;
794
- } else if (num_expected_callbacks > 0 ) {
795
- // Discard lc_actual only if there is still a chance to get new
796
- // set of params.
797
- lc_mail.free (lc_actual);
798
821
}
799
822
}
800
- TEST_ASSERT_EQUAL_INT (lc_expected->baud , lc_actual->baud );
801
- TEST_ASSERT_EQUAL_INT (lc_expected->bits , lc_actual->bits );
802
- TEST_ASSERT_EQUAL_INT (lc_expected->parity , lc_actual->parity );
803
- TEST_ASSERT_EQUAL_INT (lc_expected->stop , lc_actual->stop );
804
- lc_mail.free (lc_actual);
823
+ TEST_ASSERT_EQUAL_INT (lc_expected->baud , lc_actual.baud );
824
+ TEST_ASSERT_EQUAL_INT (lc_expected->bits , lc_actual.bits );
825
+ TEST_ASSERT_EQUAL_INT (lc_expected->parity , lc_actual.parity );
826
+ TEST_ASSERT_EQUAL_INT (lc_expected->stop , lc_actual.stop );
805
827
lc_prev = lc_expected;
806
828
}
807
829
// Wait for the host to close its port.
808
830
while (usb_serial.ready ()) {
809
- ThisThread::sleep_for (1 );
831
+ ThisThread::sleep_for (1ms );
810
832
}
811
833
usb_serial.disconnect ();
812
834
}
@@ -858,5 +880,4 @@ int main()
858
880
}
859
881
860
882
#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
861
- #endif // !defined(MBED_CONF_RTOS_PRESENT)
862
883
#endif // !defined(USB_DEVICE_TESTS)
0 commit comments