trace hook macros (PIC24)

paraparvy wrote on Wednesday, July 06, 2016:

Hi All,

I am teaching myself FreeRTOS…so far so good with most of the functionality with your kind help and the excellent online documentaion.

At the moment I am having some diffiulty in undesrstanding how to use the trace macros.

To start with I want to see a task being switched in and out.

For this exercise I have created a simple task that just prints a character to IO (using simulator mode in MPLAB x)

I couldnt find much documentation (or dont know where to look) on how to use the trace macros…namely traceTASK_SWITCHED_IN() and traceTASK_SWITCHED_OUT() to start with

This is the task that i want to see being moved in and out of the running state:


static void vPrintTask( void pvParameters )
{
while(1)
{
printf(".");
vTaskDelay(100/portTICKRATEMS);
}
}

How do i associate vPrintTask with the said trace macros?

Could someone help in starting me off please?

Cheers

rtel wrote on Wednesday, July 06, 2016:

Store the handle of vPrintTask() in a variable, say xPrintTask. Then
define your traceTASK_SWITCHED_IN() and traceTASK_SWITCHED_OUT() macros
at the END of FreeRTOSConfig.h. The task being switched in or out is
stored in the pxCurrentTCB variable. So…

#define traceTASK_SWITCHED_OUT()     \
     if( xPrintTask == pxCurrentTCB ) \
     {                                \
         /* xPrintTask is being switched out. */ \
     }


#define traceTASK_SWITCHED_IN()      \
     if( xPrintTask == pxCurrentTCB ) \
     {                                \
         /* xPrintTask is being switched in. */ \
     }

See the table on http://www.freertos.org/rtos-trace-macros.html

1 Like

paraparvy wrote on Wednesday, July 06, 2016:

Thanks
OK in my main file i have the task handle as:

TaskHandle_t xHandle;

If i put the macros at the end of FreeRTOSConfig.h what is the best way to resolve the location of xHandle which is declared in another file?

Have tried putting extern TaskHandle_t xHandle in FreeRTOSConfig.h but have compile errors…

…or should I create another header file for referencing xHandle and iclude this in FreeRTOSConfig.h?

Also, just for this tutorial can i use a printf statement in the macros ie at

/* xPrintTask is being switched in. */
printf(“IN\n”); etc

So in summary using extern in i get these errors:

if extern statement is declared in FreeRTOSConfig.h the error is:

…/FreeRTOSConfig.h:89:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘xHandle’
In file included from …/…/…/Source/include/FreeRTOS.h:98:0,

if extern statement is excluded from FreeRTOSConfig.h the error is:

…/…/…/Source/tasks.c:2772:3: error: ‘xPrintTask’ undeclared (first use in this function)
…/…/…/Source/tasks.c:2772:3: note: each undeclared identifier is reported only once for each function it appears in
make[2]: *** [build/default/production/_ext/449926602/tasks.o] Error 255

(ie cannot find xPrintTask handle)

and clicking on the errot takes you to traceTASK_SWITCHED_OUT in task.c

else
{
	xYieldPending = pdFALSE;
	traceTASK_SWITCHED_OUT();

Thanks

rtel wrote on Wednesday, July 06, 2016:

Extern the variable in the macro itself as the TaskHandle_t type will be
in scope there:

traceTASK_SWITCHED_IN/OUT()
{
extern TaskHandle_t xHandle:

     if( xHandle == pxCurrentTCB ) /* etc.*/
}

paraparvy wrote on Wednesday, July 06, 2016:

Sorry, Im being a bit dim here, but is this the correct definition?

#define traceTASK_SWITCHED_IN()      \
extern TaskHandle_t xHandle      \
     if( xPrintTask == pxCurrentTCB ) \
     {                                \
         /* xPrintTask is being switched in. */ \
     }

as this still doesnt compile

paraparvy wrote on Wednesday, July 06, 2016:

sorry, please ignore the last message, its compiling now after a clean build

paraparvy wrote on Wednesday, July 06, 2016:

…and IN/OUT macros are working…

Thanks a lot for all your help…much appreciated

paraparvy wrote on Thursday, July 07, 2016:

Hi All,

I have made some progress on using trace macros.
I’ve put the macro definitions in seperate file and it all seems fine.

I am still playing around with the IN/OUT macros…I notice that if i have both macros “enabled” then RTOS stops running…and ive traced out where it ends up…in vApplicationStackOverflowHook() where it executes taskDISABLE_INTERRUPTS();

Just to elaborate, with the IN macro running, all is well

#define traceTASK_SWITCHED_IN()                                                \
extern TaskHandle_t xHandle;                                                   \
    if( pxCurrentTCB != xHandle )                                      	       \
    {                                                                          \
        sprintf( cBuffer, "%s switched out, %s switched in, tick count = %u\n",\
                                ( ( tskTCB* ) xHandle )->pcTaskName,   	       \
                                ( ( tskTCB* ) pxCurrentTCB )->pcTaskName,      \
                                xTickCount );                                  \
        printf( cBuffer );													   \
    }
However with the OUT macro also defined  i end up in vApplicationStackOverflowHook()
#define traceTASK_SWITCHED_OUT()                                               \
extern TaskHandle_t xHandle;                                                   \
xHandle = pxCurrentTCB

Could someone please throw some light on this?
Cheers

rtel wrote on Thursday, July 07, 2016:

  1. sprintf() is probably using masses of stack, and may even be calling
    malloc().

  2. it is never a good idea to use any of the printf() style string
    formatting functions inside an interrupt, or malloc, or any other
    non-reentrant stack hungry function that could potentially take a long
    time to execute - these macros are called from inside an interrupt.

paraparvy wrote on Thursday, July 07, 2016:

OK, thanks…ofcourse i would never use printf etc from an ISR in a real application, this is just something im doing to learn FreeRTOS…will use the macros to toggle an I/O line or something on a demo board etc…thanks a lot

paraparvy wrote on Thursday, July 07, 2016:

Incidentally, if i wanted to gather task switching data etc in a real application, how would one go about doing this?..are there function available that are safe to use for formatting data in the printf style ?(task names being switched in and out, quesus being created, waiting on queues etc)…ie a safe way to log switching data for later review?
Thanks

rtel wrote on Thursday, July 07, 2016:

Have you seen the trace tool?
http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_Trace/FreeRTOS_Plus_Trace.shtml

paraparvy wrote on Thursday, July 07, 2016:

Thanks, the trace tool looks fantastic;
I’ll have a play around with the free edition later on…

Anyway, If I wanted to use the trace macros in a real world app, what approach do you recommend for logging the data from the macros (in light of the fact that printf, sprintf should not be used)

…i.e. what sort of event logging scheme do people use?

Cheers

rtel wrote on Thursday, July 07, 2016:

Something much faster than sprintf’ing - which would probably all but
bring your system to a standstill. Digital outputs would be the older
way, on ARM hardware ETM is the fast modern way. Not sure about the
capabilities of PIC24 for trace output though.

paraparvy wrote on Thursday, July 07, 2016:

OK, thanks a lot