diff --git a/clock.c b/clock.c index 860caa0..e28d632 100644 --- a/clock.c +++ b/clock.c @@ -17,6 +17,46 @@ #endif #include "memory_barrier.h" + +/// Every time our clock fires we increment this, +/// so we know when 10ms has elapsed. +uint8_t clock_counter_10ms = 0; +/// Keep track of when 250ms has elapsed. +uint8_t clock_counter_250ms = 0; +/// Keep track of when 1s has elapsed. +uint8_t clock_counter_1s = 0; + +/// Flags to tell main loop when above have elapsed. +volatile uint8_t clock_flag_10ms = 0; +volatile uint8_t clock_flag_250ms = 0; +volatile uint8_t clock_flag_1s = 0; + + +/** Advance our clock by a tick. + + Update clock counters accordingly. Should be called from the TICK_TIME + Interrupt in timer.c. +*/ +void clock_tick(void) { + clock_counter_10ms += TICK_TIME_MS; + if (clock_counter_10ms >= 10) { + clock_counter_10ms -= 10; + clock_flag_10ms = 1; + + clock_counter_250ms++; + if (clock_counter_250ms >= 25) { + clock_counter_250ms = 0; + clock_flag_250ms = 1; + + clock_counter_1s++; + if (clock_counter_1s >= 4) { + clock_counter_1s = 0; + clock_flag_1s = 1; + } + } + } +} + /*! do stuff every 1/4 second called from clock_10ms(), do not call directly diff --git a/clock.h b/clock.h index c0266b8..9cd55cd 100644 --- a/clock.h +++ b/clock.h @@ -1,6 +1,21 @@ #ifndef _CLOCK_H #define _CLOCK_H +#include + + +extern volatile uint8_t clock_flag_10ms; +extern volatile uint8_t clock_flag_250ms; +extern volatile uint8_t clock_flag_1s; + + +// If the specific bit is set, execute the following block exactly once +// and then clear the flag. +#define ifclock(F) for ( ; F; F = 0) + +// Should be called every TICK_TIME (currently 2 ms). +void clock_tick(void); + void clock(void); #endif /* _CLOCK_H */ diff --git a/timer.c b/timer.c index 18f061a..d0aa9ca 100644 --- a/timer.c +++ b/timer.c @@ -17,11 +17,13 @@ #include "arduino.h" #include "config_wrapper.h" +#include "clock.h" #ifdef MOTHERBOARD #include "dda_queue.h" #endif + /// time until next step, as output compare register is too small for long step times uint32_t next_step_time; @@ -30,44 +32,13 @@ uint32_t next_step_time; uint32_t step_extra_time = 0; #endif /* ACCELERATION_TEMPORAL */ -/// every time our clock fires, we increment this so we know when 10ms has elapsed -uint8_t clock_counter_10ms = 0; -/// keep track of when 250ms has elapsed -uint8_t clock_counter_250ms = 0; -/// keep track of when 1s has elapsed -uint8_t clock_counter_1s = 0; - -/// flags to tell main loop when above have elapsed -volatile uint8_t clock_flag_10ms = 0; -volatile uint8_t clock_flag_250ms = 0; -volatile uint8_t clock_flag_1s = 0; /// comparator B is the system clock, happens every TICK_TIME ISR(TIMER1_COMPB_vect) { // set output compare register to the next clock tick OCR1B = (OCR1B + TICK_TIME) & 0xFFFF; - /* - clock stuff - */ - clock_counter_10ms += TICK_TIME_MS; - if (clock_counter_10ms >= 10) { - clock_counter_10ms -= 10; - clock_flag_10ms = 1; - - clock_counter_250ms++; - if (clock_counter_250ms >= 25) { - clock_counter_250ms = 0; - clock_flag_250ms = 1; - - clock_counter_1s++; - if (clock_counter_1s >= 4) { - clock_counter_1s = 0; - clock_flag_1s = 1; - } - } - } - + clock_tick(); dda_clock(); } diff --git a/timer.h b/timer.h index 9bfd487..0154191 100644 --- a/timer.h +++ b/timer.h @@ -7,6 +7,7 @@ #endif #include "simulator.h" + // time-related constants #define US * (F_CPU / 1000000) #define MS * (F_CPU / 1000) @@ -19,16 +20,6 @@ /// properly if you change TICK_TIME. #define TICK_TIME_MS (TICK_TIME / (F_CPU / 1000)) -/* -clock stuff -*/ -extern volatile uint8_t clock_flag_10ms; -extern volatile uint8_t clock_flag_250ms; -extern volatile uint8_t clock_flag_1s; - -// If the specific bit is set, execute the following block exactly once -// and then clear the flag. -#define ifclock(F) for (;F;F=0 ) /* timer stuff