Page 1 of 1

How run a FreeRTOS task at a specific hertz

Posted: Mon Jan 31, 2022 10:11 am
by brahmajit
I'm new to programming and electronics in general. I want run a task at 100 Hz. Unfortunately I don't know how to do that with FreeRTOS, if anyone could help it would be very helpful.

Code: Select all

void Task1code( void * parameter ){
  Serial.print("Task1 running on core ");
  Serial.println(xPortGetCoreID());

  for(;;){
    // some code 
   // Pause the task for 500ms
    vTaskDelay(500 / portTICK_PERIOD_MS); // I guess this is something I need to change?
  } 
}
Thank you.

Re: How run a FreeRTOS task at a specific hertz

Posted: Mon Jan 31, 2022 12:19 pm
by brahmajit
Thanks to idahowalker from Arduino forum for this code snippet.

Code: Select all

void Task1code( void * parameter ){
  Serial.print("Task1 running on core ");
  Serial.println(xPortGetCoreID());
  TickType_t xLastWakeTime = xTaskGetTickCount();
  const TickType_t xFrequency = 5000; //delay for mS

  for(;;)
  {
        xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil( &xLastWakeTime, xFrequency );

   // some code 
   // Pause the task for 500ms
      } 
}  // put your main code here, to run repeatedly: