Skip to content

Commit

Permalink
one handler setter and clearer for all timers #49
Browse files Browse the repository at this point in the history
  • Loading branch information
SergNikitin committed May 18, 2014
1 parent 826fc7d commit 2a25aa3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ void syncControlInterruptHadler(void) {
}

void enableSyncControl() {
setTimer0IntrHandler(&syncControlInterruptHadler);
setTimerIntrHandler(0, &syncControlInterruptHadler);
}

void disableSyncControl() {
clearTimer0IntrHandler();
clearTimerIntrHandler(0);
}
43 changes: 20 additions & 23 deletions firmware/SatStepperBegin/timers.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@
#include "utils/macros.h"
#include "timers.h"

static volatile struct CPUTIMER_REGS* const timerRegs[3] = {
static volatile struct CPUTIMER_REGS* const timerRegs[ALL_TIMERS] = {
&CpuTimer0Regs,
&CpuTimer1Regs,
&CpuTimer2Regs
};

static _controlTimerInterruptHandler _tmr0Handler;
static _controlTimerInterruptHandler _tmr1Handler;
static _controlTimerInterruptHandler _tmrHandlers[ALL_TIMERS];

void emptyTimerIntrHandler(){}
static void emptyTimerIntrHandler() {}

interrupt void TMR0_Interrupt(void)
{
_tmr0Handler();
_tmrHandlers[0]();
// CpuTimer0Regs.TCR.bit.TIF = 0; // TODO: writes of 0 are ignored, check it

// TIMH:TIM is loaded with the value in the PRDH:PRD,
Expand All @@ -37,7 +36,7 @@ interrupt void TMR0_Interrupt(void)

interrupt void TMR1_Interrupt(void)
{
_tmr1Handler();
_tmrHandlers[1]();
// CpuTimer1Regs.TCR.bit.TIF = 0;
CpuTimer1Regs.TCR.bit.TRB = 1;

Expand All @@ -58,8 +57,8 @@ static inline void setTimerSettingsToDefaultByNum(uint_fast8_t timerNum) {
timerRegs[timerNum]->TPRH.bit.TDDRH = 0x0;
}

void timer0Init() {
clearTimer0IntrHandler();
static void timer0Init() {
clearTimerIntrHandler(0);

EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TINT0 = TMR0_Interrupt;
Expand All @@ -72,8 +71,8 @@ void timer0Init() {
setTimerSettingsToDefaultByNum(0);
}

void timer1Init() {
clearTimer1IntrHandler();
static void timer1Init() {
clearTimerIntrHandler(1);

EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TINT1 = &TMR1_Interrupt;
Expand All @@ -85,24 +84,22 @@ void timer1Init() {
setTimerSettingsToDefaultByNum(1);
}

void setTimer0IntrHandler(_controlTimerInterruptHandler handler) {
if (handler != NULL) {
_tmr0Handler = handler;
}
}

void clearTimer0IntrHandler() {
_tmr0Handler = &emptyTimerIntrHandler;
void timersInit() {
timer0Init();
timer1Init();
}

void setTimer1IntrHandler(_controlTimerInterruptHandler handler) {
if (handler != NULL) {
_tmr1Handler = handler;
void setTimerIntrHandler( uint_fast8_t timerNum,
_controlTimerInterruptHandler handler) {
if (handler != NULL && timerNum < ALL_TIMERS) {
_tmrHandlers[timerNum] = handler;
}
}

void clearTimer1IntrHandler() {
_tmr1Handler = &emptyTimerIntrHandler;
void clearTimerIntrHandler(uint_fast8_t timerNum) {
if (timerNum < ALL_TIMERS) {
_tmrHandlers[timerNum] = &emptyTimerIntrHandler;
}
}

void setTimerPeriodByNum(uint_fast8_t timerNum, uint32_t periodInUsec) {
Expand Down
13 changes: 6 additions & 7 deletions firmware/SatStepperBegin/timers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@

#include "stdint.h"

typedef void (* _controlTimerInterruptHandler)(void);
#define ALL_TIMERS 3

void timer0Init();
void timer1Init();
typedef void (* _controlTimerInterruptHandler)(void);

void setTimer0IntrHandler(_controlTimerInterruptHandler);
void clearTimer0IntrHandler();
void timersInit();

void setTimer1IntrHandler(_controlTimerInterruptHandler);
void clearTimer1IntrHandler();
void setTimerIntrHandler( uint_fast8_t timerNum,
_controlTimerInterruptHandler handler);
void clearTimerIntrHandler(uint_fast8_t timerNum);

void setTimerPeriodByNum(uint_fast8_t timerNum, uint32_t periodInUsec);
void stopTimerByNum(uint_fast8_t timerNum);
Expand Down

0 comments on commit 2a25aa3

Please sign in to comment.