diff --git a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c index 44465a3..72dde0e 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c @@ -321,7 +321,7 @@ uint32_t bootloader_init(void) } -uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms) +uint32_t bootloader_dfu_start(uint32_t timeout_ms) { uint32_t err_code; @@ -329,23 +329,17 @@ uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms) err_code = dfu_init(); VERIFY_SUCCESS(err_code); - if ( ota ) + // timeout_ms > 0 is forced startup DFU + if ( timeout_ms ) { - err_code = dfu_transport_ble_update_start(); - }else - { - // timeout_ms > 0 is forced startup DFU - if ( timeout_ms ) - { - dfu_startup_packet_received = false; + dfu_startup_packet_received = false; - 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(); + 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(); + wait_for_events(); return err_code; diff --git a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.h b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.h index 3a5fa42..f3e7d54 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.h +++ b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.h @@ -45,7 +45,7 @@ bool bootloader_app_is_valid(uint32_t app_addr); * * @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(uint32_t timeout_ms); /**@brief Function for exiting bootloader and booting into application. * diff --git a/src/boards.c b/src/boards.c index b4d7ede..6177f24 100644 --- a/src/boards.c +++ b/src/boards.c @@ -40,6 +40,7 @@ #endif //------------- IMPLEMENTATION -------------// +#if BUTTONS_NUMBER >= 2 void button_init(uint32_t pin) { if ( BUTTON_PULL == NRF_GPIO_PIN_PULLDOWN ) @@ -51,12 +52,15 @@ void button_init(uint32_t pin) nrf_gpio_cfg_sense_input(pin, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); } } +#endif +#if BUTTONS_NUMBER >= 2 bool button_pressed(uint32_t pin) { uint32_t const active_state = (BUTTON_PULL == NRF_GPIO_PIN_PULLDOWN ? 1 : 0); return nrf_gpio_pin_read(pin) == active_state; } +#endif void board_init(void) { @@ -67,9 +71,11 @@ void board_init(void) NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_RC; NRF_CLOCK->TASKS_LFCLKSTART = 1UL; +#if BUTTONS_NUMBER >= 2 button_init(BUTTON_DFU); button_init(BUTTON_FRESET); NRFX_DELAY_US(100); // wait for the pin state is stable +#endif // BUTTONS >= 2 // use PMW0 for LED RED led_pwm_init(LED_PRIMARY, LED_PRIMARY_PIN); diff --git a/src/boards.h b/src/boards.h index 595f232..c957cb9 100644 --- a/src/boards.h +++ b/src/boards.h @@ -94,11 +94,15 @@ void led_tick(void); //--------------------------------------------------------------------+ // Make sure we have at least two buttons (DFU + FRESET since DFU+FRST=OTA) #if BUTTONS_NUMBER < 2 -#error "At least two buttons required in the BSP (see 'BUTTONS_NUMBER')" -#endif +#define button_init(pin) +#define button_pressed(pin) false +#else +// #error "At least two buttons required in the BSP (see 'BUTTONS_NUMBER')" +// #endif void button_init(uint32_t pin); bool button_pressed(uint32_t pin); +#endif bool is_ota(void); diff --git a/src/main.c b/src/main.c index 5c974c1..a6bc0c6 100644 --- a/src/main.c +++ b/src/main.c @@ -136,17 +136,15 @@ enum { BLE_CONN_CFG_HIGH_BANDWIDTH = 1 }; void adafruit_factory_reset(void); -static uint32_t softdev_init(bool init_softdevice); uint32_t* dbl_reset_mem = ((uint32_t*) DFU_DBL_RESET_MEM ); // true if ble, false if serial -bool _ota_dfu = false; bool _ota_connected = false; bool is_ota(void) { - return _ota_dfu; + return false; } void softdev_mbr_init(void) @@ -160,14 +158,11 @@ int main(void) // SD is already Initialized in case of BOOTLOADER_DFU_OTA_MAGIC bool sd_inited = (NRF_POWER->GPREGRET == DFU_MAGIC_OTA_APPJUM); - // Start Bootloader in BLE OTA mode - _ota_dfu = (NRF_POWER->GPREGRET == DFU_MAGIC_OTA_APPJUM) || (NRF_POWER->GPREGRET == DFU_MAGIC_OTA_RESET); - // Serial only mode bool serial_only_dfu = (NRF_POWER->GPREGRET == DFU_MAGIC_SERIAL_ONLY_RESET); // start either serial, uf2 or ble - bool dfu_start = _ota_dfu || serial_only_dfu || (NRF_POWER->GPREGRET == DFU_MAGIC_UF2_RESET) || + bool dfu_start = serial_only_dfu || (NRF_POWER->GPREGRET == DFU_MAGIC_UF2_RESET) || (((*dbl_reset_mem) == DFU_DBL_RESET_MAGIC) && (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk)); // Clear GPREGRET if it is our values @@ -199,9 +194,6 @@ int main(void) // DFU button pressed dfu_start = dfu_start || button_pressed(BUTTON_DFU); - // DFU + FRESET are pressed --> OTA - _ota_dfu = _ota_dfu || ( button_pressed(BUTTON_DFU) && button_pressed(BUTTON_FRESET) ) ; - bool const valid_app = bootloader_app_is_valid(DFU_BANK_0_REGION_START); bool const just_start_app = valid_app && !dfu_start && (*dbl_reset_mem) == DFU_DBL_RESET_APP; @@ -221,7 +213,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(DFU_SERIAL_STARTUP_INTERVAL); #else // if RST is pressed during this delay --> if will enter dfu NRFX_DELAY_MS(DFU_DBL_RESET_DELAY); @@ -235,28 +227,13 @@ int main(void) if ( dfu_start || !valid_app ) { - if ( _ota_dfu ) - { - led_state(STATE_BLE_DISCONNECTED); - softdev_init(!sd_inited); - sd_inited = true; - } - else - { - led_state(STATE_USB_UNMOUNTED); - usb_init(serial_only_dfu); - } + led_state(STATE_USB_UNMOUNTED); + usb_init(serial_only_dfu); // Initiate an update of the firmware. - APP_ERROR_CHECK( bootloader_dfu_start(_ota_dfu, 0) ); + APP_ERROR_CHECK( bootloader_dfu_start(0) ); - if ( _ota_dfu ) - { - sd_softdevice_disable(); - }else - { - usb_teardown(); - } + usb_teardown(); } // Adafruit Factory reset @@ -304,77 +281,6 @@ void adafruit_factory_reset(void) led_state(STATE_FACTORY_RESET_FINISHED); } -/** - * Initializes the SoftDevice and the BLE event interrupt. - * @param[in] init_softdevice true if SoftDevice should be initialized. The SoftDevice must only - * be initialized if a chip reset has occured. Soft reset (jump ) from - * application must not reinitialize the SoftDevice. - */ -static uint32_t softdev_init(bool init_softdevice) -{ - if (init_softdevice) softdev_mbr_init(); - - // map vector table to bootloader address - APP_ERROR_CHECK( sd_softdevice_vector_table_base_set(BOOTLOADER_REGION_START) ); - - // Enable Softdevice, Use Internal OSC to compatible with all boards - nrf_clock_lf_cfg_t clock_cfg = - { - .source = NRF_CLOCK_LF_SRC_RC, - .rc_ctiv = 16, - .rc_temp_ctiv = 2, - .accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM - }; - - APP_ERROR_CHECK( sd_softdevice_enable(&clock_cfg, app_error_fault_handler) ); - sd_nvic_EnableIRQ(SD_EVT_IRQn); - - /*------------- Configure BLE params -------------*/ - extern uint32_t __data_start__[]; // defined in linker - uint32_t ram_start = (uint32_t) __data_start__; - - ble_cfg_t blecfg; - - // Configure the maximum number of connections. - varclr(&blecfg); - blecfg.gap_cfg.role_count_cfg.adv_set_count = 1; - blecfg.gap_cfg.role_count_cfg.periph_role_count = 1; - blecfg.gap_cfg.role_count_cfg.central_role_count = 0; - blecfg.gap_cfg.role_count_cfg.central_sec_count = 0; - APP_ERROR_CHECK( sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &blecfg, ram_start) ); - - // NRF_DFU_BLE_REQUIRES_BONDS - varclr(&blecfg); - blecfg.gatts_cfg.service_changed.service_changed = 1; - APP_ERROR_CHECK( sd_ble_cfg_set(BLE_GATTS_CFG_SERVICE_CHANGED, &blecfg, ram_start) ); - - // ATT MTU - varclr(&blecfg); - blecfg.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_HIGH_BANDWIDTH; - blecfg.conn_cfg.params.gatt_conn_cfg.att_mtu = BLEGATT_ATT_MTU_MAX; - APP_ERROR_CHECK( sd_ble_cfg_set(BLE_CONN_CFG_GATT, &blecfg, ram_start) ); - - // Event Length + HVN queue + WRITE CMD queue setting affecting bandwidth - varclr(&blecfg); - blecfg.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_HIGH_BANDWIDTH; - blecfg.conn_cfg.params.gap_conn_cfg.conn_count = 1; - blecfg.conn_cfg.params.gap_conn_cfg.event_length = BLEGAP_EVENT_LENGTH; - APP_ERROR_CHECK( sd_ble_cfg_set(BLE_CONN_CFG_GAP, &blecfg, ram_start) ); - - // Enable BLE stack. - // Note: Interrupt state (enabled, forwarding) is not work properly if not enable ble - APP_ERROR_CHECK( sd_ble_enable(&ram_start) ); - -#if 0 - ble_opt_t opt; - varclr(&opt); - opt.common_opt.conn_evt_ext.enable = 1; // enable Data Length Extension - APP_ERROR_CHECK( sd_ble_opt_set(BLE_COMMON_OPT_CONN_EVT_EXT, &opt) ); -#endif - - return NRF_SUCCESS; -} - //--------------------------------------------------------------------+ // Error Handler @@ -395,43 +301,6 @@ void assert_nrf_callback (uint16_t line_num, uint8_t const * p_file_name) /* SoftDevice Event handler *------------------------------------------------------------------*/ -// Process BLE event from SD -uint32_t proc_ble(void) -{ - __ALIGN(4) uint8_t ev_buf[ BLE_EVT_LEN_MAX(BLEGATT_ATT_MTU_MAX) ]; - uint16_t ev_len = BLE_EVT_LEN_MAX(BLEGATT_ATT_MTU_MAX); - - // Get BLE Event - uint32_t err = sd_ble_evt_get(ev_buf, &ev_len); - - // Handle valid event, ignore error - if( NRF_SUCCESS == err) - { - ble_evt_t* evt = (ble_evt_t*) ev_buf; - - switch (evt->header.evt_id) - { - case BLE_GAP_EVT_CONNECTED: - _ota_connected = true; - led_state(STATE_BLE_CONNECTED); - break; - - case BLE_GAP_EVT_DISCONNECTED: - _ota_connected = false; - led_state(STATE_BLE_DISCONNECTED); - break; - - default: break; - } - - // from dfu_transport_ble - extern void ble_evt_dispatch(ble_evt_t * p_ble_evt); - ble_evt_dispatch(evt); - } - - return err; -} - // process SOC event from SD uint32_t proc_soc(void) { @@ -462,7 +331,7 @@ void ada_sd_task(void* evt_data, uint16_t evt_size) (void) evt_size; // process BLE and SOC until there is no more events - while( (NRF_ERROR_NOT_FOUND != proc_ble()) || (NRF_ERROR_NOT_FOUND != proc_soc()) ) + while( NRF_ERROR_NOT_FOUND != proc_soc() ) { }