Skip to content

Commit 194c004

Browse files
committed
feat(uf2): record the last bootloader fault and show it in INFO_UF2.TXT
1 parent 646c07b commit 194c004

5 files changed

Lines changed: 54 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ In OTA DFU mode:
168168

169169
The OTA fallback is intentional and prevents devices from getting stuck in UF2 mode after failed OTA updates.
170170

171+
If the bootloader reset itself on a fault, `INFO_UF2.TXT` on the UF2 drive shows a `Last-Fault:` line with the fault id, program counter, and info word.
172+
171173
---
172174

173175
### OTA update fails with `Error: Operation Failed`

‎changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ it and is upstream Adafruit history, kept for provenance.
55

66
## OTAFIX 2.6
77

8+
- `INFO_UF2.TXT` shows a `Last-Fault: id=0x.. pc=0x.. info=0x..` line when the bootloader's last reset came from a SoftDevice or SDK fault. The record lives in `.noinit` behind a magic word and is cleared once printed; for an SDK error `info` is the error code.
89
- A device with no valid application that is plugged into a USB host comes up as a UF2 drive showing NO FIRMWARE instead of waiting for a phone. With no host enumerating within 3 s (battery, or a charger) it falls through to the BLE OTA wait. A cabled board with no application returns to the drive after an aborted copy or an eject. On battery, or when the cable is pulled while the drive is up (VBUS gone for 250 ms), it goes to the BLE OTA wait. Plugging a cable into a board with no application that is waiting for BLE, with no phone connected, brings the drive up (VBUS present for 250 ms); a cable already present when the wait began, or a phone connected, leaves the wait alone.
910
- A UF2 application copy marks `bank_0` invalid when its first application block arrives, so a cable pulled mid-copy leaves the device with no application instead of an old `bank_0` marked valid over a half-written image. The completion path records the real application size as before.
1011
- DFU screens on I2C mono OLEDs (SSD1306 and SH1106), with a four-line layout: title, mode, version, and banner. A board opts in by defining `DISPLAY_PIN_SDA` in its `board.h`; `boards.h` rejects a board that also defines `DISPLAY_PIN_SCK`, since TWIM0 and SPIM0 share a peripheral. The bootloader probes 0x3C and 0x3D and reads the controller type off the panel, and a board with no panel shows nothing and otherwise behaves as before. Enabled on `wiscore_rak4631_board`, `wiscore_rak3401`, `wio_tracker_l1`, `promicro_nrf52840`, `xiao_nrf52840_ble`, and `xiao_nrf52840_ble_sense`, for about 2 KB of bootloader flash each. Verified on the RAK4631 and the Wio Tracker L1; the other four follow their firmware variants' pins. On the RAK4631 and RAK3401, DFU raises the switched 3V3_S rail (WB_IO2) that feeds the WisBlock slots, and lowers it again when no panel answers.

‎src/fault_record.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef FAULT_RECORD_H
2+
#define FAULT_RECORD_H
3+
4+
#include <stdint.h>
5+
6+
// The magic word tells a real record from power-on RAM garbage.
7+
#define FAULT_RECORD_MAGIC 0x4641554Cu
8+
9+
typedef struct {
10+
uint32_t magic, id, pc, info;
11+
} fault_record_t;
12+
13+
// Second half of the linker's 0x80-byte NOINIT region at 0x20007F80; the first
14+
// half holds the BLE peer data. Placed by address because a second .noinit input
15+
// section from another object file is not merged into the NOINIT output section.
16+
#define FAULT_RECORD_ADDR 0x20007FC0u
17+
#define g_fault_record (*(fault_record_t*) FAULT_RECORD_ADDR)
18+
19+
#endif

‎src/main.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
#ifdef NRF_USBD
7373

7474
#include "uf2/uf2.h"
75+
#include "fault_record.h"
76+
#include "dfu_ble_svc.h"
7577
#include "nrf_usbd.h"
7678
#include "tusb.h"
7779
#include "app_timer.h"
@@ -496,7 +498,17 @@ static uint32_t ble_stack_init(void) {
496498
//--------------------------------------------------------------------+
497499
// Error Handler
498500
//--------------------------------------------------------------------+
501+
// The peer data and its CRC (dfu_ble_svc.c) occupy the first 0x40 bytes of NOINIT.
502+
STATIC_ASSERT(sizeof(dfu_ble_peer_data_t) + sizeof(uint16_t) <= 0x40);
503+
STATIC_ASSERT(FAULT_RECORD_ADDR + sizeof(fault_record_t) <= 0x20008000u);
504+
499505
void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info) {
506+
// For SDK errors info points at a stack error_info_t that will not survive the reset.
507+
if (id == NRF_FAULT_ID_SDK_ERROR && info != 0) info = ((error_info_t const*) info)->err_code;
508+
g_fault_record.id = id;
509+
g_fault_record.pc = pc;
510+
g_fault_record.info = info;
511+
g_fault_record.magic = FAULT_RECORD_MAGIC;
500512
volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */
501513
if ((*ARM_CM_DHCSR) & 1UL) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */
502514
NVIC_SystemReset();

‎src/usb/uf2/ghostfat.c‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "bootloader_settings.h"
3737
#include "bootloader.h"
3838
#include "boards.h"
39+
#include "fault_record.h"
3940

4041
//--------------------------------------------------------------------+
4142
//
@@ -133,9 +134,9 @@ STATIC_ASSERT(FAT_ENTRIES_PER_SECTOR == 256); // FAT
133134
"Factory-Erase: UF2 family 0x4D455348\r\n"
134135

135136
char infoUf2File[128*3] = INFO_UF2_TEXT;
136-
// uf2_init() appends "SoftDevice: S140 7.3.0\r\n" at runtime; 40 covers the
137-
// widest values the id/version fields can print
138-
STATIC_ASSERT(sizeof(INFO_UF2_TEXT) + 40 <= sizeof(infoUf2File));
137+
// uf2_init() appends "SoftDevice: S140 7.3.0\r\n" (40 covers the widest id/version
138+
// fields) and "Last-Fault: id=0x<8> pc=0x<8> info=0x<8>\r\n" (57 bytes)
139+
STATIC_ASSERT(sizeof(INFO_UF2_TEXT) + 40 + 57 <= sizeof(infoUf2File));
139140

140141
const char indexFile[] =
141142
"<!doctype html>\n"
@@ -343,6 +344,22 @@ void uf2_init(void)
343344
{
344345
strcat(infoUf2File, "not found\r\n");
345346
}
347+
348+
if (g_fault_record.magic == FAULT_RECORD_MAGIC)
349+
{
350+
char str[10];
351+
strcat(infoUf2File, "Last-Fault: id=0x");
352+
utoa(g_fault_record.id, str, 16);
353+
strcat(infoUf2File, str);
354+
strcat(infoUf2File, " pc=0x");
355+
utoa(g_fault_record.pc, str, 16);
356+
strcat(infoUf2File, str);
357+
strcat(infoUf2File, " info=0x");
358+
utoa(g_fault_record.info, str, 16);
359+
strcat(infoUf2File, str);
360+
strcat(infoUf2File, "\r\n");
361+
g_fault_record.magic = 0;
362+
}
346363
}
347364

348365
/*------------------------------------------------------------------*/

0 commit comments

Comments
 (0)