Clean up PWM stick reads, remove double averaging (#5995)

We already do averaging in the normal anaIn/getADC().
Also according to Mike's and my own research we don't need the averaging.

Tested on two X-Lites with PMW sticks
This commit is contained in:
Arne Schwabe
2018-06-27 16:10:27 +02:00
committed by Bertrand Songis
parent 17b4aa2466
commit 968e7233e5
3 changed files with 15 additions and 36 deletions
@@ -20,14 +20,9 @@
#include "opentx.h" #include "opentx.h"
#define TIMESAMPLE_COUNT 6 bool sticks_pwm_disabled = false;
uint8_t sticks_pwm_disabled = 0;
volatile uint32_t pwm_interrupt_count; volatile uint32_t pwm_interrupt_count;
volatile uint8_t timer_capture_states[4]; volatile uint16_t timer_capture_values[NUM_PWMSTICKS];
volatile uint32_t timer_capture_rising_time[4];
volatile uint32_t timer_capture_values[4][TIMESAMPLE_COUNT];
volatile uint8_t timer_capture_indexes[4];
void sticksPwmInit() void sticksPwmInit()
{ {
@@ -57,7 +52,7 @@ void sticksPwmInit()
NVIC_SetPriority(PWM_IRQn, 10); NVIC_SetPriority(PWM_IRQn, 10);
} }
inline uint32_t TIM_GetCapture(uint8_t n) inline uint32_t TIM_GetCapture_Stick(uint8_t n)
{ {
switch (n) { switch (n) {
case 0: case 0:
@@ -98,15 +93,17 @@ inline uint32_t diff_with_16bits_overflow(uint32_t a, uint32_t b)
extern "C" void PWM_IRQHandler(void) extern "C" void PWM_IRQHandler(void)
{ {
for (int i=0; i<4; i++) { static uint8_t timer_capture_states[NUM_PWMSTICKS];
static uint32_t timer_capture_rising_time[NUM_PWMSTICKS];
for (uint8_t i=0; i<NUM_PWMSTICKS; i++) {
if (PWM_TIMER->SR & (TIM_DIER_CC1IE << i)) { if (PWM_TIMER->SR & (TIM_DIER_CC1IE << i)) {
uint32_t capture = TIM_GetCapture(i); uint32_t capture = TIM_GetCapture_Stick(i);
pwm_interrupt_count++; // overflow may happen but we only use this to detect PWM / ADC on radio startup pwm_interrupt_count++; // overflow may happen but we only use this to detect PWM / ADC on radio startup
if (timer_capture_states[i] != 0) { if (timer_capture_states[i] != 0) {
uint32_t value = diff_with_16bits_overflow(timer_capture_rising_time[i], capture); uint32_t value = diff_with_16bits_overflow(timer_capture_rising_time[i], capture);
if (value < 10000) { if (value < 10000) {
timer_capture_values[i][timer_capture_indexes[i]++] = value; timer_capture_values[i] = (uint16_t) value;
timer_capture_indexes[i] %= TIMESAMPLE_COUNT;
} }
TIM_SetPolarityRising(i); TIM_SetPolarityRising(i);
timer_capture_states[i] = 0; timer_capture_states[i] = 0;
@@ -123,26 +120,8 @@ extern "C" void PWM_IRQHandler(void)
void sticksPwmRead(uint16_t * values) void sticksPwmRead(uint16_t * values)
{ {
uint32_t tmp[4]; values[0] = timer_capture_values[0];
values[1] = timer_capture_values[1];
for (int i=0; i<4; i++) { values[2] = timer_capture_values[3];
uint32_t mycount = 0; values[3] = timer_capture_values[2];
uint32_t mymax = 0;
uint32_t mymin = 4095;
for (uint32_t j=0; j<TIMESAMPLE_COUNT; j++) {
uint32_t value = timer_capture_values[i][j];
mycount += value;
if (value > mymax)
mymax = value;
if (value < mymin)
mymin = value;
}
// from the 6 values, remove the min and max and divide by 4
tmp[i] = (mycount - mymax - mymin) >> 2;
}
values[0] = tmp[0];
values[1] = tmp[1];
values[2] = tmp[3];
values[3] = tmp[2];
} }
+1 -1
View File
@@ -409,7 +409,7 @@ uint16_t getAnalogValue(uint8_t index);
#endif #endif
#if NUM_PWMSTICKS > 0 #if NUM_PWMSTICKS > 0
extern uint8_t sticks_pwm_disabled; extern bool sticks_pwm_disabled;
#define STICKS_PWM_ENABLED() (sticks_pwm_disabled == false) #define STICKS_PWM_ENABLED() (sticks_pwm_disabled == false)
void sticksPwmInit(void); void sticksPwmInit(void);
void sticksPwmRead(uint16_t * values); void sticksPwmRead(uint16_t * values);
+1 -1
View File
@@ -445,7 +445,7 @@ enum Analogs {
#if defined(PCBXLITE) #if defined(PCBXLITE)
#define NUM_PWMSTICKS 4 #define NUM_PWMSTICKS 4
extern uint8_t sticks_pwm_disabled; extern bool sticks_pwm_disabled;
#define STICKS_PWM_ENABLED() (sticks_pwm_disabled == false) #define STICKS_PWM_ENABLED() (sticks_pwm_disabled == false)
void sticksPwmInit(void); void sticksPwmInit(void);
void sticksPwmRead(uint16_t * values); void sticksPwmRead(uint16_t * values);