diff --git a/stm-firmware/include/reflow-controller/stack-check.h b/stm-firmware/include/reflow-controller/stack-check.h index eb9f6c3..f73dd29 100644 --- a/stm-firmware/include/reflow-controller/stack-check.h +++ b/stm-firmware/include/reflow-controller/stack-check.h @@ -18,8 +18,24 @@ * If not, see . */ - #ifndef __STACK_CHECK_H__ #define __STACK_CHECK_H__ +#include + +#define STACK_CHECK_MIN_HEAP_GAP 16UL + +int32_t stack_check_get_usage(); + +int32_t stack_check_get_free(); + +static inline uint32_t read_stack_pointer() +{ + uint32_t stack_pointer; + + __asm__ __volatile__ ("mov %0, sp\n\t" : "=r"(stack_pointer) : : ); + + return stack_pointer; +} + #endif /* __STACK_CHECK_H__ */ diff --git a/stm-firmware/shell.c b/stm-firmware/shell.c index 1fa3e3f..5502c66 100644 --- a/stm-firmware/shell.c +++ b/stm-firmware/shell.c @@ -29,6 +29,7 @@ #include #include #include +#include #ifndef GIT_VER #define GIT_VER "VERSION NOT SET" @@ -201,6 +202,24 @@ static shellmatta_retCode_t shell_cmd_cal(const shellmatta_handle_t handle, calibration_sequence_shell_cmd(handle); return SHELLMATTA_OK; } + +static shellmatta_retCode_t shell_get_sp(const shellmatta_handle_t handle, + const char *arguments, + uint32_t length) +{ + (void)arguments; + (void)length; + + shellmatta_printf(handle, + "Stack pointer: %p\r\n" + "Stack usage: 0x%x bytes\r\n" + "Lim to heap: 0x%x bytes\r\n", + read_stack_pointer(), + stack_check_get_usage(), + stack_check_get_free()); + return SHELLMATTA_OK; +} + //typedef struct shellmatta_cmd //{ // char *cmd; /**< command name */ @@ -211,7 +230,7 @@ static shellmatta_retCode_t shell_cmd_cal(const shellmatta_handle_t handle, // struct shellmatta_cmd *next; /**< pointer to next command or NULL */ //} shellmatta_cmd_t; -static shellmatta_cmd_t cmd[7] = { +static shellmatta_cmd_t cmd[8] = { { .cmd = "version", .cmdAlias = "ver", @@ -266,6 +285,14 @@ static shellmatta_cmd_t cmd[7] = { .helpText = "Calibrate resistance measurement", .usageText = "", .cmdFct = shell_cmd_cal, + .next = &cmd[7], + }, + { + .cmd = "get-stack-pointer", + .cmdAlias = "sp", + .helpText = "Get the stack pointer", + .usageText = "", + .cmdFct = shell_get_sp, .next = NULL, } }; diff --git a/stm-firmware/stack-check.c b/stm-firmware/stack-check.c index f449e72..4a40dff 100644 --- a/stm-firmware/stack-check.c +++ b/stm-firmware/stack-check.c @@ -19,3 +19,29 @@ */ #include +#include + +extern char _estack; +extern char heap_top; + +int32_t stack_check_get_usage() +{ + uint32_t stack_top; + uint32_t stack_ptr; + + stack_ptr = read_stack_pointer(); + stack_top = (uint32_t)&_estack; + + return stack_top - stack_ptr; +} + +int32_t stack_check_get_free() +{ + uint32_t upper_heap_boundary; + uint32_t stack_ptr; + + stack_ptr = read_stack_pointer(); + upper_heap_boundary = (uint32_t)&heap_top; + + return stack_ptr - upper_heap_boundary; +}