2011-11-12 23:02:24 -04:00
|
|
|
|
|
|
|
#include "AP_TimerProcess.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "WConstants.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
int AP_TimerProcess::_period;
|
2011-12-09 01:27:50 -04:00
|
|
|
ap_procedure AP_TimerProcess::_proc[AP_TIMERPROCESS_MAX_PROCS];
|
|
|
|
int AP_TimerProcess::_pidx = 0;
|
2011-11-12 23:02:24 -04:00
|
|
|
|
|
|
|
AP_TimerProcess::AP_TimerProcess(int period)
|
|
|
|
{
|
|
|
|
_period = period;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_TimerProcess::init( Arduino_Mega_ISR_Registry * isr_reg )
|
|
|
|
{
|
|
|
|
// Enable Timer2 Overflow interrupt to trigger process.
|
|
|
|
TIMSK2 = 0; // Disable interrupts
|
|
|
|
TCCR2A = 0; // normal counting mode
|
|
|
|
TCCR2B = _BV(CS21) | _BV(CS22); // Set prescaler of clk/256
|
|
|
|
TCNT2 = 0; // Set count to zero, so it goes off right away.
|
|
|
|
TIFR2 = _BV(TOV2); // clear pending interrupts;
|
|
|
|
TIMSK2 = _BV(TOIE2); // enable the overflow interrupt
|
|
|
|
|
2011-12-09 01:27:50 -04:00
|
|
|
for (int i = 0; i < AP_TIMERPROCESS_MAX_PROCS; i++)
|
|
|
|
_proc[i] = NULL;
|
|
|
|
|
2011-11-12 23:02:24 -04:00
|
|
|
isr_reg->register_signal( ISR_REGISTRY_TIMER2_OVF, AP_TimerProcess::run);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_TimerProcess::register_process(void (*proc)(void) )
|
|
|
|
{
|
2011-12-09 01:27:50 -04:00
|
|
|
if (_pidx < AP_TIMERPROCESS_MAX_PROCS)
|
|
|
|
_proc[_pidx++] = proc;
|
2011-11-12 23:02:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_TimerProcess::run(void)
|
|
|
|
{
|
|
|
|
TCNT2 = _period;
|
2011-12-09 01:27:50 -04:00
|
|
|
for (int i = 0; i < _pidx; i++) {
|
|
|
|
if (_proc[i] != NULL)
|
|
|
|
_proc[i]();
|
|
|
|
}
|
2011-11-12 23:02:24 -04:00
|
|
|
}
|