StackMacros.h
Go to the documentation of this file.
1 /*
2  FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.
3  All rights reserved
4 
5  VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6 
7  This file is part of the FreeRTOS distribution.
8 
9  FreeRTOS is free software; you can redistribute it and/or modify it under
10  the terms of the GNU General Public License (version 2) as published by the
11  Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12 
13  ***************************************************************************
14  >>! NOTE: The modification to the GPL is included to allow you to !<<
15  >>! distribute a combined work that includes FreeRTOS without being !<<
16  >>! obliged to provide the source code for proprietary components !<<
17  >>! outside of the FreeRTOS kernel. !<<
18  ***************************************************************************
19 
20  FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22  FOR A PARTICULAR PURPOSE. Full license text is available on the following
23  link: http://www.freertos.org/a00114.html
24 
25  ***************************************************************************
26  * *
27  * FreeRTOS provides completely free yet professionally developed, *
28  * robust, strictly quality controlled, supported, and cross *
29  * platform software that is more than just the market leader, it *
30  * is the industry's de facto standard. *
31  * *
32  * Help yourself get started quickly while simultaneously helping *
33  * to support the FreeRTOS project by purchasing a FreeRTOS *
34  * tutorial book, reference manual, or both: *
35  * http://www.FreeRTOS.org/Documentation *
36  * *
37  ***************************************************************************
38 
39  http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40  the FAQ page "My application does not run, what could be wrong?". Have you
41  defined configASSERT()?
42 
43  http://www.FreeRTOS.org/support - In return for receiving this top quality
44  embedded software for free we request you assist our global community by
45  participating in the support forum.
46 
47  http://www.FreeRTOS.org/training - Investing in training allows your team to
48  be as productive as possible as early as possible. Now you can receive
49  FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50  Ltd, and the world's leading authority on the world's leading RTOS.
51 
52  http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53  including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54  compatible FAT file system, and our tiny thread aware UDP/IP stack.
55 
56  http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57  Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58 
59  http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60  Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61  licenses offer ticketed support, indemnification and commercial middleware.
62 
63  http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64  engineered and independently SIL3 certified version for use in safety and
65  mission critical applications that require provable dependability.
66 
67  1 tab == 4 spaces!
68 */
69 
70 #ifndef STACK_MACROS_H
71 #define STACK_MACROS_H
72 
73 /*
74  * Call the stack overflow hook function if the stack of the task being swapped
75  * out is currently overflowed, or looks like it might have overflowed in the
76  * past.
77  *
78  * Setting configCHECK_FOR_STACK_OVERFLOW to 1 will cause the macro to check
79  * the current stack state only - comparing the current top of stack value to
80  * the stack limit. Setting configCHECK_FOR_STACK_OVERFLOW to greater than 1
81  * will also cause the last few stack bytes to be checked to ensure the value
82  * to which the bytes were set when the task was created have not been
83  * overwritten. Note this second test does not guarantee that an overflowed
84  * stack will always be recognised.
85  */
86 
87 /*-----------------------------------------------------------*/
88 
89 #if( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) )
90 
91  /* Only the current stack state is to be checked. */
92  #define taskCHECK_FOR_STACK_OVERFLOW() \
93  { \
94  /* Is the currently saved stack pointer within the stack limit? */ \
95  if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack ) \
96  { \
97  vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
98  } \
99  }
100 
101 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
102 /*-----------------------------------------------------------*/
103 
104 #if( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) )
105 
106  /* Only the current stack state is to be checked. */
107  #define taskCHECK_FOR_STACK_OVERFLOW() \
108  { \
109  \
110  /* Is the currently saved stack pointer within the stack limit? */ \
111  if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack ) \
112  { \
113  vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
114  } \
115  }
116 
117 #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */
118 /*-----------------------------------------------------------*/
119 
120 #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) )
121 
122  #define taskCHECK_FOR_STACK_OVERFLOW() \
123  { \
124  const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \
125  const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5; \
126  \
127  if( ( pulStack[ 0 ] != ulCheckValue ) || \
128  ( pulStack[ 1 ] != ulCheckValue ) || \
129  ( pulStack[ 2 ] != ulCheckValue ) || \
130  ( pulStack[ 3 ] != ulCheckValue ) ) \
131  { \
132  vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
133  } \
134  }
135 
136 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
137 /*-----------------------------------------------------------*/
138 
139 #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) )
140 
141  #define taskCHECK_FOR_STACK_OVERFLOW() \
142  { \
143  int8_t *pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack; \
144  static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
145  tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
146  tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
147  tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \
148  tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \
149  \
150  \
151  pcEndOfStack -= sizeof( ucExpectedStackBytes ); \
152  \
153  /* Has the extremity of the task stack ever been written over? */ \
154  if( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) \
155  { \
156  vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \
157  } \
158  }
159 
160 #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */
161 /*-----------------------------------------------------------*/
162 
163 /* Remove stack overflow macro if not being used. */
164 #ifndef taskCHECK_FOR_STACK_OVERFLOW
165  #define taskCHECK_FOR_STACK_OVERFLOW()
166 #endif
167 
168 
169 
170 #endif /* STACK_MACROS_H */
171