1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-13 11:29:58 +03:00

FIX: Serial ESC communication when using digital protocols (#14214)

* FIX: Serial ESC communication when using digital protocols

* Fix error following rebase, and amending based on comments from @ledvinap
This commit is contained in:
Jay Blackman 2025-01-30 00:49:11 +11:00 committed by GitHub
parent b277364b2c
commit cd84e10fa5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 100 additions and 62 deletions

View file

@ -362,6 +362,14 @@ timeMs_t motorGetMotorEnableTimeMs(void)
} }
#endif #endif
IO_t motorGetIo(unsigned index)
{
if (index >= motorDevice.count) {
return IO_NONE;
}
return motorDevice.vTable->getMotorIO ? motorDevice.vTable->getMotorIO(index) : IO_NONE;
}
/* functions below for empty methods and no active motors */ /* functions below for empty methods and no active motors */
void motorPostInitNull(void) void motorPostInitNull(void)
{ {
@ -433,6 +441,7 @@ static const motorVTable_t motorNullVTable = {
.shutdown = motorShutdownNull, .shutdown = motorShutdownNull,
.requestTelemetry = NULL, .requestTelemetry = NULL,
.isMotorIdle = NULL, .isMotorIdle = NULL,
.getMotorIO = NULL,
}; };
void motorNullDevInit(motorDevice_t *device) void motorNullDevInit(motorDevice_t *device)

View file

@ -54,5 +54,7 @@ float motorEstimateMaxRpm(void);
bool motorIsEnabled(void); bool motorIsEnabled(void);
bool motorIsMotorEnabled(unsigned index); bool motorIsMotorEnabled(unsigned index);
bool motorIsMotorIdle(unsigned index); bool motorIsMotorIdle(unsigned index);
IO_t motorGetIo(unsigned index);
timeMs_t motorGetMotorEnableTimeMs(void); timeMs_t motorGetMotorEnableTimeMs(void);
void motorShutdown(void); // Replaces stopPwmAllMotors void motorShutdown(void); // Replaces stopPwmAllMotors

View file

@ -22,6 +22,7 @@
#pragma once #pragma once
#include "common/time.h" #include "common/time.h"
#include "drivers/io_types.h"
#define ALL_MOTORS 255 #define ALL_MOTORS 255
#define MOTOR_OUTPUT_LIMIT_PERCENT_MIN 1 #define MOTOR_OUTPUT_LIMIT_PERCENT_MIN 1
@ -64,6 +65,7 @@ typedef struct motorVTable_s {
void (*updateComplete)(void); void (*updateComplete)(void);
void (*shutdown)(void); void (*shutdown)(void);
bool (*isMotorIdle)(unsigned index); bool (*isMotorIdle)(unsigned index);
IO_t (*getMotorIO)(unsigned index);
// Digital commands // Digital commands
void (*requestTelemetry)(unsigned index); void (*requestTelemetry)(unsigned index);

View file

@ -48,4 +48,23 @@ void analogInitEndpoints(const motorConfig_t *motorConfig, float outputLimit, fl
} }
} }
IO_t pwmGetMotorIO(unsigned index)
{
if (index >= pwmMotorCount) {
return IO_NONE;
}
return motors[index].io;
}
bool pwmIsMotorEnabled(unsigned index)
{
return motors[index].enabled;
}
bool pwmEnableMotors(void)
{
/* check motors can be enabled */
return pwmMotorCount > 0;
}
#endif #endif

View file

@ -0,0 +1,28 @@
/*
* This file is part of Betaflight.
*
* Betaflight is free software. You can redistribute this software
* and/or modify this software under the terms of the GNU General
* Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later
* version.
*
* Betaflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "drivers/io_types.h"
IO_t pwmGetMotorIO(unsigned index);
bool pwmIsMotorEnabled(unsigned index);
bool pwmEnableMotors(void);

View file

@ -36,7 +36,7 @@
#include "drivers/time.h" #include "drivers/time.h"
#include "drivers/timer.h" #include "drivers/timer.h"
#include "drivers/light_led.h" #include "drivers/light_led.h"
#include "drivers/pwm_output.h" #include "drivers/motor.h"
#include "flight/mixer.h" #include "flight/mixer.h"
#include "io/beeper.h" #include "io/beeper.h"
@ -143,23 +143,23 @@ inline void setEscOutput(uint8_t selEsc)
uint8_t esc4wayInit(void) uint8_t esc4wayInit(void)
{ {
// StopPwmAllMotors(); motorShutdown();
// XXX Review effect of motor refactor uint8_t escIndex = 0;
//pwmDisableMotors();
escCount = 0;
memset(&escHardware, 0, sizeof(escHardware)); memset(&escHardware, 0, sizeof(escHardware));
pwmOutputPort_t *pwmMotors = pwmGetMotors();
for (volatile uint8_t i = 0; i < MAX_SUPPORTED_MOTORS; i++) { for (volatile uint8_t i = 0; i < MAX_SUPPORTED_MOTORS; i++) {
if (pwmMotors[i].enabled) { if (motorIsMotorEnabled(i)) {
if (pwmMotors[i].io != IO_NONE) { const IO_t io = motorGetIo(i);
escHardware[escCount].io = pwmMotors[i].io; if (io != IO_NONE) {
setEscInput(escCount); escHardware[escIndex].io = io;
setEscHi(escCount); setEscInput(escIndex);
escCount++; setEscHi(escIndex);
escIndex++;
} }
} }
} }
motorDisable(); motorDisable();
escCount = escIndex;
return escCount; return escCount;
} }

View file

@ -669,6 +669,14 @@ static void bbPostInit(void)
} }
} }
static IO_t bbGetMotorIO(unsigned index)
{
if (index >= dshotMotorCount) {
return IO_NONE;
}
return bbMotors[index].io;
}
static motorVTable_t bbVTable = { static motorVTable_t bbVTable = {
.postInit = bbPostInit, .postInit = bbPostInit,
.enable = bbEnableMotors, .enable = bbEnableMotors,
@ -685,6 +693,7 @@ static motorVTable_t bbVTable = {
.shutdown = bbShutdown, .shutdown = bbShutdown,
.isMotorIdle = bbDshotIsMotorIdle, .isMotorIdle = bbDshotIsMotorIdle,
.requestTelemetry = bbDshotRequestTelemetry, .requestTelemetry = bbDshotRequestTelemetry,
.getMotorIO = bbGetMotorIO,
}; };
dshotBitbangStatus_e dshotBitbangGetStatus(void) dshotBitbangStatus_e dshotBitbangGetStatus(void)

View file

@ -31,6 +31,7 @@
#include "drivers/io.h" #include "drivers/io.h"
#include "drivers/motor.h" #include "drivers/motor.h"
#include "drivers/pwm_output.h" #include "drivers/pwm_output.h"
#include "drivers/pwm_output_impl.h"
#include "drivers/time.h" #include "drivers/time.h"
#include "drivers/timer.h" #include "drivers/timer.h"
@ -108,16 +109,6 @@ static void pwmDisableMotors(void)
} }
static motorVTable_t motorPwmVTable; static motorVTable_t motorPwmVTable;
static bool pwmEnableMotors(void)
{
/* check motors can be enabled */
return pwmMotorCount > 0;
}
static bool pwmIsMotorEnabled(unsigned index)
{
return motors[index].enabled;
}
static void pwmCompleteMotorUpdate(void) static void pwmCompleteMotorUpdate(void)
{ {
@ -158,6 +149,7 @@ static motorVTable_t motorPwmVTable = {
.updateComplete = pwmCompleteMotorUpdate, .updateComplete = pwmCompleteMotorUpdate,
.requestTelemetry = NULL, .requestTelemetry = NULL,
.isMotorIdle = NULL, .isMotorIdle = NULL,
.getMotorIO = pwmGetMotorIO,
}; };
bool motorPwmDevInit(motorDevice_t *device, const motorDevConfig_t *motorConfig, uint16_t idlePulse) bool motorPwmDevInit(motorDevice_t *device, const motorDevConfig_t *motorConfig, uint16_t idlePulse)

View file

@ -31,6 +31,7 @@
#include "drivers/io.h" #include "drivers/io.h"
#include "drivers/motor_impl.h" #include "drivers/motor_impl.h"
#include "drivers/pwm_output.h" #include "drivers/pwm_output.h"
#include "drivers/pwm_output_impl.h"
#include "drivers/time.h" #include "drivers/time.h"
#include "drivers/timer.h" #include "drivers/timer.h"
@ -98,18 +99,6 @@ static void pwmDisableMotors(void)
pwmShutdownPulsesForAllMotors(); pwmShutdownPulsesForAllMotors();
} }
static motorVTable_t motorPwmVTable;
static bool pwmEnableMotors(void)
{
/* check motors can be enabled */
return (pwmMotorDevice->vTable);
}
static bool pwmIsMotorEnabled(unsigned index)
{
return motors[index].enabled;
}
static bool useContinuousUpdate = true; static bool useContinuousUpdate = true;
static void pwmCompleteMotorUpdate(void) static void pwmCompleteMotorUpdate(void)
@ -151,6 +140,7 @@ static motorVTable_t motorPwmVTable = {
.updateComplete = pwmCompleteMotorUpdate, .updateComplete = pwmCompleteMotorUpdate,
.requestTelemetry = NULL, .requestTelemetry = NULL,
.isMotorIdle = NULL, .isMotorIdle = NULL,
.getMotorIO = pwmGetMotorIO,
}; };
bool motorPwmDevInit(motorDevice_t *device, const motorDevConfig_t *motorConfig, uint16_t idlePulse) bool motorPwmDevInit(motorDevice_t *device, const motorDevConfig_t *motorConfig, uint16_t idlePulse)

View file

@ -41,6 +41,7 @@
#include "drivers/system.h" #include "drivers/system.h"
#include "drivers/time.h" #include "drivers/time.h"
#include "drivers/pwm_output.h" #include "drivers/pwm_output.h"
#include "drivers/pwm_output_impl.h"
#include "drivers/light_led.h" #include "drivers/light_led.h"
#include "drivers/timer.h" #include "drivers/timer.h"
@ -543,8 +544,6 @@ void servoDevInit(const servoDevConfig_t *servoConfig)
} }
} }
static motorDevice_t pwmMotorDevice; // Forward
pwmOutputPort_t *pwmGetMotors(void) pwmOutputPort_t *pwmGetMotors(void)
{ {
return motors; return motors;
@ -562,14 +561,7 @@ static uint16_t pwmConvertToExternal(float motorValue)
static void pwmDisableMotors(void) static void pwmDisableMotors(void)
{ {
pwmMotorDevice.enabled = false; // NOOP
}
static bool pwmEnableMotors(void)
{
pwmMotorDevice.enabled = true;
return true;
} }
static void pwmWriteMotor(uint8_t index, float value) static void pwmWriteMotor(uint8_t index, float value)
@ -594,12 +586,7 @@ static void pwmWriteMotorInt(uint8_t index, uint16_t value)
static void pwmShutdownPulsesForAllMotors(void) static void pwmShutdownPulsesForAllMotors(void)
{ {
pwmMotorDevice.enabled = false; // NOOP
}
static bool pwmIsMotorEnabled(unsigned index)
{
return motors[index].enabled;
} }
static void pwmCompleteMotorUpdate(void) static void pwmCompleteMotorUpdate(void)
@ -647,7 +634,7 @@ static const motorVTable_t vTable = {
.shutdown = pwmShutdownPulsesForAllMotors, .shutdown = pwmShutdownPulsesForAllMotors,
.requestTelemetry = NULL, .requestTelemetry = NULL,
.isMotorIdle = NULL, .isMotorIdle = NULL,
.getMotorIO = NULL,
}; };
bool motorPwmDevInit(motorDevice_t *device, const motorDevConfig_t *motorConfig, uint16_t _idlePulse) bool motorPwmDevInit(motorDevice_t *device, const motorDevConfig_t *motorConfig, uint16_t _idlePulse)

View file

@ -29,6 +29,7 @@
#include "drivers/io.h" #include "drivers/io.h"
#include "drivers/pwm_output.h" #include "drivers/pwm_output.h"
#include "drivers/pwm_output_impl.h"
#include "drivers/time.h" #include "drivers/time.h"
#include "drivers/timer.h" #include "drivers/timer.h"
@ -128,17 +129,6 @@ static void pwmDisableMotors(void)
pwmShutdownPulsesForAllMotors(); pwmShutdownPulsesForAllMotors();
} }
static bool pwmEnableMotors(void)
{
/* check motors can be enabled */
return pwmMotorCount > 0;
}
static bool pwmIsMotorEnabled(unsigned index)
{
return motors[index].enabled;
}
static void pwmCompleteMotorUpdate(void) static void pwmCompleteMotorUpdate(void)
{ {
if (useContinuousUpdate) { if (useContinuousUpdate) {
@ -178,6 +168,7 @@ static const motorVTable_t motorPwmVTable = {
.updateComplete = pwmCompleteMotorUpdate, .updateComplete = pwmCompleteMotorUpdate,
.requestTelemetry = NULL, .requestTelemetry = NULL,
.isMotorIdle = NULL, .isMotorIdle = NULL,
.getMotorIO = pwmGetMotorIO,
}; };
bool motorPwmDevInit(motorDevice_t *device, const motorDevConfig_t *motorConfig, uint16_t idlePulse) bool motorPwmDevInit(motorDevice_t *device, const motorDevConfig_t *motorConfig, uint16_t idlePulse)

View file

@ -126,6 +126,14 @@ static bool dshotPwmIsMotorEnabled(unsigned index)
return motors[index].enabled; return motors[index].enabled;
} }
static IO_t pwmDshotGetMotorIO(unsigned index)
{
if (index >= dshotMotorCount) {
return IO_NONE;
}
return motors[index].io;
}
static FAST_CODE void dshotWriteInt(uint8_t index, uint16_t value) static FAST_CODE void dshotWriteInt(uint8_t index, uint16_t value)
{ {
pwmWriteDshotInt(index, value); pwmWriteDshotInt(index, value);
@ -150,6 +158,7 @@ static const motorVTable_t dshotPwmVTable = {
.shutdown = dshotPwmShutdown, .shutdown = dshotPwmShutdown,
.requestTelemetry = pwmDshotRequestTelemetry, .requestTelemetry = pwmDshotRequestTelemetry,
.isMotorIdle = pwmDshotIsMotorIdle, .isMotorIdle = pwmDshotIsMotorIdle,
.getMotorIO = pwmDshotGetMotorIO,
}; };
bool dshotPwmDevInit(motorDevice_t *device, const motorDevConfig_t *motorConfig) bool dshotPwmDevInit(motorDevice_t *device, const motorDevConfig_t *motorConfig)