From a4ec6248ec9b6a912c23ed202ecc1c274f3f59bd Mon Sep 17 00:00:00 2001 From: Wastl Kraus Date: Sun, 31 Aug 2025 20:23:04 +0200 Subject: [PATCH] ...not really happy with callback --- DShotRMT.cpp | 40 +++++++++++++++++++++++++--------------- DShotRMT.h | 9 +++++++-- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/DShotRMT.cpp b/DShotRMT.cpp index 55840f1..750f323 100644 --- a/DShotRMT.cpp +++ b/DShotRMT.cpp @@ -108,13 +108,13 @@ bool DShotRMT::_initTXChannel() // Initialize RMT RX channel bool DShotRMT::_initRXChannel() { - // Create a queue to receive data from the RX callback + // Create a queue to receive data from the RX callback _rx_queue = xQueueCreate(1, sizeof(rmt_rx_done_event_data_t)); if (_rx_queue == nullptr) { return DSHOT_ERROR; } - + // Configure RX channel parameters _rx_channel_config.gpio_num = _gpio; _rx_channel_config.clk_src = DSHOT_CLOCK_SRC_DEFAULT; @@ -122,8 +122,8 @@ bool DShotRMT::_initRXChannel() _rx_channel_config.mem_block_symbols = RX_BUFFER_SIZE; // Configure reception parameters - _receive_config.signal_range_min_ns = 2; - _receive_config.signal_range_max_ns = 200; + _receive_config.signal_range_min_ns = DSHOT_PULSE_MIN; + _receive_config.signal_range_max_ns = DSHOT_PULSE_MAX; // Create RMT RX channel if (rmt_new_rx_channel(&_rx_channel_config, &_rmt_rx_channel) != DSHOT_OK) @@ -139,7 +139,7 @@ bool DShotRMT::_initRXChannel() _dshot_log(RX_INIT_FAILED); return DSHOT_ERROR; } - + return (rmt_enable(_rmt_rx_channel) == DSHOT_OK); } @@ -224,7 +224,7 @@ uint16_t DShotRMT::getERPM() } rmt_rx_done_event_data_t rx_data; - + // Wait for data from the RX callback for a certain timeout if (xQueueReceive(_rx_queue, &rx_data, pdMS_TO_TICKS(DSHOT_RX_TIMEOUT_MS)) == pdTRUE) { @@ -288,7 +288,7 @@ uint16_t DShotRMT::_calculateCRC(const dshot_packet_t &packet) // Transmit DShot packet via RMT uint16_t DShotRMT::_sendDShotFrame(const dshot_packet_t &packet) { - // Check timing requirements + // Check timing requirements if (!_timer_signal()) { return DSHOT_ERROR; @@ -298,6 +298,9 @@ uint16_t DShotRMT::_sendDShotFrame(const dshot_packet_t &packet) if (_is_bidirectional) { rmt_receive(_rmt_rx_channel, _rx_symbols, sizeof(_rx_symbols), &_receive_config); + + // Disable RMT RX for sending + rmt_disable(_rmt_rx_channel); } // Encode DShot packet into RMT symbols @@ -313,10 +316,18 @@ uint16_t DShotRMT::_sendDShotFrame(const dshot_packet_t &packet) { return DSHOT_ERROR; } - - // Update timestamp - _timer_reset(); + // Re-enable RMT RX + if (_is_bidirectional) + { + if (rmt_enable(_rmt_rx_channel) != DSHOT_OK) + { + _dshot_log(RX_RMT_RECEIVER_ERROR); + } + } + + // Update timestamp and return success + _timer_reset(); return DSHOT_OK; } @@ -410,10 +421,10 @@ void DShotRMT::printDshotInfo(Stream &output) const // Current DShot mode output.printf("Current Mode: DSHOT%d\n", - _mode == DSHOT150 ? 150 : - _mode == DSHOT300 ? 300 : - _mode == DSHOT600 ? 600 : - _mode == DSHOT1200 ? 1200 : 0); + _mode == DSHOT150 ? 150 : _mode == DSHOT300 ? 300 + : _mode == DSHOT600 ? 600 + : _mode == DSHOT1200 ? 1200 + : 0); output.printf("Bidirectional: %s\n", _is_bidirectional ? "YES" : "NO"); @@ -451,4 +462,3 @@ void DShotRMT::printCpuInfo(Stream &output) const output.printf("XTAL Freq = %lu MHz\n", getXtalFrequencyMhz()); output.printf("APB Freq = %lu Hz\n", getApbFrequency()); } - diff --git a/DShotRMT.h b/DShotRMT.h index 8a6f631..707247b 100644 --- a/DShotRMT.h +++ b/DShotRMT.h @@ -30,6 +30,11 @@ constexpr auto RMT_BUFFER_SIZE = DSHOT_BITS_PER_FRAME; constexpr auto RX_BUFFER_SIZE = 64; constexpr auto TX_BUFFER_SIZE = 64; +// Smallest pulse for DShot1200 is 2us. Largest for DShot150 is 40us. +// The range is set from 3us (3000ns) to 60us (60000ns) to be safe across all modes. +constexpr uint32_t DSHOT_PULSE_MIN = 3000; +constexpr uint32_t DSHOT_PULSE_MAX = 60000; + // DShot Mode Enumeration typedef enum dshot_mode_e { @@ -100,10 +105,10 @@ public: // --- DEPRECATED METHODS --- [[deprecated("Use sendThrottle() instead")]] - bool setThrottle(uint16_t throttle) { return sendThrottle(throttle); } + bool setThrottle(uint16_t throttle) { return sendThrottle(throttle); } [[deprecated("Use sendCommand() instead")]] - bool sendDShotCommand(uint16_t command) { return sendCommand(command); } + bool sendDShotCommand(uint16_t command) { return sendCommand(command); } private: // --- CONFIG ---