From cb19a4c7098f6b972cdd33b4796ea550a816fcf1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joakim=20Nohlg=C3=A5rd?= <joakim.nohlgard@eistec.se>
Date: Mon, 20 Jun 2016 14:33:17 +0200
Subject: [PATCH] cortexm_common: Fix isr_stack_usage

Refactor and add multiply by word size to get the usage in number of
bytes instead of in number of words.

Verified implementation by manual memory inspection in GDB.
---
 cpu/cortexm_common/thread_arch.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/cpu/cortexm_common/thread_arch.c b/cpu/cortexm_common/thread_arch.c
index 97def4ea9d..34a1733408 100644
--- a/cpu/cortexm_common/thread_arch.c
+++ b/cpu/cortexm_common/thread_arch.c
@@ -257,10 +257,14 @@ void thread_arch_stack_print(void)
 /* This function returns the number of bytes used on the ISR stack */
 int thread_arch_isr_stack_usage(void)
 {
-    uint32_t  *ptr = &_sstack;
+    uint32_t *ptr = &_sstack;
 
-    while (*(ptr++) == STACK_CANARY_WORD) {}
-    return (ISR_STACKSIZE - (ptr - &_sstack));
+    while(((*ptr) == STACK_CANARY_WORD) && (ptr < &_estack)) {
+        ++ptr;
+    }
+
+    ptrdiff_t num_used_words = &_estack - ptr;
+    return num_used_words * sizeof(*ptr);
 }
 
 __attribute__((naked)) void NORETURN thread_arch_start_threading(void)
-- 
GitLab