Scheduler triggers tasks not on time

I found the two shoots of the task , triggered by the scheduler, is not stick to vTaskDelayUntil(10) value.
it’s ranged from 9 - 13ms, tick is 1ms.
did anybody meet similar situation before?
and how to fix it.

You have a time granularity of one tick - or 1ms in your case. That means asking for a delay of 10 will result in a delay of between 9.999 ms if the delay starts immediately after a tick interrupt, and 9.001ms if the delay starts immediately before the next tick interrupt. When the task actually runs after the delay expires depends on the state of the other tasks, and if any other tasks are running, its priority relative to the running task.

I only tested 2 tasks,

xTaskCreate( InitJobTask, "IK", 512 , NULL, mainQUEUE_SEND_TASK_PRIORITY + 8, &Rt_InitTaskxHandle); // Init Task
xTaskCreate( DrvTask, "TH", 512 , NULL, mainQUEUE_SEND_TASK_PRIORITY + 11, &Rt_DrvTaskxHandle );

vTaskStartScheduler();

 

static void InitJobTask( void *pvParameters )
{
     for( ;; )
     {
         SystemInit();
        vTaskSuspend(Rt_InitTaskxHandle);
     }
}

 

uint32_t drvt1 = 0;
uint32_t drvt2 = 0;
uint32_t drvDelt[32] = {0};
static void DrvTask( void *pvParameters )
{
     static TickType_t xLastWakeTimeMs;
     int i = 0;

     xLastWakeTimeMs = xTaskGetTickCount();

    for( ;; )
    {
         drvt2 = GetTimeStampTimeMs(); 

         DrvTask();

         drvDelt[i] = drvt2 - drvt1; 
         drvt1 = drvt2;
        i += 1;
       if (i >= 32 )
       {
          i = 0;
       }

      vTaskDelayUntil( &xLastWakeTimeMs, 5);
   }
}

debug result:
image

Which hardware are you running this on? How fast and stable is its clock? If you run from an internal oscillator it won’t be very stable. How long does DrvTask() take to execute?

nxp s32k148, DrvTask() execution time, I am not sure, I think it is less than 2 ms.
Debug result is 0,
i think it is less than 1 tick.

void DrvTask(void)
{
   uint32_t uFuncStartTime = GetTimeStampTimeMs();
  SensorDrvTask();
  UpdateProcTime(&ProcTime_Drv,uFuncStartTime);
}

uint32_t tstDelt[32] = {0};
int tsti = 0;
void UpdateProcTime( ProcTimeStat *ProcTimeStat, uint32_t startTimeStamp)
{
  uint32_t uCurTimeStamp  = GetTimeStampTimeMs();
  uint32 uDeltaTime = uCurTimeStamp - startTimeStamp;

  tstDelt[tsti] = uDeltaTime;
  tsti += 1;
  if (tsti >= 32)
  {
      tsti = 0;
  }

  if (uDeltaTime > 0xFFu)
 {
    uDeltaTime = 0xFFu;
 }
  ....

}

Debug result is: tstDelt[…] = 0;

Is it possible you have a very high interrupt load - or that you have an interrupt that is being continuously re-entered?

[edit]please post your FreeRTOSConfig.h file[/edit]

#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H


#define configCPU_CLOCK_HZ                          ( 80000000UL )
#define configTICK_RATE_HZ                          ((TickType_t) 1000 )
#define configMAX_PRIORITIES                        15
#define configMINIMAL_STACK_SIZE                    ((unsigned short) 100 )
#define configMAX_TASK_NAME_LEN                     12
#define configUSE_16_BIT_TICKS                      0
#define configIDLE_SHOULD_YIELD                     1
#define configUSE_PREEMPTION                        1
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS     0
#define configUSE_PORT_OPTIMISED_TASK_SELECTION     0
#define configUSE_TASK_NOTIFICATIONS                1
#define configUSE_TIME_SLICING                      1
#define configUSE_NEWLIB_REENTRANT                  0
#define configENABLE_BACKWARD_COMPATIBILITY         1
#define configUSE_POSIX_ERRNO                       0

/* Definition assert() function. */
#define configASSERT(x)                             if((x)==0) { taskDISABLE_INTERRUPTS(); for( ;; ); }

/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ( 5 )

/* Memory allocation related definitions. */
#define configSUPPORT_STATIC_ALLOCATION             0
#define configSUPPORT_DYNAMIC_ALLOCATION            1
#define configTOTAL_HEAP_SIZE                       (( size_t ) 65536 )
#define configAPPLICATION_ALLOCATED_HEAP            0

/* Hook function related definitions. */
#define configUSE_IDLE_HOOK                         1
#define configUSE_TICK_HOOK                         1
#define configUSE_MALLOC_FAILED_HOOK                1
#define configCHECK_FOR_STACK_OVERFLOW              2
#define configUSE_DAEMON_TASK_STARTUP_HOOK          0

/* Run time and task stats gathering related definitions. */
#define configGENERATE_RUN_TIME_STATS               0
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()    
#define portGET_RUN_TIME_COUNTER_VALUE()            xTaskGetTickCount()
#define configUSE_TRACE_FACILITY                    1
#define configUSE_STATS_FORMATTING_FUNCTIONS        1

/* Co-routine related definitions. */
#define configUSE_CO_ROUTINES                       0
#define configMAX_CO_ROUTINE_PRIORITIES             2

/* SEMAPHORES and MUTEXS */
#define configUSE_MUTEXES                           1
#define configUSE_RECURSIVE_MUTEXES                 1
#define configUSE_COUNTING_SEMAPHORES               1

/* Software timer related definitions. */
#define configUSE_TIMERS                            1
#define configTIMER_TASK_PRIORITY                   3
#define configTIMER_QUEUE_LENGTH                    10
#define configTIMER_TASK_STACK_DEPTH                180

/* Tickless Idle Mode */
#define configUSE_TICKLESS_IDLE                     0

/* QUEUE */
#define configQUEUE_REGISTRY_SIZE                   8
#define configUSE_QUEUE_SETS                        0

/* Optional functions - most linkers will remove unused functions anyway. */
#define INCLUDE_vTaskPrioritySet                    1
#define INCLUDE_uxTaskPriorityGet                   1
#define INCLUDE_vTaskDelete                         1
#define INCLUDE_vTaskSuspend                        1
#define INCLUDE_vTaskDelayUntil                     1
#define INCLUDE_vTaskDelay                          1
#define INCLUDE_xTaskGetSchedulerState              1
#define INCLUDE_xTaskGetCurrentTaskHandle           1
#define INCLUDE_uxTaskGetStackHighWaterMark         1
#define INCLUDE_xTaskGetIdleTaskHandle              0
#define INCLUDE_eTaskGetState                       1
#define INCLUDE_xEventGroupSetBitFromISR            1
#define INCLUDE_xTimerPendFunctionCall              1
#define INCLUDE_xTaskAbortDelay                     1
#define INCLUDE_xTaskGetHandle                      1
#define INCLUDE_xTaskResumeFromISR                  1
#define INCLUDE_xQueueGetMutexHolder                1


/* Run time stats gathering definitions. */
#ifdef __GNUC__
    /* The #ifdef just prevents this C specific syntax from being included in
    assembly files. */
    void vMainConfigureTimerForRunTimeStats( void );
    unsigned long ulMainGetRunTimeCounterValue( void );
#endif

/* Cortex-M specific definitions. */
#ifdef __NVIC_PRIO_BITS
  #define configPRIO_BITS                         __NVIC_PRIO_BITS
#else
  #define configPRIO_BITS                         4
#endif

/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY  (0x0F)

/* Interrupt priorities used by the kernel port layer itself.  These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#ifndef configKERNEL_INTERRUPT_PRIORITY
  #define configKERNEL_INTERRUPT_PRIORITY         (configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8-configPRIO_BITS))  
#endif
  
#ifndef configMAX_SYSCALL_INTERRUPT_PRIORITY
  #define configMAX_SYSCALL_INTERRUPT_PRIORITY    (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8-configPRIO_BITS))
#endif

/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler                             SVC_Handler
#define xPortPendSVHandler                          PendSV_Handler
#define xPortSysTickHandler                         SysTick_Handler   


#endif /* FREERTOS_CONFIG_H */

Why does your InitJob task call SystemInit() repeatedly? Would you not rather want to be sytem init a once event? Depending on what your SystemInit does, it may interfere with the timing, even though its priority appears to be lower.

In any case, for investigating timing issues, tracealyzer is your tool of choice.

Edit: You appear to have overloaded implementations of DrvTask(). What does the “inner” implementation do? If it takes more time to finish than your delay time, it will push the expiration ahead. That is by design.

I found in task.c , NXP SDK, /SDK/rtos/FreeRTOS/Source/tasks.c,
the macro defined in FreeRTOSConfig.h , seems not working,
for example,

The value of configGENERATE_RUN_TIME_STATS is what matters, not simply whether it is defined.

#define configGENERATE_RUN_TIME_STATS    0
#define configGENERATE_RUN_TIME_STATS    1

The first one turns off the feature, while the second one turns on the feature.

IDE issue,i guess, just cleaned the project several times, it resolved.