#### 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"

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

{
// 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);
PORTB &= ~(1 << pin);
}
}

int main(void)
{
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_attr.priority = 1;
led_1_attr.update_interval = (1000 * 8);  // Execute every 1000 ms

led_2_attr.priority = 2;
led_2_attr.update_interval = (500 * 8);  // Execute every 500 ms

led_3_attr.priority = 3;
led_3_attr.update_interval = (250 * 8);  // Execute every 250 ms

// 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);
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

#### 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);
PORTB &= ~(1 << pin);
}
}

#### trebla

Joined Jun 29, 2019
215
When attaching tasks to RTOS you describe task running conditions, see post #1 code lines 37-39

#### rt694157

Joined Dec 15, 2019
57
When attaching tasks to RTOS you describe task running conditions, see post #1 code lines 37-39
I don't understand function that start from lines 14-27. What is use of this function ?

#### 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.