mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-16 21:05:35 +03:00
Performance improvement based on feedback from @martinbudden
This commit is contained in:
parent
a7f50ad30a
commit
3c20108a99
3 changed files with 37 additions and 30 deletions
|
@ -108,6 +108,12 @@ static void pwmOutConfig(pwmOutputPort_t *port, const timerHardware_t *timerHard
|
||||||
*port->ccr = 0;
|
*port->ccr = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void pwmWriteUnused(uint8_t index, uint16_t value)
|
||||||
|
{
|
||||||
|
UNUSED(index);
|
||||||
|
UNUSED(value);
|
||||||
|
}
|
||||||
|
|
||||||
static void pwmWriteBrushed(uint8_t index, uint16_t value)
|
static void pwmWriteBrushed(uint8_t index, uint16_t value)
|
||||||
{
|
{
|
||||||
*motors[index].ccr = (value - 1000) * motors[index].period / 1000;
|
*motors[index].ccr = (value - 1000) * motors[index].period / 1000;
|
||||||
|
@ -135,9 +141,7 @@ static void pwmWriteMultiShot(uint8_t index, uint16_t value)
|
||||||
|
|
||||||
void pwmWriteMotor(uint8_t index, uint16_t value)
|
void pwmWriteMotor(uint8_t index, uint16_t value)
|
||||||
{
|
{
|
||||||
if (motors[index].enabled) {
|
pwmWritePtr(index, value);
|
||||||
pwmWritePtr(index, value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pwmShutdownPulsesForAllMotors(uint8_t motorCount)
|
void pwmShutdownPulsesForAllMotors(uint8_t motorCount)
|
||||||
|
@ -158,7 +162,8 @@ void pwmDisableMotors(void)
|
||||||
|
|
||||||
void pwmEnableMotors(void)
|
void pwmEnableMotors(void)
|
||||||
{
|
{
|
||||||
pwmMotorsEnabled = true;
|
/* check motors can be enabled */
|
||||||
|
pwmMotorsEnabled = (pwmWritePtr != pwmWriteUnused);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pwmAreMotorsEnabled(void)
|
bool pwmAreMotorsEnabled(void)
|
||||||
|
@ -166,23 +171,15 @@ bool pwmAreMotorsEnabled(void)
|
||||||
return pwmMotorsEnabled;
|
return pwmMotorsEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void pwmCompleteWriteUnused(uint8_t motorCount)
|
||||||
|
{
|
||||||
|
UNUSED(motorCount);
|
||||||
|
}
|
||||||
|
|
||||||
static void pwmCompleteOneshotMotorUpdate(uint8_t motorCount)
|
static void pwmCompleteOneshotMotorUpdate(uint8_t motorCount)
|
||||||
{
|
{
|
||||||
for (int index = 0; index < motorCount; index++) {
|
for (int index = 0; index < motorCount; index++) {
|
||||||
|
if (motors[index].forceOverflow) {
|
||||||
if (!motors[index].enabled) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool overflowed = false;
|
|
||||||
// If we have not already overflowed this timer
|
|
||||||
for (int j = 0; j < index; j++) {
|
|
||||||
if (motors[j].tim == motors[index].tim) {
|
|
||||||
overflowed = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!overflowed) {
|
|
||||||
timerForceOverflow(motors[index].tim);
|
timerForceOverflow(motors[index].tim);
|
||||||
}
|
}
|
||||||
// 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.
|
||||||
|
@ -193,9 +190,7 @@ static void pwmCompleteOneshotMotorUpdate(uint8_t motorCount)
|
||||||
|
|
||||||
void pwmCompleteMotorUpdate(uint8_t motorCount)
|
void pwmCompleteMotorUpdate(uint8_t motorCount)
|
||||||
{
|
{
|
||||||
if (pwmCompleteWritePtr) {
|
pwmCompleteWritePtr(motorCount);
|
||||||
pwmCompleteWritePtr(motorCount);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void motorInit(const motorConfig_t *motorConfig, uint16_t idlePulse, uint8_t motorCount)
|
void motorInit(const motorConfig_t *motorConfig, uint16_t idlePulse, uint8_t motorCount)
|
||||||
|
@ -244,22 +239,20 @@ void motorInit(const motorConfig_t *motorConfig, uint16_t idlePulse, uint8_t mot
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!useUnsyncedPwm && !isDigital) {
|
if (!isDigital) {
|
||||||
pwmCompleteWritePtr = pwmCompleteOneshotMotorUpdate;
|
pwmCompleteWritePtr = useUnsyncedPwm ? pwmCompleteWriteUnused : pwmCompleteOneshotMotorUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int motorIndex = 0; motorIndex < MAX_SUPPORTED_MOTORS && motorIndex < motorCount; motorIndex++) {
|
for (int motorIndex = 0; motorIndex < MAX_SUPPORTED_MOTORS && motorIndex < motorCount; motorIndex++) {
|
||||||
const ioTag_t tag = motorConfig->ioTags[motorIndex];
|
const ioTag_t tag = motorConfig->ioTags[motorIndex];
|
||||||
|
|
||||||
if (!tag) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const timerHardware_t *timerHardware = timerGetByTag(tag, TIM_USE_ANY);
|
const timerHardware_t *timerHardware = timerGetByTag(tag, TIM_USE_ANY);
|
||||||
|
|
||||||
if (timerHardware == NULL) {
|
if (timerHardware == NULL) {
|
||||||
/* flag failure and disable ability to arm */
|
/* not enough motors initialised for the mixer or a break in the motors */
|
||||||
break;
|
pwmWritePtr = pwmWriteUnused;
|
||||||
|
pwmCompleteWritePtr = pwmCompleteWriteUnused;
|
||||||
|
/* TODO: block arming and add reason system cannot arm */
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
motors[motorIndex].io = IOGetByTag(tag);
|
motors[motorIndex].io = IOGetByTag(tag);
|
||||||
|
@ -285,6 +278,15 @@ void motorInit(const motorConfig_t *motorConfig, uint16_t idlePulse, uint8_t mot
|
||||||
} else {
|
} else {
|
||||||
pwmOutConfig(&motors[motorIndex], timerHardware, timerMhzCounter, 0xFFFF, 0);
|
pwmOutConfig(&motors[motorIndex], timerHardware, timerMhzCounter, 0xFFFF, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool timerAlreadyUsed = false;
|
||||||
|
for (int i = 0; i < motorIndex; i++) {
|
||||||
|
if (motors[i].tim == motors[motorIndex].tim) {
|
||||||
|
timerAlreadyUsed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
motors[motorIndex].forceOverflow = !timerAlreadyUsed;
|
||||||
motors[motorIndex].enabled = true;
|
motors[motorIndex].enabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -105,6 +105,7 @@ typedef void(*pwmCompleteWriteFuncPtr)(uint8_t motorCount); // function pointe
|
||||||
typedef struct {
|
typedef struct {
|
||||||
volatile timCCR_t *ccr;
|
volatile timCCR_t *ccr;
|
||||||
TIM_TypeDef *tim;
|
TIM_TypeDef *tim;
|
||||||
|
bool forceOverflow;
|
||||||
uint16_t period;
|
uint16_t period;
|
||||||
bool enabled;
|
bool enabled;
|
||||||
IO_t io;
|
IO_t io;
|
||||||
|
|
|
@ -769,6 +769,10 @@ void timerForceOverflow(TIM_TypeDef *tim)
|
||||||
|
|
||||||
const timerHardware_t *timerGetByTag(ioTag_t tag, timerUsageFlag_e flag)
|
const timerHardware_t *timerGetByTag(ioTag_t tag, timerUsageFlag_e flag)
|
||||||
{
|
{
|
||||||
|
if (!tag) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < USABLE_TIMER_CHANNEL_COUNT; i++) {
|
for (int i = 0; i < USABLE_TIMER_CHANNEL_COUNT; i++) {
|
||||||
if (timerHardware[i].tag == tag) {
|
if (timerHardware[i].tag == tag) {
|
||||||
if (timerHardware[i].usageFlags & flag || flag == 0) {
|
if (timerHardware[i].usageFlags & flag || flag == 0) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue