#include <aversive.h>
#include <aversive/wait.h>
-#include <scheduler.h>
#include <cirbuf.h>
#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];
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);
}
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)
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);
}
#include <parse.h>
#include <rdline.h>
#include <uart.h>
-#include <clock_time.h>
#include "callout.h"
#include "main.h"
* 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)
}
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"));
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;
if (range_cur_count == 0) {
range_running = 0;
- return;
+ callout_stop(cm, clt);
}
callout_reschedule(cm, clt, range_period_ms);
}
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")) {
return;
}
range_running = 0;
- callout_stop(&cm, &range_event);
+ callout_stop(&xbeeboard.mainloop_cm, &range_event);
}
}
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"),
return;
}
monitor_running = 0;
- callout_stop(&cm, &monitor_event);
+ callout_stop(&xbeeboard.mainloop_cm, &monitor_event);
}
}
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;
}
&cmd_log_show,
&cmd_log_type,
&cmd_stack_space,
- &cmd_scheduler,
+ &cmd_callout,
&cmd_help,
&cmd_neigh_del,
&cmd_neigh_add,
#include <aversive/queue.h>
#include <uart.h>
-#include <clock_time.h>
-
-#include <scheduler.h>
-#include <scheduler_stats.h>
#include <rdline.h>
#include <parse.h>
};
/**********************************************************/
-/* 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,
},
};
#include <errno.h>
#include <ctype.h>
-#include <scheduler.h>
-#include <clock_time.h>
#include <parse.h>
#include <rdline.h>
#include <timer.h>
#include "main.h"
struct xbeeboard xbeeboard;
-volatile uint16_t global_ms;
-struct callout_mgr cm;
+volatile uint32_t global_ms;
#define TIMEOUT_MS 1000
/* 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)
void xbee_mainloop(void)
{
while (1) {
- callout_manage(&cm);
+ callout_manage(&xbeeboard.mainloop_cm);
if (xbee_raw) {
int16_t c;
/* 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)
cpt++;
- /* interrupt every 2048 cycles */
- cycles += 2048;
- if (cycles >= 12000) {
- cycles -= 12000;
- global_ms ++;
- }
-
/* LED blink */
if (global_ms & 0x80)
LED1_ON();
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)
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)
#include <aversive/error.h>
#include <uart.h>
-#include <scheduler.h>
-#include <clock_time.h>
#include <parse.h>
#include <rdline.h>
#include <timer.h>
#include "rc_proto.h"
#include "spi_servo.h"
-extern volatile uint16_t global_ms;
-
#define NB_LOGS 4
/** ERROR NUMS */
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;
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);
#include <uart.h>
-#include <scheduler.h>
-#include <clock_time.h>
#include <parse.h>
#include <rdline.h>
#include <timer.h>
#include <aversive.h>
#include <aversive/wait.h>
-#include <scheduler.h>
+#include <callout.h>
#include "spi_servo.h"
#include "main.h"
* callback is unloaded.
*/
-/* 1 scheduler unit (682us at 12Mhz) */
-#define SPI_EVT_PERIOD (1)
-
#define PPM_BIT 0x01
#define BYPASS_BIT 0x02
}
/* 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;
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 */
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)
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);
}