Compare commits

...

2 Commits

@ -43,16 +43,14 @@ struct
| 0 -> abs a | 0 -> abs a
| b -> gcd b (a mod b) | b -> gcd b (a mod b)
let fold_gcd l = List.fold_left gcd (List.hd l) (List.tl l)
let lcm a b = match a, b with let lcm a b = match a, b with
| 0, _ | _, 0 -> 0 | 0, _ | _, 0 -> 0
| _ -> abs (a * b) / (gcd a b) | _ -> abs (a * b) / (gcd a b)
let fold_lcm l = List.fold_left lcm (List.hd l) (List.tl l) let fold_lcm l = List.fold_left lcm (List.hd l) (List.tl l)
let count_of_ack base = function let ms_of_ack = function
| Timer_ms ms -> ms / base | Timer_ms ms -> ms
let incr_mod name modulo = let incr_mod name modulo =
let one_const = Cconst (Ccint 1) in let one_const = Cconst (Ccint 1) in
@ -60,12 +58,12 @@ struct
let incr = Cbop ("+", Cvar name, one_const) in let incr = Cbop ("+", Cvar name, one_const) in
Caffect (CLvar name, Cbop ("%", incr, modulo_const)) Caffect (CLvar name, Cbop ("%", incr, modulo_const))
let call_step_async base tick_var (od, ack) = let call_step_async tick_var (od, ack) =
let step = (cname_of_qn od.o_class) ^ "_async_step" in let step = (cname_of_qn od.o_class) ^ "_async_step" in
let global = async_global_var_name od in let global = async_global_var_name od in
let call = Csexpr (Cfun_call (step, [Caddrof (Cvar global)])) in let call = Csexpr (Cfun_call (step, [Caddrof (Cvar global)])) in
let zero = Cconst (Ccint 0) in let zero = Cconst (Ccint 0) in
let timer = Cconst (Ccint (count_of_ack base ack)) in let timer = Cconst (Ccint (ms_of_ack ack)) in
let cond = Cbop ("==", Cbop ("%", tick_var, timer), zero) in let cond = Cbop ("==", Cbop ("%", tick_var, timer), zero) in
Cif (cond, [call], []) Cif (cond, [call], [])
@ -83,12 +81,11 @@ struct
let body = match timers with let body = match timers with
| [] -> [] | [] -> []
| _ -> | _ ->
let base_timer = fold_gcd timers in let lcm_timer = fold_lcm timers in
let max_timer = fold_lcm timers in
let steps = let steps =
List.map (call_step_async base_timer (Cvar "tick")) trans List.map (call_step_async (Cvar "tick")) trans
in in
let incr = incr_mod "tick" (max_timer / base_timer) in let incr = incr_mod "tick" lcm_timer in
steps @ [incr] steps @ [incr]
in in
(* run_timers is declared in avr.h (because of the ISR macro which (* run_timers is declared in avr.h (because of the ISR macro which

@ -32,7 +32,7 @@
#include <avr/io.h> #include <avr/io.h>
#include "avr.h" #include "avr.h"
ISR(TIMER0_OVF_vect) { ISR(TIMER1_COMPA_vect) {
run_timers(); run_timers();
} }
@ -50,7 +50,7 @@ void init_timer1() {
} }
void atomic_memcpy(void *dest, const void *src, size_t size) { void atomic_memcpy(void *dest, const void *src, size_t size) {
ATOMIC_BLOCK(ATOMIC_FORCEON) { ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
memcpy(dest, src, size); memcpy(dest, src, size);
} }
} }

Loading…
Cancel
Save