Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

Wierd timing error

Posted by timonm on March 11, 2015

I got a wierd timing problem.

I have two tasks. There is a constant task switch between these two tasks. I use the cycle counter timer to measure the total length of the task switch. But here is the wierd part.

When i add one line of code, the code is faster, than when i remove the line. How is this possible?

when i add the code. the total cycles is: 4846. When i remove the line. the total cycles is: 4897.

Below the source code:

/*******************************************************************
* Includes
*******************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "swv.h"
/*******************************************************************
* Defines
*******************************************************************/
#define COUNTER

#define MAX_LOOPS 10

#define DEMCR           (*((volatile uint32_t *)0xE000EDFC))
#define DWT_CTRL        (*(volatile uint32_t *)0xe0001000)

#define mainFIRST_TASK_PRIORITY (tskIDLE_PRIORITY + 2)
#define mainSECOND_TASK_PRIORITY (tskIDLE_PRIORITY + 2)

/*******************************************************************
* Types
*******************************************************************/
xTaskHandle xHandleFirst;
xTaskHandle xHandleSecond;
/*******************************************************************
* Data
*******************************************************************/
uint32_t m_nStart;               //DEBUG Stopwatch start cycle counter value
uint32_t m_nStop;                //DEBUG Stopwatch stop cycle counter value

/*******************************************************************
* Local Function Prototypes
*******************************************************************/
static void prvFirst(void *pvParameters);
static void prvSecond(void *pvParameters);

int main(void)
{
    /* Enable DWT */
    DEMCR |= 0x01000000;
    /* Enable CPU cycle counter */
    DWT_CTRL |= 1;

    while(1)
    {
        xTaskCreate(prvSecond, (const char *) "S",
                    configMINIMAL_STACK_SIZE,NULL,
                    mainSECOND_TASK_PRIORITY, &xHandleSecond);
        xTaskCreate(prvFirst, (const char *) "F",
                    configMINIMAL_STACK_SIZE,NULL,
                    mainFIRST_TASK_PRIORITY, &xHandleFirst);
        vTaskStartScheduler();
    }
}

//Task 1
static void prvFirst(void *pvParameters)
{
    while(1)
    {
        m_nStart = *((volatile uint32_t *)0xE0001004); // save clockcycle count
        for(uint32_t count1 = 0; count1 < MAX_LOOPS; count1++)
        {
            taskYIELD();
        }
        vTaskDelete(xHandleFirst);
    }
}

//task 2
static void prvSecond(void *pvParameters)
{
    while(1)
    {
        for (uint32_t count2 = 0; count2 < MAX_LOOPS-1; count2++)
        {
            taskYIELD();
        }
        m_nStop = *((volatile uint32_t *)0xE0001004); // save clockcycle count
        
// ADD ONE LINE OF CODE AFTER THIS. THE CODE RUNS FASTER
        SWV_printnum(m_nStop);
        SWV_puts("\r\n");
        SWV_printnum(m_nStart);
        SWV_puts("\r\n"); 
        
        vTaskDelete(xHandleSecond);
    }
    
}

Wierd timing error

Posted by rtel on March 11, 2015

When you say "// ADD ONE LINE OF CODE AFTER THIS. THE CODE RUNS FASTER" do you mean add a line of code between that comment and the call to SWV_printnum()? If so, what is the line?

If you are trying to get the switch as fast as possible then:

  • ensure configCHECKFORSTACK_OVERFLOW is 0
  • ensure no trace macros are enabled
  • ensure run-time stats are disable
  • ensure configUSEPORTOPTIMISEDTASKSELECTION is set to 1.

Regards.


Wierd timing error

Posted by timonm on March 11, 2015

Thanks for the answer.

When i remove one line of code below the "//ADD ONE LINE etc.. "the code runs faster. for example the line "SWV_puts("rn");

With the settings you send i get the following timing:

code: SWVprintnum(mnStop); SWVputs("rn"); SWVprintnum(mnStart); SWVputs("rn");

The timing is: 4542

code: SWVprintnum(mnStop); SWVputs("rn"); SWVprintnum(mnStart); // SWVputs("rn");

The timing is: 4488

So then its the other way aroud.. but its still wierd, because i already saved the clockcyclecounter..


Wierd timing error

Posted by rtel on March 11, 2015

Do you have compiler optimisation turned on? I suspect so. With optimisation turned off I would not expect any difference.

Regards.


Wierd timing error

Posted by timonm on March 11, 2015

Compiler optimalisation and optiimize for time are turned off

Compiler settings: --c99 -c --cpu Cortex-M4.fp -DEVAL -DMICROLIB -g -O0 --apcs=interwork --split_sections


Wierd timing error

Posted by timonm on March 13, 2015

After enabeling Flash prefetch, Instruction cache, Data cache, the delay is 50 ns instead of 5 ms


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner