From a53014b7f52462e163a7d7ac64866aa75977422f Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Mon, 24 Feb 2014 23:27:52 +0100 Subject: [PATCH] use callout instead of scheduler --- beep.c | 18 +++++++++++------- cmdline.c | 14 ++++++++------ commands.c | 18 ++++++++++-------- commands_gen.c | 40 +++++++++++++++++++--------------------- main.c | 42 ++++++++++++++++++++---------------------- main.h | 13 ++++++------- rc_proto.c | 2 -- spi_servo.c | 21 ++++++++++++--------- 8 files changed, 86 insertions(+), 82 deletions(-) diff --git a/beep.c b/beep.c index f34219c..fadd16d 100644 --- a/beep.c +++ b/beep.c @@ -31,13 +31,12 @@ #include #include -#include #include #include "main.h" /* 100 ms */ -#define BEEP_PERIOD (100000UL/SCHEDULER_UNIT) +#define BEEP_PERIOD_MS 100 static struct cirbuf beep_fifo; static char beep_fifo_buf[16]; @@ -54,14 +53,15 @@ union beep_t { static volatile union beep_t current_beep; /* called by the scheduler */ -static void beep_cb(void *arg) +static void beep_cb(struct callout_mgr *cm, struct callout *tim, void *arg) { (void)arg; beep_mask = 0; if (current_beep.len == 0 && current_beep.pause == 0) { if (CIRBUF_GET_LEN(&beep_fifo) == 0) - return; + goto reschedule; + current_beep.u08 = cirbuf_get_head(&beep_fifo); cirbuf_del_head(&beep_fifo); } @@ -75,12 +75,15 @@ static void beep_cb(void *arg) case 3: beep_mask = 8; break; default: break; } - return; + goto reschedule; } if (current_beep.pause > 0) { current_beep.pause --; } + + reschedule: + callout_reschedule(cm, tim, BEEP_PERIOD_MS); } void beep(uint8_t tone, uint8_t len, uint8_t pause) @@ -99,6 +102,7 @@ void beep(uint8_t tone, uint8_t len, uint8_t pause) void beep_init(void) { cirbuf_init(&beep_fifo, beep_fifo_buf, 0, sizeof(beep_fifo_buf)); - scheduler_add_periodical_event_priority(&beep_cb, NULL, - BEEP_PERIOD, BEEP_PRIO); + callout_init(&xbeeboard.beep_timer, beep_cb, NULL, BEEP_PRIO); + callout_schedule(&xbeeboard.intr_cm, &xbeeboard.beep_timer, + BEEP_PERIOD_MS); } diff --git a/cmdline.c b/cmdline.c index 0b0c606..736fa52 100644 --- a/cmdline.c +++ b/cmdline.c @@ -30,7 +30,6 @@ #include #include #include -#include #include "callout.h" #include "main.h" @@ -137,12 +136,12 @@ void emergency(char c) * it dynamically */ void mylog(struct error * e, ...) { - va_list ap; #ifndef HOST_VERSION u16 stream_flags = stdout->flags; #endif - uint8_t i; - time_h tv; + va_list ap; + uint8_t i, flags; + uint32_t ms; if (e->severity > ERROR_SEVERITY_ERROR) { if (xbeeboard.log_level < e->severity) @@ -156,8 +155,11 @@ void mylog(struct error * e, ...) } va_start(ap, e); - tv = time_get_time(); - printf_P(PSTR("%d.%.3d: "), (int)tv.s, (int)(tv.us/1000UL)); + IRQ_LOCK(flags); + ms = global_ms; + IRQ_UNLOCK(flags); + + printf_P(PSTR("%d.%.3d: "), (int)(ms/1000UL), (int)(ms%1000UL)); vfprintf_P(stdout, e->text, ap); printf_P(PSTR("\r\n")); diff --git a/commands.c b/commands.c index df76f47..54e56d7 100644 --- a/commands.c +++ b/commands.c @@ -56,7 +56,7 @@ extern const parse_inst_t PROGMEM cmd_log; extern const parse_inst_t PROGMEM cmd_log_show; extern const parse_inst_t PROGMEM cmd_log_type; extern const parse_inst_t PROGMEM cmd_stack_space; -extern const parse_inst_t PROGMEM cmd_scheduler; +extern const parse_inst_t PROGMEM cmd_callout; static int monitor_period_ms = 1000; static int monitor_running = 0; @@ -119,7 +119,7 @@ static void range_cb(struct callout_mgr *cm, if (range_cur_count == 0) { range_running = 0; - return; + callout_stop(cm, clt); } callout_reschedule(cm, clt, range_period_ms); @@ -741,7 +741,8 @@ static void cmd_range_parsed(void *parsed_result, void *data) } range_cur_count = range_count; callout_init(&range_event, range_cb, NULL, 0); - callout_schedule(&cm, &range_event, 0); /* immediate */ + callout_schedule(&xbeeboard.mainloop_cm, + &range_event, 0); /* immediate */ range_running = 1; } else if (!strcmp(res->action, "end")) { @@ -750,7 +751,7 @@ static void cmd_range_parsed(void *parsed_result, void *data) return; } range_running = 0; - callout_stop(&cm, &range_event); + callout_stop(&xbeeboard.mainloop_cm, &range_event); } } @@ -990,7 +991,8 @@ static void cmd_monitor_parsed(void *parsed_result, void *data) return; } callout_init(&monitor_event, monitor_cb, NULL, 0); - callout_schedule(&cm, &monitor_event, 0); /* immediate */ + callout_schedule(&xbeeboard.mainloop_cm, + &monitor_event, 0); /* immediate */ monitor_running = 1; monitor_current = LIST_FIRST(&xbee_monitor_list); printf_P(PSTR("monitor cb: %S %s\r\n"), @@ -1004,7 +1006,7 @@ static void cmd_monitor_parsed(void *parsed_result, void *data) return; } monitor_running = 0; - callout_stop(&cm, &monitor_event); + callout_stop(&xbeeboard.mainloop_cm, &monitor_event); } } @@ -1167,7 +1169,7 @@ static void cmd_monitor_del_parsed(void *parsed_result, void *data) monitor_count --; if (monitor_count == 0) { printf_P(PSTR("Disable monitoring, no more event\r\n")); - callout_stop(&cm, &monitor_event); + callout_stop(&xbeeboard.mainloop_cm, &monitor_event); monitor_running = 0; return; } @@ -1892,7 +1894,7 @@ const parse_ctx_t PROGMEM main_ctx[] = { &cmd_log_show, &cmd_log_type, &cmd_stack_space, - &cmd_scheduler, + &cmd_callout, &cmd_help, &cmd_neigh_del, &cmd_neigh_add, diff --git a/commands_gen.c b/commands_gen.c index 2c1a11d..d09d69e 100644 --- a/commands_gen.c +++ b/commands_gen.c @@ -29,10 +29,6 @@ #include #include -#include - -#include -#include #include #include @@ -113,36 +109,38 @@ const parse_inst_t PROGMEM cmd_bootloader = { }; /**********************************************************/ -/* Scheduler show */ +/* Callout show */ -/* this structure is filled when cmd_scheduler is parsed successfully */ -struct cmd_scheduler_result { +/* this structure is filled when cmd_callout is parsed successfully */ +struct cmd_callout_result { fixed_string_t arg0; fixed_string_t arg1; }; -/* function called when cmd_scheduler is parsed successfully */ -static void cmd_scheduler_parsed(void *parsed_result, void *data) +/* function called when cmd_callout is parsed successfully */ +static void cmd_callout_parsed(void *parsed_result, void *data) { (void)parsed_result; (void)data; - scheduler_dump_events(); - scheduler_stats_dump(); + printf_P(PSTR("intr_cm:\n")); + callout_dump_stats(&xbeeboard.intr_cm); + printf_P(PSTR("mainloop_cm:\n")); + callout_dump_stats(&xbeeboard.mainloop_cm); } -const char PROGMEM str_scheduler_arg0[] = "scheduler"; -const parse_token_string_t PROGMEM cmd_scheduler_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_scheduler_result, arg0, str_scheduler_arg0); -const char PROGMEM str_scheduler_arg1[] = "show"; -const parse_token_string_t PROGMEM cmd_scheduler_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_scheduler_result, arg1, str_scheduler_arg1); +const char PROGMEM str_callout_arg0[] = "callout"; +const parse_token_string_t PROGMEM cmd_callout_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_callout_result, arg0, str_callout_arg0); +const char PROGMEM str_callout_arg1[] = "show"; +const parse_token_string_t PROGMEM cmd_callout_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_callout_result, arg1, str_callout_arg1); -const char PROGMEM help_scheduler[] = "Show scheduler events"; -const parse_inst_t PROGMEM cmd_scheduler = { - .f = cmd_scheduler_parsed, /* function to call */ +const char PROGMEM help_callout[] = "Show callout events"; +const parse_inst_t PROGMEM cmd_callout = { + .f = cmd_callout_parsed, /* function to call */ .data = NULL, /* 2nd arg of func */ - .help_str = help_scheduler, + .help_str = help_callout, .tokens = { /* token list, NULL terminated */ - (PGM_P)&cmd_scheduler_arg0, - (PGM_P)&cmd_scheduler_arg1, + (PGM_P)&cmd_callout_arg0, + (PGM_P)&cmd_callout_arg1, NULL, }, }; diff --git a/main.c b/main.c index 3959ee4..b81e0a8 100644 --- a/main.c +++ b/main.c @@ -46,8 +46,6 @@ #include #include -#include -#include #include #include #include @@ -57,8 +55,7 @@ #include "main.h" struct xbeeboard xbeeboard; -volatile uint16_t global_ms; -struct callout_mgr cm; +volatile uint32_t global_ms; #define TIMEOUT_MS 1000 @@ -517,17 +514,19 @@ static void evt_timeout(struct callout_mgr *cm, struct callout *clt, /* free event */ xbee_unregister_channel(xbee_dev, ctx->channel); + + callout_stop(cm, clt); } void xbee_load_timeout(struct xbee_ctx *ctx) { callout_init(&ctx->timeout, evt_timeout, ctx, 0); - callout_schedule(&cm, &ctx->timeout, TIMEOUT_MS); + callout_schedule(&xbeeboard.mainloop_cm, &ctx->timeout, TIMEOUT_MS); } void xbee_unload_timeout(struct xbee_ctx *ctx) { - callout_stop(&cm, &ctx->timeout); + callout_stop(&xbeeboard.mainloop_cm, &ctx->timeout); } void bootloader(void) @@ -560,7 +559,7 @@ void bootloader(void) void xbee_mainloop(void) { while (1) { - callout_manage(&cm); + callout_manage(&xbeeboard.mainloop_cm); if (xbee_raw) { int16_t c; @@ -601,7 +600,7 @@ void xbee_mainloop(void) /* return time in milliseconds on unsigned 16 bits */ static uint16_t get_time_ms(void) { - return global_ms; + return (uint16_t)global_ms; } static void main_timer_interrupt(void) @@ -611,13 +610,6 @@ static void main_timer_interrupt(void) cpt++; - /* interrupt every 2048 cycles */ - cycles += 2048; - if (cycles >= 12000) { - cycles -= 12000; - global_ms ++; - } - /* LED blink */ if (global_ms & 0x80) LED1_ON(); @@ -629,10 +621,17 @@ static void main_timer_interrupt(void) else BUZZER_OFF(); - /* call scheduler every 682us with interrupt unlocked */ + /* interrupt every 2048 cycles */ + cycles += 2048; + if (cycles >= 12000) { + cycles -= 12000; + global_ms ++; + } + + /* called every 682us (at 12 Mhz), but global_ms is not incremented at + * each call */ sei(); - if ((cpt & 0x3) == 0) - scheduler_interrupt(); + callout_manage(&xbeeboard.intr_cm); } int main(void) @@ -649,17 +648,16 @@ int main(void) fdevopen(cmdline_dev_send, cmdline_dev_recv); xbee_file = fdevopen(xbee_dev_send, xbee_dev_recv); - scheduler_init(); timer_init(); timer0_register_OV_intr(main_timer_interrupt); + callout_mgr_init(&xbeeboard.intr_cm, get_time_ms); + callout_mgr_init(&xbeeboard.mainloop_cm, get_time_ms); + cmdline_init(); spi_servo_init(); beep_init(); - callout_mgr_init(&cm, get_time_ms); - //callout_reset(&cm, &t1, 500, PERIODICAL, do_led_blink, NULL); - /* initialize libxbee */ err = xbee_init(); if (err < 0) diff --git a/main.h b/main.h index f77a535..30a9262 100644 --- a/main.h +++ b/main.h @@ -31,8 +31,6 @@ #include #include -#include -#include #include #include #include @@ -43,8 +41,6 @@ #include "rc_proto.h" #include "spi_servo.h" -extern volatile uint16_t global_ms; - #define NB_LOGS 4 /** ERROR NUMS */ @@ -76,6 +72,11 @@ struct xbeeboard { struct rdline rdl; char prompt[RDLINE_PROMPT_SIZE]; + struct callout_mgr mainloop_cm; + struct callout_mgr intr_cm; + struct callout spi_timer; + struct callout beep_timer; + /* log */ uint8_t logs[NB_LOGS+1]; uint8_t log_level; @@ -99,9 +100,7 @@ extern struct xbee_dev *xbee_dev; extern int xbee_raw; extern int xbee_hexdump; extern int xbee_debug; - -extern struct callout_mgr cm; - +extern volatile uint32_t global_ms; void bootloader(void); diff --git a/rc_proto.c b/rc_proto.c index b04e18c..be30c39 100644 --- a/rc_proto.c +++ b/rc_proto.c @@ -34,8 +34,6 @@ #include -#include -#include #include #include #include diff --git a/spi_servo.c b/spi_servo.c index c180bc9..917769c 100644 --- a/spi_servo.c +++ b/spi_servo.c @@ -4,7 +4,7 @@ #include #include -#include +#include #include "spi_servo.h" #include "main.h" @@ -24,9 +24,6 @@ * callback is unloaded. */ -/* 1 scheduler unit (682us at 12Mhz) */ -#define SPI_EVT_PERIOD (1) - #define PPM_BIT 0x01 #define BYPASS_BIT 0x02 @@ -124,7 +121,7 @@ static void decode_rx_servo(union spi_byte0 byte0, union spi_byte1 byte1) } /* called by the scheduler */ -static void spi_servo_cb(void *arg) +static void spi_servo_cb(struct callout_mgr *cm, struct callout *tim, void *arg) { uint8_t idx; union spi_byte0 byte0; @@ -147,13 +144,13 @@ static void spi_servo_cb(void *arg) if (spi_servo_tx.next_byte != 0) { spi_send_byte(spi_servo_tx.next_byte); spi_servo_tx.next_byte = 0; - return; + goto reschedule; } /* if there is no updated servo, send 0 and return. */ if (spi_servo_tx.cmd_mask == 0) { spi_send_byte(0); - return; + goto reschedule; } /* else find it and send it */ @@ -170,6 +167,11 @@ static void spi_servo_cb(void *arg) spi_send_one_servo(idx, spi_servo_tx.servo[idx]); spi_servo_tx.cmd_mask &= (~(1 << idx)); spi_servo_tx.cur_idx = idx; + + reschedule: + /* don't use callout_reschedule() here, we want to schedule in one tick + * relative to current time: 1 tick is 682us at 12Mhz */ + callout_schedule(cm, tim, 0); } void spi_servo_init(void) @@ -185,8 +187,9 @@ void spi_servo_init(void) SS_HIGH(); - scheduler_add_periodical_event_priority(&spi_servo_cb, NULL, - SPI_EVT_PERIOD, SPI_PRIO); + callout_init(&xbeeboard.spi_timer, spi_servo_cb, NULL, SPI_PRIO); + callout_schedule(&xbeeboard.intr_cm, + &xbeeboard.spi_timer, 0); /* immediate */ spi_servo_set_bypass(1); } -- 2.39.5