Skip to content

Commit b4719c4

Browse files
committed
add dbgStackUsed() and dbgStackTotal()
update stack usage
1 parent 555cca8 commit b4719c4

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

cores/nRF5/debug.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ int dbgHeapUsed(void)
9191
return (mallinfo()).uordblks;
9292
}
9393

94+
int dbgStackTotal(void)
95+
{
96+
return ((uint32_t) __StackTop) - ((uint32_t) __StackLimit);
97+
}
98+
99+
int dbgStackUsed(void)
100+
{
101+
enum { STACK_PATTERN = 0xADADADAD };
102+
103+
uint32_t * p_start = (uint32_t*) &__StackLimit;
104+
uint32_t * p_end = (uint32_t*) &__StackTop;
105+
106+
uint32_t * p_buf = p_start;
107+
while( *p_buf == STACK_PATTERN && p_buf != p_end)
108+
{
109+
p_buf++;
110+
}
111+
112+
if (p_buf == p_end) return (-1);
113+
114+
return ((uint32_t) p_end) - ((uint32_t) p_buf);
115+
}
116+
94117
static void printMemRegion(const char* name, uint32_t top, uint32_t bottom, uint32_t used)
95118
{
96119
char buffer[30];
@@ -111,10 +134,10 @@ void dbgMemInfo(void)
111134
Serial.println("| |");
112135

113136
// Pritn SRAM used for Stack executed by S132 and ISR
114-
printMemRegion("Stack", ((uint32_t) __StackTop), ((uint32_t) __StackLimit), 0);
137+
printMemRegion("Stack", ((uint32_t) __StackTop), ((uint32_t) __StackLimit), dbgStackUsed() );
115138

116139
// Print Heap usage overall (including memory malloced to tasks)
117-
printMemRegion("Heap", ((uint32_t) __HeapLimit), ((uint32_t) __HeapBase), dbgHeapUsed());
140+
printMemRegion("Heap", ((uint32_t) __HeapLimit), ((uint32_t) __HeapBase), dbgHeapUsed() );
118141

119142
// DATA + BSS
120143
printMemRegion("Data & Bss", ((uint32_t) __bss_end__), ((uint32_t) __data_start__), 0);

cores/nRF5/debug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const char* dbg_ble_event_str(uint16_t evt_id);
4646
int dbgHeapTotal(void);
4747
int dbgHeapUsed(void);
4848

49+
int dbgStackTotal(void);
50+
int dbgStackUsed(void);
51+
4952
static inline int dbgHeapFree(void)
5053
{
5154
return dbgHeapTotal() - dbgHeapUsed();

0 commit comments

Comments
 (0)