Skip to main content
Visitor II
August 1, 2020
Question

FreeRTOS ulTotalRunTime should be defined as volatile

  • August 1, 2020
  • 2 replies
  • 1605 views

After failing to view run time results of tasks in FreeRTOS, found this blog from Atollic which describes ulTutoralRunTime in tasks.c has to be defined as volatile otherwise it won't be updated whenever optimization is above -O0 .

    This topic has been closed for replies.

    2 replies

    Graduate II
    August 1, 2020

    Above -O0 many debugging related things break apart...

    Visitor II
    August 2, 2020

    From gcc documents:

    Optimize debugging experience. -Og should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience. It is a better choice than -O0 for producing debuggable code because some compiler passes that collect debug information are disabled at -O0.

    Like -O0, -Og completely disables a number of optimization passes so that individual options controlling them have no effect. Otherwise -Og enables all -O1 optimization flags except for those that may interfere with debugging

    Graduate II
    August 2, 2020

    Even -Og optimizes out variables and does a few things, which can make debugging cumbersome. Anyway it's not what your original topic is about. The FreeRTOS doesn't need that variable to be volatile, it's only that viewing tool under specific optimization levels, that needs it.

    Super User
    August 2, 2020

    > ulTutoralRunTime in tasks.c has to be defined as volatile

    This may be a Atollic TrueStudio specific requirement for updating their GUI FreeRTOS Task List view.

    In general, no changes are required and the code works out-of-the-box when using the FreeRTOS supplied vTaskGetRunTimeStats function.

    Set up an up-counting timer running at a frequency considerably higher than the FreeRTOS tick, say TIM2 at 1 MHz.

    Activate in FreeRTOSConfig.h:

    #define configGENERATE_RUN_TIME_STATS 1
    #define configUSE_TRACE_FACILITY 1
    #define configUSE_STATS_FORMATTING_FUNCTIONS 1

    Override the _weak defaults by implementing:

    void configureTimerForRunTimeStats(void)
    {
    	HAL_TIM_Base_Start(&htim2);
    }
     
    unsigned long getRunTimeCounterValue(void)
    {
    	return __HAL_TIM_GET_COUNTER(&htim2);
    }

    And now you can periodically read-out the statistics in a tasks by:

    char pcWriteBuffer[256];
    vTaskGetRunTimeStats(pcWriteBuffer);

    and send the results to an UART or other interface. You get output like

    ...
    defaultTask 24120810 8%
    IDLE 270603162 91%
    Tmr Svc 58 <1%

    Btw: There is no need to setup an additional timer interrupt as suggested in that tutorial. That creates only overhead...