Skip to content

Commit

Permalink
restruct timer handler setter functions #49
Browse files Browse the repository at this point in the history
  • Loading branch information
akindyakov committed May 18, 2014
2 parents a0c4c82 + 43da75b commit 479335d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
6 changes: 3 additions & 3 deletions firmware/SatStepperBegin/control_algo.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ void stop() {
}

void step(int_fast8_t dist) {
gState.stepTicker += dist;

if (currentAlgo == NULL) {
return;
}

int_fast8_t nextStep = gState.stepTicker % currentAlgo->algoStepsNumber;
gState.stepTicker += dist;

int_fast8_t nextStep = gState.stepTicker % currentAlgo->algoStepsNumber;
if (nextStep < 0) {
nextStep += currentAlgo->algoStepsNumber;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ void syncControlInterruptHadler(void) {
}

void enableSyncControl() {
timer0Init(&syncControlInterruptHadler);
installTimer0IntrHandler(&syncControlInterruptHadler);
}

void disableSyncControl() {
timer0Init(&emptyTimerIntrHandler);
installTimer0IntrHandler(&emptyTimerIntrHandler);
}
18 changes: 9 additions & 9 deletions firmware/SatStepperBegin/pwm_wrap_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

#include "pwm_wrap_module.h"

#define MAX_PWM_DUTY 1024
static int16_t pmwCoefficient = 0;
static int16_t pwmCoefficient = 0;

static volatile struct EPWM_REGS* pwm_control_regs[2] = {
&EPwm1Regs,
Expand All @@ -25,10 +24,10 @@ static inline void initPWMChannel(volatile struct EPWM_REGS* pwmRegs) {
pwmRegs->TBCTL.bit.SYNCOSEL = 0; // Pass through

// Init Timer-Base Period Register for EPWM1-EPWM3
pwmRegs->TBPRD = MAX_PWM_DUTY;
pwmRegs->TBPRD = MAX_PWM;

// Init Compare Register for EPWM1-EPWM3
pwmRegs->CMPA.half.CMPA = MAX_PWM_DUTY;
pwmRegs->CMPA.half.CMPA = MAX_PWM;

// Init Timer-Base Phase Register for EPWM1-EPWM3
pwmRegs->TBPHS.half.TBPHS = 0;
Expand Down Expand Up @@ -97,10 +96,11 @@ void setPwm(uint16_t pwm) {
if (pwm > MAX_PWM) {
pwm = MAX_PWM;
}
uint32_t truePwm = pwm;
truePwm *= pmwCoefficient;
truePwm >>= PWM_COEFF_RANK;
uint16_t pwmDutyCycle = MAX_PWM - truePwm;

uint32_t algoStepPwm = pwm;
algoStepPwm *= pwmCoefficient;
algoStepPwm >>= PWM_COEFF_RANK;
uint16_t pwmDutyCycle = MAX_PWM - algoStepPwm;

EPwm1Regs.CMPA.half.CMPA = pwmDutyCycle;
EPwm2Regs.CMPA.half.CMPA = pwmDutyCycle;
Expand Down Expand Up @@ -141,7 +141,7 @@ void setBDirection(int_fast8_t direct) {
}

void setCoeff(uint16_t pwmCoeff) {
pmwCoefficient = pwmCoeff;
pwmCoefficient = pwmCoeff;
}

void deactivate_pwm_driver() {
Expand Down
2 changes: 1 addition & 1 deletion firmware/SatStepperBegin/pwm_wrap_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
void initPwm();

#define MAX_PWM 1024
void setPwm(uint16_t pwmDutyCycle);
void setPwm(uint16_t pwm);

#define PWM_COEFF_RANK 10
#define PWM_COEFF_DIVIDER (1 << PWM_COEFF_RANK)
Expand Down
12 changes: 10 additions & 2 deletions firmware/SatStepperBegin/timers.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ interrupt void TMR0_Interrupt(void)
// PSCH:PSC is loaded with the value in the TDDRH:TDDR.
CpuTimer0Regs.TCR.bit.TRB = 1;

// Acknowledge interrupt to recieve more interrupts from PIE group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
ACKNOWLEDGE_ONE_MORE_INTERRUPT_FROM_GROUP(PIEACK_GROUP1);
}

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

// TINT1 is not part of any interrupt group, so it doesn't require
// acknowledgement
}
Expand Down Expand Up @@ -85,17 +85,25 @@ void timer1Init() {
setTimerSettingsToDefaultByNum(1);
}

<<<<<<< HEAD
void setTimer0IntrHandler(_controlTimerInterruptHandler handler) {
=======
void installTimer0IntrHandler(_controlTimerInterruptHandler handler) {
>>>>>>> 43da75bbeb3629a01497d820b8bf6a3439c3e1d6
if (handler != NULL) {
_tmr0Handler = handler;
}
}

<<<<<<< HEAD
void clearTimer0IntrHandler() {
_tmr0Handler = &emptyTimerIntrHandler;
}

void setTimer1IntrHandler(_controlTimerInterruptHandler handler) {
=======
void installTimer1IntrHandler(_controlTimerInterruptHandler handler) {
>>>>>>> 43da75bbeb3629a01497d820b8bf6a3439c3e1d6
if (handler != NULL) {
_tmr1Handler = handler;
}
Expand Down
5 changes: 5 additions & 0 deletions firmware/SatStepperBegin/timers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ typedef void (* _controlTimerInterruptHandler)(void);
void timer0Init();
void timer1Init();

<<<<<<< HEAD
void setTimer0IntrHandler(_controlTimerInterruptHandler);
void clearTimer0IntrHandler();

void setTimer1IntrHandler(_controlTimerInterruptHandler);
void clearTimer1IntrHandler();
=======
void installTimer0IntrHandler(_controlTimerInterruptHandler);
void installTimer1IntrHandler(_controlTimerInterruptHandler);
>>>>>>> 43da75bbeb3629a01497d820b8bf6a3439c3e1d6

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

0 comments on commit 479335d

Please sign in to comment.