From 06ee4d00a087f0b51526d5388ef977693f2ad7bc Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Wed, 23 Jul 2025 00:12:08 +0200 Subject: [PATCH 1/9] ...use dshot_packet structure --- DShotRMT.cpp | 74 ++++++++++++++++++++++------------------------------ DShotRMT.h | 21 +++++++-------- 2 files changed, 41 insertions(+), 54 deletions(-) diff --git a/DShotRMT.cpp b/DShotRMT.cpp index 0d00a66..5b7056a 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -41,6 +41,8 @@ void DShotRMT::begin() _receive_config.signal_range_min_ns = 300; _receive_config.signal_range_max_ns = 5000; + + _dshot_packet.telemetric_request = _isBidirectional; } // Configure TX RMT Channel @@ -82,17 +84,15 @@ void DShotRMT::begin() // Encodes and transmits a valid DShot throttle value (48 - 2047) void DShotRMT::setThrottle(uint16_t throttle) { - // Clamp input range and mask to 11 bits - throttle = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0x7FF; + // Clamp input range for packet + _dshot_packet.throttle_value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0b0000011111111111; - _lastThrottle = throttle; - - // Convert throttle value to DShot packet format - _tx_packet = assambleDShotPaket(_lastThrottle); + // Calculate CRC for every throttle value + calculateCRC(&_dshot_packet); // Encode RMT symbols size_t count = 0; - encodeDShotTX(_tx_packet, _tx_symbols, count); + encodeDShotTX(&_dshot_packet, _tx_symbols, count); // Transmit the packet if (rmt_transmit(_rmt_tx_channel, _dshot_encoder, _tx_symbols, count * sizeof(rmt_symbol_word_t), &_transmit_config) != 0) @@ -111,13 +111,17 @@ uint32_t DShotRMT::getERPM() if (_isBidirectional) { if (_rmt_rx_channel == nullptr) + { Serial.println("No bidirectional DShot support."); return _last_erpm; + } // Try to receive a new frame if (!rmt_receive(_rmt_rx_channel, _rx_symbols, sizeof(_rx_symbols), &_receive_config)) + { Serial.println("No valid DShot frame received"); return _last_erpm; + } _last_erpm = decodeDShotRX(_rx_symbols, DSHOT_BITS_PER_FRAME); return _last_erpm; @@ -138,45 +142,35 @@ uint32_t DShotRMT::getMotorRPM(uint8_t magnet_count) } // Calculates CRC for DShot packet -uint16_t DShotRMT::calculateCRC(uint16_t dshot_packet) +void DShotRMT::calculateCRC(dshot_packet_t *dshot_packet) { - uint16_t packet = (dshot_packet << 1) | (_isBidirectional ? 1 : 0); + uint16_t packet = (dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request); // Reset CRC container - _packet_crc = DSHOT_NULL_PACKET; + dshot_packet->checksum = DSHOT_NULL_PACKET; // CRC calculation for DShot (4 bits) - _packet_crc = ((packet ^ (packet >> 4) ^ (packet >> 8)) & 0xF); + dshot_packet->checksum = ((packet ^ (packet >> 4) ^ (packet >> 8)) & 0b0000000000001111); // CRC is inverted for bidirectional DShot - if (_isBidirectional) - _packet_crc = (~_packet_crc) & 0xF; - - return _packet_crc; + if (dshot_packet->telemetric_request) + dshot_packet->checksum = (~dshot_packet->checksum) & 0b0000000000001111; } // Assembles DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC) -uint16_t DShotRMT::assambleDShotPaket(uint16_t value) +uint16_t DShotRMT::parseDShotPacket(const dshot_packet_t *dshot_packet) const { - uint16_t throttle = value & 0x7FF; - - // Reset packet container - _tx_packet = DSHOT_NULL_PACKET; - - // Assemble raw DShot packet and add checksum - _packet_crc = calculateCRC(throttle); - - _tx_packet = (throttle << 1) | (_isBidirectional ? 1 : 0); - _tx_packet = (_tx_packet << 4) | _packet_crc; - - return _tx_packet; + uint16_t parsed_packet = ((dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request)) & 0b0000111111111111; + return ((parsed_packet << 4) | (dshot_packet->checksum)) & 0b1111111111111111; } // Converts a 16-bit packet into a valid DShot frame for RMT -void DShotRMT::encodeDShotTX(uint16_t dshot_packet, rmt_symbol_word_t *symbols, size_t &count) +void DShotRMT::encodeDShotTX(dshot_packet_t *dshot_packet, rmt_symbol_word_t *symbols, size_t &count) { count = 0; + uint16_t frame_bits = parseDShotPacket(dshot_packet); + uint32_t ticks_per_bit = 0; uint32_t ticks_zero_high = 0; uint32_t ticks_one_high = 0; @@ -205,20 +199,16 @@ void DShotRMT::encodeDShotTX(uint16_t dshot_packet, rmt_symbol_word_t *symbols, ticks_one_high = 6; break; case DSHOT_OFF: - default: - ticks_per_bit = 0; - ticks_zero_high = 0; - ticks_one_high = 0; - break; + return; } uint32_t ticks_zero_low = ticks_per_bit - ticks_zero_high; uint32_t ticks_one_low = ticks_per_bit - ticks_one_high; - // Fill the 16 DShot bits array with selected timings + // Convert the parsed dshot frame to rmt_tx data for (int i = 15; i >= 0; i--) { - bool bit = (dshot_packet >> i) & 0x1; + bool bit = (frame_bits >> i) & 0x1; if (_isBidirectional) { symbols[count].level0 = 0; @@ -251,12 +241,12 @@ uint16_t DShotRMT::decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t coun // Extract CRC and payload uint16_t payload = received_frame >> 4; - uint8_t crc_received = received_frame & 0xF; + uint8_t crc_received = received_frame & 0b0000000000001111; // Calculate CRC for received frame - uint8_t crc_calculated = (payload ^ (payload >> 4) ^ (payload >> 8)) & 0xF; + uint8_t crc_calculated = (payload ^ (payload >> 4) ^ (payload >> 8)) & 0b0000000000001111; if (_isBidirectional) - crc_calculated = (~crc_calculated) & 0xF; + crc_calculated = (~crc_calculated) & 0b0000000000001111; // Check CRC if (crc_received != crc_calculated) @@ -265,8 +255,6 @@ uint16_t DShotRMT::decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t coun return _last_erpm; } - // Remove telemetry bit, keep raw value - uint16_t raw = payload >> 1; - - return _last_erpm = raw; + // Remove telemetry bit + return _last_erpm = payload >> 1; } diff --git a/DShotRMT.h b/DShotRMT.h index a512525..e00d1e4 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -32,15 +32,15 @@ static constexpr size_t TX_BUFFER_SIZE = DSHOT_BITS_PER_FRAME; static constexpr size_t RX_BUFFER_SIZE = 32; // Padding for RX decoding // DShot Packet structure -typedef struct +typedef struct dshot_packet_s { uint16_t throttle_value : 11; bool telemetric_request : 1; - uint16_t checksum : 4; + uint8_t checksum : 4; } dshot_packet_t; // --- DShot Mode Selection --- -typedef enum +typedef enum dshot_mode_s { DSHOT_OFF, DSHOT150, @@ -72,19 +72,20 @@ public: uint8_t getPauseDuration() const { return _pauseDuration; } void setPauseDuration(uint8_t pauseDuration) { _pauseDuration = pauseDuration; } -private: +protected: // Calculates the checksum for a DShot packet - uint16_t calculateCRC(uint16_t dshot_packet); + void calculateCRC(dshot_packet_t *dshot_packet); - // Assembles DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC) - uint16_t assambleDShotPaket(uint16_t value); + // Parses the DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC) + uint16_t parseDShotPacket(const dshot_packet_t *dshot_packet) const; // Converts a 16-bit DShot packet into RMT symbols - void encodeDShotTX(uint16_t dshot_packet, rmt_symbol_word_t *symbols, size_t &count); + void encodeDShotTX(dshot_packet_t *dshot_packet, rmt_symbol_word_t *symbols, size_t &count); // Decodes the ESC answer uint16_t decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t count); +private: // --- Configuration Parameters --- gpio_num_t _gpio; dshot_mode_t _mode; @@ -92,10 +93,8 @@ private: uint8_t _pauseDuration; // --- DShot Packets Container --- - uint16_t _lastThrottle = DSHOT_NULL_PACKET; uint16_t _rx_packet = DSHOT_NULL_PACKET; - uint16_t _tx_packet = DSHOT_NULL_PACKET; - uint8_t _packet_crc = 0; + dshot_packet_t _dshot_packet = {}; // --- RMT Channel Handles --- rmt_channel_handle_t _rmt_rx_channel = nullptr; From 4db92a9b31de65c52fbc13635fb5b47a8878e006 Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Fri, 25 Jul 2025 12:15:52 +0200 Subject: [PATCH 2/9] ...non-blocking interval ...not using "delay" anymore --- DShotRMT.cpp | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/DShotRMT.cpp b/DShotRMT.cpp index 5b7056a..2f8579a 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -84,25 +84,31 @@ void DShotRMT::begin() // Encodes and transmits a valid DShot throttle value (48 - 2047) void DShotRMT::setThrottle(uint16_t throttle) { - // Clamp input range for packet - _dshot_packet.throttle_value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0b0000011111111111; + // Simple timer + static long last_time = 0; - // Calculate CRC for every throttle value - calculateCRC(&_dshot_packet); - - // Encode RMT symbols - size_t count = 0; - encodeDShotTX(&_dshot_packet, _tx_symbols, count); - - // Transmit the packet - if (rmt_transmit(_rmt_tx_channel, _dshot_encoder, _tx_symbols, count * sizeof(rmt_symbol_word_t), &_transmit_config) != 0) + // Keep a pause between the frames + if (micros() - last_time >= _pauseDuration) { - Serial.println("Failed to transmit DShot packet"); - return; - } + last_time = micros(); - // Pause between frames - esp_rom_delay_us(_pauseDuration); + // Clamp input range for throttle value + _dshot_packet.throttle_value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0b0000011111111111; + + // Calculate CRC for every throttle value + calculateCRC(&_dshot_packet); + + // Encode RMT symbols + size_t count = 0; + encodeDShotTX(&_dshot_packet, _tx_symbols, count); + + // Transmit the packet + if (rmt_transmit(_rmt_tx_channel, _dshot_encoder, _tx_symbols, count * sizeof(rmt_symbol_word_t), &_transmit_config) != 0) + { + Serial.println("Failed to transmit DShot packet"); + return; + } + } } // Receives and decodes a response frame from ESC containing eRPM info From 90eaecd61d216795d766f9daffd1df6e0fe15410 Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Fri, 25 Jul 2025 17:41:38 +0200 Subject: [PATCH 3/9] ...fixed frame lenght ....fixed and fixed frame lenght --- DShotRMT.cpp | 47 +++++++++++++++++++++++++++++++++++++++-------- DShotRMT.h | 7 +++---- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/DShotRMT.cpp b/DShotRMT.cpp index 2f8579a..3fd7ab3 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -12,8 +12,38 @@ // This class provides an abstraction for sending and optionally receiving DShot frames. // It uses ESP32's RMT peripheral for precise timing control, including BiDirectional RX. -DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional, uint8_t pauseDuration) - : _gpio(gpio), _mode(mode), _isBidirectional(isBidirectional), _pauseDuration(pauseDuration) {} +DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional) + : _gpio(gpio), _mode(mode), _isBidirectional(isBidirectional) +{ + + // Setting up fixed DShot Frame length + switch (_mode) + { + case DSHOT_OFF: + _frameLenght = 0; + break; + case DSHOT150: + _frameLenght = 128; + break; + case DSHOT300: + _frameLenght = 64; + break; + case DSHOT600: + _frameLenght = 32; + break; + case DSHOT1200: + _frameLenght = 16; + break; + default: + break; + } + + // DShot Frame length incl. DShot answer duration + if (_isBidirectional) + { + _frameLenght += _frameLenght; + } +} // Initializes RMT TX and RX channels and encoder configuration void DShotRMT::begin() @@ -85,13 +115,11 @@ void DShotRMT::begin() void DShotRMT::setThrottle(uint16_t throttle) { // Simple timer - static long last_time = 0; + static unsigned long last_time = NULL; - // Keep a pause between the frames - if (micros() - last_time >= _pauseDuration) + // Ensure frame lenght for compatibility + if (micros() - last_time >= _frameLenght) { - last_time = micros(); - // Clamp input range for throttle value _dshot_packet.throttle_value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0b0000011111111111; @@ -99,7 +127,7 @@ void DShotRMT::setThrottle(uint16_t throttle) calculateCRC(&_dshot_packet); // Encode RMT symbols - size_t count = 0; + size_t count = NULL; encodeDShotTX(&_dshot_packet, _tx_symbols, count); // Transmit the packet @@ -108,6 +136,9 @@ void DShotRMT::setThrottle(uint16_t throttle) Serial.println("Failed to transmit DShot packet"); return; } + + // Timestamp + last_time = micros(); } } diff --git a/DShotRMT.h b/DShotRMT.h index e00d1e4..2cec328 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -54,7 +54,7 @@ class DShotRMT { public: // Constructor: initializes configuration state - DShotRMT(gpio_num_t gpio, dshot_mode_t mode = DSHOT300, bool isBidirectional = false, uint8_t pauseDuration = 120); + DShotRMT(gpio_num_t gpio, dshot_mode_t mode = DSHOT300, bool isBidirectional = false); // Initializes the RMT TX and RX channels void begin(); @@ -69,8 +69,7 @@ public: // Accessors for GPIO and DShot settings gpio_num_t getGPIO() const { return _gpio; } dshot_mode_t getDShotMode() const { return _mode; } - uint8_t getPauseDuration() const { return _pauseDuration; } - void setPauseDuration(uint8_t pauseDuration) { _pauseDuration = pauseDuration; } + uint8_t getFrameLenght() const { return _frameLenght; } protected: // Calculates the checksum for a DShot packet @@ -90,7 +89,7 @@ private: gpio_num_t _gpio; dshot_mode_t _mode; bool _isBidirectional; - uint8_t _pauseDuration; + uint16_t _frameLenght; // --- DShot Packets Container --- uint16_t _rx_packet = DSHOT_NULL_PACKET; From 4b809d26e112eb0cbecb96d09320040b18083fdb Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Fri, 25 Jul 2025 21:04:27 +0200 Subject: [PATCH 4/9] ...cosmetics --- DShotRMT.cpp | 17 ++++++++++------- DShotRMT.h | 13 +++++++------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/DShotRMT.cpp b/DShotRMT.cpp index 3fd7ab3..d2deeff 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -20,19 +20,19 @@ DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional) switch (_mode) { case DSHOT_OFF: - _frameLenght = 0; + _frameLength = 0; break; case DSHOT150: - _frameLenght = 128; + _frameLength = 128; break; case DSHOT300: - _frameLenght = 64; + _frameLength = 64; break; case DSHOT600: - _frameLenght = 32; + _frameLength = 32; break; case DSHOT1200: - _frameLenght = 16; + _frameLength = 16; break; default: break; @@ -41,8 +41,11 @@ DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional) // DShot Frame length incl. DShot answer duration if (_isBidirectional) { - _frameLenght += _frameLenght; + _frameLength += _frameLength; } + + // Add frame tolerance + _frameLength = _frameLength + DSHOT_SWITCH_TIME; } // Initializes RMT TX and RX channels and encoder configuration @@ -118,7 +121,7 @@ void DShotRMT::setThrottle(uint16_t throttle) static unsigned long last_time = NULL; // Ensure frame lenght for compatibility - if (micros() - last_time >= _frameLenght) + if (micros() - last_time >= _frameLength) { // Clamp input range for throttle value _dshot_packet.throttle_value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0b0000011111111111; diff --git a/DShotRMT.h b/DShotRMT.h index 2cec328..c154ea2 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -19,6 +19,7 @@ static constexpr uint16_t DSHOT_THROTTLE_FAILSAVE = 0; static constexpr uint16_t DSHOT_THROTTLE_MIN = 48; static constexpr uint16_t DSHOT_THROTTLE_MAX = 2047; static constexpr uint8_t DSHOT_BITS_PER_FRAME = 16; +static constexpr uint8_t DSHOT_SWITCH_TIME = 21; static constexpr uint16_t DSHOT_NULL_PACKET = 0x0000; static constexpr uint16_t DSHOT_FULL_PACKET = 0xFFFF; @@ -69,7 +70,7 @@ public: // Accessors for GPIO and DShot settings gpio_num_t getGPIO() const { return _gpio; } dshot_mode_t getDShotMode() const { return _mode; } - uint8_t getFrameLenght() const { return _frameLenght; } + uint8_t getFrameLenght() const { return _frameLength; } protected: // Calculates the checksum for a DShot packet @@ -86,10 +87,10 @@ protected: private: // --- Configuration Parameters --- - gpio_num_t _gpio; - dshot_mode_t _mode; - bool _isBidirectional; - uint16_t _frameLenght; + gpio_num_t _gpio = GPIO_NUM_NC; + dshot_mode_t _mode = DSHOT_OFF; + bool _isBidirectional = false; + uint16_t _frameLength = NULL; // --- DShot Packets Container --- uint16_t _rx_packet = DSHOT_NULL_PACKET; @@ -113,5 +114,5 @@ private: rmt_symbol_word_t _tx_symbols[TX_BUFFER_SIZE] = {}; // Stores the last valid eRPM received from the ESC - uint16_t _last_erpm = 0; + uint16_t _last_erpm = NULL; }; From ebbeb99d4fd2561d95f676a43637ddb6262791f8 Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Fri, 25 Jul 2025 21:06:48 +0200 Subject: [PATCH 5/9] ...fix Accessors --- DShotRMT.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DShotRMT.h b/DShotRMT.h index c154ea2..049aab6 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -70,7 +70,7 @@ public: // Accessors for GPIO and DShot settings gpio_num_t getGPIO() const { return _gpio; } dshot_mode_t getDShotMode() const { return _mode; } - uint8_t getFrameLenght() const { return _frameLength; } + uint16_t getFrameLenght() const { return _frameLength; } protected: // Calculates the checksum for a DShot packet From 3a80bdef750de4f85d9c5832bb8876f06f8029ff Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Fri, 25 Jul 2025 22:00:26 +0200 Subject: [PATCH 6/9] ...simplify packet assambly --- DShotRMT.cpp | 10 +++++----- DShotRMT.h | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/DShotRMT.cpp b/DShotRMT.cpp index d2deeff..673f28c 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -184,13 +184,13 @@ uint32_t DShotRMT::getMotorRPM(uint8_t magnet_count) // Calculates CRC for DShot packet void DShotRMT::calculateCRC(dshot_packet_t *dshot_packet) { - uint16_t packet = (dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request); + _parsed_dshot_tx_packet = (dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request); // Reset CRC container dshot_packet->checksum = DSHOT_NULL_PACKET; // CRC calculation for DShot (4 bits) - dshot_packet->checksum = ((packet ^ (packet >> 4) ^ (packet >> 8)) & 0b0000000000001111); + dshot_packet->checksum = ((_parsed_dshot_tx_packet ^ (_parsed_dshot_tx_packet >> 4) ^ (_parsed_dshot_tx_packet >> 8)) & 0b0000000000001111); // CRC is inverted for bidirectional DShot if (dshot_packet->telemetric_request) @@ -198,10 +198,10 @@ void DShotRMT::calculateCRC(dshot_packet_t *dshot_packet) } // Assembles DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC) -uint16_t DShotRMT::parseDShotPacket(const dshot_packet_t *dshot_packet) const +uint16_t DShotRMT::parseDShotPacket(const dshot_packet_t *dshot_packet) { - uint16_t parsed_packet = ((dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request)) & 0b0000111111111111; - return ((parsed_packet << 4) | (dshot_packet->checksum)) & 0b1111111111111111; + _parsed_dshot_tx_packet = ((dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request)) & 0b0000111111111111; + return ((_parsed_dshot_tx_packet << 4) | (dshot_packet->checksum)) & 0b1111111111111111; } // Converts a 16-bit packet into a valid DShot frame for RMT diff --git a/DShotRMT.h b/DShotRMT.h index 049aab6..c763e02 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -71,13 +71,14 @@ public: gpio_num_t getGPIO() const { return _gpio; } dshot_mode_t getDShotMode() const { return _mode; } uint16_t getFrameLenght() const { return _frameLength; } + uint16_t getDShotPacket() const { return _parsed_dshot_tx_packet; } -protected: +private: // Calculates the checksum for a DShot packet void calculateCRC(dshot_packet_t *dshot_packet); // Parses the DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC) - uint16_t parseDShotPacket(const dshot_packet_t *dshot_packet) const; + uint16_t parseDShotPacket(const dshot_packet_t *dshot_packet); // Converts a 16-bit DShot packet into RMT symbols void encodeDShotTX(dshot_packet_t *dshot_packet, rmt_symbol_word_t *symbols, size_t &count); @@ -85,8 +86,7 @@ protected: // Decodes the ESC answer uint16_t decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t count); -private: - // --- Configuration Parameters --- +// --- Configuration Parameters --- gpio_num_t _gpio = GPIO_NUM_NC; dshot_mode_t _mode = DSHOT_OFF; bool _isBidirectional = false; @@ -94,6 +94,7 @@ private: // --- DShot Packets Container --- uint16_t _rx_packet = DSHOT_NULL_PACKET; + uint16_t _parsed_dshot_tx_packet = DSHOT_NULL_PACKET; dshot_packet_t _dshot_packet = {}; // --- RMT Channel Handles --- From 2f59227788603e1101326b62a4028395be9f14b3 Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Wed, 30 Jul 2025 00:40:09 +0200 Subject: [PATCH 7/9] ...simplify --- DShotRMT.cpp | 82 +++++++++++++++++----------------- DShotRMT.h | 48 ++++++++++---------- examples/dshot300/dshot300.ino | 4 +- 3 files changed, 67 insertions(+), 67 deletions(-) diff --git a/DShotRMT.cpp b/DShotRMT.cpp index 673f28c..1acc3ec 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -58,7 +58,7 @@ void DShotRMT::begin() .gpio_num = _gpio, .clk_src = DSHOT_CLOCK_SRC_DEFAULT, .resolution_hz = DSHOT_RMT_RESOLUTION, - .mem_block_symbols = 64, + .mem_block_symbols = DSHOT_SYMBOLS_SIZE, }; if (rmt_new_rx_channel(&_rmt_rx_channel_config, &_rmt_rx_channel) != 0) @@ -66,6 +66,7 @@ void DShotRMT::begin() Serial.println("Failed to create RX channel"); return; } + if (rmt_enable(_rmt_rx_channel) != 0) { Serial.println("Failed to enable RX channel"); @@ -74,8 +75,6 @@ void DShotRMT::begin() _receive_config.signal_range_min_ns = 300; _receive_config.signal_range_max_ns = 5000; - - _dshot_packet.telemetric_request = _isBidirectional; } // Configure TX RMT Channel @@ -83,9 +82,8 @@ void DShotRMT::begin() .gpio_num = _gpio, .clk_src = DSHOT_CLOCK_SRC_DEFAULT, .resolution_hz = DSHOT_RMT_RESOLUTION, - .mem_block_symbols = 64, - .trans_queue_depth = 10, - }; + .mem_block_symbols = DSHOT_SYMBOLS_SIZE, + .trans_queue_depth = TX_BUFFER_SIZE}; // Transmission configuration _transmit_config.loop_count = 0; @@ -96,6 +94,7 @@ void DShotRMT::begin() Serial.println("Failed to create TX channel"); return; } + if (rmt_enable(_rmt_tx_channel) != 0) { Serial.println("Failed to enable TX channel"); @@ -120,21 +119,22 @@ void DShotRMT::setThrottle(uint16_t throttle) // Simple timer static unsigned long last_time = NULL; + // + dshot_packet_t packet = {}; + + packet.throttle_value = (constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0b0000011111111111); + packet.telemetric_request = _isBidirectional; + packet.checksum = calculateCRC(packet); + + // DShot transcoding + rmt_symbol_word_t tx_symbols[DSHOT_BITS_PER_FRAME] = {}; + encodeDShotTX(packet, tx_symbols); + // Ensure frame lenght for compatibility if (micros() - last_time >= _frameLength) { - // Clamp input range for throttle value - _dshot_packet.throttle_value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX) & 0b0000011111111111; - - // Calculate CRC for every throttle value - calculateCRC(&_dshot_packet); - - // Encode RMT symbols - size_t count = NULL; - encodeDShotTX(&_dshot_packet, _tx_symbols, count); - // Transmit the packet - if (rmt_transmit(_rmt_tx_channel, _dshot_encoder, _tx_symbols, count * sizeof(rmt_symbol_word_t), &_transmit_config) != 0) + if (rmt_transmit(_rmt_tx_channel, _dshot_encoder, tx_symbols, DSHOT_SYMBOLS_SIZE, &_transmit_config) != 0) { Serial.println("Failed to transmit DShot packet"); return; @@ -157,7 +157,7 @@ uint32_t DShotRMT::getERPM() } // Try to receive a new frame - if (!rmt_receive(_rmt_rx_channel, _rx_symbols, sizeof(_rx_symbols), &_receive_config)) + if (!rmt_receive(_rmt_rx_channel, _rx_symbols, DSHOT_SYMBOLS_SIZE, &_receive_config)) { Serial.println("No valid DShot frame received"); return _last_erpm; @@ -182,38 +182,33 @@ uint32_t DShotRMT::getMotorRPM(uint8_t magnet_count) } // Calculates CRC for DShot packet -void DShotRMT::calculateCRC(dshot_packet_t *dshot_packet) +uint16_t DShotRMT::calculateCRC(dshot_packet_t dshot_packet) { - _parsed_dshot_tx_packet = (dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request); - - // Reset CRC container - dshot_packet->checksum = DSHOT_NULL_PACKET; + uint16_t crc = (dshot_packet.throttle_value << 1) | (dshot_packet.telemetric_request); // CRC calculation for DShot (4 bits) - dshot_packet->checksum = ((_parsed_dshot_tx_packet ^ (_parsed_dshot_tx_packet >> 4) ^ (_parsed_dshot_tx_packet >> 8)) & 0b0000000000001111); + dshot_packet.checksum = ((crc ^ (crc >> 4) ^ (crc >> 8)) & 0b0000000000001111); // CRC is inverted for bidirectional DShot - if (dshot_packet->telemetric_request) - dshot_packet->checksum = (~dshot_packet->checksum) & 0b0000000000001111; + if (dshot_packet.telemetric_request) + dshot_packet.checksum = (~dshot_packet.checksum) & 0b0000000000001111; + + return dshot_packet.checksum; } // Assembles DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC) -uint16_t DShotRMT::parseDShotPacket(const dshot_packet_t *dshot_packet) +uint16_t DShotRMT::parseDShotPacket(const dshot_packet_t dshot_packet) { - _parsed_dshot_tx_packet = ((dshot_packet->throttle_value << 1) | (dshot_packet->telemetric_request)) & 0b0000111111111111; - return ((_parsed_dshot_tx_packet << 4) | (dshot_packet->checksum)) & 0b1111111111111111; + uint16_t raw = (((dshot_packet.throttle_value << 1) | (dshot_packet.telemetric_request)) & 0b0000111111111111); + return (((raw << 4) | (dshot_packet.checksum)) & 0b1111111111111111); } // Converts a 16-bit packet into a valid DShot frame for RMT -void DShotRMT::encodeDShotTX(dshot_packet_t *dshot_packet, rmt_symbol_word_t *symbols, size_t &count) +void DShotRMT::encodeDShotTX(dshot_packet_t dshot_packet, rmt_symbol_word_t *symbols) { - count = 0; - - uint16_t frame_bits = parseDShotPacket(dshot_packet); - - uint32_t ticks_per_bit = 0; - uint32_t ticks_zero_high = 0; - uint32_t ticks_one_high = 0; + uint16_t ticks_per_bit = 0; + uint16_t ticks_zero_high = 0; + uint16_t ticks_one_high = 0; // Select timing based on DShot mode switch (_mode) @@ -242,13 +237,18 @@ void DShotRMT::encodeDShotTX(dshot_packet_t *dshot_packet, rmt_symbol_word_t *sy return; } - uint32_t ticks_zero_low = ticks_per_bit - ticks_zero_high; - uint32_t ticks_one_low = ticks_per_bit - ticks_one_high; + uint16_t ticks_zero_low = ticks_per_bit - ticks_zero_high; + uint16_t ticks_one_low = ticks_per_bit - ticks_one_high; + + uint16_t frame_bits = parseDShotPacket(dshot_packet); + + // Always start with the "first" bit + size_t count = NULL; // Convert the parsed dshot frame to rmt_tx data - for (int i = 15; i >= 0; i--) + for (int i = DSHOT_BITS_PER_FRAME - 1; i >= 0; i--) { - bool bit = (frame_bits >> i) & 0x1; + bool bit = (frame_bits >> i) & 0b0000000000000001; if (_isBidirectional) { symbols[count].level0 = 0; diff --git a/DShotRMT.h b/DShotRMT.h index c763e02..aff4be9 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -14,30 +14,20 @@ #include #include +static constexpr bool DSHOT_OK = 0; +static constexpr bool DSHOT_FAILED = 1; + // --- DShot Protocol Constants --- static constexpr uint16_t DSHOT_THROTTLE_FAILSAVE = 0; static constexpr uint16_t DSHOT_THROTTLE_MIN = 48; static constexpr uint16_t DSHOT_THROTTLE_MAX = 2047; -static constexpr uint8_t DSHOT_BITS_PER_FRAME = 16; -static constexpr uint8_t DSHOT_SWITCH_TIME = 21; - -static constexpr uint16_t DSHOT_NULL_PACKET = 0x0000; -static constexpr uint16_t DSHOT_FULL_PACKET = 0xFFFF; -static constexpr uint16_t NO_ERPM_SIGNAL = 0; - -// RMT configuration parameters -static constexpr rmt_clock_source_t DSHOT_CLOCK_SRC_DEFAULT = RMT_CLK_SRC_DEFAULT; -static constexpr uint32_t DSHOT_RMT_RESOLUTION = 10 * 1000 * 1000; // 10 MHz Clock - -static constexpr size_t TX_BUFFER_SIZE = DSHOT_BITS_PER_FRAME; -static constexpr size_t RX_BUFFER_SIZE = 32; // Padding for RX decoding // DShot Packet structure typedef struct dshot_packet_s { uint16_t throttle_value : 11; bool telemetric_request : 1; - uint8_t checksum : 4; + uint16_t checksum : 4; } dshot_packet_t; // --- DShot Mode Selection --- @@ -47,7 +37,8 @@ typedef enum dshot_mode_s DSHOT150, DSHOT300, DSHOT600, - DSHOT1200 + DSHOT1200, + COUNTER } dshot_mode_t; // --- DShotRMT Class --- @@ -71,22 +62,36 @@ public: gpio_num_t getGPIO() const { return _gpio; } dshot_mode_t getDShotMode() const { return _mode; } uint16_t getFrameLenght() const { return _frameLength; } - uint16_t getDShotPacket() const { return _parsed_dshot_tx_packet; } private: + static constexpr uint8_t DSHOT_BITS_PER_FRAME = 16; + static constexpr uint8_t DSHOT_SWITCH_TIME = 21; + + static constexpr uint16_t DSHOT_NULL_PACKET = 0b000000000000000; + static constexpr uint16_t DSHOT_FULL_PACKET = 0b111111111111111; + static constexpr uint16_t NO_ERPM_SIGNAL = 0; + + // RMT configuration parameters + static constexpr rmt_clock_source_t DSHOT_CLOCK_SRC_DEFAULT = RMT_CLK_SRC_DEFAULT; + static constexpr uint32_t DSHOT_RMT_RESOLUTION = 10 * 1000 * 1000; // 10 MHz Clock + + static constexpr size_t TX_BUFFER_SIZE = DSHOT_BITS_PER_FRAME; + static constexpr size_t RX_BUFFER_SIZE = 32; + static constexpr size_t DSHOT_SYMBOLS_SIZE = 64; + // Calculates the checksum for a DShot packet - void calculateCRC(dshot_packet_t *dshot_packet); + uint16_t calculateCRC(dshot_packet_t dshot_packet); // Parses the DShot packet (11 bit throttle + 1 bit telemetry request + 4 bit CRC) - uint16_t parseDShotPacket(const dshot_packet_t *dshot_packet); + uint16_t parseDShotPacket(const dshot_packet_t dshot_packet); // Converts a 16-bit DShot packet into RMT symbols - void encodeDShotTX(dshot_packet_t *dshot_packet, rmt_symbol_word_t *symbols, size_t &count); + void encodeDShotTX(dshot_packet_t dshot_packet, rmt_symbol_word_t *symbols); // Decodes the ESC answer uint16_t decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t count); -// --- Configuration Parameters --- + // --- Configuration Parameters --- gpio_num_t _gpio = GPIO_NUM_NC; dshot_mode_t _mode = DSHOT_OFF; bool _isBidirectional = false; @@ -94,8 +99,6 @@ private: // --- DShot Packets Container --- uint16_t _rx_packet = DSHOT_NULL_PACKET; - uint16_t _parsed_dshot_tx_packet = DSHOT_NULL_PACKET; - dshot_packet_t _dshot_packet = {}; // --- RMT Channel Handles --- rmt_channel_handle_t _rmt_rx_channel = nullptr; @@ -112,7 +115,6 @@ private: // --- RMT Symbol Buffers --- rmt_symbol_word_t _rx_symbols[RX_BUFFER_SIZE] = {}; - rmt_symbol_word_t _tx_symbols[TX_BUFFER_SIZE] = {}; // Stores the last valid eRPM received from the ESC uint16_t _last_erpm = NULL; diff --git a/examples/dshot300/dshot300.ino b/examples/dshot300/dshot300.ino index 368ca62..79b5d2a 100644 --- a/examples/dshot300/dshot300.ino +++ b/examples/dshot300/dshot300.ino @@ -73,12 +73,10 @@ uint16_t readSerialThrottle() String input = USB_SERIAL.readStringUntil('\n'); int throttle_input = input.toInt(); - // Clamp the value to the DShot range - throttle_input = constrain(throttle_input, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX); - if (throttle_input < DSHOT_THROTTLE_MIN || throttle_input > DSHOT_THROTTLE_MAX) { USB_SERIAL.println("Invalid input. Please enter a value between 48 and 2047."); + return last_throttle; } else { From 11cc41f7427447efed81b85b2f4d214d6466a19a Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Wed, 30 Jul 2025 14:44:01 +0200 Subject: [PATCH 8/9] ...DShot Mode rework --- DShotRMT.cpp | 78 +++++----------------------------- DShotRMT.h | 40 ++++++++++++----- README.md | 2 +- examples/dshot300/dshot300.ino | 9 ++-- 4 files changed, 47 insertions(+), 82 deletions(-) diff --git a/DShotRMT.cpp b/DShotRMT.cpp index 1acc3ec..4fcaa1f 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -15,37 +15,14 @@ DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional) : _gpio(gpio), _mode(mode), _isBidirectional(isBidirectional) { + // Fixed Timings for compatibility + _frame_time = dshot_times.frameLength + DSHOT_SWITCH_TIME; - // Setting up fixed DShot Frame length - switch (_mode) - { - case DSHOT_OFF: - _frameLength = 0; - break; - case DSHOT150: - _frameLength = 128; - break; - case DSHOT300: - _frameLength = 64; - break; - case DSHOT600: - _frameLength = 32; - break; - case DSHOT1200: - _frameLength = 16; - break; - default: - break; - } - - // DShot Frame length incl. DShot answer duration + // DShot Frame answer padding if (_isBidirectional) { - _frameLength += _frameLength; + _frame_time += _frame_time; } - - // Add frame tolerance - _frameLength = _frameLength + DSHOT_SWITCH_TIME; } // Initializes RMT TX and RX channels and encoder configuration @@ -86,7 +63,7 @@ void DShotRMT::begin() .trans_queue_depth = TX_BUFFER_SIZE}; // Transmission configuration - _transmit_config.loop_count = 0; + _transmit_config.loop_count = NULL; _transmit_config.flags.eot_level = _isBidirectional; if (rmt_new_tx_channel(&_rmt_tx_channel_config, &_rmt_tx_channel) != 0) @@ -131,7 +108,7 @@ void DShotRMT::setThrottle(uint16_t throttle) encodeDShotTX(packet, tx_symbols); // Ensure frame lenght for compatibility - if (micros() - last_time >= _frameLength) + if (micros() - last_time >= _frame_time) { // Transmit the packet if (rmt_transmit(_rmt_tx_channel, _dshot_encoder, tx_symbols, DSHOT_SYMBOLS_SIZE, &_transmit_config) != 0) @@ -206,40 +183,7 @@ uint16_t DShotRMT::parseDShotPacket(const dshot_packet_t dshot_packet) // Converts a 16-bit packet into a valid DShot frame for RMT void DShotRMT::encodeDShotTX(dshot_packet_t dshot_packet, rmt_symbol_word_t *symbols) { - uint16_t ticks_per_bit = 0; - uint16_t ticks_zero_high = 0; - uint16_t ticks_one_high = 0; - - // Select timing based on DShot mode - switch (_mode) - { - case DSHOT150: - ticks_per_bit = 64; - ticks_zero_high = 24; - ticks_one_high = 48; - break; - case DSHOT300: - ticks_per_bit = 32; - ticks_zero_high = 12; - ticks_one_high = 24; - break; - case DSHOT600: - ticks_per_bit = 16; - ticks_zero_high = 6; - ticks_one_high = 12; - break; - case DSHOT1200: - ticks_per_bit = 8; - ticks_zero_high = 3; - ticks_one_high = 6; - break; - case DSHOT_OFF: - return; - } - - uint16_t ticks_zero_low = ticks_per_bit - ticks_zero_high; - uint16_t ticks_one_low = ticks_per_bit - ticks_one_high; - + // Encoding to "raw" DShot Packet uint16_t frame_bits = parseDShotPacket(dshot_packet); // Always start with the "first" bit @@ -252,16 +196,16 @@ void DShotRMT::encodeDShotTX(dshot_packet_t dshot_packet, rmt_symbol_word_t *sym if (_isBidirectional) { symbols[count].level0 = 0; - symbols[count].duration0 = bit ? ticks_one_high : ticks_zero_high; + symbols[count].duration0 = bit ? dshot_times.ticks_one_high : dshot_times.ticks_zero_high; symbols[count].level1 = 1; - symbols[count].duration1 = bit ? ticks_one_low : ticks_zero_low; + symbols[count].duration1 = bit ? dshot_times.ticks_one_low : dshot_times.ticks_zero_low; } else { symbols[count].level0 = 1; - symbols[count].duration0 = bit ? ticks_one_high : ticks_zero_high; + symbols[count].duration0 = bit ? dshot_times.ticks_one_high : dshot_times.ticks_zero_high; symbols[count].level1 = 0; - symbols[count].duration1 = bit ? ticks_one_low : ticks_zero_low; + symbols[count].duration1 = bit ? dshot_times.ticks_one_low : dshot_times.ticks_zero_low; } count++; } diff --git a/DShotRMT.h b/DShotRMT.h index aff4be9..6cc0ece 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -37,10 +37,30 @@ typedef enum dshot_mode_s DSHOT150, DSHOT300, DSHOT600, - DSHOT1200, - COUNTER + DSHOT1200 } dshot_mode_t; +// --- DShot Timings --- +typedef struct dshot_timing_s +{ + uint16_t frameLength; + uint16_t ticks_per_bit; + uint16_t ticks_one_high; + uint16_t ticks_zero_high; + uint16_t ticks_zero_low; + uint16_t ticks_one_low; +} dshot_timing_t; + +// DShot Pulse Length Settings +const dshot_timing_t dshot_timings[] = { + {0, 0, 0, 0, 0, 0}, // DSHOT_OFF + {128, 64, 48, 24, 40, 16}, // DSHOT150 + {64, 32, 24, 12, 20, 8}, // DSHOT300 + {32, 16, 12, 6, 10, 4}, // DSHOT600 + {16, 8, 6, 3, 5, 2} // DSHOT1200 +}; + +// // --- DShotRMT Class --- class DShotRMT { @@ -61,11 +81,17 @@ public: // Accessors for GPIO and DShot settings gpio_num_t getGPIO() const { return _gpio; } dshot_mode_t getDShotMode() const { return _mode; } - uint16_t getFrameLenght() const { return _frameLength; } + + // --- Configuration Parameters --- + gpio_num_t _gpio = GPIO_NUM_NC; + dshot_mode_t _mode = DSHOT_OFF; + bool _isBidirectional = false; + uint16_t _frame_time; + const dshot_timing_t &dshot_times = dshot_timings[_mode]; private: static constexpr uint8_t DSHOT_BITS_PER_FRAME = 16; - static constexpr uint8_t DSHOT_SWITCH_TIME = 21; + static constexpr uint8_t DSHOT_SWITCH_TIME = 42; static constexpr uint16_t DSHOT_NULL_PACKET = 0b000000000000000; static constexpr uint16_t DSHOT_FULL_PACKET = 0b111111111111111; @@ -91,12 +117,6 @@ private: // Decodes the ESC answer uint16_t decodeDShotRX(const rmt_symbol_word_t *symbols, uint32_t count); - // --- Configuration Parameters --- - gpio_num_t _gpio = GPIO_NUM_NC; - dshot_mode_t _mode = DSHOT_OFF; - bool _isBidirectional = false; - uint16_t _frameLength = NULL; - // --- DShot Packets Container --- uint16_t _rx_packet = DSHOT_NULL_PACKET; diff --git a/README.md b/README.md index a35443c..1fd1186 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ Perfect for DShot: ## 📝 API Reference -- `DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional, uint8_t pauseDuration = 120)` +- `DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool isBidirectional)` - `void begin()` - `void setThrottle(uint16_t throttle)` - `uint32_t getERPM()` diff --git a/examples/dshot300/dshot300.ino b/examples/dshot300/dshot300.ino index 79b5d2a..281fd63 100644 --- a/examples/dshot300/dshot300.ino +++ b/examples/dshot300/dshot300.ino @@ -32,6 +32,7 @@ void printRPMPeriodically(uint16_t throttle); // Reads throttle value from serial input uint16_t readSerialThrottle(); +// void setup() { // Start the USB Serial Port @@ -48,6 +49,7 @@ void setup() USB_SERIAL.println("Enter a throttle value (48–2047):"); } +// void loop() { // Read value input from Serial @@ -96,17 +98,16 @@ uint16_t readSerialThrottle() void printRPMPeriodically(uint16_t throttle) { static unsigned long last_print_time = 0; - unsigned long now = millis(); - if (now - last_print_time >= 2000) + if (millis() - last_print_time >= 2000) { - last_print_time = now; - uint32_t rpm = motor01.getMotorRPM(MOTOR01_MAGNET_COUNT); USB_SERIAL.print("Throttle: "); USB_SERIAL.print(throttle); USB_SERIAL.print(" | RPM: "); USB_SERIAL.println(rpm); + + last_print_time = millis(); } } From 2cdfd0f484d4848005c22501e4e76b53ee2082fd Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Wed, 30 Jul 2025 14:47:40 +0200 Subject: [PATCH 9/9] ...cosmetic --- examples/dshot300/dshot300.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/dshot300/dshot300.ino b/examples/dshot300/dshot300.ino index 281fd63..1753da0 100644 --- a/examples/dshot300/dshot300.ino +++ b/examples/dshot300/dshot300.ino @@ -46,7 +46,7 @@ void setup() USB_SERIAL.println("**********************"); USB_SERIAL.println("DShotRMT Demo started."); - USB_SERIAL.println("Enter a throttle value (48–2047):"); + USB_SERIAL.println("Enter a throttle value (48 – 2047):"); } // @@ -88,7 +88,7 @@ uint16_t readSerialThrottle() } USB_SERIAL.println("*********************************"); - USB_SERIAL.println("Enter a throttle value (48–2047):"); + USB_SERIAL.println("Enter a throttle value (48 – 2047):"); } return last_throttle;