52
52
// This way the device has to correctly handle data bigger that its buffer.
53
53
#define HOST_RX_BUFF_SIZE_RATIO 64
54
54
55
+ // A DTR line is used to signal that the host has configured a terminal and
56
+ // is ready to transmit and receive data from the USB CDC/Serial device.
57
+ // When this test suite is run with the use of a Linux host, a workaround has
58
+ // to be used to overcome some platform specific DTR line behavior.
59
+ // Every time the serial port file descriptor is opened, the DTR line is
60
+ // asserted until the terminal attributes are set.
61
+ // As a consequence, the device receives a premature DTR signal with a
62
+ // duration of 200-500 us before the correct, long-lasting DTR signal set by
63
+ // the host-side test script. (tested on the Linux kernel 4.15.0)
64
+ //
65
+ // Online references:
66
+ // https://github.com/pyserial/pyserial/issues/124#issuecomment-227235402
67
+ //
68
+ // The solution is to wait for the first DTR spike, ignore it, and wait for
69
+ // the correct DTR signal again.
70
+ #define LINUX_HOST_DTR_FIX 1
71
+ #define LINUX_HOST_DTR_FIX_DELAY_MS 1
72
+
55
73
#define CDC_LOOPBACK_REPS 1200
56
74
#define SERIAL_LOOPBACK_REPS 100
57
75
#define USB_RECONNECT_DELAY_MS 1
@@ -275,6 +293,10 @@ void test_cdc_usb_reconnect()
275
293
276
294
greentea_send_kv (MSG_KEY_PORT_OPEN_WAIT, MSG_VALUE_DUMMY);
277
295
// Wait for the host to open the port.
296
+ #if LINUX_HOST_DTR_FIX
297
+ usb_cdc.wait_ready ();
298
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
299
+ #endif
278
300
usb_cdc.wait_ready ();
279
301
TEST_ASSERT_TRUE (usb_cdc.configured ());
280
302
TEST_ASSERT_TRUE (usb_cdc.ready ());
@@ -296,6 +318,10 @@ void test_cdc_usb_reconnect()
296
318
297
319
greentea_send_kv (MSG_KEY_PORT_OPEN_WAIT, MSG_VALUE_DUMMY);
298
320
// Wait for the host to open the port again.
321
+ #if LINUX_HOST_DTR_FIX
322
+ usb_cdc.wait_ready ();
323
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
324
+ #endif
299
325
usb_cdc.wait_ready ();
300
326
TEST_ASSERT_TRUE (usb_cdc.configured ());
301
327
TEST_ASSERT_TRUE (usb_cdc.ready ());
@@ -317,6 +343,10 @@ void test_cdc_rx_single_bytes()
317
343
TestUSBCDC usb_cdc (USB_CDC_VID, USB_CDC_PID, 1 , usb_dev_sn);
318
344
usb_cdc.connect ();
319
345
greentea_send_kv (MSG_KEY_SEND_BYTES_SINGLE, MSG_VALUE_DUMMY);
346
+ #if LINUX_HOST_DTR_FIX
347
+ usb_cdc.wait_ready ();
348
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
349
+ #endif
320
350
usb_cdc.wait_ready ();
321
351
uint8_t buff = 0x01 ;
322
352
for (int expected = 0xff ; expected >= 0 ; expected--) {
@@ -360,6 +390,10 @@ void test_cdc_rx_single_bytes_concurrent()
360
390
TestUSBCDC usb_cdc (USB_CDC_VID, USB_CDC_PID, 1 , usb_dev_sn);
361
391
usb_cdc.connect ();
362
392
greentea_send_kv (MSG_KEY_SEND_BYTES_SINGLE, MSG_VALUE_DUMMY);
393
+ #if LINUX_HOST_DTR_FIX
394
+ usb_cdc.wait_ready ();
395
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
396
+ #endif
363
397
usb_cdc.wait_ready ();
364
398
Thread tx_thread;
365
399
event_flags.set (EF_SEND);
@@ -393,6 +427,10 @@ void test_cdc_rx_multiple_bytes()
393
427
TestUSBCDC usb_cdc (USB_CDC_VID, USB_CDC_PID, 1 , usb_dev_sn);
394
428
usb_cdc.connect ();
395
429
greentea_send_kv (MSG_KEY_SEND_BYTES_MULTIPLE, HOST_RX_BUFF_SIZE_RATIO);
430
+ #if LINUX_HOST_DTR_FIX
431
+ usb_cdc.wait_ready ();
432
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
433
+ #endif
396
434
usb_cdc.wait_ready ();
397
435
uint8_t buff[RX_BUFF_SIZE] = { 0 };
398
436
uint8_t expected_buff[RX_BUFF_SIZE] = { 0 };
@@ -429,6 +467,10 @@ void test_cdc_rx_multiple_bytes_concurrent()
429
467
TestUSBCDC usb_cdc (USB_CDC_VID, USB_CDC_PID, 1 , usb_dev_sn);
430
468
usb_cdc.connect ();
431
469
greentea_send_kv (MSG_KEY_SEND_BYTES_MULTIPLE, HOST_RX_BUFF_SIZE_RATIO);
470
+ #if LINUX_HOST_DTR_FIX
471
+ usb_cdc.wait_ready ();
472
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
473
+ #endif
432
474
usb_cdc.wait_ready ();
433
475
Thread tx_thread;
434
476
event_flags.set (EF_SEND);
@@ -470,6 +512,10 @@ void test_cdc_loopback()
470
512
TestUSBCDC usb_cdc (USB_CDC_VID, USB_CDC_PID, 1 , usb_dev_sn);
471
513
usb_cdc.connect ();
472
514
greentea_send_kv (MSG_KEY_LOOPBACK, MSG_VALUE_DUMMY);
515
+ #if LINUX_HOST_DTR_FIX
516
+ usb_cdc.wait_ready ();
517
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
518
+ #endif
473
519
usb_cdc.wait_ready ();
474
520
uint8_t rx_buff, tx_buff;
475
521
for (int i = 0 ; i < CDC_LOOPBACK_REPS; i++) {
@@ -511,6 +557,10 @@ void test_serial_usb_reconnect()
511
557
512
558
greentea_send_kv (MSG_KEY_PORT_OPEN_WAIT, MSG_VALUE_DUMMY);
513
559
// Wait for the host to open the port.
560
+ #if LINUX_HOST_DTR_FIX
561
+ usb_serial.wait_ready ();
562
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
563
+ #endif
514
564
while (!usb_serial.connected ()) {
515
565
wait_ms (1 );
516
566
}
@@ -537,6 +587,10 @@ void test_serial_usb_reconnect()
537
587
538
588
greentea_send_kv (MSG_KEY_PORT_OPEN_WAIT, MSG_VALUE_DUMMY);
539
589
// Wait for the host to open the port again.
590
+ #if LINUX_HOST_DTR_FIX
591
+ usb_serial.wait_ready ();
592
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
593
+ #endif
540
594
while (!usb_serial.connected ()) {
541
595
wait_ms (1 );
542
596
}
@@ -563,6 +617,10 @@ void test_serial_term_reopen()
563
617
usb_serial.connect ();
564
618
greentea_send_kv (MSG_KEY_PORT_OPEN_CLOSE, MSG_VALUE_DUMMY);
565
619
// Wait for the host to open the terminal.
620
+ #if LINUX_HOST_DTR_FIX
621
+ usb_serial.wait_ready ();
622
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
623
+ #endif
566
624
while (!usb_serial.connected ()) {
567
625
wait_ms (1 );
568
626
}
@@ -582,6 +640,10 @@ void test_serial_term_reopen()
582
640
583
641
greentea_send_kv (MSG_KEY_PORT_OPEN_CLOSE, MSG_VALUE_DUMMY);
584
642
// Wait for the host to open the terminal again.
643
+ #if LINUX_HOST_DTR_FIX
644
+ usb_serial.wait_ready ();
645
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
646
+ #endif
585
647
while (!usb_serial.connected ()) {
586
648
wait_ms (1 );
587
649
}
@@ -613,9 +675,11 @@ void test_serial_getc()
613
675
TestUSBSerial usb_serial (USB_SERIAL_VID, USB_SERIAL_PID, 1 , usb_dev_sn);
614
676
usb_serial.connect ();
615
677
greentea_send_kv (MSG_KEY_SEND_BYTES_SINGLE, MSG_VALUE_DUMMY);
616
- while (!usb_serial.connected ()) {
617
- wait_ms (1 );
618
- }
678
+ #if LINUX_HOST_DTR_FIX
679
+ usb_serial.wait_ready ();
680
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
681
+ #endif
682
+ usb_serial.wait_ready ();
619
683
for (int expected = 0xff ; expected >= 0 ; expected--) {
620
684
TEST_ASSERT_EQUAL_INT (expected, usb_serial.getc ());
621
685
}
@@ -643,6 +707,10 @@ void test_serial_printf_scanf()
643
707
TestUSBSerial usb_serial (USB_SERIAL_VID, USB_SERIAL_PID, 1 , usb_dev_sn);
644
708
usb_serial.connect ();
645
709
greentea_send_kv (MSG_KEY_LOOPBACK, MSG_VALUE_DUMMY);
710
+ #if LINUX_HOST_DTR_FIX
711
+ usb_serial.wait_ready ();
712
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
713
+ #endif
646
714
usb_serial.wait_ready ();
647
715
static const char fmt[] = " Formatted\n string %i." ;
648
716
int tx_val, rx_val, rc;
@@ -684,6 +752,10 @@ void test_serial_line_coding_change()
684
752
TestUSBSerial usb_serial (USB_SERIAL_VID, USB_SERIAL_PID, 1 , usb_dev_sn);
685
753
usb_serial.connect ();
686
754
greentea_send_kv (MSG_KEY_CHANGE_LINE_CODING, MSG_VALUE_DUMMY);
755
+ #if LINUX_HOST_DTR_FIX
756
+ usb_serial.wait_ready ();
757
+ wait_ms (LINUX_HOST_DTR_FIX_DELAY_MS);
758
+ #endif
687
759
usb_serial.wait_ready ();
688
760
usb_serial.attach (line_coding_changed_cb);
689
761
size_t num_line_codings = sizeof test_codings / sizeof test_codings[0 ];
0 commit comments