here an simple method to check and print the thead stack usage at runtime.
Note: the option CH_DBG_FILL_THREADS in chconf.h must be TRUE
Code: Select all
#define stk_siprintf(b,s,n,z) \
siprintf(b, "%s free stack memory : %u%% of %u bytes", \
s, (n)*100/((z) - sizeof(Thread)), ((z) - sizeof(Thread)));
size_t get_thd_free_stack(void *wsp, size_t size)
{
size_t n = 0;
#if CH_DBG_FILL_THREADS
uint8_t *startp = (uint8_t *)wsp + sizeof(Thread);
uint8_t *endp = (uint8_t *)wsp + size;
while (startp < endp)
if(*startp++ == STACK_FILL_VALUE) ++n;
#endif
return n;
}
static WORKING_AREA(waStcThread, 2000);
static msg_t StcThread(void *arg);
#define DYN_THD_WA_SIZE THD_WA_SIZE(2048)
static Thread *dynThreadp = NULL;
void print_mem(BaseChannel *chp, int argc, char *argv[]) {
size_t n;
char buf[80];
// check stack of an dynamic Thread
if(dynThreadp != NULL){
n = get_thd_free_stack(dynThreadp , DYN_THD_WA_SIZE);
stk_siprintf(buf, "dynThread ", n, DYN_THD_WA_SIZE);
shellPrintLine(buf);
}
// check stack of an static Thread
n = get_thd_free_stack(waStcThread, sizeof(waStcThread));
stk_siprintf(buf, "StcThread ", n, sizeof(waStcThread));
shellPrintLine(buf);
}
Alberto