1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-21 15:25:36 +03:00

Improvement to the way that timers are forced to overflow, which should allow PPM on CC3D to work properly.

This commit is contained in:
Ben Hitchcock 2014-11-30 08:08:34 +08:00
parent c0ed21a818
commit 5096873ab1
2 changed files with 13 additions and 14 deletions

View file

@ -37,7 +37,7 @@ typedef void (*pwmWriteFuncPtr)(uint8_t index, uint16_t value); // function poi
typedef struct { typedef struct {
volatile timCCR_t *ccr; volatile timCCR_t *ccr;
volatile timCNT_t *cnt; volatile TIM_TypeDef *tim;
uint16_t period; uint16_t period;
pwmWriteFuncPtr pwmWritePtr; pwmWriteFuncPtr pwmWritePtr;
} pwmOutputPort_t; } pwmOutputPort_t;
@ -46,7 +46,6 @@ static pwmOutputPort_t pwmOutputPorts[MAX_PWM_OUTPUT_PORTS];
static pwmOutputPort_t *motors[MAX_PWM_MOTORS]; static pwmOutputPort_t *motors[MAX_PWM_MOTORS];
static pwmOutputPort_t *servos[MAX_PWM_SERVOS]; static pwmOutputPort_t *servos[MAX_PWM_SERVOS];
static volatile uint16_t* lastCounterPtr;
#define PWM_BRUSHED_TIMER_MHZ 8 #define PWM_BRUSHED_TIMER_MHZ 8
@ -120,7 +119,7 @@ static pwmOutputPort_t *pwmOutConfig(const timerHardware_t *timerHardware, uint8
break; break;
} }
p->period = period; p->period = period;
p->cnt = &timerHardware->tim->CNT; p->tim = timerHardware->tim;
return p; return p;
} }
@ -144,20 +143,20 @@ void pwmWriteMotor(uint8_t index, uint16_t value)
void pwmFinishedWritingMotors(uint8_t numberMotors) void pwmFinishedWritingMotors(uint8_t numberMotors)
{ {
uint8_t index; uint8_t index;
volatile TIM_TypeDef *lastTimerPtr = NULL;
if(feature(FEATURE_ONESHOT125)){ if(feature(FEATURE_ONESHOT125)){
for(index = 0; index < numberMotors; index++){ for(index = 0; index < numberMotors; index++){
// Force the counter to overflow if it's the first motor to output, or if we change timers
if((index == 0) || (motors[index]->cnt != lastCounterPtr)){
lastCounterPtr = motors[index]->cnt;
*motors[index]->cnt = 0xfffe; // Force the timer to overflow if it's the first motor to output, or if we change timers
} if(motors[index]->tim != lastTimerPtr){
} lastTimerPtr = motors[index]->tim;
// Wait until the timers have overflowed (should take less than 0.125 uS) // Force an overflow by setting the UG bit
while(*motors[numberMotors - 1]->cnt >= 0xfffe){ motors[index]->tim->EGR |= 0x0001;
}
} }
// Set the compare register to 0, which stops the output pulsing if the timer overflows before the main loop completes again. // Set the compare register to 0, which stops the output pulsing if the timer overflows before the main loop completes again.

View file

@ -82,7 +82,7 @@ static uint16_t captures[PWM_PORTS_OR_PPM_CAPTURE_COUNT];
static uint8_t ppmFrameCount = 0; static uint8_t ppmFrameCount = 0;
static uint8_t lastPPMFrameCount = 0; static uint8_t lastPPMFrameCount = 0;
static uint8_t ppmCountDivisor = 1; static uint8_t ppmCountShift = 0;
typedef struct ppmDevice { typedef struct ppmDevice {
uint8_t pulseIndex; uint8_t pulseIndex;
@ -161,7 +161,7 @@ static void ppmEdgeCallback(timerCCHandlerRec_t* cbRec, captureCompare_t capture
ppmDev.currentTime += ppmDev.largeCounter; ppmDev.currentTime += ppmDev.largeCounter;
// Divide by 8 if Oneshot125 is active and this is a CC3D board // Divide by 8 if Oneshot125 is active and this is a CC3D board
ppmDev.currentTime = ppmDev.currentTime / ppmCountDivisor; ppmDev.currentTime = ppmDev.currentTime >> ppmCountShift;
/* Capture computation */ /* Capture computation */
ppmDev.deltaTime = ppmDev.currentTime - ppmDev.previousTime; ppmDev.deltaTime = ppmDev.currentTime - ppmDev.previousTime;
@ -332,7 +332,7 @@ void ppmInConfig(const timerHardware_t *timerHardwarePtr)
timerConfigure(timerHardwarePtr, (uint16_t)PPM_TIMER_PERIOD, PWM_TIMER_MHZ); timerConfigure(timerHardwarePtr, (uint16_t)PPM_TIMER_PERIOD, PWM_TIMER_MHZ);
if((timerHardwarePtr->tim == TIM4) && (feature(FEATURE_ONESHOT125))){ if((timerHardwarePtr->tim == TIM4) && (feature(FEATURE_ONESHOT125))){
ppmCountDivisor = 8; ppmCountShift = 3; // Divide by 8 if the timer is running at 8 MHz
} }