From 3cdbaaf14d9875402740f2ef7d4ce1cd876651cf Mon Sep 17 00:00:00 2001 From: Martin Budden Date: Tue, 4 Jul 2017 20:44:33 +0100 Subject: [PATCH 1/4] Remove duplication in motor enable/disable --- src/main/drivers/pwm_output.c | 4 +--- src/main/drivers/pwm_output.h | 4 ++-- src/main/fc/cli.c | 4 ++-- src/main/fc/fc_core.c | 6 ++---- src/main/fc/fc_core.h | 2 -- src/main/fc/fc_init.c | 4 +--- 6 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/main/drivers/pwm_output.c b/src/main/drivers/pwm_output.c index 29167328c9..cf86734c6e 100644 --- a/src/main/drivers/pwm_output.c +++ b/src/main/drivers/pwm_output.c @@ -169,9 +169,7 @@ static uint8_t loadDmaBufferProshot(motorDmaOutput_t *const motor, uint16_t pack void pwmWriteMotor(uint8_t index, float value) { - if (pwmMotorsEnabled) { - pwmWrite(index, value); - } + pwmWrite(index, value); } void pwmShutdownPulsesForAllMotors(uint8_t motorCount) diff --git a/src/main/drivers/pwm_output.h b/src/main/drivers/pwm_output.h index f0ef307c2a..601161fe87 100644 --- a/src/main/drivers/pwm_output.h +++ b/src/main/drivers/pwm_output.h @@ -134,11 +134,11 @@ typedef void pwmCompleteWriteFunc(uint8_t motorCount); // function pointer use typedef struct { volatile timCCR_t *ccr; TIM_TypeDef *tim; + float pulseScale; + float pulseOffset; bool forceOverflow; bool enabled; IO_t io; - float pulseScale; - float pulseOffset; } pwmOutputPort_t; typedef struct motorDevConfig_s { diff --git a/src/main/fc/cli.c b/src/main/fc/cli.c index 2d122a3ef3..2c47db92dd 100755 --- a/src/main/fc/cli.c +++ b/src/main/fc/cli.c @@ -2231,7 +2231,7 @@ static void cliDshotProg(char *cmdline) break; default: - motorControlEnable = false; + pwmDisableMotors(); int command = atoi(pch); if (command >= 0 && command < DSHOT_MIN_THROTTLE) { @@ -2259,7 +2259,7 @@ static void cliDshotProg(char *cmdline) pch = strtok_r(NULL, " ", &saveptr); } - motorControlEnable = true; + pwmEnableMotors(); } #endif diff --git a/src/main/fc/fc_core.c b/src/main/fc/fc_core.c index f3dffa82de..754abfca14 100644 --- a/src/main/fc/fc_core.c +++ b/src/main/fc/fc_core.c @@ -108,7 +108,6 @@ int16_t magHold; int16_t headFreeModeHold; -uint8_t motorControlEnable = false; static bool reverseMotors = false; static uint32_t disarmAt; // Time of automatic disarm when "Don't spin the motors when armed" is enabled and auto_disarm_delay is nonzero @@ -627,9 +626,8 @@ static void subTaskMotorUpdate(void) } #endif - if (motorControlEnable) { - writeMotors(); - } + writeMotors(); + DEBUG_SET(DEBUG_PIDLOOP, 3, micros() - startTime); } diff --git a/src/main/fc/fc_core.h b/src/main/fc/fc_core.h index b7e0d6dcb6..085ce3d758 100644 --- a/src/main/fc/fc_core.h +++ b/src/main/fc/fc_core.h @@ -27,8 +27,6 @@ extern int16_t magHold; extern bool isRXDataNew; extern int16_t headFreeModeHold; -extern uint8_t motorControlEnable; - typedef struct throttleCorrectionConfig_s { uint16_t throttle_correction_angle; // the angle when the throttle correction is maximal. in 0.1 degres, ex 225 = 22.5 ,30.0, 450 = 45.0 deg uint8_t throttle_correction_value; // the correction that will be applied at throttle_correction_angle. diff --git a/src/main/fc/fc_init.c b/src/main/fc/fc_init.c index c56da96854..0ea310861f 100644 --- a/src/main/fc/fc_init.c +++ b/src/main/fc/fc_init.c @@ -137,8 +137,6 @@ void targetPreInit(void); #endif -extern uint8_t motorControlEnable; - #ifdef SOFTSERIAL_LOOPBACK serialPort_t *loopbackPort; #endif @@ -709,7 +707,7 @@ void init(void) // Latch active features AGAIN since some may be modified by init(). latchActiveFeatures(); - motorControlEnable = true; + pwmEnableMotors(); #ifdef USE_OSD_SLAVE osdSlaveTasksInit(); From 87ae1616d602839033923dca24459c9d4c33ba05 Mon Sep 17 00:00:00 2001 From: Martin Budden Date: Wed, 5 Jul 2017 06:21:36 +0100 Subject: [PATCH 2/4] Further better use of pwmAreMotorsEnabled --- src/main/drivers/pwm_output.c | 8 +++++--- src/main/drivers/pwm_output.h | 2 -- src/main/drivers/pwm_output_dshot.c | 4 ---- src/main/flight/mixer.c | 3 +-- src/main/target/SITL/target.c | 2 +- 5 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main/drivers/pwm_output.c b/src/main/drivers/pwm_output.c index cf86734c6e..27fb8fc073 100644 --- a/src/main/drivers/pwm_output.c +++ b/src/main/drivers/pwm_output.c @@ -45,8 +45,8 @@ static pwmOutputPort_t beeperPwm; static uint16_t freqBeep = 0; #endif -bool pwmMotorsEnabled = false; -bool isDshot = false; +static bool pwmMotorsEnabled = false; +static bool isDshot = false; static void pwmOCConfig(TIM_TypeDef *tim, uint8_t channel, uint16_t value, uint8_t output) { @@ -395,7 +395,9 @@ void pwmWriteDshotCommand(uint8_t index, uint8_t command) for (; repeats; repeats--) { motor->requestTelemetry = true; pwmWriteDshotInt(index, command); - pwmCompleteMotorUpdate(0); + if (pwmMotorsEnabled) { + pwmCompleteDshotMotorUpdate(0); + } delay(1); } diff --git a/src/main/drivers/pwm_output.h b/src/main/drivers/pwm_output.h index 601161fe87..5c2b3cf7ce 100644 --- a/src/main/drivers/pwm_output.h +++ b/src/main/drivers/pwm_output.h @@ -125,8 +125,6 @@ typedef struct { motorDmaOutput_t *getMotorDmaOutput(uint8_t index); -extern bool pwmMotorsEnabled; - struct timerHardware_s; typedef void pwmWriteFunc(uint8_t index, float value); // function pointer used to write motors typedef void pwmCompleteWriteFunc(uint8_t motorCount); // function pointer used after motors are written diff --git a/src/main/drivers/pwm_output_dshot.c b/src/main/drivers/pwm_output_dshot.c index 3236c9cfdf..ff38430937 100644 --- a/src/main/drivers/pwm_output_dshot.c +++ b/src/main/drivers/pwm_output_dshot.c @@ -74,10 +74,6 @@ void pwmCompleteDshotMotorUpdate(uint8_t motorCount) { UNUSED(motorCount); - if (!pwmMotorsEnabled) { - return; - } - for (int i = 0; i < dmaMotorTimerCount; i++) { TIM_SetCounter(dmaMotorTimers[i].timer, 0); TIM_DMACmd(dmaMotorTimers[i].timer, dmaMotorTimers[i].timerDmaSources, ENABLE); diff --git a/src/main/flight/mixer.c b/src/main/flight/mixer.c index 41dafd5ece..fc5f5531f9 100755 --- a/src/main/flight/mixer.c +++ b/src/main/flight/mixer.c @@ -469,9 +469,8 @@ void writeMotors(void) for (int i = 0; i < motorCount; i++) { pwmWriteMotor(i, motor[i]); } + pwmCompleteMotorUpdate(motorCount); } - - pwmCompleteMotorUpdate(motorCount); } static void writeAllMotors(int16_t mc) diff --git a/src/main/target/SITL/target.c b/src/main/target/SITL/target.c index 0ec676edd7..f7802a1f6a 100644 --- a/src/main/target/SITL/target.c +++ b/src/main/target/SITL/target.c @@ -374,7 +374,7 @@ int timeval_sub(struct timespec *result, struct timespec *x, struct timespec *y) // PWM part -bool pwmMotorsEnabled = false; +static bool pwmMotorsEnabled = false; static pwmOutputPort_t motors[MAX_SUPPORTED_MOTORS]; static pwmOutputPort_t servos[MAX_SUPPORTED_SERVOS]; From 51ca725f097d8beef041a529ac36826058ea3b32 Mon Sep 17 00:00:00 2001 From: Martin Budden Date: Thu, 6 Jul 2017 09:00:20 +0100 Subject: [PATCH 3/4] SITL fix --- src/main/target/SITL/target.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/target/SITL/target.c b/src/main/target/SITL/target.c index f7802a1f6a..2189ddf3e1 100644 --- a/src/main/target/SITL/target.c +++ b/src/main/target/SITL/target.c @@ -406,6 +406,10 @@ pwmOutputPort_t *pwmGetMotors(void) { return motors; } +void pwmEnableMotors(void) { + pwmMotorsEnabled = true; +} + bool pwmAreMotorsEnabled(void) { return pwmMotorsEnabled; } From 8b36027d653536459df18fdfd43069c7b2738417 Mon Sep 17 00:00:00 2001 From: Martin Budden Date: Thu, 6 Jul 2017 09:01:56 +0100 Subject: [PATCH 4/4] pwmWriteDshotCommand fix --- src/main/drivers/pwm_output.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/drivers/pwm_output.c b/src/main/drivers/pwm_output.c index 27fb8fc073..9c05608fb7 100644 --- a/src/main/drivers/pwm_output.c +++ b/src/main/drivers/pwm_output.c @@ -395,10 +395,7 @@ void pwmWriteDshotCommand(uint8_t index, uint8_t command) for (; repeats; repeats--) { motor->requestTelemetry = true; pwmWriteDshotInt(index, command); - if (pwmMotorsEnabled) { - pwmCompleteDshotMotorUpdate(0); - } - + pwmCompleteDshotMotorUpdate(0); delay(1); } }