From 2b98f137dbc03dce31e0715ad3f19138fae025ef Mon Sep 17 00:00:00 2001 From: mikeller Date: Tue, 10 Sep 2019 01:48:25 +1200 Subject: [PATCH 1/2] Fixed problems with bitbanged Dshot and lazily allocated timers. --- src/main/cli/settings.c | 6 ++++++ src/main/cli/settings.h | 1 + src/main/drivers/dshot_bitbang.c | 5 +++++ src/main/pg/motor.c | 1 + src/main/pg/motor.h | 7 +++++++ 5 files changed, 20 insertions(+) diff --git a/src/main/cli/settings.c b/src/main/cli/settings.c index a845d7ac0..9947ba306 100644 --- a/src/main/cli/settings.c +++ b/src/main/cli/settings.c @@ -477,6 +477,10 @@ static const char* const lookupTableInterpolatedSetpoint[] = { "OFF", "ON", "AVERAGED" }; +static const char* const lookupTableDshotBitbangedTimer[] = { + "AUTO", "TIM1", "TIM8" +}; + #define LOOKUP_TABLE_ENTRY(name) { name, ARRAYLEN(name) } @@ -594,6 +598,7 @@ const lookupTableEntry_t lookupTables[] = { LOOKUP_TABLE_ENTRY(lookupTablePositionAltSource), LOOKUP_TABLE_ENTRY(lookupTableOffOnAuto), LOOKUP_TABLE_ENTRY(lookupTableInterpolatedSetpoint), + LOOKUP_TABLE_ENTRY(lookupTableDshotBitbangedTimer), }; #undef LOOKUP_TABLE_ENTRY @@ -768,6 +773,7 @@ const clivalue_t valueTable[] = { #endif #ifdef USE_DSHOT_BITBANG { "dshot_bitbang", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON_AUTO }, PG_MOTOR_CONFIG, offsetof(motorConfig_t, dev.useDshotBitbang) }, + { "dshot_bitbang_timer", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_DSHOT_BITBANGED_TIMER }, PG_MOTOR_CONFIG, offsetof(motorConfig_t, dev.useDshotBitbangedTimer) }, #endif #endif { "use_unsynced_pwm", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_MOTOR_CONFIG, offsetof(motorConfig_t, dev.useUnsyncedPwm) }, diff --git a/src/main/cli/settings.h b/src/main/cli/settings.h index cbb32d48c..31e47d626 100644 --- a/src/main/cli/settings.h +++ b/src/main/cli/settings.h @@ -137,6 +137,7 @@ typedef enum { TABLE_POSITION_ALT_SOURCE, TABLE_OFF_ON_AUTO, TABLE_INTERPOLATED_SP, + TABLE_DSHOT_BITBANGED_TIMER, LOOKUP_TABLE_COUNT } lookupTableIndex_e; diff --git a/src/main/drivers/dshot_bitbang.c b/src/main/drivers/dshot_bitbang.c index 50d361994..141995ba7 100644 --- a/src/main/drivers/dshot_bitbang.c +++ b/src/main/drivers/dshot_bitbang.c @@ -311,6 +311,10 @@ static void bbFindPacerTimer(void) for (unsigned timerIndex = 0; timerIndex < ARRAYLEN(bbTimerHardware); timerIndex++) { timer = &bbTimerHardware[timerIndex]; int timNumber = timerGetTIMNumber(timer->tim); + if ((motorConfig()->dev.useDshotBitbangedTimer == DSHOT_BITBANGED_TIMER_TIM1 && timNumber != 1) + || (motorConfig()->dev.useDshotBitbangedTimer == DSHOT_BITBANGED_TIMER_TIM8 && timNumber != 8)) { + continue; + } 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; @@ -334,6 +338,7 @@ static void bbFindPacerTimer(void) if (dmaGetOwner(dmaIdentifier)->owner == OWNER_FREE && !usedTimerChannels[timNumber][timer->channel]) { usedTimerChannels[timNumber][timer->channel] = true; + break; } } diff --git a/src/main/pg/motor.c b/src/main/pg/motor.c index 0e9690b72..7c219b18e 100644 --- a/src/main/pg/motor.c +++ b/src/main/pg/motor.c @@ -76,6 +76,7 @@ void pgResetFn_motorConfig(motorConfig_t *motorConfig) #ifdef USE_DSHOT_BITBANG motorConfig->dev.useDshotBitbang = DSHOT_BITBANG_AUTO; + motorConfig->dev.useDshotBitbangedTimer = DSHOT_BITBANGED_TIMER_AUTO; #endif } diff --git a/src/main/pg/motor.h b/src/main/pg/motor.h index 1b386a61d..8322f9c0a 100644 --- a/src/main/pg/motor.h +++ b/src/main/pg/motor.h @@ -25,6 +25,12 @@ #include "drivers/io.h" #include "drivers/dshot_bitbang.h" +typedef enum { + DSHOT_BITBANGED_TIMER_AUTO = 0, + DSHOT_BITBANGED_TIMER_TIM1, + DSHOT_BITBANGED_TIMER_TIM8, +} dshotBitbangedTimer_e; + typedef struct motorDevConfig_s { uint16_t motorPwmRate; // The update rate of motor outputs (50-498Hz) uint8_t motorPwmProtocol; // Pwm Protocol @@ -35,6 +41,7 @@ typedef struct motorDevConfig_s { ioTag_t ioTags[MAX_SUPPORTED_MOTORS]; uint8_t motorTransportProtocol; uint8_t useDshotBitbang; + uint8_t useDshotBitbangedTimer; } motorDevConfig_t; typedef struct motorConfig_s { From 99bd45d13c1b28ff9f67401681d355fcfe6d4f30 Mon Sep 17 00:00:00 2001 From: Michael Keller Date: Tue, 10 Sep 2019 13:40:10 +1200 Subject: [PATCH 2/2] Added target specific defines for bitbanged Dshot. --- src/main/cli/settings.c | 4 ++-- src/main/pg/motor.c | 4 ++-- src/main/target/common_defaults_post.h | 10 ++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/cli/settings.c b/src/main/cli/settings.c index 9947ba306..14494340a 100644 --- a/src/main/cli/settings.c +++ b/src/main/cli/settings.c @@ -772,8 +772,8 @@ const clivalue_t valueTable[] = { { "dshot_bidir", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_MOTOR_CONFIG, offsetof(motorConfig_t, dev.useDshotTelemetry) }, #endif #ifdef USE_DSHOT_BITBANG - { "dshot_bitbang", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON_AUTO }, PG_MOTOR_CONFIG, offsetof(motorConfig_t, dev.useDshotBitbang) }, - { "dshot_bitbang_timer", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_DSHOT_BITBANGED_TIMER }, PG_MOTOR_CONFIG, offsetof(motorConfig_t, dev.useDshotBitbangedTimer) }, + { "dshot_bitbang", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON_AUTO }, PG_MOTOR_CONFIG, offsetof(motorConfig_t, dev.useDshotBitbang) }, + { "dshot_bitbang_timer", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_DSHOT_BITBANGED_TIMER }, PG_MOTOR_CONFIG, offsetof(motorConfig_t, dev.useDshotBitbangedTimer) }, #endif #endif { "use_unsynced_pwm", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_MOTOR_CONFIG, offsetof(motorConfig_t, dev.useUnsyncedPwm) }, diff --git a/src/main/pg/motor.c b/src/main/pg/motor.c index 7c219b18e..85968097e 100644 --- a/src/main/pg/motor.c +++ b/src/main/pg/motor.c @@ -75,8 +75,8 @@ void pgResetFn_motorConfig(motorConfig_t *motorConfig) motorConfig->motorPoleCount = 14; // Most brushes motors that we use are 14 poles #ifdef USE_DSHOT_BITBANG - motorConfig->dev.useDshotBitbang = DSHOT_BITBANG_AUTO; - motorConfig->dev.useDshotBitbangedTimer = DSHOT_BITBANGED_TIMER_AUTO; + motorConfig->dev.useDshotBitbang = DSHOT_BITBANG_DEFAULT; + motorConfig->dev.useDshotBitbangedTimer = DSHOT_BITBANGED_TIMER_DEFAULT; #endif } diff --git a/src/main/target/common_defaults_post.h b/src/main/target/common_defaults_post.h index 9033c7860..361c0bbc2 100644 --- a/src/main/target/common_defaults_post.h +++ b/src/main/target/common_defaults_post.h @@ -661,3 +661,13 @@ #endif #define MAX_SUPPORTED_SERVOS 8 #endif + +#if defined(USE_DSHOT_BITBANG) +#if !defined(DSHOT_BITBANG_DEFAULT) +#define DSHOT_BITBANG_DEFAULT DSHOT_BITBANG_AUTO +#endif + +#if !defined(DSHOT_BITBANGED_TIMER_DEFAULT) +#define DSHOT_BITBANGED_TIMER_DEFAULT DSHOT_BITBANGED_TIMER_AUTO +#endif +#endif // USE_DSHOT_BITBANG