|
39 | 39 | #include <stddef.h> |
40 | 40 |
|
41 | 41 | #include "nrfx.h" |
42 | | -#include "nrf_clock.h" |
43 | 42 | #include "nrfx_power.h" |
44 | | -#include "nrfx_pwm.h" |
45 | | - |
46 | | -#include "nordic_common.h" |
47 | 43 | #include "sdk_common.h" |
48 | | -#include "dfu_transport.h" |
49 | 44 | #include "bootloader.h" |
50 | 45 | #include "bootloader_util.h" |
51 | 46 |
|
52 | 47 | #include "nrf.h" |
53 | 48 | #include "nrf_soc.h" |
54 | 49 | #include "nrf_nvic.h" |
55 | 50 | #include "app_error.h" |
56 | | -#include "nrf_gpio.h" |
57 | 51 | #include "ble.h" |
58 | | -#include "nrf.h" |
59 | | -#include "ble_hci.h" |
60 | 52 | #include "app_scheduler.h" |
61 | 53 | #include "nrf_error.h" |
62 | 54 |
|
63 | 55 | #include "boards.h" |
64 | 56 |
|
65 | 57 | #include "pstorage_platform.h" |
66 | 58 | #include "nrf_mbr.h" |
67 | | -#include "pstorage.h" |
68 | | -#include "nrfx_nvmc.h" |
69 | 59 |
|
70 | 60 | #ifdef NRF_USBD |
71 | 61 |
|
72 | 62 | #include "uf2/uf2.h" |
73 | | -#include "nrf_usbd.h" |
74 | 63 | #include "tusb.h" |
75 | 64 |
|
76 | 65 | void usb_init(bool cdc_only); |
@@ -250,31 +239,34 @@ static void check_dfu_mode(void) { |
250 | 239 | (((*dbl_reset_mem) == DFU_DBL_RESET_MAGIC) && reason_reset_pin); |
251 | 240 |
|
252 | 241 | // Clear GPREGRET if it is our values |
253 | | - if (dfu_start || dfu_skip) NRF_POWER->GPREGRET = 0; |
| 242 | + if (dfu_start || dfu_skip) { |
| 243 | + NRF_POWER->GPREGRET = 0; |
| 244 | + } |
254 | 245 |
|
255 | 246 | // skip dfu entirely |
256 | | - if (dfu_skip) return; |
| 247 | + if (dfu_skip) { |
| 248 | + return; |
| 249 | + } |
257 | 250 |
|
258 | 251 | /*------------- Determine DFU mode (Serial, OTA, FRESET or normal) -------------*/ |
259 | | - // DFU button pressed |
260 | | - dfu_start = dfu_start || button_pressed(BUTTON_DFU); |
| 252 | + dfu_start = dfu_start || button_pressed(BUTTON_DFU); // DFU button pressed |
261 | 253 |
|
262 | 254 | // DFU + FRESET are pressed --> OTA |
263 | 255 | _ota_dfu = _ota_dfu || (button_pressed(BUTTON_DFU) && button_pressed(BUTTON_FRESET)); |
264 | 256 |
|
265 | 257 | bool const valid_app = bootloader_app_is_valid(); |
266 | 258 | bool const just_start_app = valid_app && !dfu_start && (*dbl_reset_mem) == DFU_DBL_RESET_APP; |
267 | 259 |
|
268 | | - if (!just_start_app && APP_ASKS_FOR_SINGLE_TAP_RESET()) dfu_start = 1; |
| 260 | + if (!just_start_app && APP_ASKS_FOR_SINGLE_TAP_RESET()) { |
| 261 | + dfu_start = 1; |
| 262 | + } |
269 | 263 |
|
270 | 264 | // App mode: Double Reset detection or DFU startup for nrf52832 |
271 | 265 | if (!(just_start_app || dfu_start || !valid_app)) { |
272 | 266 | #ifdef NRF52832_XXAA |
273 | 267 | /* Even DFU is not active, we still force an 1000 ms dfu serial mode when startup |
274 | 268 | * to support auto programming from Arduino IDE |
275 | | - * |
276 | | - * Note: Double Reset WONT work with nrf52832 since all its SRAM got cleared with GPIO reset. |
277 | | - */ |
| 269 | + * Note: Double Reset WONT work with nrf52832 since all its SRAM got cleared with GPIO reset. */ |
278 | 270 | bootloader_dfu_start(false, DFU_SERIAL_STARTUP_INTERVAL, false); |
279 | 271 | #else |
280 | 272 | // Note: RESETREAS is not clear by bootloader, it should be cleared by application upon init() |
@@ -323,6 +315,10 @@ static void check_dfu_mode(void) { |
323 | 315 | } |
324 | 316 | } |
325 | 317 |
|
| 318 | +//--------------------------------------------------------------------+ |
| 319 | +// BLE |
| 320 | +//--------------------------------------------------------------------+ |
| 321 | + |
326 | 322 | // Initializes the SoftDevice by following SD specs section |
327 | 323 | // "Master Boot Record and SoftDevice initializaton procedure" |
328 | 324 | static uint32_t ble_stack_init(void) { |
@@ -389,23 +385,6 @@ static uint32_t ble_stack_init(void) { |
389 | 385 | return NRF_SUCCESS; |
390 | 386 | } |
391 | 387 |
|
392 | | -//--------------------------------------------------------------------+ |
393 | | -// Error Handler |
394 | | -//--------------------------------------------------------------------+ |
395 | | -void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info) { |
396 | | - volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ |
397 | | - if ((*ARM_CM_DHCSR) & 1UL) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */ |
398 | | - NVIC_SystemReset(); |
399 | | -} |
400 | | - |
401 | | -void assert_nrf_callback(uint16_t line_num, uint8_t const* p_file_name) { |
402 | | - app_error_fault_handler(0xDEADBEEF, 0, 0); |
403 | | -} |
404 | | - |
405 | | -/*------------------------------------------------------------------*/ |
406 | | -/* SoftDevice Event handler |
407 | | - *------------------------------------------------------------------*/ |
408 | | - |
409 | 388 | // Process BLE event from SD |
410 | 389 | uint32_t proc_ble(void) { |
411 | 390 | __ALIGN(4) uint8_t ev_buf[BLE_EVT_LEN_MAX(BLEGATT_ATT_MTU_MAX)]; |
@@ -479,6 +458,22 @@ void SD_EVT_IRQHandler(void) { |
479 | 458 | app_sched_event_put(NULL, 0, proc_sd_task); |
480 | 459 | } |
481 | 460 |
|
| 461 | +//--------------------------------------------------------------------+ |
| 462 | +// Error Handler |
| 463 | +//--------------------------------------------------------------------+ |
| 464 | +void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info) { |
| 465 | + volatile uint32_t *ARM_CM_DHCSR = ((volatile uint32_t *)0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ |
| 466 | + if ((*ARM_CM_DHCSR) & 1UL) { |
| 467 | + __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */ |
| 468 | + } |
| 469 | + NVIC_SystemReset(); |
| 470 | +} |
| 471 | + |
| 472 | +void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) { |
| 473 | + app_error_fault_handler(0xDEADBEEF, 0, 0); |
| 474 | +} |
| 475 | + |
| 476 | + |
482 | 477 | //--------------------------------------------------------------------+ |
483 | 478 | // RTT printf retarget for Debug |
484 | 479 | //--------------------------------------------------------------------+ |
|
0 commit comments