From 53fbcc09d2c7952f717519b33551bdc892a331e6 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 16 Apr 2018 17:29:24 +0700 Subject: [PATCH] stop usb before jumping to application. Correct irq disable for nrf52840 --- .../libraries/bootloader_dfu/bootloader.c | 6 ++-- src/main.c | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c index 7983fa6..b7f7823 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c @@ -35,10 +35,10 @@ #define IRQ_ENABLED 0x01 /**< Field identifying if an interrupt is enabled. */ -#if defined(NRF52832_XXAA) || defined(NRF52840_XXAA) +#if defined(NRF52832_XXAA) #define MAX_NUMBER_INTERRUPTS 39 -#else -#define MAX_NUMBER_INTERRUPTS 32 /**< Maximum number of interrupts available. */ +#elif defined(NRF52840_XXAA) +#define MAX_NUMBER_INTERRUPTS 48 /**< Maximum number of interrupts available. */ #endif /**@brief Enumeration for specifying current bootloader status. diff --git a/src/main.c b/src/main.c index 758c0dc..1996894 100644 --- a/src/main.c +++ b/src/main.c @@ -57,6 +57,7 @@ #include "nrf_delay.h" #include "pstorage.h" +#include "nrf_usbd.h" #include "tusb.h" #include "tusb_descriptors.h" @@ -419,6 +420,36 @@ int main(void) if (bootloader_app_is_valid(DFU_BANK_0_REGION_START) && !bootloader_dfu_sd_in_progress()) { + // Stop RTC1 + NVIC_DisableIRQ(RTC1_IRQn); + NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk; + NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk; + NRF_RTC1->TASKS_STOP = 1; + NRF_RTC1->TASKS_CLEAR = 1; + + // Stop USB + if ( NRF_USBD->ENABLE ) + { + // Abort all transfers + + // Disable pull up + nrf_usbd_pullup_disable(); + + // Disable Interrupt + NVIC_DisableIRQ(USBD_IRQn); + + // disable all interrupt + NRF_USBD->INTENCLR = NRF_USBD->INTEN; + + nrf_usbd_disable(); + sd_clock_hfclk_release(); + + sd_power_usbdetected_enable(false); + sd_power_usbpwrrdy_enable(false); + sd_power_usbremoved_enable(false); + } + + // Select a bank region to use as application region. // @note: Only applications running from DFU_BANK_0_REGION_START is supported. bootloader_app_start(DFU_BANK_0_REGION_START);