In a project with a single task whenever its priority is set to 0 the system runs as expected, but when its priority is set a value other than 0 (with configMAX_PRIORITIES set to 3) then the function vTaskDelayUntil() never returns. Instead, the IDLE hook fires repeatedly.
For example, the serial output when its priority is set to 0 is:
…
KRNL_IN
KRNL_FRV
IDL
KRNL_FRV
IDL
KRNL_FRV
IDL
…
which is ok. But when its priority is set to 1 or 2, then the output is:
…
KRNL_IN
IDL
IDL
IDL
IDL
IDL
…
I’ve run out of ideas. Here is the complete code:
#include <Arduino.h>
#include <FreeRTOS.h>
#include <task.h>
#include <event_groups.h>
#include <timers.h>
#define FOREVER while( 1 )
// we pass on the LEONARDO symbol through the Makefile
#ifdef LEONARDO
const int LED_BUILTIN = 13;
#endif
//----------------------------------------------------------------------
// plc_kernel_task()
//----------------------------------------------------------------------
static const uint16_t TASK_PLC_KERNEL_STACK_SIZE = 64 * 2;
static StaticTask_t plc_kernel_task_tcb;
static StackType_t plc_kernel_task_ram[ TASK_PLC_KERNEL_STACK_SIZE ];
TaskHandle_t main_task_handler;
void plc_kernel_task( void* pvParameters )
{
uint8_t ticks{ 5 };
uint8_t state{ 0 };
TickType_t last_wake_time = xTaskGetTickCount();
Serial.println( "KRNL_IN" );
FOREVER
{
vTaskDelayUntil( &last_wake_time, pdMS_TO_TICKS( 100 ) );
Serial.println( "KRNL_FRV" );
--ticks;
if( ticks == 0 ){
if( state == 0 ){
ticks = 5;
state = 1;
digitalWrite( 13, LOW );
} else{
ticks = 5;
state = 0;
digitalWrite( 13, HIGH );
}
}
}
}
int main(void)
{
cli();
xTaskCreateStatic(
plc_kernel_task,
( const portCHAR* ) "PLC",
TASK_PLC_KERNEL_STACK_SIZE,
NULL,
2,
plc_kernel_task_ram,
&plc_kernel_task_tcb );
init();
// init the Arduino's hardware and stuff
Serial.begin( 115200 );
pinMode( 13, OUTPUT );
digitalWrite( 13, LOW );
vTaskStartScheduler();
while( 1 )
{
// nothing to do in here. Everything is done in the task's code
}
return 0;
}
#ifdef __cplusplus
extern "C" {
#endif
void vApplicationIdleHook()
{
static uint16_t ticks{ 50000 };
--ticks;
if( ticks == 0 ){
ticks = 50000;
Serial.println( "IDL" );
}
}
void vApplicationMallocFailedHook()
{
while( 1 )
{
;
}
}
void vApplicationStackOverflowHook( TaskHandle_t xTask, signed char *pcTaskName )
{
while( 1 )
{
Serial.println( "SOF!" );
delay( 500 );
}
}
#ifdef __cplusplus
}
#endif
This is a reduced task from a project that involves 3 tasks. T1 and T2 are closely related because of a semaphore, and T3 is kind of independent. Whenever the priorities of T1 and T2 are set other than 0, the system stucks. T3 runs with the highest priority.
That’s way I reduced the code to its minimal, but I cannot see where and how I’m failing.
Any help will be highly appreciated!