fix(diag): real heap-free via mallinfo, flush-align Errors row

sbrk(0)-high-water counted freed-and-recycled blocks as permanently
used, understating free heap. mallinfo().uordblks reflects bytes
actually outstanding. Also strip the trailing space from the decoded
error-flag string so right-alignment sits flush.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jakub
2026-06-19 23:00:40 +02:00
co-authored by Claude Sonnet 4.6
parent 11a3799a47
commit aba924bac9
2 changed files with 10 additions and 5 deletions
@@ -110,6 +110,8 @@ class DiagnosticsScreen : public UIScreen {
if (err & ERR_EVENT_FULL) strcat(buf, "F "); if (err & ERR_EVENT_FULL) strcat(buf, "F ");
if (err & ERR_EVENT_CAD_TIMEOUT) strcat(buf, "C "); if (err & ERR_EVENT_CAD_TIMEOUT) strcat(buf, "C ");
if (err & ERR_EVENT_STARTRX_TIMEOUT) strcat(buf, "R "); if (err & ERR_EVENT_STARTRX_TIMEOUT) strcat(buf, "R ");
int len = strlen(buf); // drop the trailing space so right-alignment sits flush
if (len > 0 && buf[len - 1] == ' ') buf[len - 1] = '\0';
} }
addRow("Errors", buf); addRow("Errors", buf);
} }
+8 -5
View File
@@ -3,17 +3,20 @@
#if defined(NRF52_PLATFORM) #if defined(NRF52_PLATFORM)
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include <malloc.h>
// Same linker symbols + _sbrk() the core's own new.cpp uses to implement // Heap region size from the linker symbols the core's new.cpp uses for sbrk
// malloc's heap (see framework-arduinoadafruitnrf52/cores/nRF5/new.cpp) // (framework-arduinoadafruitnrf52/cores/nRF5/new.cpp). "Used" comes from
// there's no separate "free heap" API on this core, so this mirrors it. // newlib's mallinfo().uordblks (bytes actually outstanding) rather than
extern "C" char* _sbrk(int incr); // sbrk(0) - __HeapBase: sbrk only ever grows, so a high-water mark there
// counts freed-and-recycled blocks as permanently "used" and free heap looks
// far lower than it really is.
extern unsigned char __HeapBase[]; extern unsigned char __HeapBase[];
extern unsigned char __HeapLimit[]; extern unsigned char __HeapLimit[];
void DeviceDiag::getHeapStats(uint32_t& free_bytes, uint32_t& total_bytes) { void DeviceDiag::getHeapStats(uint32_t& free_bytes, uint32_t& total_bytes) {
total_bytes = (uint32_t)(__HeapLimit - __HeapBase); total_bytes = (uint32_t)(__HeapLimit - __HeapBase);
uint32_t used = (uint32_t)((unsigned char*)_sbrk(0) - __HeapBase); uint32_t used = (uint32_t)mallinfo().uordblks;
free_bytes = (used < total_bytes) ? (total_bytes - used) : 0; free_bytes = (used < total_bytes) ? (total_bytes - used) : 0;
} }