testing program (blinks lights)

Thread Starter

rt694157

Joined Dec 15, 2019
57
When I was searching on Google I found a link that contains a program. https://teknoman117.wordpress.com/2012/06/13/simple-avr-rtos-c-version-2/ I don't have an idea what function led_task supposed to do and how it supposed to do

C:
#include <avr/io.h>
#include <avr/interrupt.h>

#include "avr-rtos.h"

struct _avr_rtos_task_attr led_1_attr;
struct _avr_rtos_task_attr led_2_attr;
struct _avr_rtos_task_attr led_3_attr;

uint8_t                    led_1_stack[128];
uint8_t                    led_2_stack[128];
uint8_t                    led_3_stack[128];

void led_task(struct _avr_rtos_task_attr *attr)
{
    // To demonstate the passing of operand to the tasks, use the thread id - 1 as the pin to change
    uint8_t pin = attr->id - 1;
    DDRB |= (1 << pin);
    while(1)
    {
        PORTB |= (1 << pin);
        avr_rtos_task_yield();
        PORTB &= ~(1 << pin);
        avr_rtos_task_yield();
    }
}

int main(void)
{
    // Make ready the RTOS!
    avr_rtos_init();

    // We will be starting three tasks, all use the same function - although they have independent stacks and
    // take their ID - 1 as the pin to toggle

    // LED 1 Task
    led_1_attr.priority = 1;
    led_1_attr.update_interval = (1000 * 8);  // Execute every 1000 ms
    avr_rtos_add_task(&led_task, &led_1_stack[127], &led_1_attr);

    // LED 2 Task
    led_2_attr.priority = 2;
    led_2_attr.update_interval = (500 * 8);  // Execute every 500 ms
    avr_rtos_add_task(&led_task, &led_2_stack[127], &led_2_attr);

    // LED 3 Task
    led_3_attr.priority = 3;
    led_3_attr.update_interval = (250 * 8);  // Execute every 250 ms
    avr_rtos_add_task(&led_task, &led_3_stack[127], &led_3_attr);

    // Set up the timer for interrupts (this program was written to run at 11.0592 MHz.  Finagle with
    // the prescaler settings and the compare match register.  The goal is to have a compare match 8000 times
    // per second
    //
    // interrupts per second = F_CPU / (OCR0A * prescaler)
    //

    TIMSK0 = _BV(OCIE0A);
    TCCR0A = _BV(WGM01);
    OCR0A = 0xAD;
    TCCR0B = _BV(CS01);
    sei();

    while (1)
    {
        // Since this is the thread when there is nothing to do, and we don't have anything to do,
        // just call thread yield.  Though we could do something useful....
        //avr_rtos_task_yield();    // cause a lock up, check in simulator later....
    }

    // return control to libgcc.S
    return 0;
}

ISR(TIMER0_COMPA_vect)
{
    // Only call the context switcher every 8'th interrupt (1 KHz)
    if(!(avr_rtos_tick() % 8))
        avr_rtos_isr();
}
I will appreciate every help
 

BobTPH

Joined Jun 5, 2013
2,576
This program is a demo of a multitasking real-time operating system. Is that what you are looking for?

In any case, it works by sceduling tasks to run periodically and flip the state of an LED each time they are run.

Bob
 

Thread Starter

rt694157

Joined Dec 15, 2019
57
This program is a demo of a multitasking real-time operating system. Is that what you are looking for?

In any case, it works by sceduling tasks to run periodically and flip the state of an LED each time they are run.

Bob
I appreciate your help, I don't understand below function work. I don't understand how does program know which task will run and when It will be run
Code:
void led_task(struct _avr_rtos_task_attr *attr)
{
    // To demonstate the passing of operand to the tasks, use the thread id - 1 as the pin to change
    uint8_t pin = attr->id - 1;
    DDRB |= (1 << pin);
    while(1)
    {
        PORTB |= (1 << pin);
        avr_rtos_task_yield();
        PORTB &= ~(1 << pin);
        avr_rtos_task_yield();
    }
}
 

trebla

Joined Jun 29, 2019
215
This code in lines 14-27 defines function which toggles a pin on PORTB and gives then control back to RTOS. Tasks functions must be in in infinite loop and using avr_rtos_task_yield() it returns to RTOS. Pin number to toggle is passed to this function in post #1 code lines 39, 44 and 49 by attaching tasks to RTOS. By attaching tasks you also tell to RTOS how often and with which priority you want to execute those tasks. In this example you execute three tasks with same function in 1000, 500, and 250 milliseconds. I haven't used this this type of RTOS and can't tell exactly how the attributes are managed but this is how RTOS works.
 
Top