Incorrect Timer period and stops after some time

arshad wrote on Monday, June 10, 2019:

Hello all,

Just starting with FreeRTOS and Arduino (Mega2560). I created a simple program to blink two LED,
Green every 5 sec
Blue turn on after 500ms

Behaviour i got:
-Blue turns on.
-Green blink but what seems like every 1 second
-After a minute Green is stuck at On and BLue starts blinking

help is appreciated
thanks

#include <Arduino_FreeRTOS.h>
#include "timers.h"
#include <Arduino.h>

#define ONE_SHOT_TIMER_PERIOD       (pdMS_TO_TICKS(500))
#define AUTO_RELOAD_TIMER_PERIOD    (pdMS_TO_TICKS(5000))


int blueLedPin = 13, greenLedPin = 11;
const uint8_t *blueLed = (uint8_t *)&blueLedPin;
const uint8_t *greenLed = (uint8_t *)&greenLedPin;


TimerHandle_t xOneShotTimer;
TimerHandle_t xAutoReloadTimer;

BaseType_t xOneShotTimerStarted;
BaseType_t xAutoReloadTimerStarted;

void prvOneShotTimerCallback(TimerHandle_t xTimer);
void prvAutoReloadTimerCallback(TimerHandle_t xTimer);

void setup()
{
   Serial.begin(115200);
   Serial.println("Setup ");

   pinMode(blueLedPin, OUTPUT);
   pinMode(greenLedPin, OUTPUT);
   

   xOneShotTimer = xTimerCreate("One Shot",
                                       ONE_SHOT_TIMER_PERIOD,
                                       pdFALSE,
                                       (void *)0,
                                       prvOneShotTimerCallback
                                       );
   xAutoReloadTimer = xTimerCreate("Auto Reload",
                                           AUTO_RELOAD_TIMER_PERIOD,
                                           pdTRUE,
                                           (void *)0,
                                           prvAutoReloadTimerCallback
                                           );


   if ((xOneShotTimer != NULL) && (xAutoReloadTimer != NULL))
   {
       xOneShotTimerStarted = xTimerStart(xOneShotTimer, 0);
       xAutoReloadTimerStarted = xTimerStart(xAutoReloadTimer, 0);
   }

   while ((xOneShotTimerStarted != pdPASS) && (xAutoReloadTimerStarted != pdPASS)){}
   vTaskStartScheduler();
   Serial.println("end");
}

void prvOneShotTimerCallback(TimerHandle_t xTimer)
{
   TickType_t xTimerNow;
   xTimerNow = xTaskGetTickCount();
   Serial.print("One-shot timer : ");
   Serial.println(xTimerNow);
   digitalWrite(blueLedPin, digitalRead(blueLedPin) ^ 1);
}


void prvAutoReloadTimerCallback(TimerHandle_t xTimer)
{
   TickType_t xTimerNow;
   xTimerNow = xTaskGetTickCount();
   Serial.print("Auto- Reload timer : ");
   Serial.println(xTimerNow);
   digitalWrite(greenLedPin, digitalRead(greenLedPin) ^ 1);
}

void loop(){}
`

rtel wrote on Monday, June 10, 2019:

I don’t know how the Arduino port works, or really how Arduino works,
but there is nothing obviously wrong with your code. Is the LED used by
any other code? Maybe the unexpected flashing pattern is controlled by
something else - an error handler for example.

Look though the following list for the normal options, such as ensuring
you don’t have stack overflow, you do have asserts defined, etc. In
this case it is the timer task stack that is of interest as the timer
callbacks both execute in the context of the timer task (which is also
why it is ok to access the serial port from both as they will never
interrupt each other).

https://www.freertos.org/FAQHelp.html

arshad wrote on Tuesday, June 11, 2019:

Thanks Richard

Looks like i got some reading to do

arshad wrote on Tuesday, June 11, 2019:

follow up question.
Since Timer are executed by TimerTask, then will it be same if i have
two taks, one with 1/2 sec and one with 5 sec delay using vTaskDelay
instead of two timer?

rtel wrote on Tuesday, June 11, 2019:

Will what be the same?