Skip to content

Commit

Permalink
Fix the problem of multiple timer allocations with bitbanged Dshot.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeller committed Sep 8, 2019
1 parent f331cb3 commit 4acf089
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
16 changes: 0 additions & 16 deletions src/main/cli/cli.c
Expand Up @@ -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;
Expand Down
16 changes: 13 additions & 3 deletions src/main/drivers/dshot_bitbang.c
Expand Up @@ -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]
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/drivers/dshot_bitbang.h
Expand Up @@ -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);
2 changes: 2 additions & 0 deletions src/main/drivers/timer.h
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions src/main/drivers/timer_common.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4acf089

Please sign in to comment.