Move clock stuff from timer.c/.h to clock.c/.h.
I guess that's where it belongs.
This commit is contained in:
parent
fc7365b320
commit
f26623d173
40
clock.c
40
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
|
||||
|
|
|
|||
15
clock.h
15
clock.h
|
|
@ -1,6 +1,21 @@
|
|||
#ifndef _CLOCK_H
|
||||
#define _CLOCK_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
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 */
|
||||
|
|
|
|||
35
timer.c
35
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();
|
||||
}
|
||||
|
||||
|
|
|
|||
11
timer.h
11
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue