1919#error [NOT_SUPPORTED] usb device tests not enabled
2020#else
2121
22- #if !defined(MBED_CONF_RTOS_PRESENT)
23- #error [NOT_SUPPORTED] USB stack and test cases require RTOS to run.
24- #else
25-
2622#if !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
2723#error [NOT_SUPPORTED] USB Device not supported for this target
2824#else
7874// The solution is to wait for the first DTR spike, ignore it, and wait for
7975// the correct DTR signal again.
8076#define LINUX_HOST_DTR_FIX 1
81- #define LINUX_HOST_DTR_FIX_DELAY_MS 1
77+ #define LINUX_HOST_DTR_FIX_DELAY_MS 1ms
8278
8379#define CDC_LOOPBACK_REPS 1200
8480#define SERIAL_LOOPBACK_REPS 100
85- #define USB_RECONNECT_DELAY_MS 1
81+ #define USB_RECONNECT_DELAY_MS 1ms
8682
8783#define LINE_CODING_SEP (' ,' )
8884#define LINE_CODING_DELIM (' ;' )
@@ -153,8 +149,7 @@ line_coding_t test_codings[] = {
153149 { 9600 , 8 , 0 , 0 },
154150 { 57600 , 8 , 0 , 0 },
155151};
156-
157- Mail<line_coding_t , 8 > lc_mail;
152+ static CircularBuffer<line_coding_t , 8 > lc_data;
158153
159154#define EF_SEND (1ul << 0 )
160155EventFlags event_flags;
@@ -297,7 +292,7 @@ void test_cdc_usb_reconnect()
297292 usb_cdc.connect ();
298293 // Wait for the USB enumeration to complete.
299294 while (!usb_cdc.configured ()) {
300- ThisThread::sleep_for (1 );
295+ ThisThread::sleep_for (1ms );
301296 }
302297 TEST_ASSERT_TRUE (usb_cdc.configured ());
303298 TEST_ASSERT_FALSE (usb_cdc.ready ());
@@ -322,7 +317,7 @@ void test_cdc_usb_reconnect()
322317 usb_cdc.connect ();
323318 // Wait for the USB enumeration to complete.
324319 while (!usb_cdc.configured ()) {
325- ThisThread::sleep_for (1 );
320+ ThisThread::sleep_for (1ms );
326321 }
327322 TEST_ASSERT_TRUE (usb_cdc.configured ());
328323 TEST_ASSERT_FALSE (usb_cdc.ready ());
@@ -370,7 +365,7 @@ void test_cdc_rx_single_bytes()
370365 }
371366 // Wait for the host to close its port.
372367 while (usb_cdc.ready ()) {
373- ThisThread::sleep_for (1 );
368+ ThisThread::sleep_for (1ms );
374369 }
375370 usb_cdc.disconnect ();
376371}
@@ -381,14 +376,25 @@ void tx_thread_fun(USBCDC *usb_cdc)
381376 uint8_t buff[TX_BUFF_SIZE] = { 0 };
382377 while (event_flags.get () & EF_SEND) {
383378 if (!usb_cdc->send (buff, TX_BUFF_SIZE)) {
384- ThisThread::sleep_for (1 );
379+ ThisThread::sleep_for (1ms );
385380 continue ;
386381 }
387382 buff_val++;
388383 memset (buff, buff_val, TX_BUFF_SIZE);
389384 }
390385}
391386
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+
392398/* * Test CDC receive single bytes concurrently
393399 *
394400 * Given the USB CDC device connected to a host
@@ -406,9 +412,15 @@ void test_cdc_rx_single_bytes_concurrent()
406412 ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
407413#endif
408414 usb_cdc.wait_ready ();
415+ #if defined(MBED_CONF_RTOS_PRESENT)
409416 Thread tx_thread;
410417 event_flags.set (EF_SEND);
411418 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+
412424 uint8_t buff = 0x01 ;
413425 for (int expected = 0xff ; expected >= 0 ; expected--) {
414426 TEST_ASSERT (usb_cdc.receive (&buff, 1 , NULL ));
@@ -418,11 +430,16 @@ void test_cdc_rx_single_bytes_concurrent()
418430 TEST_ASSERT (usb_cdc.receive (&buff, 1 , NULL ));
419431 TEST_ASSERT_EQUAL_UINT8 (expected, buff);
420432 }
433+
434+ #if defined(MBED_CONF_RTOS_PRESENT)
421435 event_flags.clear (EF_SEND);
422436 tx_thread.join ();
437+ #else
438+ t.detach ();
439+ #endif
423440 // Wait for the host to close its port.
424441 while (usb_cdc.ready ()) {
425- ThisThread::sleep_for (1 );
442+ ThisThread::sleep_for (1ms );
426443 }
427444 usb_cdc.disconnect ();
428445}
@@ -461,7 +478,7 @@ void test_cdc_rx_multiple_bytes()
461478 }
462479 // Wait for the host to close its port.
463480 while (usb_cdc.ready ()) {
464- ThisThread::sleep_for (1 );
481+ ThisThread::sleep_for (1ms );
465482 }
466483 usb_cdc.disconnect ();
467484}
@@ -483,9 +500,14 @@ void test_cdc_rx_multiple_bytes_concurrent()
483500 ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
484501#endif
485502 usb_cdc.wait_ready ();
503+ #if defined(MBED_CONF_RTOS_PRESENT)
486504 Thread tx_thread;
487505 event_flags.set (EF_SEND);
488506 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
489511 uint8_t buff[RX_BUFF_SIZE] = { 0 };
490512 uint8_t expected_buff[RX_BUFF_SIZE] = { 0 };
491513 for (int expected = 0xff ; expected >= 0 ; expected--) {
@@ -502,11 +524,15 @@ void test_cdc_rx_multiple_bytes_concurrent()
502524 TEST_ASSERT_EQUAL_UINT8_ARRAY (expected_buff, buff, RX_BUFF_SIZE);
503525 }
504526 }
527+ #if defined(MBED_CONF_RTOS_PRESENT)
505528 event_flags.clear (EF_SEND);
506529 tx_thread.join ();
530+ #else
531+ t.detach ();
532+ #endif
507533 // Wait for the host to close its port.
508534 while (usb_cdc.ready ()) {
509- ThisThread::sleep_for (1 );
535+ ThisThread::sleep_for (1ms );
510536 }
511537 usb_cdc.disconnect ();
512538}
@@ -538,7 +564,7 @@ void test_cdc_loopback()
538564 }
539565 // Wait for the host to close its port.
540566 while (usb_cdc.ready ()) {
541- ThisThread::sleep_for (1 );
567+ ThisThread::sleep_for (1ms );
542568 }
543569 usb_cdc.disconnect ();
544570}
@@ -560,7 +586,7 @@ void test_serial_usb_reconnect()
560586 usb_serial.connect ();
561587 // Wait for the USB enumeration to complete.
562588 while (!usb_serial.configured ()) {
563- ThisThread::sleep_for (1 );
589+ ThisThread::sleep_for (1ms );
564590 }
565591 TEST_ASSERT_TRUE (usb_serial.configured ());
566592 TEST_ASSERT_FALSE (usb_serial.connected ());
@@ -573,7 +599,7 @@ void test_serial_usb_reconnect()
573599 ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
574600#endif
575601 while (!usb_serial.connected ()) {
576- ThisThread::sleep_for (1 );
602+ ThisThread::sleep_for (1ms );
577603 }
578604 TEST_ASSERT_TRUE (usb_serial.configured ());
579605 TEST_ASSERT_TRUE (usb_serial.connected ());
@@ -590,7 +616,7 @@ void test_serial_usb_reconnect()
590616 usb_serial.connect ();
591617 // Wait for the USB enumeration to complete.
592618 while (!usb_serial.configured ()) {
593- ThisThread::sleep_for (1 );
619+ ThisThread::sleep_for (1ms );
594620 }
595621 TEST_ASSERT_TRUE (usb_serial.configured ());
596622 TEST_ASSERT_FALSE (usb_serial.connected ());
@@ -603,7 +629,7 @@ void test_serial_usb_reconnect()
603629 ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
604630#endif
605631 while (!usb_serial.connected ()) {
606- ThisThread::sleep_for (1 );
632+ ThisThread::sleep_for (1ms );
607633 }
608634 TEST_ASSERT_TRUE (usb_serial.configured ());
609635 TEST_ASSERT_TRUE (usb_serial.connected ());
@@ -633,7 +659,7 @@ void test_serial_term_reopen()
633659 ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
634660#endif
635661 while (!usb_serial.connected ()) {
636- ThisThread::sleep_for (1 );
662+ ThisThread::sleep_for (1ms );
637663 }
638664 TEST_ASSERT_TRUE (usb_serial.configured ());
639665 TEST_ASSERT_TRUE (usb_serial.ready ());
@@ -642,7 +668,7 @@ void test_serial_term_reopen()
642668
643669 // Wait for the host to close the terminal.
644670 while (usb_serial.ready ()) {
645- ThisThread::sleep_for (1 );
671+ ThisThread::sleep_for (1ms );
646672 }
647673 TEST_ASSERT_TRUE (usb_serial.configured ());
648674 TEST_ASSERT_FALSE (usb_serial.ready ());
@@ -656,7 +682,7 @@ void test_serial_term_reopen()
656682 ThisThread::sleep_for (LINUX_HOST_DTR_FIX_DELAY_MS);
657683#endif
658684 while (!usb_serial.connected ()) {
659- ThisThread::sleep_for (1 );
685+ ThisThread::sleep_for (1ms );
660686 }
661687 TEST_ASSERT_TRUE (usb_serial.configured ());
662688 TEST_ASSERT_TRUE (usb_serial.ready ());
@@ -665,7 +691,7 @@ void test_serial_term_reopen()
665691
666692 // Wait for the host to close the terminal again.
667693 while (usb_serial.ready ()) {
668- ThisThread::sleep_for (1 );
694+ ThisThread::sleep_for (1ms );
669695 }
670696 TEST_ASSERT_TRUE (usb_serial.configured ());
671697 TEST_ASSERT_FALSE (usb_serial.ready ());
@@ -699,7 +725,7 @@ void test_serial_getc()
699725 }
700726 // Wait for the host to close its port.
701727 while (usb_serial.ready ()) {
702- ThisThread::sleep_for (1 );
728+ ThisThread::sleep_for (1ms );
703729 }
704730 usb_serial.disconnect ();
705731}
@@ -736,19 +762,21 @@ void test_serial_printf_scanf()
736762 }
737763 // Wait for the host to close its port.
738764 while (usb_serial.ready ()) {
739- ThisThread::sleep_for (1 );
765+ ThisThread::sleep_for (1ms );
740766 }
741767 usb_serial.disconnect ();
742768}
743769
744770void line_coding_changed_cb (int baud, int bits, int parity, int stop)
745771{
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);
752780}
753781
754782/* * Test Serial / CDC line coding change
@@ -772,9 +800,9 @@ void test_serial_line_coding_change()
772800 size_t num_line_codings = sizeof test_codings / sizeof test_codings[0 ];
773801 line_coding_t *lc_prev = &default_lc;
774802 line_coding_t *lc_expected = NULL ;
775- line_coding_t *lc_actual = NULL ;
776803 int num_expected_callbacks, rc;
777804 for (size_t i = 0 ; i < num_line_codings; i++) {
805+ line_coding_t lc_actual = {0 };
778806 lc_expected = &(test_codings[i]);
779807 num_expected_callbacks = lc_prev->get_num_diffs (*lc_expected);
780808 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()
786814 // calls to line_coding_changed callback on the device.
787815 while (num_expected_callbacks > 0 ) {
788816 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 ) {
793820 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);
798821 }
799822 }
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 );
805827 lc_prev = lc_expected;
806828 }
807829 // Wait for the host to close its port.
808830 while (usb_serial.ready ()) {
809- ThisThread::sleep_for (1 );
831+ ThisThread::sleep_for (1ms );
810832 }
811833 usb_serial.disconnect ();
812834}
@@ -858,5 +880,4 @@ int main()
858880}
859881
860882#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
861- #endif // !defined(MBED_CONF_RTOS_PRESENT)
862883#endif // !defined(USB_DEVICE_TESTS)
0 commit comments