From 6feea4e0692352c259b4d8ea53895e8379fc342b Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 21 Aug 2018 14:14:23 +0700 Subject: [PATCH] fix issue with dfu startup on nrf52832, change to adafruit-nrfutil clean up dfu startup --- Makefile | 2 +- README.md | 2 +- .../libraries/bootloader_dfu/bootloader.c | 38 ++--- .../bootloader_dfu/dfu_transport_serial.c | 4 +- src/boards.c | 3 + src/sdk_config.h | 130 ------------------ 6 files changed, 19 insertions(+), 160 deletions(-) diff --git a/Makefile b/Makefile index ea55342..7db8286 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,7 @@ MK_DIS_FIRMWARE = "$(SD_NAME) $(SD_VERSION) r$(SD_VER4)" #****************************************************************************** # Tool configure #****************************************************************************** -NRFUTIL = nrfutil +NRFUTIL = adafruit-nrfutil ifneq ($(JLINK),) diff --git a/README.md b/README.md index 8c71a2b..b0ea0c9 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This repository contains the bootloader for Adafruit nRF52 series and other popu - Bluefruit Feather nRF52840 Express - Nordic nRF52840DK PCA10056 -This bootloader is meant to use with [adafruit-nrfutil](https://github.com/adafruit/Adafruit_nRF52_nrfutil), a folk of [Nordic nrfutil](https://github.com/NordicSemiconductor/pc-nrfutil). To install +[adafruit-nrfutil](https://github.com/adafruit/Adafruit_nRF52_nrfutil), modified version of [Nordic nrfutil](https://github.com/NordicSemiconductor/pc-nrfutil), is required to perform DFU. Install python3 to your system if it is not installed already and run this command to install adafruit-nrfutil from PyPi: $ pip3 install --user adafruit-nrfutil diff --git a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c index da9c2e7..c1351b7 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c @@ -60,9 +60,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. */ -APP_TIMER_DEF( _forced_startup_dfu_timer ); -volatile bool forced_startup_dfu_packet_received = false; -volatile static bool _terminate_startup_dfu = false; +APP_TIMER_DEF( _dfu_startup_timer ); +volatile bool dfu_startup_packet_received = false; /**@brief Function for handling callbacks from pstorage module. * @@ -85,25 +84,19 @@ static void pstorage_callback_handler(pstorage_handle_t * p_handle, APP_ERROR_CHECK(result); } -// Adafruit modifcation -static void terminate_startup_dfu(void * p_event_data, uint16_t event_size) -{ - (void) p_event_data; - (void) event_size; - - _terminate_startup_dfu = true; -} - /* Terminate the forced DFU mode on startup if no packets is received * by put an terminal handler to scheduler */ -static void forced_startup_dfu_timer_handler(void * p_context) +static void dfu_startup_timer_handler(void * p_context) { // No packets are received within timeout, terminal and DFU mode - // forced_startup_dfu_packet_received is set by process_dfu_packet() in dfu_transport_serial.c - if (!forced_startup_dfu_packet_received) + // dfu_startup_packet_received is set by process_dfu_packet() in dfu_transport_serial.c + if (!dfu_startup_packet_received) { - app_sched_event_put(NULL, 0, terminate_startup_dfu); + dfu_update_status_t update_status; + update_status.status_code = DFU_TIMEOUT; + + bootloader_dfu_update_process(update_status); } } @@ -142,9 +135,6 @@ static void wait_for_events(void) // When update has completed or a timeout/reset occured we will return. return; } - - // Forced startup dfu mode timeout without any received packet - if ( _terminate_startup_dfu ) return; } } @@ -339,11 +329,10 @@ uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms) // timeout_ms > 0 is forced startup DFU if ( timeout_ms ) { - forced_startup_dfu_packet_received = false; - _terminate_startup_dfu = false; + dfu_startup_packet_received = false; - (void) app_timer_create(&_forced_startup_dfu_timer, APP_TIMER_MODE_SINGLE_SHOT, forced_startup_dfu_timer_handler); - app_timer_start(_forced_startup_dfu_timer, APP_TIMER_TICKS(timeout_ms), NULL); + app_timer_create(&_dfu_startup_timer, APP_TIMER_MODE_SINGLE_SHOT, dfu_startup_timer_handler); + app_timer_start(_dfu_startup_timer, APP_TIMER_TICKS(timeout_ms), NULL); } err_code = dfu_transport_serial_update_start(); @@ -351,9 +340,6 @@ uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms) wait_for_events(); - // Close Serial transport after done - if ( !ota ) dfu_transport_serial_close(); - return err_code; } diff --git a/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_serial.c b/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_serial.c index ba7cc68..b66d59a 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_serial.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_serial.c @@ -198,8 +198,8 @@ static void process_dfu_packet(void * p_event_data, uint16_t event_size) dfu_update_packet_t * packet; // Adafruit modification for startup dfu - extern bool forced_startup_dfu_packet_received; - forced_startup_dfu_packet_received = true; + extern bool dfu_startup_packet_received; + dfu_startup_packet_received = true; while (false == DATA_QUEUE_EMPTY()) { diff --git a/src/boards.c b/src/boards.c index ab90a3e..b2b0fff 100644 --- a/src/boards.c +++ b/src/boards.c @@ -61,6 +61,9 @@ uint16_t _pwm_blue_seq[PWM_CHANNEL_NUM] = { PWM_MAXCOUNT/2, 0, 0 , 0 }; void board_init(void) { + NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk); + NRF_CLOCK->TASKS_LFCLKSTART = 1UL; + // stop WDT if started by application, when jumping from application using BLE DFU if ( NRF_WDT->RUNSTATUS ) { diff --git a/src/sdk_config.h b/src/sdk_config.h index 5fa6595..92d31d6 100644 --- a/src/sdk_config.h +++ b/src/sdk_config.h @@ -252,136 +252,6 @@ // -// PWM_ENABLED - nrf_drv_pwm - PWM peripheral driver -//========================================================== -#ifndef PWM_ENABLED -#define PWM_ENABLED 0 -#endif -// PWM_DEFAULT_CONFIG_OUT0_PIN - Out0 pin <0-31> - - -#ifndef PWM_DEFAULT_CONFIG_OUT0_PIN -#define PWM_DEFAULT_CONFIG_OUT0_PIN 31 -#endif - -// PWM_DEFAULT_CONFIG_OUT1_PIN - Out1 pin <0-31> - - -#ifndef PWM_DEFAULT_CONFIG_OUT1_PIN -#define PWM_DEFAULT_CONFIG_OUT1_PIN 31 -#endif - -// PWM_DEFAULT_CONFIG_OUT2_PIN - Out2 pin <0-31> - - -#ifndef PWM_DEFAULT_CONFIG_OUT2_PIN -#define PWM_DEFAULT_CONFIG_OUT2_PIN 31 -#endif - -// PWM_DEFAULT_CONFIG_OUT3_PIN - Out3 pin <0-31> - - -#ifndef PWM_DEFAULT_CONFIG_OUT3_PIN -#define PWM_DEFAULT_CONFIG_OUT3_PIN 31 -#endif - -// PWM_DEFAULT_CONFIG_BASE_CLOCK - Base clock - -// <0=> 16 MHz -// <1=> 8 MHz -// <2=> 4 MHz -// <3=> 2 MHz -// <4=> 1 MHz -// <5=> 500 kHz -// <6=> 250 kHz -// <7=> 125 kHz - -#ifndef PWM_DEFAULT_CONFIG_BASE_CLOCK -#define PWM_DEFAULT_CONFIG_BASE_CLOCK 4 -#endif - -// PWM_DEFAULT_CONFIG_COUNT_MODE - Count mode - -// <0=> Up -// <1=> Up and Down - -#ifndef PWM_DEFAULT_CONFIG_COUNT_MODE -#define PWM_DEFAULT_CONFIG_COUNT_MODE 0 -#endif - -// PWM_DEFAULT_CONFIG_TOP_VALUE - Top value -#ifndef PWM_DEFAULT_CONFIG_TOP_VALUE -#define PWM_DEFAULT_CONFIG_TOP_VALUE 1000 -#endif - -// PWM_DEFAULT_CONFIG_LOAD_MODE - Load mode - -// <0=> Common -// <1=> Grouped -// <2=> Individual -// <3=> Waveform - -#ifndef PWM_DEFAULT_CONFIG_LOAD_MODE -#define PWM_DEFAULT_CONFIG_LOAD_MODE 0 -#endif - -// PWM_DEFAULT_CONFIG_STEP_MODE - Step mode - -// <0=> Auto -// <1=> Triggered - -#ifndef PWM_DEFAULT_CONFIG_STEP_MODE -#define PWM_DEFAULT_CONFIG_STEP_MODE 0 -#endif - -// PWM_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority - - -// Priorities 0,1,4,5 (nRF52) are reserved for SoftDevice -// <0=> 0 (highest) -// <1=> 1 -// <2=> 2 -// <3=> 3 -// <4=> 4 -// <5=> 5 -// <6=> 6 -// <7=> 7 - -#ifndef PWM_DEFAULT_CONFIG_IRQ_PRIORITY -#define PWM_DEFAULT_CONFIG_IRQ_PRIORITY 7 -#endif - -// PWM0_ENABLED - Enable PWM0 instance - - -#ifndef PWM0_ENABLED -#define PWM0_ENABLED 0 -#endif - -// PWM1_ENABLED - Enable PWM1 instance - - -#ifndef PWM1_ENABLED -#define PWM1_ENABLED 0 -#endif - -// PWM2_ENABLED - Enable PWM2 instance - - -#ifndef PWM2_ENABLED -#define PWM2_ENABLED 0 -#endif - -// PWM3_ENABLED - Enable PWM3 instance - - -#ifndef PWM3_ENABLED -#define PWM3_ENABLED 0 -#endif - -// - - // RTC_ENABLED - nrf_drv_rtc - RTC peripheral driver //========================================================== #ifndef RTC_ENABLED