Add stack checking functions

This commit is contained in:
Mario Hüttel 2020-02-21 23:01:04 +01:00
parent b5d4bf5528
commit 68fc473372
3 changed files with 71 additions and 2 deletions

View File

@ -18,8 +18,24 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __STACK_CHECK_H__
#define __STACK_CHECK_H__
#include <stdint.h>
#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__ */

View File

@ -29,6 +29,7 @@
#include <stm-periph/unique-id.h>
#include <reflow-controller/calibration.h>
#include <reflow-controller/temp-converter.h>
#include <reflow-controller/stack-check.h>
#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,
}
};

View File

@ -19,3 +19,29 @@
*/
#include <reflow-controller/stack-check.h>
#include <stdint.h>
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;
}