[SITL] Enable telemetry: LTM and MAVLink (#8940)
[inav.git] / src / main / drivers / stack_check.c
blobf86d82d38eddb643a61a1adb5ebc3bfdc1a29d89
1 /*
2 * stack_check.c
4 * Created on: 23 Aug 2016
5 * Author: martinbudden
6 */
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <string.h>
12 #include "platform.h"
14 #include "build/build_config.h"
15 #include "build/debug.h"
17 #define STACK_FILL_CHAR 0xa5
19 extern char _estack; // end of stack, declared in .LD file
20 extern char _Min_Stack_Size; // declared in .LD file
23 * The ARM processor uses a full descending stack. This means the stack pointer holds the address
24 * of the last stacked item in memory. When the processor pushes a new item onto the stack,
25 * it decrements the stack pointer and then writes the item to the new memory location.
28 * RAM layout is generally as below, although some targets vary
30 * F1 Boards
31 * RAM is origin 0x20000000 length 20K that is:
32 * 0x20000000 to 0x20005000
34 * F3 Boards
35 * RAM is origin 0x20000000 length 40K that is:
36 * 0x20000000 to 0x2000a000
38 * F4 Boards
39 * RAM is origin 0x20000000 length 128K that is:
40 * 0x20000000 to 0x20020000
44 #ifdef STACK_CHECK
46 static uint32_t usedStackSize;
48 void taskStackCheck(timeUs_t currentTimeUs)
50 (void)currentTimeUs;
52 char * const stackHighMem = &_estack;
53 const uint32_t stackSize = (uint32_t)&_Min_Stack_Size;
54 char * const stackLowMem = stackHighMem - stackSize;
55 const char * const stackCurrent = (char *)&stackLowMem;
57 char *p;
58 for (p = stackLowMem; p < stackCurrent; ++p) {
59 if (*p != STACK_FILL_CHAR) {
60 break;
64 usedStackSize = (uint32_t)stackHighMem - (uint32_t)p;
66 #ifdef DEBUG_STACK
67 debug[0] = (uint32_t)stackHighMem & 0xffff;
68 debug[1] = (uint32_t)stackLowMem & 0xffff;
69 debug[2] = (uint32_t)stackCurrent & 0xffff;
70 debug[3] = (uint32_t)p & 0xffff;
71 #endif
74 uint32_t stackUsedSize(void)
76 return usedStackSize;
78 #endif
80 #if !defined(SITL_BUILD)
82 uint32_t stackTotalSize(void)
84 return (uint32_t)&_Min_Stack_Size;
87 uint32_t stackHighMem(void)
89 return (uint32_t)&_estack;
92 #endif