Creating our own timer according to the FreeRTOS Task cycle

Hello,

In the project I am developing, there are processes that require time within certain tasks. For example, did the X value fall below 60 within 10 seconds? Here, instead of using the timers that FreeRTOS has, I placed a for loop and counter in the task called every 1 ms. For example, the counter increments by 1 each time the task returns. When the counter value is 1000, it is thought that 1 second time has passed, and when it is 10000, 10 seconds is considered to be completed.
Do you think this is the right approach? Are there any drawbacks? What is the advantage of using FreeRTOS software timer instead? You can see the code block I wrote for the counter incrementing process I mentioned below.

if( ucFiveSecTimerStatus ==   TIMER_START)                                          
{       
    if( ucErrorTimerMsFive < 1000U)                                                     
    {       
            ucErrorTimerMsFive ++;                                                        
    }       
    else if( ucErrorTimerMsFive == 1000U)                                               
    {     
            ucErrorTimerMsFive = 0;                                                       
        if( ucErrorTimerSecFive < 5U)                                                  
        {       
                ucErrorTimerSecFive ++;                                                  
        }       
        else if( ucErrorTimerSecFive == 5U)                                            
        {       
                ucErrorTimerMsFive = 0;                                                   
                ucErrorTimerSecFive = 0;     
                ucFiveSecTimerStatus =   TIMER_TIME_IS_UP;                             
        }       
        else
        {                   
                ucStatus = statusERROR;                                         
        }       
    }       
    else
    {                    
            ucStatus = statusERROR;                                              
    }
}
else if( ucFiveSecTimerStatus ==   TIMER_STOP ||  ucFiveSecTimerStatus ==   TIMER_TIME_IS_UP)
{
        ucErrorTimerMsFive = 0;
        ucErrorTimerSecFive = 0;
}
else 
{            
        ucStatus = statusERROR;                                                  
} 

It is not completely clear what your code is doing, but some notes:

  • It assumes configTICK_RATE_HZ is 1000, which in most cases it won’t be.
  • It never blocks, so if this code is running, nothing at a lower priority will run at all.
  • It’s not clear where you are reading the time.

CPU will not be used when your code does not need to run. Is there a reason for not using FreeRTOS timers? Aside, can you elaborate on your problem. What you are trying to do is not clear from the code snippet you shared.

You also need to share with us how you acquire X to determine if it has change within a time period. The logical approach would be to check a real time count on each event of X and compare with a “start time”, where the real time count is maintained by RTOS, or otherwise if greater resolution is required.