Generate run_timers with correct timing

This commit is contained in:
jeltz 2020-12-25 23:05:39 +01:00
parent 5346c720d2
commit 74f5e9a2e8
Signed by: jeltz
GPG Key ID: 800882B66C0C3326
1 changed files with 20 additions and 4 deletions

View File

@ -52,10 +52,23 @@ struct
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
| Timer_ms ms -> ms / base
let incr_mod name modulo =
let one_const = Cconst (Ccint 1) in
let modulo_const = Cconst (Ccint modulo) in
let incr = Cbop ("+", Cvar name, one_const) in
Caffect (CLvar name, Cbop ("%", incr, modulo_const))
let call_step_async base tick_var (od, ack) = let call_step_async base 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
Csexpr (Cfun_call (step, [Caddrof (Cvar global)])) let call = Csexpr (Cfun_call (step, [Caddrof (Cvar global)])) in
let zero = Cconst (Ccint 0) in
let timer = Cconst (Ccint (count_of_ack base ack)) in
let cond = Cbop ("==", Cbop ("%", tick_var, timer), zero) in
Cif (cond, [call], [])
let decls_and_defs objs = let decls_and_defs objs =
let trans = List.map let trans = List.map
@ -68,13 +81,16 @@ struct
(fun (_, ack) -> match ack with Timer_ms ms -> ms) (fun (_, ack) -> match ack with Timer_ms ms -> ms)
trans trans
in in
let tick = "mod_ticks" in
let body = match timers with let body = match timers with
| [] -> [] | [] -> []
| _ -> | _ ->
let base_timer = fold_gcd timers in let base_timer = fold_gcd timers in
let max_timer = fold_lcm timers in let max_timer = fold_lcm timers in
List.map (call_step_async base_timer (Cvar tick)) trans let steps =
List.map (call_step_async base_timer (Cvar "tick")) trans
in
let incr = incr_mod "tick" (max_timer / base_timer) in
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
* I don't know how to generate here) *) * I don't know how to generate here) *)
@ -85,7 +101,7 @@ struct
f_args = []; f_args = [];
f_body = { f_body = {
var_decls = [ var_decls = [
mk_vardecl_val ~static:true tick Cty_int (Cconst (Ccint 0)) mk_vardecl_val ~static:true "tick" Cty_int (Cconst (Ccint 0))
]; ];
block_body = body block_body = body
} }