From c8e61243008ea9f74fb6b744407fd5c629058d4a Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Mon, 17 Jul 2023 22:51:25 +0200 Subject: [PATCH] [MAINT] Add dshot_telemetry_start_margin setting (#12912) (#12964) --- src/main/cli/settings.c | 1 + src/main/drivers/dshot_bitbang_decode.c | 17 ++++++++++++++--- src/main/pg/motor.c | 19 ++++++++++++++++++- src/main/pg/motor.h | 1 + 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main/cli/settings.c b/src/main/cli/settings.c index 1aaba47b692..ee895408aeb 100644 --- a/src/main/cli/settings.c +++ b/src/main/cli/settings.c @@ -847,6 +847,7 @@ const clivalue_t valueTable[] = { #ifdef USE_DSHOT_BITBANG { "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) }, + { "dshot_telemetry_start_margin", VAR_UINT8 | HARDWARE_VALUE , .config.minmaxUnsigned = { 0, 100 }, PG_MOTOR_CONFIG, offsetof(motorConfig_t, dev.telemetryStartMargin) }, #endif #endif { PARAM_NAME_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/drivers/dshot_bitbang_decode.c b/src/main/drivers/dshot_bitbang_decode.c index 591436cdeea..8b723acd84b 100644 --- a/src/main/drivers/dshot_bitbang_decode.c +++ b/src/main/drivers/dshot_bitbang_decode.c @@ -49,7 +49,6 @@ uint16_t bbBuffer[134]; #define BITBAND_SRAM_BASE 0x22000000 #define BITBAND_SRAM(a,b) ((BITBAND_SRAM_BASE + (((a)-BITBAND_SRAM_REF)<<5) + ((b)<<2))) // Convert SRAM address -#define DSHOT_TELEMETRY_START_MARGIN 10 static uint8_t preambleSkip = 0; typedef struct bitBandWord_s { @@ -220,7 +219,13 @@ uint32_t decode_bb_bitband( uint16_t buffer[], uint32_t count, uint32_t bit) #endif // The anticipated edges were observed - preambleSkip = startMargin - DSHOT_TELEMETRY_START_MARGIN; + + // Attempt to skip the preamble ahead of the telemetry to save CPU + if (startMargin > motorConfig()->dev.telemetryStartMargin) { + preambleSkip = startMargin - motorConfig()->dev.telemetryStartMargin; + } else { + preambleSkip = 0; + } if (nlen > 0) { value <<= nlen; @@ -330,7 +335,13 @@ FAST_CODE uint32_t decode_bb( uint16_t buffer[], uint32_t count, uint32_t bit) } // The anticipated edges were observed - preambleSkip = startMargin - DSHOT_TELEMETRY_START_MARGIN; + + // Attempt to skip the preamble ahead of the telemetry to save CPU + if (startMargin > motorConfig()->dev.telemetryStartMargin) { + preambleSkip = startMargin - motorConfig()->dev.telemetryStartMargin; + } else { + preambleSkip = 0; + } if (nlen > 0) { value <<= nlen; diff --git a/src/main/pg/motor.c b/src/main/pg/motor.c index 3f70786fd0c..1707c64d96e 100644 --- a/src/main/pg/motor.c +++ b/src/main/pg/motor.c @@ -33,7 +33,23 @@ #include "pg/pg_ids.h" #include "pg/motor.h" -PG_REGISTER_WITH_RESET_FN(motorConfig_t, motorConfig, PG_MOTOR_CONFIG, 1); +#if !defined(DEFAULT_DSHOT_BITBANG) +#define DEFAULT_DSHOT_BITBANG DSHOT_BITBANG_AUTO +#endif + +#if !defined(DSHOT_BITBANGED_TIMER_DEFAULT) +#define DSHOT_BITBANGED_TIMER_DEFAULT DSHOT_BITBANGED_TIMER_AUTO +#endif + +#if !defined(DEFAULT_DSHOT_BURST) +#define DEFAULT_DSHOT_BURST DSHOT_DMAR_OFF +#endif + +#if !defined(DSHOT_TELEMETRY_START_MARGIN) +#define DSHOT_TELEMETRY_START_MARGIN 10 +#endif + +PG_REGISTER_WITH_RESET_FN(motorConfig_t, motorConfig, PG_MOTOR_CONFIG, 2); void pgResetFn_motorConfig(motorConfig_t *motorConfig) { @@ -81,6 +97,7 @@ void pgResetFn_motorConfig(motorConfig_t *motorConfig) #ifdef USE_DSHOT_BITBANG motorConfig->dev.useDshotBitbang = DSHOT_BITBANG_DEFAULT; motorConfig->dev.useDshotBitbangedTimer = DSHOT_BITBANGED_TIMER_DEFAULT; + motorConfig->dev.telemetryStartMargin = DSHOT_TELEMETRY_START_MARGIN; #endif } diff --git a/src/main/pg/motor.h b/src/main/pg/motor.h index a2ea0fb5515..929b4195676 100644 --- a/src/main/pg/motor.h +++ b/src/main/pg/motor.h @@ -50,6 +50,7 @@ typedef struct motorDevConfig_s { uint8_t useDshotBitbang; uint8_t useDshotBitbangedTimer; uint8_t motorOutputReordering[MAX_SUPPORTED_MOTORS]; // Reindexing motors for "remap motors" feature in Configurator + uint8_t telemetryStartMargin; } motorDevConfig_t; typedef struct motorConfig_s {