From 6b4cf21ba2e1eaf500e41fd2fbabe56fb78aa0b9 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Wed, 8 Jul 2020 13:49:53 -0700 Subject: [PATCH] Reset into app, if no USB connection in 3s (only applies in SINGLE_TAP_RESET mode (MakeCode)) --- .../libraries/bootloader_dfu/bootloader.c | 13 ++++++++++++- .../libraries/bootloader_dfu/bootloader.h | 2 +- src/main.c | 11 +++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c index 748ccbc..dd4c428 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c @@ -50,6 +50,8 @@ typedef enum static pstorage_handle_t m_bootsettings_handle; /**< Pstorage handle to use for registration and identifying the bootloader module on subsequent calls to the pstorage module for load and store of bootloader setting in flash. */ static bootloader_status_t m_update_status; /**< Current update status for the bootloader module to ensure correct behaviour when updating settings and when update completes. */ +static bool m_cancel_timeout_on_usb; + APP_TIMER_DEF( _dfu_startup_timer ); volatile bool dfu_startup_packet_received = false; @@ -79,6 +81,13 @@ static void pstorage_callback_handler(pstorage_handle_t * p_handle, */ static void dfu_startup_timer_handler(void * p_context) { +#ifdef NRF_USBD + if (m_cancel_timeout_on_usb && tud_mounted()) + { + return; + } +#endif + // No packets are received within timeout, terminal and DFU mode // dfu_startup_packet_received is set by process_dfu_packet() in dfu_transport_serial.c if (!dfu_startup_packet_received) @@ -313,10 +322,12 @@ uint32_t bootloader_init(void) } -uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms) +uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms, bool cancel_timeout_on_usb) { uint32_t err_code; + m_cancel_timeout_on_usb = cancel_timeout_on_usb; + // Clear swap if banked update is used. err_code = dfu_init(); VERIFY_SUCCESS(err_code); diff --git a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.h b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.h index 9354ffe..efd72e7 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.h +++ b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.h @@ -42,7 +42,7 @@ bool bootloader_app_is_valid(void); * * @retval NRF_SUCCESS If new application image was successfully transferred. */ -uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms); +uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms, bool cancel_timeout_on_usb); /**@brief Function for exiting bootloader and booting into application. * diff --git a/src/main.c b/src/main.c index 6148298..d03a29e 100644 --- a/src/main.c +++ b/src/main.c @@ -223,7 +223,7 @@ int main(void) * Note: Supposedly during this time if RST is press, it will count as double reset. * However Double Reset WONT work with nrf52832 since its SRAM got cleared anyway. */ - bootloader_dfu_start(false, DFU_SERIAL_STARTUP_INTERVAL); + bootloader_dfu_start(false, DFU_SERIAL_STARTUP_INTERVAL, false); #else // if RST is pressed during this delay --> if will enter dfu NRFX_DELAY_MS(DFU_DBL_RESET_DELAY); @@ -250,7 +250,14 @@ int main(void) } // Initiate an update of the firmware. - APP_ERROR_CHECK( bootloader_dfu_start(_ota_dfu, 0) ); + if (APP_ASKS_FOR_SINGLE_TAP_RESET()) + { + APP_ERROR_CHECK( bootloader_dfu_start(_ota_dfu, 3000, true) ); + } + else + { + APP_ERROR_CHECK( bootloader_dfu_start(_ota_dfu, 0, false) ); + } if ( _ota_dfu ) {