Skip to content

Commit 99124c5

Browse files
committed
Make USB disconnect + connect time explicit
Create the define MIN_DISCONNECT_TIME_US to be used as the amount of time that must pass between connect and disconnect for the host to reliably detect reconnection. Replace the wait calls delaying for this value to wait_us to indicate the precision required. Finally, remove the unnecessary calls to wait_ms in repeated_construction_destruction_test.
1 parent cf48e63 commit 99124c5

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

TESTS/usb_device/basic/main.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323

2424
#include "USBTester.h"
2525

26+
// If disconnect() + connect() occur too fast the reset event will be dropped.
27+
// At a minimum there should be a 200us delay between disconnect and connect.
28+
// To be on the safe side I would recommend a 1ms delay, so the host controller
29+
// has an entire USB frame to detect the disconnect.
30+
#define MIN_DISCONNECT_TIME_US 1000
31+
2632
#if !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
2733
#error [NOT_SUPPORTED] USB Device not supported for this target
2834
#endif
@@ -166,11 +172,7 @@ void device_soft_reconnection_test()
166172

167173
for(int i = 0; i < reconnect_try_count; i++) {
168174
serial.disconnect();
169-
// If disconnect() + connect() occur too fast the reset event will be dropped.
170-
// At a minimum there should be a 200us delay between disconnect and connect.
171-
// To be on the safe side I would recommend a 1ms delay, so the host controller
172-
// has an entire USB frame to detect the disconnect.
173-
wait_ms(1);
175+
wait_us(MIN_DISCONNECT_TIME_US);
174176
serial.connect();
175177
greentea_send_kv("device_soft_reconnection_test", serial.get_serial_desc_string());
176178
// Wait for host before terminating
@@ -179,13 +181,13 @@ void device_soft_reconnection_test()
179181
}
180182

181183
serial.disconnect();
182-
wait_ms(1);
184+
wait_us(MIN_DISCONNECT_TIME_US);
183185
serial.connect();
184186
serial.disconnect();
185-
wait_ms(1);
187+
wait_us(MIN_DISCONNECT_TIME_US);
186188
serial.connect();
187189
serial.disconnect();
188-
wait_ms(1);
190+
wait_us(MIN_DISCONNECT_TIME_US);
189191
serial.connect();
190192
greentea_send_kv("device_soft_reconnection_test", serial.get_serial_desc_string());
191193
// Wait for host before terminating
@@ -228,50 +230,44 @@ void repeated_construction_destruction_test()
228230
{
229231
USBTester serial(vendor_id, product_id, product_release, true);
230232
TEST_ASSERT_EQUAL(true, serial.configured());
231-
wait_ms(1);
232233
}
233234

234-
wait_ms(1);
235+
wait_us(MIN_DISCONNECT_TIME_US);
235236
{
236237
USBTester serial(vendor_id, product_id, product_release, true);
237238
TEST_ASSERT_EQUAL(true, serial.configured());
238-
wait_ms(1);
239239
}
240240

241-
wait_ms(1);
241+
wait_us(MIN_DISCONNECT_TIME_US);
242242
{
243243
USBTester serial(vendor_id, product_id, product_release, true);
244244
TEST_ASSERT_EQUAL(true, serial.configured());
245-
wait_ms(1);
246245
}
247246

248-
wait_ms(1);
247+
wait_us(MIN_DISCONNECT_TIME_US);
249248
{
250249
USBTester serial(vendor_id, product_id, product_release, true);
251250
TEST_ASSERT_EQUAL(true, serial.configured());
252-
wait_ms(1);
253251
greentea_send_kv("repeated_construction_destruction_test", serial.get_serial_desc_string());
254252
// Wait for host before terminating
255253
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
256254
TEST_ASSERT_EQUAL_STRING("pass", _key);
257255
}
258256

259-
wait_ms(1);
257+
wait_us(MIN_DISCONNECT_TIME_US);
260258
{
261259
USBTester serial(vendor_id, product_id, product_release, true);
262260
TEST_ASSERT_EQUAL(true, serial.configured());
263-
wait_ms(1);
264261
greentea_send_kv("repeated_construction_destruction_test", serial.get_serial_desc_string());
265262
// Wait for host before terminating
266263
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
267264
TEST_ASSERT_EQUAL_STRING("pass", _key);
268265
}
269266

270-
wait_ms(1);
267+
wait_us(MIN_DISCONNECT_TIME_US);
271268
{
272269
USBTester serial(vendor_id, product_id, product_release, true);
273270
TEST_ASSERT_EQUAL(true, serial.configured());
274-
wait_ms(1);
275271
greentea_send_kv("repeated_construction_destruction_test", serial.get_serial_desc_string());
276272
// Wait for host before terminating
277273
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));

0 commit comments

Comments
 (0)