Fix the problem of multiple timer allocations with bitbanged Ds… (#8852)

Fix the problem of multiple timer allocations with bitbanged Dshot.
This commit is contained in:
Michael Keller 2019-09-10 01:06:31 +12:00 committed by GitHub
commit 91b4129a48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 24 deletions

View File

@ -5626,28 +5626,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;

View File

@ -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;
}

View File

@ -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);

View File

@ -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

View File

@ -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)