1 # AVR Bare Metal Task Scheduler 2 3 This is a simple program that implements a task scheduler for avr based mcus, 4 in particular ATmega328. It executes a series of tasks attached to the 5 scheduler after their specified task period has passed 6 7 ## In this example 8 9 Four LEDs are controlled: two blink at different rates, and the other two 10 controlled by two buttons. The first button implements a 150 ms button debounce, 11 on idle the LED is off, when the button is pressed the LED toggles on and keeps 12 on that state for 150 ms, independently of the button state, if the button is 13 kept pressed the LED will keep on, if the button is released the LED will turn 14 off. The second button is simpler, if the button is pressed the led is on, and 15 if the button is not pressed the led is off 16 17 ## Scheduler usage 18 19 The tasks are assigned to the scheduler with the following statements: 20 21 ```c 22 task_t tasks[] = { 23 // on_update period id 24 { toggle_led_1, 1000, TOGGLE_LED_1_ID }, 25 { toggle_led_2, 500, TOGGLE_LED_2_ID }, 26 { read_button_a, 0, READ_BTN_A_ID }, 27 { read_button_b, 0, READ_BTN_B_ID } 28 }; 29 30 scheduler_init(tasks, sizeof(tasks)/sizeof(tasks[0])); 31 ``` 32 33 The scheduler invokes the `on_update` field function when the specified period 34 for that task has passed, the period being in milliseconds. When the period is 35 zero, the function updates on every millisecond. The `id` field is optional, in 36 the example is used to change the period of the `READ_BTN_A` task, when the 37 button is pressed the period is set to 150 ms, and on the next upadate it will 38 be set back to 0 if the button has been released, if the button has not been 39 released the period stays the same at 150 ms 40 41 The `scheduler_tick` must be called on every millisecond, this only signals the 42 scheduler that a tick has passed, all the tasks execution happens when the 43 `scheduler_update` function get called 44 45 > **NOTE**: tasks are executed sequentially, so if one task takes more than 1 ms 46 > to execute and another task needs to run at the same time, the first task will 47 > delay the other 48 49 ## Dependencies 50 51 On Linux the following packages are needed to compile the code: 52 53 * `avr-gcc` and `avr-libc` to compile the code 54 * `avrdude` for uploading the program to the microcontroller 55 56 ## Compile and upload to the board 57 58 Having the dependencies installed, compile the code using `make` from a shell, 59 as follows: 60 61 ```console 62 $ make compile 63 ``` 64 65 Then with the board connected to the computer, check the port with: 66 67 ```console 68 $ ls /dev/tty* 69 ``` 70 71 Often appears as `/dev/ttyUSB*` or `/dev/ttyACM*`, by default `/dev/ttyUSB0` is 72 used, to override this execute make assigning the corred port to the `PORT` 73 shell variable, for example: 74 75 ```console 76 $ PORT=/dev/ttyUSB1 make upload 77 ```
