Last active
November 30, 2023 11:32
-
-
Save Lauszus/3251ef567475e5b59e1f8b61f6cd5edf to your computer and use it in GitHub Desktop.
FreeRTOS get stack size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* task.h | |
* <PRE>UBaseType_t uxTaskGetStackSize( TaskHandle_t xTask );</PRE> | |
* | |
* Returns the stack size associated with xTask. That is, the stack | |
* size (in words, so on a 32 bit machine a value of 1 means 4 bytes) of the task. | |
* | |
* @param xTask Handle of the task associated with the stack to be checked. | |
* Set xTask to NULL to check the stack of the calling task. | |
* | |
* @return The stack size (in words) associated with xTask. | |
*/ | |
UBaseType_t uxTaskGetStackSize( TaskHandle_t xTask ) PRIVILEGED_FUNCTION; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UBaseType_t uxTaskGetStackSize( TaskHandle_t xTask ) | |
{ | |
TCB_t *pxTCB; | |
UBaseType_t uxReturn; | |
pxTCB = prvGetTCBFromHandle( xTask ); | |
#if( portSTACK_GROWTH < 0 ) | |
{ | |
uxReturn = pxTCB->pxEndOfStack - pxTCB->pxStack + 1UL; | |
} | |
#else /* portSTACK_GROWTH */ | |
{ | |
uxReturn = pxTCB->pxStack - pxTCB->pxEndOfStack + 1UL; | |
} | |
#endif /* portSTACK_GROWTH */ | |
return uxReturn; | |
} |
Both pxEndOfStack and pxStack are both StackType_t in the kernel,
pxEndOfStack and pxStack are both StackType_t*
not StackType_t
. Subtracting them will give you the distance apart of the two pointers in units of StackType_t which may be bigger than what can be contained in StackType_t. Compare to uxTaskGetStackHighWaterMark that returns UBaseType_t.
Thanks guys! I've now fixed it :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi :)
Both
pxEndOfStack
andpxStack
are bothStackType_t
in the kernel, so the difference should always fit inStackType_t
. See: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.cThat depends on the size of
StackType_t
. On a 32-bit system it will return the size in words. I kept is like this, as that is the way it's used in the FreeRTOS API. For instance the stack size is set in words on a 32-bit system.This is only correct if you return the result in bytes. In my case it is not for the reasons explained above.
I have to disagree for the reasons explained above :)