From 4e355293ac664f5db1fca59e5c9c140a8a3e58f0 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Tue, 26 Jul 2022 18:47:49 -0700 Subject: [PATCH] Similar startup delay code between Teensy 3 and 4 --- teensy3/pins_teensy.c | 4 ++-- teensy4/startup.c | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/teensy3/pins_teensy.c b/teensy3/pins_teensy.c index 601cfc616..06a2ca879 100644 --- a/teensy3/pins_teensy.c +++ b/teensy3/pins_teensy.c @@ -599,9 +599,9 @@ void _init_Teensyduino_internal_(void) // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273 startup_middle_hook(); - delay(TEENSY_INIT_USB_DELAY_BEFORE); + while (millis() < TEENSY_INIT_USB_DELAY_BEFORE) ; // wait usb_init(); - delay(TEENSY_INIT_USB_DELAY_AFTER); + while (millis() < TEENSY_INIT_USB_DELAY_AFTER + TEENSY_INIT_USB_DELAY_BEFORE) ; // wait } diff --git a/teensy4/startup.c b/teensy4/startup.c index b8c261db0..e7b660192 100644 --- a/teensy4/startup.c +++ b/teensy4/startup.c @@ -146,10 +146,20 @@ void ResetHandler(void) pwm_init(); tempmon_init(); startup_middle_hook(); - while (millis() < 20) ; // wait at least 20ms before starting USB - usb_init(); - while (millis() < 300) ; // wait at least 300ms before calling user code +#if !defined(TEENSY_INIT_USB_DELAY_BEFORE) + #define TEENSY_INIT_USB_DELAY_BEFORE 20 +#endif +#if !defined(TEENSY_INIT_USB_DELAY_AFTER) + #define TEENSY_INIT_USB_DELAY_AFTER 280 +#endif + // for background about this startup delay, please see these conversations + // https://forum.pjrc.com/threads/36606?p=113980&viewfull=1#post113980 + // https://forum.pjrc.com/threads/31290?p=87273&viewfull=1#post87273 + + while (millis() < TEENSY_INIT_USB_DELAY_BEFORE) ; // wait + usb_init(); + while (millis() < TEENSY_INIT_USB_DELAY_AFTER + TEENSY_INIT_USB_DELAY_BEFORE) ; // wait //printf("before C++ constructors\n"); startup_late_hook(); __libc_init_array();