@@ -97,15 +97,16 @@ void usb_teardown(void);
9797 */
9898
9999/* Magic that written to NRF_POWER->GPREGRET by application when it wish to go into DFU
100- * - DFU_MAGIC_OTA_APPJUM used by BLEDfu service : SD is already init
101- * - DFU_MAGIC_OTA_RESET entered by soft reset : SD is not init
102- * - DFU_MAGIC_SERIAL_ONLY_RESET with CDC interface only
103- * - DFU_MAGIC_UF2_RESET with CDC and MSC interfaces
100+ * - DFU_MAGIC_OTA_APPJUM : used by BLEDfu service, SD is already inited
101+ * - DFU_MAGIC_OTA_RESET : entered by soft reset, SD is not inited yet
102+ * - DFU_MAGIC_SERIAL_ONLY_RESET : with CDC interface only
103+ * - DFU_MAGIC_UF2_RESET : with CDC and MSC interfaces
104+ * - DFU_MAGIC_SKIP : skip DFU entirely including double reset delay
104105 *
105106 * Note: for DFU_MAGIC_OTA_APPJUM Softdevice must not initialized.
106107 * since it is already in application. In all other case of OTA SD must be initialized
107108 */
108- #define DFU_MAGIC_OTA_APPJUM BOOTLOADER_DFU_START // 0xB1
109+ #define DFU_MAGIC_OTA_APPJUM BOOTLOADER_DFU_START // 0xB1
109110#define DFU_MAGIC_OTA_RESET 0xA8
110111#define DFU_MAGIC_SERIAL_ONLY_RESET 0x4e
111112#define DFU_MAGIC_UF2_RESET 0x57
@@ -177,7 +178,7 @@ int main(void)
177178 bool dfu_start = _ota_dfu || serial_only_dfu || uf2_dfu ||
178179 (((* dbl_reset_mem ) == DFU_DBL_RESET_MAGIC ) && (NRF_POWER -> RESETREAS & POWER_RESETREAS_RESETPIN_Msk ));
179180
180- bool dfu_skip = (NRF_POWER -> GPREGRET == DFU_MAGIC_SKIP );
181+ bool const dfu_skip = (NRF_POWER -> GPREGRET == DFU_MAGIC_SKIP );
181182
182183 // Clear GPREGRET if it is our values
183184 if (dfu_start || dfu_skip ) NRF_POWER -> GPREGRET = 0 ;
@@ -203,79 +204,85 @@ int main(void)
203204 }
204205
205206 /*------------- Determine DFU mode (Serial, OTA, FRESET or normal) -------------*/
206- // DFU button pressed
207- dfu_start = dfu_start || (button_pressed (BUTTON_DFU ) && !dfu_skip );
207+ if ( !dfu_skip )
208+ {
209+ // DFU button pressed
210+ dfu_start = dfu_start || (button_pressed (BUTTON_DFU ) && !dfu_skip );
208211
209- // DFU + FRESET are pressed --> OTA
210- _ota_dfu = _ota_dfu || ( button_pressed (BUTTON_DFU ) && button_pressed (BUTTON_FRESET ) ) ;
212+ // DFU + FRESET are pressed --> OTA
213+ _ota_dfu = _ota_dfu || ( button_pressed (BUTTON_DFU ) && button_pressed (BUTTON_FRESET ) ) ;
211214
212- bool const valid_app = bootloader_app_is_valid ();
213- bool const just_start_app = valid_app && !dfu_start && (* dbl_reset_mem ) == DFU_DBL_RESET_APP ;
215+ bool const valid_app = bootloader_app_is_valid ();
216+ bool const just_start_app = valid_app && !dfu_start && (* dbl_reset_mem ) == DFU_DBL_RESET_APP ;
214217
215- if (!just_start_app && APP_ASKS_FOR_SINGLE_TAP_RESET ())
216- dfu_start = 1 ;
218+ if (!just_start_app && APP_ASKS_FOR_SINGLE_TAP_RESET ()) dfu_start = 1 ;
217219
218- // App mode: register 1st reset and DFU startup (nrf52832)
219- if ( ! (just_start_app || dfu_start || !valid_app ) )
220- {
221- // Register our first reset for double reset detection
222- (* dbl_reset_mem ) = DFU_DBL_RESET_MAGIC ;
220+ // App mode: register 1st reset and DFU startup (nrf52832)
221+ if ( ! (just_start_app || dfu_start || !valid_app ) )
222+ {
223+ // Register our first reset for double reset detection
224+ (* dbl_reset_mem ) = DFU_DBL_RESET_MAGIC ;
223225
224226#ifdef NRF52832_XXAA
225- /* Even DFU is not active, we still force an 1000 ms dfu serial mode when startup
226- * to support auto programming from Arduino IDE
227- *
228- * Note: Supposedly during this time if RST is press, it will count as double reset.
229- * However Double Reset WONT work with nrf52832 since its SRAM got cleared anyway.
230- */
231- bootloader_dfu_start (false, DFU_SERIAL_STARTUP_INTERVAL , false);
227+ /* Even DFU is not active, we still force an 1000 ms dfu serial mode when startup
228+ * to support auto programming from Arduino IDE
229+ *
230+ * Note: Supposedly during this time if RST is press, it will count as double reset.
231+ * However Double Reset WONT work with nrf52832 since its SRAM got cleared anyway.
232+ */
233+ bootloader_dfu_start (false, DFU_SERIAL_STARTUP_INTERVAL , false);
232234#else
233- // if RST is pressed during this delay --> if will enter dfu
234- NRFX_DELAY_MS (DFU_DBL_RESET_DELAY );
235+ // if RST is pressed during this delay --> if will enter dfu
236+ NRFX_DELAY_MS (DFU_DBL_RESET_DELAY );
235237#endif
236- }
237-
238- if (APP_ASKS_FOR_SINGLE_TAP_RESET ())
239- (* dbl_reset_mem ) = DFU_DBL_RESET_APP ;
240- else
241- (* dbl_reset_mem ) = 0 ;
242-
243- if ( dfu_start || !valid_app )
244- {
245- if ( _ota_dfu )
246- {
247- led_state (STATE_BLE_DISCONNECTED );
248- softdev_init (!sd_inited );
249- sd_inited = true;
250- }
251- else
252- {
253- led_state (STATE_USB_UNMOUNTED );
254- usb_init (serial_only_dfu );
255238 }
256239
257- // Initiate an update of the firmware.
258- if (APP_ASKS_FOR_SINGLE_TAP_RESET () || uf2_dfu || serial_only_dfu )
240+ if (APP_ASKS_FOR_SINGLE_TAP_RESET ())
259241 {
260- // If USB is not enumerated in 3s (eg. because we're running on battery), we restart into app.
261- APP_ERROR_CHECK ( bootloader_dfu_start (_ota_dfu , 3000 , true) );
242+ (* dbl_reset_mem ) = DFU_DBL_RESET_APP ;
262243 }
263244 else
264245 {
265- // No timeout if bootloader requires user action (double-reset).
266- APP_ERROR_CHECK ( bootloader_dfu_start (_ota_dfu , 0 , false) );
246+ (* dbl_reset_mem ) = 0 ;
267247 }
268248
269- if ( _ota_dfu )
249+ if ( dfu_start || ! valid_app )
270250 {
271- sd_softdevice_disable ();
272- }else
273- {
274- usb_teardown ();
251+ if ( _ota_dfu )
252+ {
253+ led_state (STATE_BLE_DISCONNECTED );
254+ softdev_init (!sd_inited );
255+ sd_inited = true;
256+ }
257+ else
258+ {
259+ led_state (STATE_USB_UNMOUNTED );
260+ usb_init (serial_only_dfu );
261+ }
262+
263+ // Initiate an update of the firmware.
264+ if (APP_ASKS_FOR_SINGLE_TAP_RESET () || uf2_dfu || serial_only_dfu )
265+ {
266+ // If USB is not enumerated in 3s (eg. because we're running on battery), we restart into app.
267+ APP_ERROR_CHECK ( bootloader_dfu_start (_ota_dfu , 3000 , true) );
268+ }
269+ else
270+ {
271+ // No timeout if bootloader requires user action (double-reset).
272+ APP_ERROR_CHECK ( bootloader_dfu_start (_ota_dfu , 0 , false) );
273+ }
274+
275+ if ( _ota_dfu )
276+ {
277+ sd_softdevice_disable ();
278+ }else
279+ {
280+ usb_teardown ();
281+ }
275282 }
276283 }
277284
278- // Reset Board
285+ // Reset peripherals
279286 board_teardown ();
280287
281288 /* Jump to application if valid
0 commit comments