avr-task-scheduler

AVR mcu bare metal task scheduler
Index Commits Files Refs README LICENSE
commit 834225bdde4e5d28524b0431555a6b85782e6837
parent 7fd5611193a02323fb7271aed2b5625572862ee7
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Tue, 14 Oct 2025 09:29:15 -0300

rename scheduler functions

Diffstat:
MMakefile | 7+++----
Msrc/main.c | 12+++++++-----
Msrc/scheduler.c | 6+++---
Msrc/scheduler.h | 6+++---
4 files changed, 16 insertions(+), 15 deletions(-)
diff --git a/Makefile b/Makefile
@@ -7,7 +7,7 @@ LIBS := $(SRC_DIR/*.h)
 OBJS := $(patsubst $(SRC_DIR)/%.c,$(BIN_DIR)/%.o,$(SRCS))
 
 CC := avr-gcc
-CFLAGS := -Wall -Os -DF_CPU=16000000UL -mmcu=atmega328p
+CFLAGS := -Wall -Wextra -pedantic -std=c99 -Os -mmcu=atmega328p
 
 .PHONY: all
 
@@ -15,7 +15,7 @@ all: $(BIN_DIR) $(BIN_DIR)/main.hex
 
 $(BIN_DIR)/%.o: $(SRC_DIR)/%.c
     $(CC) $(CFLAGS) \
-        -c $< -o $@
+        -o $@ -c $<
 
 $(BIN_DIR)/main.elf: $(OBJS)
     $(CC) $(CFLAGS) \
@@ -25,8 +25,7 @@ $(BIN_DIR)/main.hex: $(BIN_DIR)/main.elf
     avr-objcopy -O ihex -R .eeprom $(BIN_DIR)/main.elf $(BIN_DIR)/main.hex
 
 upload: $(BIN_DIR)/main.hex
-    avrdude -F -V -c arduino -p ATMEGA328P \
-        -P $(PORT) -b 115200 -U flash:w:$(BIN_DIR)/main.hex
+    avrdude -F -V -c arduino -p ATMEGA328P -P $(PORT) -b 115200 -U flash:w:$(BIN_DIR)/main.hex
 
 $(BIN_DIR):
     @if [ ! -d "$(BIN_DIR)" ]; then \
diff --git a/src/main.c b/src/main.c
@@ -25,10 +25,11 @@ typedef enum {
 } task_id_e;
 
 task_t tasks[] = {
-    { toggle_led_1,  1000,  TOGGLE_LED_1_ID },
-    { toggle_led_2,   500,  TOGGLE_LED_2_ID },
-    { read_button_a,    0,    READ_BTN_A_ID },
-    { read_button_b,    0,    READ_BTN_B_ID }
+    //   on_update  period                id
+    { toggle_led_1,   1000,  TOGGLE_LED_1_ID },
+    { toggle_led_2,    500,  TOGGLE_LED_2_ID },
+    { read_button_a,     0,    READ_BTN_A_ID },
+    { read_button_b,     0,    READ_BTN_B_ID }
 };
 
 void io_setup(void) {
@@ -43,7 +44,8 @@ void io_setup(void) {
 int main(void) {
     io_setup();
 
-    millis_init(&scheduler_inc_update_tick);
+    millis_init(&scheduler_update_tick);
+
     scheduler_init(tasks, sizeof(tasks)/sizeof(tasks[0]));
 
     while(1) {
diff --git a/src/scheduler.c b/src/scheduler.c
@@ -16,7 +16,7 @@ void scheduler_init(task_t *tasks, uint8_t task_count) {
     scheduler.task_cnt = task_count;
 }
 
-void scheduler_inc_update_tick(void) {
+void scheduler_update_tick(void) {
     scheduler.update_cnt += (scheduler.update_cnt == 255 ? 0 : 1);
 }
 
@@ -35,13 +35,13 @@ void scheduler_update(void) {
         }
 
         scheduler.tasks[i].ticks = scheduler.tasks[i].period;
-        scheduler.tasks[i].task_update();
+        scheduler.tasks[i].on_update();
     }
 }
 
 void scheduler_set_task_period(uint8_t task_id, uint16_t new_period) {
     for (uint8_t i = 0; i < scheduler.task_cnt; ++i) {
-        if (scheduler.tasks[i].task_id == task_id) {
+        if (scheduler.tasks[i].id == task_id) {
             scheduler.tasks[i].period = new_period;
             scheduler.tasks[i].ticks = new_period;
         }
diff --git a/src/scheduler.h b/src/scheduler.h
@@ -8,15 +8,15 @@ extern "C" {
 #include <stdint.h>
 
 typedef struct {
-    void (*task_update)(void);
+    void (*on_update)(void);
     uint16_t period;
-    uint8_t task_id;
+    uint8_t id;
     uint16_t ticks;
 } task_t;
 
 void scheduler_init(task_t *tasks, uint8_t task_count);
 void scheduler_update(void);
-void scheduler_inc_update_tick(void);
+void scheduler_update_tick(void);
 void scheduler_set_task_period(uint8_t task_id, uint16_t new_period);
 
 #ifdef __cplusplus