diff --git a/src/main/cli/cli.c b/src/main/cli/cli.c index 8d1eac3b21..67d2491dc4 100644 --- a/src/main/cli/cli.c +++ b/src/main/cli/cli.c @@ -1588,7 +1588,7 @@ static void cliSerialPassthrough(const char *cmdName, char *cmdline) for (unsigned i = 0; i < getMotorCount(); i++) { const ioTag_t tag = motorConfig()->dev.ioTags[i]; if (tag) { - const timerHardware_t *timerHardware = timerGetByTag(tag); + const timerHardware_t *timerHardware = timerGetConfiguredByTag(tag); if (timerHardware) { IO_t io = IOGetByTag(tag); IOInit(io, OWNER_MOTOR, 0); @@ -5645,7 +5645,7 @@ static void cliDmaopt(const char *cmdName, char *cmdline) #if defined(USE_TIMER_MGMT) timerIoConfig = timerIoConfigByTag(ioTag); #endif - timer = timerGetByTag(ioTag); + timer = timerGetConfiguredByTag(ioTag); } // opt or list @@ -5863,7 +5863,8 @@ static void showTimers(void) 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)); + const timerHardware_t *timer = timerGetAllocatedByNumberAndChannel(timerNumber, CC_CHANNEL_FROM_INDEX(timerIndex)); + const resourceOwner_t *timerOwner = timerGetOwner(timer); if (timerOwner->owner) { if (!timerUsed) { timerUsed = true; @@ -5872,9 +5873,9 @@ static void showTimers(void) } if (timerOwner->resourceIndex > 0) { - cliPrintLinef(" CH%d: %s %d", timerIndex + 1, ownerNames[timerOwner->owner], timerOwner->resourceIndex); + cliPrintLinef(" CH%d%s: %s %d", timerIndex + 1, timer->output & TIMER_OUTPUT_N_CHANNEL ? "N" : " ", ownerNames[timerOwner->owner], timerOwner->resourceIndex); } else { - cliPrintLinef(" CH%d: %s", timerIndex + 1, ownerNames[timerOwner->owner]); + cliPrintLinef(" CH%d%s: %s", timerIndex + 1, timer->output & TIMER_OUTPUT_N_CHANNEL ? "N" : " ", ownerNames[timerOwner->owner]); } } } diff --git a/src/main/config/config.c b/src/main/config/config.c index 26248476a4..90b79dde81 100644 --- a/src/main/config/config.c +++ b/src/main/config/config.c @@ -476,7 +476,7 @@ static void validateAndFixConfig(void) #if defined(USE_BEEPER) #ifdef USE_TIMER - if (beeperDevConfig()->frequency && !timerGetByTag(beeperDevConfig()->ioTag)) { + if (beeperDevConfig()->frequency && !timerGetConfiguredByTag(beeperDevConfig()->ioTag)) { beeperDevConfigMutable()->frequency = 0; } #endif diff --git a/src/main/drivers/dshot_bitbang.c b/src/main/drivers/dshot_bitbang.c index f8f9379f10..3a3c4060f6 100644 --- a/src/main/drivers/dshot_bitbang.c +++ b/src/main/drivers/dshot_bitbang.c @@ -245,11 +245,11 @@ static bbPort_t *bbAllocMotorPort(int portIndex) return bbPort; } -const resourceOwner_t *dshotBitbangTimerGetOwner(int8_t timerNumber, uint16_t timerChannel) +const resourceOwner_t *dshotBitbangTimerGetOwner(const timerHardware_t *timer) { for (int index = 0; index < usedMotorPorts; index++) { - const timerHardware_t *timer = bbPorts[index].timhw; - if (timerGetTIMNumber(timer->tim) == timerNumber && timer->channel == timerChannel) { + const timerHardware_t *bitbangTimer = bbPorts[index].timhw; + if (bitbangTimer && bitbangTimer == timer) { return &bbPorts[index].owner; } } @@ -351,7 +351,8 @@ static void bbFindPacerTimer(void) } bool timerConflict = false; for (int channel = 0; channel < CC_CHANNELS_PER_TIMER; channel++) { - const resourceOwner_e timerOwner = timerGetOwner(timNumber, CC_CHANNEL_FROM_INDEX(channel))->owner; + const timerHardware_t *timer = timerGetAllocatedByNumberAndChannel(timNumber, CC_CHANNEL_FROM_INDEX(channel)); + const resourceOwner_e timerOwner = timerGetOwner(timer)->owner; if (timerOwner != OWNER_FREE && timerOwner != OWNER_DSHOT_BITBANG) { timerConflict = true; break; @@ -709,7 +710,7 @@ motorDevice_t *dshotBitbangDevInit(const motorDevConfig_t *motorConfig, uint8_t for (int motorIndex = 0; motorIndex < MAX_SUPPORTED_MOTORS && motorIndex < motorCount; motorIndex++) { const unsigned reorderedMotorIndex = motorConfig->motorOutputReordering[motorIndex]; - const timerHardware_t *timerHardware = timerGetByTag(motorConfig->ioTags[reorderedMotorIndex]); + const timerHardware_t *timerHardware = timerGetConfiguredByTag(motorConfig->ioTags[reorderedMotorIndex]); const IO_t io = IOGetByTag(motorConfig->ioTags[reorderedMotorIndex]); uint8_t output = motorConfig->motorPwmInversion ? timerHardware->output ^ TIMER_OUTPUT_INVERTED : timerHardware->output; diff --git a/src/main/drivers/dshot_bitbang.h b/src/main/drivers/dshot_bitbang.h index 050f378cba..8bfffe5476 100644 --- a/src/main/drivers/dshot_bitbang.h +++ b/src/main/drivers/dshot_bitbang.h @@ -39,4 +39,4 @@ struct motorDevConfig_s; struct motorDevice_s; struct motorDevice_s *dshotBitbangDevInit(const struct motorDevConfig_s *motorConfig, uint8_t motorCount); dshotBitbangStatus_e dshotBitbangGetStatus(); -const resourceOwner_t *dshotBitbangTimerGetOwner(int8_t timerNumber, uint16_t timerChannel); +const resourceOwner_t *dshotBitbangTimerGetOwner(const timerHardware_t *timer); diff --git a/src/main/drivers/timer.h b/src/main/drivers/timer.h index 82f0008878..f00043cc46 100644 --- a/src/main/drivers/timer.h +++ b/src/main/drivers/timer.h @@ -278,9 +278,10 @@ extern const resourceOwner_t freeOwner; struct timerIOConfig_s; struct timerIOConfig_s *timerIoConfigByTag(ioTag_t ioTag); -const resourceOwner_t *timerGetOwner(int8_t timerNumber, uint16_t timerChannel); +const timerHardware_t *timerGetAllocatedByNumberAndChannel(int8_t timerNumber, uint16_t timerChannel); +const resourceOwner_t *timerGetOwner(const timerHardware_t *timer); #endif -const timerHardware_t *timerGetByTag(ioTag_t ioTag); +const timerHardware_t *timerGetConfiguredByTag(ioTag_t ioTag); const timerHardware_t *timerAllocate(ioTag_t ioTag, resourceOwner_e owner, uint8_t resourceIndex); const timerHardware_t *timerGetByTagAndIndex(ioTag_t ioTag, unsigned timerIndex); ioTag_t timerioTagGetByUsage(timerUsageFlag_e usageFlag, uint8_t index); diff --git a/src/main/drivers/timer_common.c b/src/main/drivers/timer_common.c index c3ecaa4939..661baf60df 100644 --- a/src/main/drivers/timer_common.c +++ b/src/main/drivers/timer_common.c @@ -44,16 +44,6 @@ timerIOConfig_t *timerIoConfigByTag(ioTag_t ioTag) return NULL; } -static uint8_t timerIndexByTag(ioTag_t ioTag) -{ - for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) { - if (timerIOConfig(i)->ioTag == ioTag) { - return timerIOConfig(i)->index; - } - } - return 0; -} - const timerHardware_t *timerGetByTagAndIndex(ioTag_t ioTag, unsigned timerIndex) { @@ -74,19 +64,38 @@ const timerHardware_t *timerGetByTagAndIndex(ioTag_t ioTag, unsigned timerIndex) return NULL; } -const timerHardware_t *timerGetByTag(ioTag_t ioTag) +const timerHardware_t *timerGetConfiguredByTag(ioTag_t ioTag) { - uint8_t timerIndex = timerIndexByTag(ioTag); + uint8_t timerIndex = 0; + for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) { + if (timerIOConfig(i)->ioTag == ioTag) { + timerIndex = timerIOConfig(i)->index; + + break; + } + } return timerGetByTagAndIndex(ioTag, timerIndex); } -const resourceOwner_t *timerGetOwner(int8_t timerNumber, uint16_t timerChannel) +const timerHardware_t *timerGetAllocatedByNumberAndChannel(int8_t timerNumber, uint16_t timerChannel) +{ + 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 && timerOwners[i].owner) { + return timer; + } + } + + return NULL; +} + +const resourceOwner_t *timerGetOwner(const timerHardware_t *timer) { 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) { + const timerHardware_t *assignedTimer = timerGetByTagAndIndex(timerIOConfig(i)->ioTag, timerIOConfig(i)->index); + if (assignedTimer && assignedTimer == timer) { timerOwner = &timerOwners[i]; break; @@ -95,7 +104,7 @@ const resourceOwner_t *timerGetOwner(int8_t timerNumber, uint16_t timerChannel) #if defined(USE_DSHOT_BITBANG) if (!timerOwner->owner) { - timerOwner = dshotBitbangTimerGetOwner(timerNumber, timerChannel); + timerOwner = dshotBitbangTimerGetOwner(timer); } #endif @@ -112,7 +121,7 @@ const timerHardware_t *timerAllocate(ioTag_t ioTag, resourceOwner_e owner, uint8 if (timerIOConfig(i)->ioTag == ioTag) { const timerHardware_t *timer = timerGetByTagAndIndex(ioTag, timerIOConfig(i)->index); - if (timerGetOwner(timerGetTIMNumber(timer->tim), timer->channel)->owner) { + if (timerGetOwner(timer)->owner) { return NULL; } @@ -127,7 +136,7 @@ const timerHardware_t *timerAllocate(ioTag_t ioTag, resourceOwner_e owner, uint8 } #else -const timerHardware_t *timerGetByTag(ioTag_t ioTag) +const timerHardware_t *timerGetConfiguredByTag(ioTag_t ioTag) { #if TIMER_CHANNEL_COUNT > 0 for (unsigned i = 0; i < TIMER_CHANNEL_COUNT; i++) { @@ -146,7 +155,7 @@ const timerHardware_t *timerAllocate(ioTag_t ioTag, resourceOwner_e owner, uint8 UNUSED(owner); UNUSED(resourceIndex); - return timerGetByTag(ioTag); + return timerGetConfiguredByTag(ioTag); } #endif