Fix function brace style
[betaflight.git] / src / main / drivers / stack_check.c
blob8a5f05a8a9ea6c375fc96c5ecbec1c70337a7491
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <string.h>
25 #include "platform.h"
27 #include "build/debug.h"
29 #include "common/utils.h"
31 #include "drivers/stack_check.h"
33 #define STACK_FILL_CHAR 0xa5
35 extern char _estack; // end of stack, declared in .LD file
36 extern char _Min_Stack_Size; // declared in .LD file
39 * The ARM processor uses a full descending stack. This means the stack pointer holds the address
40 * of the last stacked item in memory. When the processor pushes a new item onto the stack,
41 * it decrements the stack pointer and then writes the item to the new memory location.
44 * RAM layout is generally as below, although some targets vary
46 * F1 Boards
47 * RAM is origin 0x20000000 length 20K that is:
48 * 0x20000000 to 0x20005000
50 * F3 Boards
51 * RAM is origin 0x20000000 length 40K that is:
52 * 0x20000000 to 0x2000a000
54 * F4 Boards
55 * RAM is origin 0x20000000 length 128K that is:
56 * 0x20000000 to 0x20020000
58 * See the linker scripts for actual stack configuration.
61 #ifdef USE_STACK_CHECK
63 static uint32_t usedStackSize;
65 void taskStackCheck(timeUs_t currentTimeUs)
67 UNUSED(currentTimeUs);
69 char * const stackHighMem = &_estack;
70 const uint32_t stackSize = (uint32_t)&_Min_Stack_Size;
71 char * const stackLowMem = stackHighMem - stackSize;
72 const char * const stackCurrent = (char *)&stackLowMem;
74 char *p;
75 for (p = stackLowMem; p < stackCurrent; ++p) {
76 if (*p != STACK_FILL_CHAR) {
77 break;
81 usedStackSize = (uint32_t)stackHighMem - (uint32_t)p;
83 DEBUG_SET(DEBUG_STACK, 0, (uint32_t)stackHighMem & 0xffff);
84 DEBUG_SET(DEBUG_STACK, 1, (uint32_t)stackLowMem & 0xffff);
85 DEBUG_SET(DEBUG_STACK, 2, (uint32_t)stackCurrent & 0xffff);
86 DEBUG_SET(DEBUG_STACK, 3, (uint32_t)p & 0xffff);
89 uint32_t stackUsedSize(void)
91 return usedStackSize;
93 #endif
95 uint32_t stackTotalSize(void)
97 return (uint32_t)(intptr_t)&_Min_Stack_Size;
100 uint32_t stackHighMem(void)
102 return (uint32_t)(intptr_t)&_estack;