Skip to content

Commit beacd62

Browse files
committed
Bare metal profile: Enable USB serial greentea test
1 parent 96c0e9c commit beacd62

File tree

1 file changed

+75
-27
lines changed

1 file changed

+75
-27
lines changed

TESTS/usb_device/serial/main.cpp

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
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
@@ -78,11 +74,11 @@
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,11 @@ line_coding_t test_codings[] = {
153149
{ 9600, 8, 0, 0 },
154150
{ 57600, 8, 0, 0 },
155151
};
156-
152+
#if defined(MBED_CONF_RTOS_PRESENT)
157153
Mail<line_coding_t, 8> lc_mail;
154+
#else
155+
static CircularBuffer<line_coding_t, 8> lc_data;
156+
#endif
158157

159158
#define EF_SEND (1ul << 0)
160159
EventFlags event_flags;
@@ -297,7 +296,7 @@ void test_cdc_usb_reconnect()
297296
usb_cdc.connect();
298297
// Wait for the USB enumeration to complete.
299298
while (!usb_cdc.configured()) {
300-
ThisThread::sleep_for(1);
299+
ThisThread::sleep_for(1ms);
301300
}
302301
TEST_ASSERT_TRUE(usb_cdc.configured());
303302
TEST_ASSERT_FALSE(usb_cdc.ready());
@@ -322,7 +321,7 @@ void test_cdc_usb_reconnect()
322321
usb_cdc.connect();
323322
// Wait for the USB enumeration to complete.
324323
while (!usb_cdc.configured()) {
325-
ThisThread::sleep_for(1);
324+
ThisThread::sleep_for(1ms);
326325
}
327326
TEST_ASSERT_TRUE(usb_cdc.configured());
328327
TEST_ASSERT_FALSE(usb_cdc.ready());
@@ -370,7 +369,7 @@ void test_cdc_rx_single_bytes()
370369
}
371370
// Wait for the host to close its port.
372371
while (usb_cdc.ready()) {
373-
ThisThread::sleep_for(1);
372+
ThisThread::sleep_for(1ms);
374373
}
375374
usb_cdc.disconnect();
376375
}
@@ -381,14 +380,25 @@ void tx_thread_fun(USBCDC *usb_cdc)
381380
uint8_t buff[TX_BUFF_SIZE] = { 0 };
382381
while (event_flags.get() & EF_SEND) {
383382
if (!usb_cdc->send(buff, TX_BUFF_SIZE)) {
384-
ThisThread::sleep_for(1);
383+
ThisThread::sleep_for(1ms);
385384
continue;
386385
}
387386
buff_val++;
388387
memset(buff, buff_val, TX_BUFF_SIZE);
389388
}
390389
}
391390

391+
void tx_ticker_fun(USBCDC *usb_cdc, uint8_t start_val)
392+
{
393+
static uint8_t buff_val = start_val;
394+
uint8_t buff[TX_BUFF_SIZE] = { 0 };
395+
memset(buff, buff_val, TX_BUFF_SIZE);
396+
while (usb_cdc->send(buff, TX_BUFF_SIZE)) {
397+
break;
398+
}
399+
buff_val++;
400+
}
401+
392402
/** Test CDC receive single bytes concurrently
393403
*
394404
* Given the USB CDC device connected to a host
@@ -406,9 +416,15 @@ void test_cdc_rx_single_bytes_concurrent()
406416
ThisThread::sleep_for(LINUX_HOST_DTR_FIX_DELAY_MS);
407417
#endif
408418
usb_cdc.wait_ready();
419+
#if defined(MBED_CONF_RTOS_PRESENT)
409420
Thread tx_thread;
410421
event_flags.set(EF_SEND);
411422
tx_thread.start(mbed::callback(tx_thread_fun, &usb_cdc));
423+
#else
424+
Ticker t;
425+
t.attach([&] { tx_ticker_fun(&usb_cdc, 0); }, 3ms);
426+
#endif
427+
412428
uint8_t buff = 0x01;
413429
for (int expected = 0xff; expected >= 0; expected--) {
414430
TEST_ASSERT(usb_cdc.receive(&buff, 1, NULL));
@@ -419,10 +435,12 @@ void test_cdc_rx_single_bytes_concurrent()
419435
TEST_ASSERT_EQUAL_UINT8(expected, buff);
420436
}
421437
event_flags.clear(EF_SEND);
438+
#if defined(MBED_CONF_RTOS_PRESENT)
422439
tx_thread.join();
440+
#endif
423441
// Wait for the host to close its port.
424442
while (usb_cdc.ready()) {
425-
ThisThread::sleep_for(1);
443+
ThisThread::sleep_for(1ms);
426444
}
427445
usb_cdc.disconnect();
428446
}
@@ -461,7 +479,7 @@ void test_cdc_rx_multiple_bytes()
461479
}
462480
// Wait for the host to close its port.
463481
while (usb_cdc.ready()) {
464-
ThisThread::sleep_for(1);
482+
ThisThread::sleep_for(1ms);
465483
}
466484
usb_cdc.disconnect();
467485
}
@@ -483,9 +501,14 @@ void test_cdc_rx_multiple_bytes_concurrent()
483501
ThisThread::sleep_for(LINUX_HOST_DTR_FIX_DELAY_MS);
484502
#endif
485503
usb_cdc.wait_ready();
504+
#if defined(MBED_CONF_RTOS_PRESENT)
486505
Thread tx_thread;
487506
event_flags.set(EF_SEND);
488507
tx_thread.start(mbed::callback(tx_thread_fun, &usb_cdc));
508+
#else
509+
Ticker t;
510+
t.attach([&] { tx_ticker_fun(&usb_cdc, 0); }, 3ms);
511+
#endif
489512
uint8_t buff[RX_BUFF_SIZE] = { 0 };
490513
uint8_t expected_buff[RX_BUFF_SIZE] = { 0 };
491514
for (int expected = 0xff; expected >= 0; expected--) {
@@ -502,11 +525,15 @@ void test_cdc_rx_multiple_bytes_concurrent()
502525
TEST_ASSERT_EQUAL_UINT8_ARRAY(expected_buff, buff, RX_BUFF_SIZE);
503526
}
504527
}
528+
#if defined(MBED_CONF_RTOS_PRESENT)
505529
event_flags.clear(EF_SEND);
506530
tx_thread.join();
531+
#else
532+
t.detach();
533+
#endif
507534
// Wait for the host to close its port.
508535
while (usb_cdc.ready()) {
509-
ThisThread::sleep_for(1);
536+
ThisThread::sleep_for(1ms);
510537
}
511538
usb_cdc.disconnect();
512539
}
@@ -538,7 +565,7 @@ void test_cdc_loopback()
538565
}
539566
// Wait for the host to close its port.
540567
while (usb_cdc.ready()) {
541-
ThisThread::sleep_for(1);
568+
ThisThread::sleep_for(1ms);
542569
}
543570
usb_cdc.disconnect();
544571
}
@@ -560,7 +587,7 @@ void test_serial_usb_reconnect()
560587
usb_serial.connect();
561588
// Wait for the USB enumeration to complete.
562589
while (!usb_serial.configured()) {
563-
ThisThread::sleep_for(1);
590+
ThisThread::sleep_for(1ms);
564591
}
565592
TEST_ASSERT_TRUE(usb_serial.configured());
566593
TEST_ASSERT_FALSE(usb_serial.connected());
@@ -573,7 +600,7 @@ void test_serial_usb_reconnect()
573600
ThisThread::sleep_for(LINUX_HOST_DTR_FIX_DELAY_MS);
574601
#endif
575602
while (!usb_serial.connected()) {
576-
ThisThread::sleep_for(1);
603+
ThisThread::sleep_for(1ms);
577604
}
578605
TEST_ASSERT_TRUE(usb_serial.configured());
579606
TEST_ASSERT_TRUE(usb_serial.connected());
@@ -590,7 +617,7 @@ void test_serial_usb_reconnect()
590617
usb_serial.connect();
591618
// Wait for the USB enumeration to complete.
592619
while (!usb_serial.configured()) {
593-
ThisThread::sleep_for(1);
620+
ThisThread::sleep_for(1ms);
594621
}
595622
TEST_ASSERT_TRUE(usb_serial.configured());
596623
TEST_ASSERT_FALSE(usb_serial.connected());
@@ -603,7 +630,7 @@ void test_serial_usb_reconnect()
603630
ThisThread::sleep_for(LINUX_HOST_DTR_FIX_DELAY_MS);
604631
#endif
605632
while (!usb_serial.connected()) {
606-
ThisThread::sleep_for(1);
633+
ThisThread::sleep_for(1ms);
607634
}
608635
TEST_ASSERT_TRUE(usb_serial.configured());
609636
TEST_ASSERT_TRUE(usb_serial.connected());
@@ -633,7 +660,7 @@ void test_serial_term_reopen()
633660
ThisThread::sleep_for(LINUX_HOST_DTR_FIX_DELAY_MS);
634661
#endif
635662
while (!usb_serial.connected()) {
636-
ThisThread::sleep_for(1);
663+
ThisThread::sleep_for(1ms);
637664
}
638665
TEST_ASSERT_TRUE(usb_serial.configured());
639666
TEST_ASSERT_TRUE(usb_serial.ready());
@@ -642,7 +669,7 @@ void test_serial_term_reopen()
642669

643670
// Wait for the host to close the terminal.
644671
while (usb_serial.ready()) {
645-
ThisThread::sleep_for(1);
672+
ThisThread::sleep_for(1ms);
646673
}
647674
TEST_ASSERT_TRUE(usb_serial.configured());
648675
TEST_ASSERT_FALSE(usb_serial.ready());
@@ -656,7 +683,7 @@ void test_serial_term_reopen()
656683
ThisThread::sleep_for(LINUX_HOST_DTR_FIX_DELAY_MS);
657684
#endif
658685
while (!usb_serial.connected()) {
659-
ThisThread::sleep_for(1);
686+
ThisThread::sleep_for(1ms);
660687
}
661688
TEST_ASSERT_TRUE(usb_serial.configured());
662689
TEST_ASSERT_TRUE(usb_serial.ready());
@@ -665,7 +692,7 @@ void test_serial_term_reopen()
665692

666693
// Wait for the host to close the terminal again.
667694
while (usb_serial.ready()) {
668-
ThisThread::sleep_for(1);
695+
ThisThread::sleep_for(1ms);
669696
}
670697
TEST_ASSERT_TRUE(usb_serial.configured());
671698
TEST_ASSERT_FALSE(usb_serial.ready());
@@ -699,7 +726,7 @@ void test_serial_getc()
699726
}
700727
// Wait for the host to close its port.
701728
while (usb_serial.ready()) {
702-
ThisThread::sleep_for(1);
729+
ThisThread::sleep_for(1ms);
703730
}
704731
usb_serial.disconnect();
705732
}
@@ -736,19 +763,29 @@ void test_serial_printf_scanf()
736763
}
737764
// Wait for the host to close its port.
738765
while (usb_serial.ready()) {
739-
ThisThread::sleep_for(1);
766+
ThisThread::sleep_for(1ms);
740767
}
741768
usb_serial.disconnect();
742769
}
743770

744771
void line_coding_changed_cb(int baud, int bits, int parity, int stop)
745772
{
773+
#if defined(MBED_CONF_RTOS_PRESENT)
746774
line_coding_t *lc = lc_mail.alloc();
747775
lc->baud = baud;
748776
lc->bits = bits;
749777
lc->parity = parity;
750778
lc->stop = stop;
751779
lc_mail.put(lc);
780+
#else
781+
line_coding_t lc = {0};
782+
lc.baud = baud;
783+
lc.bits = bits;
784+
lc.parity = parity;
785+
lc.stop = stop;
786+
lc_data.push(lc);
787+
event_flags.set(EF_SEND);
788+
#endif
752789
}
753790

754791
/** Test Serial / CDC line coding change
@@ -786,6 +823,7 @@ void test_serial_line_coding_change()
786823
// calls to line_coding_changed callback on the device.
787824
while (num_expected_callbacks > 0) {
788825
num_expected_callbacks--;
826+
#if defined(MBED_CONF_RTOS_PRESENT)
789827
osEvent event = lc_mail.get();
790828
TEST_ASSERT_EQUAL_UINT32(osEventMail, event.status);
791829
lc_actual = (line_coding_t *) event.value.p;
@@ -796,17 +834,28 @@ void test_serial_line_coding_change()
796834
// set of params.
797835
lc_mail.free(lc_actual);
798836
}
837+
#else
838+
line_coding_t lc_receive = {0};
839+
event_flags.wait_all(EF_SEND);
840+
lc_data.pop(lc_receive);
841+
if (lc_expected->get_num_diffs(lc_receive) == 0) {
842+
break;
843+
}
844+
lc_actual = &lc_receive;
845+
#endif
799846
}
800847
TEST_ASSERT_EQUAL_INT(lc_expected->baud, lc_actual->baud);
801848
TEST_ASSERT_EQUAL_INT(lc_expected->bits, lc_actual->bits);
802849
TEST_ASSERT_EQUAL_INT(lc_expected->parity, lc_actual->parity);
803850
TEST_ASSERT_EQUAL_INT(lc_expected->stop, lc_actual->stop);
851+
#if defined(MBED_CONF_RTOS_PRESENT)
804852
lc_mail.free(lc_actual);
853+
#endif
805854
lc_prev = lc_expected;
806855
}
807856
// Wait for the host to close its port.
808857
while (usb_serial.ready()) {
809-
ThisThread::sleep_for(1);
858+
ThisThread::sleep_for(1ms);
810859
}
811860
usb_serial.disconnect();
812861
}
@@ -858,5 +907,4 @@ int main()
858907
}
859908

860909
#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
861-
#endif // !defined(MBED_CONF_RTOS_PRESENT)
862910
#endif // !defined(USB_DEVICE_TESTS)

0 commit comments

Comments
 (0)