From 4acf0893dffaccfaaa1ddb8482c2027789d47bf4 Mon Sep 17 00:00:00 2001 From: mikeller Date: Mon, 9 Sep 2019 00:48:02 +1200 Subject: [PATCH] Fix the problem of multiple timer allocations with bitbanged Dshot. --- src/main/cli/cli.c | 16 ---------------- src/main/drivers/dshot_bitbang.c | 16 +++++++++++++--- src/main/drivers/dshot_bitbang.h | 2 +- src/main/drivers/timer.h | 2 ++ src/main/drivers/timer_common.c | 18 ++++++++++++++---- 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/main/cli/cli.c b/src/main/cli/cli.c index b2dadc560..72b7562e4 100644 --- a/src/main/cli/cli.c +++ b/src/main/cli/cli.c @@ -5616,28 +5616,12 @@ static void showTimers(void) cliRepeat('-', 23); #endif -#ifdef USE_DSHOT_BITBANG - resourceOwner_t bitbangOwner = { OWNER_DSHOT_BITBANG, 0 }; -#endif int8_t timerNumber; for (int i = 0; (timerNumber = timerGetNumberByIndex(i)); i++) { cliPrintf("TIM%d:", timerNumber); bool timerUsed = false; for (unsigned timerIndex = 0; timerIndex < CC_CHANNELS_PER_TIMER; timerIndex++) { const resourceOwner_t *timerOwner = timerGetOwner(timerNumber, CC_CHANNEL_FROM_INDEX(timerIndex)); -#ifdef USE_DSHOT_BITBANG - if (!timerOwner->owner) { - const timerHardware_t* timer; - int pacerIndex = 0; - while ((timer = dshotBitbangGetPacerTimer(pacerIndex++))) { - if (timerGetTIMNumber(timer->tim) == timerNumber && timer->channel == CC_CHANNEL_FROM_INDEX(timerIndex)) { - timerOwner = &bitbangOwner; - bitbangOwner.resourceIndex++; - break; - } - } - } -#endif if (timerOwner->owner) { if (!timerUsed) { timerUsed = true; diff --git a/src/main/drivers/dshot_bitbang.c b/src/main/drivers/dshot_bitbang.c index d57970e07..50d361994 100644 --- a/src/main/drivers/dshot_bitbang.c +++ b/src/main/drivers/dshot_bitbang.c @@ -206,9 +206,18 @@ static bbPort_t *bbAllocMotorPort(int portIndex) return bbPort; } -const timerHardware_t* dshotBitbangGetPacerTimer(int index) +const resourceOwner_t *dshotBitbangTimerGetOwner(int8_t timerNumber, uint16_t timerChannel) { - return index < usedMotorPorts ? bbPorts[index].timhw : NULL; + static resourceOwner_t bitbangOwner = { .owner = OWNER_DSHOT_BITBANG, .resourceIndex = 0 }; + + for (int index = 0; index < usedMotorPorts; index++) { + const timerHardware_t *timer = bbPorts[index].timhw; + if (timerGetTIMNumber(timer->tim) == timerNumber && timer->channel == timerChannel) { + return &bitbangOwner; + } + } + + return &freeOwner; } // Return frequency of smallest change [state/sec] @@ -304,7 +313,8 @@ static void bbFindPacerTimer(void) int timNumber = timerGetTIMNumber(timer->tim); bool timerConflict = false; for (int channel = 0; channel < CC_CHANNELS_PER_TIMER; channel++) { - if(timerGetOwner(timNumber, CC_CHANNEL_FROM_INDEX(channel))->owner != OWNER_FREE) { + const resourceOwner_e timerOwner = timerGetOwner(timNumber, CC_CHANNEL_FROM_INDEX(channel))->owner; + if (timerOwner != OWNER_FREE && timerOwner != OWNER_DSHOT_BITBANG) { timerConflict = true; break; } diff --git a/src/main/drivers/dshot_bitbang.h b/src/main/drivers/dshot_bitbang.h index 0404ee263..140a4c252 100644 --- a/src/main/drivers/dshot_bitbang.h +++ b/src/main/drivers/dshot_bitbang.h @@ -38,4 +38,4 @@ struct motorDevConfig_s; struct motorDevice_s; struct motorDevice_s *dshotBitbangDevInit(const struct motorDevConfig_s *motorConfig, uint8_t motorCount); dshotBitbangStatus_e dshotBitbangGetStatus(); -const timerHardware_t* dshotBitbangGetPacerTimer(int index); +const resourceOwner_t *dshotBitbangTimerGetOwner(int8_t timerNumber, uint16_t timerChannel); diff --git a/src/main/drivers/timer.h b/src/main/drivers/timer.h index 79dc5cb43..2bf975fa6 100644 --- a/src/main/drivers/timer.h +++ b/src/main/drivers/timer.h @@ -274,6 +274,8 @@ rccPeriphTag_t timerRCC(TIM_TypeDef *tim); uint8_t timerInputIrq(TIM_TypeDef *tim); #if defined(USE_TIMER_MGMT) +extern const resourceOwner_t freeOwner; + timerIOConfig_t *timerIoConfigByTag(ioTag_t ioTag); const resourceOwner_t *timerGetOwner(int8_t timerNumber, uint16_t timerChannel); #endif diff --git a/src/main/drivers/timer_common.c b/src/main/drivers/timer_common.c index 5c5e87330..4de4c6129 100644 --- a/src/main/drivers/timer_common.c +++ b/src/main/drivers/timer_common.c @@ -22,12 +22,15 @@ #ifdef USE_TIMER +#include "drivers/dshot_bitbang.h" #include "drivers/io.h" #include "timer.h" #ifdef USE_TIMER_MGMT #include "pg/timerio.h" +const resourceOwner_t freeOwner = { .owner = OWNER_FREE, .resourceIndex = 0 }; + static resourceOwner_t timerOwners[MAX_TIMER_PINMAP_COUNT]; timerIOConfig_t *timerIoConfigByTag(ioTag_t ioTag) @@ -80,16 +83,23 @@ const timerHardware_t *timerGetByTag(ioTag_t ioTag) const resourceOwner_t *timerGetOwner(int8_t timerNumber, uint16_t timerChannel) { - static resourceOwner_t freeOwner = { .owner = OWNER_FREE, .resourceIndex = 0 }; - + const resourceOwner_t *timerOwner = &freeOwner; for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) { const timerHardware_t *timer = timerGetByTagAndIndex(timerIOConfig(i)->ioTag, timerIOConfig(i)->index); if (timer && timerGetTIMNumber(timer->tim) == timerNumber && timer->channel == timerChannel) { - return &timerOwners[i]; + timerOwner = &timerOwners[i]; + + break; } } - return &freeOwner; +#if defined(USE_DSHOT_BITBANG) + if (!timerOwner->owner) { + timerOwner = dshotBitbangTimerGetOwner(timerNumber, timerChannel); + } +#endif + + return timerOwner; } const timerHardware_t *timerAllocate(ioTag_t ioTag, resourceOwner_e owner, uint8_t resourceIndex) -- 2.11.4.GIT