Fix nRF52 flash completion and preserve recoverable ExtraFS

This commit is contained in:
mikecarper committed 2026-09-22 09:29:05 -07:00
1 parent a9d7692bf3
commit 3ce499a700
20 files changed
+970 -33

No files matched your search

+10 -9
View File
@@ -2,6 +2,7 @@
#include "NRF52Board.h"
#include "PowerManagementUtils.h"
#include "Nrf52BootloaderVersion.h"
#include "nrf52/SoftDeviceState.h"
#include <target.h>
#ifdef USER_GPIO_CONTROL
#include "UserGpioPinPolicy.h"
@@ -103,7 +104,7 @@ bool NRF52Board::isUserGpioAvailable(uint8_t pin) const {
bool NRF52Board::rebootToUf2Bootloader() {
uint8_t sd_enabled = 0;
if (sd_softdevice_is_enabled(&sd_enabled) != NRF_SUCCESS) return false;
if (mesh_nrf52::softdeviceIsEnabled(sd_enabled) != NRF_SUCCESS) return false;
if (sd_enabled) {
if (sd_power_gpregret_clr(0, 0xFF) != NRF_SUCCESS
@@ -211,7 +212,7 @@ void NRF52Board::initPowerMgr() {
// Clear registers for next boot
// Note: At this point SoftDevice may or may not be enabled
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
mesh_nrf52::softdeviceIsEnabled(sd_enabled);
if (sd_enabled) {
sd_power_reset_reason_clr(0xFFFFFFFF);
sd_power_gpregret_clr(1, 0xFF);
@@ -308,7 +309,7 @@ void NRF52Board::enterSystemOff(uint8_t reason) {
// Record shutdown reason in GPREGRET2
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
mesh_nrf52::softdeviceIsEnabled(sd_enabled);
if (sd_enabled) {
sd_power_gpregret_clr(1, 0xFF);
sd_power_gpregret_set(1, reason);
@@ -399,7 +400,7 @@ void NRF52Board::configureVoltageWake(uint8_t ain_channel, uint8_t refsel) {
void NRF52Board::armVbusWake() {
// Configure VBUS (USB power) wake alongside (or instead of) LPCOMP.
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
mesh_nrf52::softdeviceIsEnabled(sd_enabled);
if (sd_enabled) {
sd_power_usbdetected_enable(1);
} else {
@@ -415,7 +416,7 @@ void NRF52BoardDCDC::begin() {
// Enable DC/DC converter for improved power efficiency
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
mesh_nrf52::softdeviceIsEnabled(sd_enabled);
if (sd_enabled) {
sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE);
} else {
@@ -426,7 +427,7 @@ void NRF52BoardDCDC::begin() {
bool NRF52Board::isExternalPowered() {
// Check if SoftDevice is enabled before using its API
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
mesh_nrf52::softdeviceIsEnabled(sd_enabled);
if (sd_enabled) {
uint32_t usb_status;
@@ -470,7 +471,7 @@ void NRF52Board::sleep(uint32_t secs) {
// On nRF52, we use event-driven sleep instead of timed sleep
// The 'secs' parameter is ignored - we wake on any interrupt
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
mesh_nrf52::softdeviceIsEnabled(sd_enabled);
if (sd_enabled) {
// A single call is required here. If an interrupt arrived since the last
@@ -492,7 +493,7 @@ void NRF52Board::sleep(uint32_t secs) {
// Temperature from NRF52 MCU
float NRF52Board::getMCUTemperature() {
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
mesh_nrf52::softdeviceIsEnabled(sd_enabled);
if (sd_enabled) {
uint32_t err_code;
int32_t temp;
@@ -587,7 +588,7 @@ void NRF52Board::powerOff() {
// Enter SYSTEMOFF
uint8_t sd_enabled = 0;
sd_softdevice_is_enabled(&sd_enabled);
mesh_nrf52::softdeviceIsEnabled(sd_enabled);
if (sd_enabled) { // SoftDevice is enabled
sd_power_system_off();
} else { // SoftDevice is not enable
@@ -0,0 +1,41 @@
#pragma once
#include <stdint.h>
namespace mesh {
namespace storage {
static const uint32_t INTERNAL_EXTRAFS_PAGE_COUNT = 25;
static const uint32_t INTERNAL_EXTRAFS_PAGE_MASK =
(1UL << INTERNAL_EXTRAFS_PAGE_COUNT) - 1UL;
inline uint8_t countInternalExtraFsBadPages(uint32_t mask) {
uint8_t count = 0;
while (mask != 0) {
count += static_cast<uint8_t>(mask & 1U);
mask >>= 1;
}
return count;
}
// The old all-good layout is the identity map. Once a confirmed bad page is
// retired, every logical 4 KiB page after it moves to the next good physical
// page. A fixed 128-byte LittleFS block never straddles those page boundaries.
inline uint32_t internalExtraFsPhysicalPage(uint32_t bad_mask,
uint32_t logical_page) {
if ((bad_mask & ~INTERNAL_EXTRAFS_PAGE_MASK) != 0
|| logical_page >= INTERNAL_EXTRAFS_PAGE_COUNT
- countInternalExtraFsBadPages(bad_mask)) {
return UINT32_MAX;
}
for (uint32_t physical = 0; physical < INTERNAL_EXTRAFS_PAGE_COUNT;
physical++) {
if ((bad_mask & (1UL << physical)) != 0) continue;
if (logical_page == 0) return physical;
logical_page--;
}
return UINT32_MAX;
}
} // namespace storage
} // namespace mesh
+2 -1
View File
@@ -1,6 +1,7 @@
#include "SerialBLEInterface.h"
#include "../BluetoothMac.h"
#include "../CompanionFrameQueue.h"
#include "SoftDeviceState.h"
#include <stdio.h>
#include <string.h>
#include "ble_gap.h"
@@ -439,7 +440,7 @@ bool SerialBLEInterface::begin(const char* prefix, const char* name,
if (!Bluefruit.begin()) {
uint8_t softdevice_enabled = 0;
const uint32_t softdevice_status =
sd_softdevice_is_enabled(&softdevice_enabled);
mesh_nrf52::softdeviceIsEnabled(softdevice_enabled);
ble_gap_addr_t address = {};
const uint32_t gap_status = softdevice_enabled
? sd_ble_gap_addr_get(&address) : NRF_ERROR_INVALID_STATE;
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <nrf_sdm.h>
namespace mesh_nrf52 {
// sd_softdevice_is_enabled is an SVC wrapper. With LTO, its assembly body
// does not tell GCC that the output pointer is written by the supervisor.
// Load the result from volatile storage after a compiler memory barrier.
inline uint32_t softdeviceIsEnabled(uint8_t& enabled) {
volatile uint8_t state = 0;
const uint32_t result = sd_softdevice_is_enabled((uint8_t*)&state);
__asm volatile ("" ::: "memory");
enabled = state;
return result;
}
} // namespace mesh_nrf52
+4 -3
View File
@@ -26,6 +26,7 @@
#include "nrf.h"
#include "nrf_soc.h"
#include "nrf_sdm.h"
#include "../nrf52/SoftDeviceState.h"
#if defined(OTA_SD_STORE)
#include "OtaStoreSdNrf52.h"
#endif
@@ -467,7 +468,7 @@ static OtaStoreFlashNrf52* g_nrf52_apply_store = nullptr;
static void ota_nrf52_set_reset_handoff(uint8_t request, uint8_t source) {
uint8_t sd_en = 0;
sd_softdevice_is_enabled(&sd_en);
mesh_nrf52::softdeviceIsEnabled(sd_en);
if (sd_en) { // POWER is SD-restricted while the SoftDevice runs
sd_power_gpregret_clr(1, 0xFFFFFFFF);
sd_power_gpregret_set(1, source);
@@ -481,7 +482,7 @@ static void ota_nrf52_set_reset_handoff(uint8_t request, uint8_t source) {
static bool ota_nrf52_clear_reset_reasons() {
uint8_t sd_en = 0;
if (sd_softdevice_is_enabled(&sd_en) != NRF_SUCCESS) return false;
if (mesh_nrf52::softdeviceIsEnabled(sd_en) != NRF_SUCCESS) return false;
if (sd_en) {
return sd_power_reset_reason_clr(0xFFFFFFFFu) == NRF_SUCCESS;
}
@@ -527,7 +528,7 @@ void ota_reboot_to_bootloader_update() {
source = GPREGRET2_OTA_STAGE_SD;
#endif
uint8_t sd_en = 0;
sd_softdevice_is_enabled(&sd_en);
mesh_nrf52::softdeviceIsEnabled(sd_en);
if (sd_en) {
sd_power_gpregret_clr(1, 0xFFFFFFFF);
sd_power_gpregret_set(1, source);