My FreeRTOS implementation doesn't completes its tasks

371 views Asked by At

My project has a Atmega328p microcontroller (same as the Arduino Uno) So I implemented a Arduino Uno ported FreeRTOS v10 project.

Some specs: - F_CPU = 1 MHz - Running on timer 0 - Preemptive mode - Time slicing = on

My two simple tasks behave weird. Task 1 is a led toggle, Task 2 is a three tone ladder

My initial alarm implementation was an alarm_on() function that had three tones produced with _delay_ms() functions in between. But that didn't work as expected, so I tried with the vTaskDelayUntil() functions.

I made a video of the behavior running the code snippet below https://www.youtube.com/watch?v=XnYaELsW1d0

So the led blink works, but that is a very easy and short task. The Sound task only produces 2 tones or sometimes only one.

My task schedule:

xTaskCreate( TaskBlinkLED,  
             (const portCHAR *)"LED", 
             256, 
             NULL, 
             1,  
             NULL );

xTaskCreate( TaskSoundAlarm,  
             (const portCHAR *)"SOUND", 
             512, 
             NULL, 
             4,  
             NULL );

vTaskStartScheduler();

The actual task code

static void TaskBlinkLED(void *pvParameters) // Main Green LED Flash
{
    (void) pvParameters;
    TickType_t xLastWakeTime;
    const TickType_t xFrequency = 800;
    /* The xLastWakeTime variable needs to be initialized with the current tick
    count.  Note that this is the only time we access this variable.  From this
    point on xLastWakeTime is managed automatically by the vTaskDelayUntil()
    API function. */
    xLastWakeTime = xTaskGetTickCount();

    for(;;)
    {
        led_toggle();
        vTaskDelayUntil( &xLastWakeTime, ( xFrequency / portTICK_PERIOD_MS ) );
    }
}

static void TaskSoundAlarm(void *pvParameters) 
{
    (void) pvParameters;
    TickType_t xLastWakeTime;
    const TickType_t xFrequency = 3000;
    xLastWakeTime = xTaskGetTickCount();

    for(;;)
    {
        alarm_on(300);
        vTaskDelayUntil( &xLastWakeTime, ( 200 / portTICK_PERIOD_MS )  );
        alarm_on(400);
        vTaskDelayUntil( &xLastWakeTime, ( 200 / portTICK_PERIOD_MS )  );
        alarm_on(500);
        vTaskDelayUntil( &xLastWakeTime, ( 200 / portTICK_PERIOD_MS )  );
        alarm_off();

        vTaskDelayUntil( &xLastWakeTime, ( xFrequency / portTICK_PERIOD_MS ) );
    }
}

My Alarm function

void alarm_on(unsigned long hz)
{
    ALARM_PORT_DDR |= (1<<ALARM_PIN);
    t1_set_ctc_a(hz, timer_freq);
}

Why doesn't it finish the complete sound task here by producing 3 tones?

0

There are 0 answers