2025-06-11 08:29:59 +01:00
|
|
|
/**
|
|
|
|
|
* @file DShotRMT.cpp
|
2025-07-30 23:36:58 +01:00
|
|
|
* @brief DShot signal generation using ESP32 RMT with bidirectional support
|
2025-06-11 08:29:59 +01:00
|
|
|
* @author Wastl Kraus
|
|
|
|
|
* @date 2025-06-11
|
|
|
|
|
* @license MIT
|
|
|
|
|
*/
|
2021-07-04 02:01:26 +01:00
|
|
|
|
2025-07-30 23:36:58 +01:00
|
|
|
#include "DShotRMT.h"
|
|
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
// Timing parameters for each DShot mode
|
|
|
|
|
// Format: {frame_length_us, ticks_per_bit, ticks_one_high, ticks_one_low, ticks_zero_high, ticks_zero_low}
|
2025-09-01 13:41:48 +01:00
|
|
|
static constexpr dshot_timing_t DSHOT_TIMINGS[] = {
|
2025-07-30 23:36:58 +01:00
|
|
|
{0, 0, 0, 0, 0, 0}, // DSHOT_OFF
|
2025-08-17 17:58:40 +01:00
|
|
|
{128, 64, 48, 16, 24, 40}, // DSHOT150
|
|
|
|
|
{64, 32, 24, 8, 12, 20}, // DSHOT300
|
|
|
|
|
{32, 16, 12, 4, 6, 10}, // DSHOT600
|
|
|
|
|
{16, 8, 6, 2, 3, 5} // DSHOT1200
|
2025-07-30 23:36:58 +01:00
|
|
|
};
|
|
|
|
|
|
2025-09-03 10:16:14 +01:00
|
|
|
// Constructor with GPIO number
|
2025-08-30 20:56:34 +01:00
|
|
|
DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool is_bidirectional)
|
|
|
|
|
: _gpio(gpio),
|
|
|
|
|
_mode(mode),
|
|
|
|
|
_is_bidirectional(is_bidirectional),
|
2025-09-07 22:21:23 +01:00
|
|
|
_last_erpm_atomic(0),
|
|
|
|
|
_telemetry_ready_flag(false),
|
2025-09-03 10:16:14 +01:00
|
|
|
_frame_timer_us(0),
|
2025-08-30 20:56:34 +01:00
|
|
|
_timing_config(DSHOT_TIMINGS[mode]),
|
2025-09-07 22:21:23 +01:00
|
|
|
_last_throttle(DSHOT_CMD_MOTOR_STOP),
|
2025-09-03 10:16:14 +01:00
|
|
|
_last_transmission_time(0),
|
2025-08-30 20:56:34 +01:00
|
|
|
_parsed_packet(0),
|
|
|
|
|
_packet{0},
|
2025-09-07 14:19:52 +01:00
|
|
|
_bitPositions{0},
|
|
|
|
|
_level0(_is_bidirectional ? 0 : 1),
|
|
|
|
|
_level1(_is_bidirectional ? 1 : 0),
|
2025-09-03 10:16:14 +01:00
|
|
|
_rmt_tx_channel(nullptr),
|
|
|
|
|
_rmt_rx_channel(nullptr),
|
|
|
|
|
_dshot_encoder(nullptr),
|
|
|
|
|
_tx_channel_config{},
|
|
|
|
|
_rx_channel_config{},
|
|
|
|
|
_transmit_config{},
|
2025-09-08 08:05:54 +01:00
|
|
|
_receive_config{},
|
|
|
|
|
_result{false, UNKNOWN_ERROR}
|
2025-07-25 16:41:38 +01:00
|
|
|
{
|
2025-08-30 20:56:34 +01:00
|
|
|
// Calculate frame timing including switch/pause time
|
2025-09-08 13:40:32 +01:00
|
|
|
_frame_timer_us = _timing_config.frame_length_us + DSHOT_PAUSE_US;
|
2025-08-17 22:00:43 +01:00
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
// Double frame time for bidirectional mode (includes response time)
|
2025-07-30 23:36:58 +01:00
|
|
|
if (_is_bidirectional)
|
2025-07-25 16:41:38 +01:00
|
|
|
{
|
2025-08-17 22:00:43 +01:00
|
|
|
_frame_timer_us = (_frame_timer_us << 1);
|
2025-07-25 16:41:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-06-11 08:29:59 +01:00
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Constructor using pin number
|
2025-08-30 20:56:34 +01:00
|
|
|
DShotRMT::DShotRMT(uint16_t pin_nr, dshot_mode_t mode, bool is_bidirectional)
|
|
|
|
|
: DShotRMT((gpio_num_t)pin_nr, mode, is_bidirectional)
|
2025-08-06 22:57:26 +01:00
|
|
|
{
|
2025-08-30 20:56:34 +01:00
|
|
|
// Delegates to primary constructor with type cast
|
2025-08-06 22:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 13:41:48 +01:00
|
|
|
// Destructor for "better" code
|
|
|
|
|
DShotRMT::~DShotRMT()
|
|
|
|
|
{
|
2025-09-05 22:28:08 +01:00
|
|
|
// ...TX
|
2025-09-01 13:41:48 +01:00
|
|
|
if (_rmt_tx_channel)
|
|
|
|
|
{
|
2025-09-05 22:28:08 +01:00
|
|
|
if (rmt_disable(_rmt_tx_channel) == DSHOT_OK)
|
|
|
|
|
{
|
|
|
|
|
rmt_del_channel(_rmt_tx_channel);
|
|
|
|
|
_rmt_tx_channel = nullptr;
|
|
|
|
|
}
|
2025-09-01 13:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-05 22:28:08 +01:00
|
|
|
// ...RX
|
2025-09-01 13:41:48 +01:00
|
|
|
if (_rmt_rx_channel)
|
|
|
|
|
{
|
2025-09-05 22:28:08 +01:00
|
|
|
if (rmt_disable(_rmt_rx_channel) == DSHOT_OK)
|
|
|
|
|
{
|
|
|
|
|
rmt_del_channel(_rmt_rx_channel);
|
|
|
|
|
_rmt_rx_channel = nullptr;
|
|
|
|
|
}
|
2025-09-01 13:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-05 22:28:08 +01:00
|
|
|
// ...Encoder
|
2025-09-01 13:41:48 +01:00
|
|
|
if (_dshot_encoder)
|
|
|
|
|
{
|
|
|
|
|
rmt_del_encoder(_dshot_encoder);
|
2025-09-04 13:41:05 +01:00
|
|
|
_dshot_encoder = nullptr;
|
2025-09-01 13:41:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 22:28:08 +01:00
|
|
|
// Init DShotRMT
|
2025-09-05 11:16:19 +01:00
|
|
|
dshot_result_t DShotRMT::begin()
|
2022-11-25 15:08:58 +00:00
|
|
|
{
|
2025-09-04 13:41:05 +01:00
|
|
|
// Init RX channel first
|
2025-08-31 10:43:48 +01:00
|
|
|
if (_is_bidirectional)
|
2025-07-19 12:26:54 +01:00
|
|
|
{
|
2025-09-07 12:48:48 +01:00
|
|
|
if (!_initRXChannel().success)
|
2025-08-31 10:43:48 +01:00
|
|
|
{
|
2025-09-08 08:05:54 +01:00
|
|
|
_result.msg = RX_INIT_FAILED;
|
|
|
|
|
return _result;
|
2025-08-31 10:43:48 +01:00
|
|
|
}
|
2025-07-19 12:26:54 +01:00
|
|
|
}
|
2025-07-29 23:40:09 +01:00
|
|
|
|
2025-09-04 13:41:05 +01:00
|
|
|
// Init TX channel
|
2025-09-07 12:48:48 +01:00
|
|
|
if (!_initTXChannel().success)
|
2025-09-04 13:41:05 +01:00
|
|
|
{
|
2025-09-08 08:05:54 +01:00
|
|
|
_result.msg = TX_INIT_FAILED;
|
|
|
|
|
return _result;
|
2025-09-04 13:41:05 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-03 10:16:14 +01:00
|
|
|
// Init DShot encoder
|
2025-09-07 12:48:48 +01:00
|
|
|
if (!_initDShotEncoder().success)
|
2025-07-19 12:26:54 +01:00
|
|
|
{
|
2025-09-08 08:05:54 +01:00
|
|
|
_result.msg = ENCODER_INIT_FAILED;
|
|
|
|
|
return _result;
|
2025-07-19 12:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 13:45:11 +01:00
|
|
|
// Bit positions precalculation
|
|
|
|
|
_preCalculateBitPositions();
|
|
|
|
|
|
2025-09-08 08:05:54 +01:00
|
|
|
_result.success = true;
|
|
|
|
|
_result.msg = INIT_SUCCESS;
|
2025-09-05 11:16:19 +01:00
|
|
|
|
2025-09-08 08:05:54 +01:00
|
|
|
return _result;
|
2025-08-30 23:21:03 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-03 10:16:14 +01:00
|
|
|
// Init RMT TX channel
|
2025-09-07 12:48:48 +01:00
|
|
|
dshot_result_t DShotRMT::_initTXChannel()
|
2025-08-30 23:21:03 +01:00
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
_result = {false, TX_INIT_FAILED};
|
2025-09-07 12:48:48 +01:00
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Configure TX channel
|
|
|
|
|
_tx_channel_config.gpio_num = _gpio;
|
|
|
|
|
_tx_channel_config.clk_src = DSHOT_CLOCK_SRC_DEFAULT;
|
|
|
|
|
_tx_channel_config.resolution_hz = DSHOT_RMT_RESOLUTION;
|
2025-09-01 13:41:48 +01:00
|
|
|
_tx_channel_config.mem_block_symbols = RMT_BUFFER_SYMBOLS;
|
2025-09-02 14:18:37 +01:00
|
|
|
_tx_channel_config.trans_queue_depth = RMT_QUEUE_DEPTH;
|
2025-08-30 23:21:03 +01:00
|
|
|
|
2025-09-03 10:16:14 +01:00
|
|
|
// Config RMT TX
|
2025-08-30 23:21:03 +01:00
|
|
|
_transmit_config.loop_count = 0; // No automatic loops - real-time calculation
|
|
|
|
|
_transmit_config.flags.eot_level = _is_bidirectional ? 1 : 0; // Telemetric Bit used as bidir flag
|
|
|
|
|
|
|
|
|
|
// Create RMT TX channel
|
|
|
|
|
if (rmt_new_tx_channel(&_tx_channel_config, &_rmt_tx_channel) != DSHOT_OK)
|
|
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
return _result;
|
2025-09-07 12:48:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2025-09-07 22:21:23 +01:00
|
|
|
if (rmt_enable(_rmt_tx_channel) != DSHOT_OK)
|
2025-09-07 12:48:48 +01:00
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
return _result;
|
2025-08-30 23:21:03 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-08 13:40:32 +01:00
|
|
|
_result.success = true;
|
|
|
|
|
_result.msg = TX_INIT_SUCCESS;
|
2025-09-07 12:48:48 +01:00
|
|
|
|
2025-09-08 13:40:32 +01:00
|
|
|
return _result;
|
2025-08-30 23:21:03 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-03 10:16:14 +01:00
|
|
|
// Init RMT RX channel
|
2025-09-07 12:48:48 +01:00
|
|
|
dshot_result_t DShotRMT::_initRXChannel()
|
2025-08-30 23:21:03 +01:00
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
_result = {false, RX_INIT_FAILED};
|
2025-09-07 12:48:48 +01:00
|
|
|
|
2025-09-07 22:21:23 +01:00
|
|
|
// Direct RMT symbol processing - Performance optimized
|
|
|
|
|
_rx_event_callbacks.on_recv_done = _rmt_rx_done_callback;
|
2025-08-31 19:23:04 +01:00
|
|
|
|
2025-09-05 11:16:19 +01:00
|
|
|
// Config RMT RX
|
2025-08-30 23:21:03 +01:00
|
|
|
_rx_channel_config.gpio_num = _gpio;
|
|
|
|
|
_rx_channel_config.clk_src = DSHOT_CLOCK_SRC_DEFAULT;
|
|
|
|
|
_rx_channel_config.resolution_hz = DSHOT_RMT_RESOLUTION;
|
2025-09-01 13:41:48 +01:00
|
|
|
_rx_channel_config.mem_block_symbols = RMT_BUFFER_SYMBOLS;
|
2025-08-30 23:21:03 +01:00
|
|
|
|
2025-09-03 10:16:14 +01:00
|
|
|
// Config RMT RX parameters
|
2025-08-31 19:23:04 +01:00
|
|
|
_receive_config.signal_range_min_ns = DSHOT_PULSE_MIN;
|
|
|
|
|
_receive_config.signal_range_max_ns = DSHOT_PULSE_MAX;
|
2025-08-30 23:21:03 +01:00
|
|
|
|
|
|
|
|
// Create RMT RX channel
|
|
|
|
|
if (rmt_new_rx_channel(&_rx_channel_config, &_rmt_rx_channel) != DSHOT_OK)
|
|
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
return _result;
|
2025-08-30 23:21:03 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 12:48:48 +01:00
|
|
|
//
|
2025-09-07 22:21:23 +01:00
|
|
|
if (rmt_enable(_rmt_rx_channel) != DSHOT_OK)
|
2025-09-07 12:48:48 +01:00
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
return _result;
|
2025-09-07 12:48:48 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-08 13:40:32 +01:00
|
|
|
_result.success = true;
|
|
|
|
|
_result.msg = RX_INIT_SUCCESS;
|
2025-09-07 12:48:48 +01:00
|
|
|
|
2025-09-08 13:40:32 +01:00
|
|
|
return _result;
|
2025-08-30 23:21:03 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-02 14:18:37 +01:00
|
|
|
// Callback for RMT RX
|
2025-09-03 10:16:14 +01:00
|
|
|
bool IRAM_ATTR DShotRMT::_rmt_rx_done_callback(rmt_channel_handle_t rmt_rx_channel, const rmt_rx_done_event_data_t *edata, void *user_data)
|
2025-08-31 10:43:48 +01:00
|
|
|
{
|
2025-09-07 22:21:23 +01:00
|
|
|
DShotRMT *instance = static_cast<DShotRMT *>(user_data);
|
|
|
|
|
|
|
|
|
|
// Minimale ISR-Verarbeitung: Nur bei gültigen Daten
|
|
|
|
|
if (edata && edata->num_symbols >= GCR_BITS_PER_FRAME &&
|
|
|
|
|
edata->num_symbols <= GCR_BITS_PER_FRAME)
|
|
|
|
|
{
|
2025-08-31 10:43:48 +01:00
|
|
|
|
2025-09-07 22:21:23 +01:00
|
|
|
// Direkte Dekodierung in der ISR (schnell!)
|
|
|
|
|
uint16_t erpm = instance->_decodeDShotFrame(edata->received_symbols);
|
|
|
|
|
|
|
|
|
|
if (erpm != DSHOT_NULL_PACKET)
|
|
|
|
|
{
|
|
|
|
|
// Atomic writes - thread-safe ohne Mutex
|
|
|
|
|
instance->_last_erpm_atomic = erpm;
|
|
|
|
|
instance->_telemetry_ready_flag = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-31 10:43:48 +01:00
|
|
|
|
2025-09-07 22:21:23 +01:00
|
|
|
return false;
|
2025-08-31 10:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Initialize DShot encoder
|
2025-09-07 12:48:48 +01:00
|
|
|
dshot_result_t DShotRMT::_initDShotEncoder()
|
2025-08-30 23:21:03 +01:00
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
_result = {false, ENCODER_INIT_FAILED};
|
|
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Create copy encoder configuration
|
|
|
|
|
rmt_copy_encoder_config_t encoder_config = {};
|
|
|
|
|
|
|
|
|
|
// Create encoder instance
|
|
|
|
|
if (rmt_new_copy_encoder(&encoder_config, &_dshot_encoder) != DSHOT_OK)
|
|
|
|
|
{
|
2025-09-08 08:05:54 +01:00
|
|
|
return _result;
|
2025-08-30 23:21:03 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-08 08:05:54 +01:00
|
|
|
_result.success = true;
|
|
|
|
|
_result.msg = ENCODER_INIT_SUCCESS;
|
2025-09-07 12:48:48 +01:00
|
|
|
|
2025-09-08 08:05:54 +01:00
|
|
|
return _result;
|
2021-06-29 19:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
// Send throttle value
|
2025-09-05 11:16:19 +01:00
|
|
|
dshot_result_t DShotRMT::sendThrottle(uint16_t throttle)
|
2025-08-06 22:57:26 +01:00
|
|
|
{
|
2025-08-30 20:56:34 +01:00
|
|
|
// Special case: if throttle is 0, use sendCommand() instead
|
|
|
|
|
if (throttle == 0)
|
2025-08-28 12:22:42 +01:00
|
|
|
{
|
2025-08-30 20:56:34 +01:00
|
|
|
return sendCommand(DSHOT_CMD_MOTOR_STOP);
|
2025-08-28 12:22:42 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Log only if throttle is out of range and different from last time
|
2025-09-07 22:21:23 +01:00
|
|
|
if ((throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX) && throttle != _last_throttle)
|
2025-08-30 20:56:34 +01:00
|
|
|
{
|
2025-09-08 08:05:54 +01:00
|
|
|
_result.msg = THROTTLE_NOT_IN_RANGE;
|
2025-08-30 20:56:34 +01:00
|
|
|
}
|
2025-08-06 22:57:26 +01:00
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Always store the original throttle value
|
2025-09-07 22:21:23 +01:00
|
|
|
_last_throttle = throttle;
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
// Constrain throttle for transmission and send
|
2025-08-31 10:43:48 +01:00
|
|
|
uint16_t new_throttle = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX);
|
|
|
|
|
_packet = _buildDShotPacket(new_throttle);
|
2025-09-05 11:16:19 +01:00
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
return _sendDShotFrame(_packet);
|
2025-08-06 22:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
// Send DShot command to ESC
|
2025-09-05 11:16:19 +01:00
|
|
|
dshot_result_t DShotRMT::sendCommand(uint16_t command)
|
2025-08-06 22:57:26 +01:00
|
|
|
{
|
2025-08-30 20:56:34 +01:00
|
|
|
// Validate command is within DShot specification range
|
2025-08-05 00:43:19 +01:00
|
|
|
if (command < DSHOT_CMD_MOTOR_STOP || command > DSHOT_CMD_MAX)
|
2025-08-02 16:20:32 +01:00
|
|
|
{
|
2025-09-08 08:05:54 +01:00
|
|
|
_result.msg = COMMAND_NOT_VALID;
|
|
|
|
|
return _result;
|
2025-08-02 16:20:32 +01:00
|
|
|
}
|
2025-08-05 21:01:00 +01:00
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
// Build packet and transmit
|
2025-08-06 22:57:26 +01:00
|
|
|
_packet = _buildDShotPacket(command);
|
2025-09-07 12:48:48 +01:00
|
|
|
|
2025-08-28 12:22:42 +01:00
|
|
|
return _sendDShotFrame(_packet);
|
2025-08-02 16:20:32 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-05 11:16:19 +01:00
|
|
|
// Get telemetry data with timing and error handling
|
2025-09-07 12:48:48 +01:00
|
|
|
dshot_telemetry_result_t DShotRMT::getTelemetry(uint16_t magnet_count)
|
2025-09-05 11:16:19 +01:00
|
|
|
{
|
2025-09-07 12:48:48 +01:00
|
|
|
// Result container
|
2025-09-07 13:45:11 +01:00
|
|
|
dshot_telemetry_result_t result = {false, NO_DSHOT_ERPM, NO_DSHOT_RPM, TELEMETRY_FAILED};
|
2025-09-05 11:16:19 +01:00
|
|
|
|
|
|
|
|
// Check if bidirectional mode is enabled
|
|
|
|
|
if (!_is_bidirectional)
|
|
|
|
|
{
|
2025-09-07 12:48:48 +01:00
|
|
|
result.msg = BIDIR_NOT_ENABLED;
|
2025-09-05 11:16:19 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-07 22:21:23 +01:00
|
|
|
if (_telemetry_ready_flag)
|
2025-06-14 17:16:45 +01:00
|
|
|
{
|
2025-09-07 22:21:23 +01:00
|
|
|
_telemetry_ready_flag = false;
|
2025-07-17 22:24:28 +01:00
|
|
|
|
2025-09-07 22:21:23 +01:00
|
|
|
uint16_t erpm = _last_erpm_atomic;
|
2025-08-31 19:23:04 +01:00
|
|
|
|
2025-09-07 22:21:23 +01:00
|
|
|
if (erpm != DSHOT_NULL_PACKET && magnet_count >= 1)
|
2025-08-31 10:43:48 +01:00
|
|
|
{
|
2025-09-07 22:21:23 +01:00
|
|
|
uint8_t pole_pairs = max(MIN_POLE_PAIRS, (magnet_count / MAGNETS_PER_POLE_PAIR));
|
|
|
|
|
uint32_t motor_rpm = (erpm / pole_pairs);
|
|
|
|
|
|
|
|
|
|
result.success = true;
|
|
|
|
|
result.erpm = erpm;
|
|
|
|
|
result.motor_rpm = motor_rpm;
|
|
|
|
|
result.msg = TELEMETRY_SUCCESS;
|
2025-08-31 10:43:48 +01:00
|
|
|
}
|
2025-07-30 23:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 22:21:23 +01:00
|
|
|
return result;
|
2025-06-13 20:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Build a complete DShot packet
|
|
|
|
|
dshot_packet_t DShotRMT::_buildDShotPacket(const uint16_t value)
|
2025-07-30 23:36:58 +01:00
|
|
|
{
|
2025-09-01 15:31:50 +01:00
|
|
|
// Init packet structure
|
2025-08-30 23:21:03 +01:00
|
|
|
dshot_packet_t packet = {};
|
2025-06-14 17:16:45 +01:00
|
|
|
|
2025-09-01 20:26:22 +01:00
|
|
|
// Re-check for valid value
|
|
|
|
|
if (value > DSHOT_THROTTLE_MAX)
|
|
|
|
|
{
|
|
|
|
|
// Something is really wrong
|
|
|
|
|
return packet;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Build packet
|
2025-09-04 13:41:05 +01:00
|
|
|
packet.throttle_value = value & 0b0000011111111111;
|
2025-08-30 23:21:03 +01:00
|
|
|
packet.telemetric_request = _is_bidirectional ? 1 : 0;
|
2025-09-05 11:16:19 +01:00
|
|
|
|
2025-09-01 15:31:50 +01:00
|
|
|
// CRC is calculated over 11bit
|
|
|
|
|
uint16_t data = (packet.throttle_value << 1) | packet.telemetric_request;
|
2025-09-05 11:16:19 +01:00
|
|
|
|
2025-09-01 15:31:50 +01:00
|
|
|
packet.checksum = _calculateCRC(data);
|
2025-07-30 23:36:58 +01:00
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
return packet;
|
2025-06-14 17:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Parse DShot packet into 16-bit format
|
|
|
|
|
uint16_t DShotRMT::_parseDShotPacket(const dshot_packet_t &packet)
|
2025-06-17 19:56:50 +01:00
|
|
|
{
|
2025-09-01 15:31:50 +01:00
|
|
|
// Parse DShot frame into "raw" 16 bit value
|
|
|
|
|
uint16_t data_and_telemetry = (packet.throttle_value << 1) | packet.telemetric_request;
|
|
|
|
|
uint16_t parsed_packet = (data_and_telemetry << 4) | packet.checksum;
|
2025-07-29 23:40:09 +01:00
|
|
|
|
2025-09-01 15:31:50 +01:00
|
|
|
return parsed_packet;
|
2025-06-17 19:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 15:31:50 +01:00
|
|
|
// Calculate CRC
|
|
|
|
|
uint16_t DShotRMT::_calculateCRC(const uint16_t data)
|
2025-06-17 19:56:50 +01:00
|
|
|
{
|
2025-09-01 15:31:50 +01:00
|
|
|
// DShot CRC
|
2025-09-08 13:40:32 +01:00
|
|
|
uint16_t crc = (data ^ (data >> 4) ^ (data >> 8)) & DSHOT_CRC_MASK;
|
2025-08-30 23:21:03 +01:00
|
|
|
|
|
|
|
|
// Invert CRC for bidirectional DShot mode
|
|
|
|
|
if (_is_bidirectional)
|
2025-08-17 22:00:43 +01:00
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
crc = (~crc) & DSHOT_CRC_MASK;
|
2025-08-17 22:00:43 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
return crc;
|
2025-06-17 19:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 13:45:11 +01:00
|
|
|
// Per calculate bits - Performance optimized
|
|
|
|
|
void DShotRMT::_preCalculateBitPositions()
|
|
|
|
|
{
|
2025-09-07 22:21:23 +01:00
|
|
|
for (int i = 0; i < DSHOT_BITS_PER_FRAME; ++i)
|
|
|
|
|
{
|
2025-09-07 13:45:11 +01:00
|
|
|
_bitPositions[i] = DSHOT_BITS_PER_FRAME - 1 - i;
|
2025-09-07 22:21:23 +01:00
|
|
|
}
|
2025-09-07 13:45:11 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
// Transmit DShot packet via RMT
|
2025-09-05 11:16:19 +01:00
|
|
|
dshot_result_t DShotRMT::_sendDShotFrame(const dshot_packet_t &packet)
|
2025-06-13 20:50:00 +01:00
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
_result = {false, UNKNOWN_ERROR};
|
|
|
|
|
|
2025-08-31 19:23:04 +01:00
|
|
|
// Check timing requirements
|
2025-08-28 12:22:42 +01:00
|
|
|
if (!_timer_signal())
|
|
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
_result.msg = TIMING_CORRECTION;
|
|
|
|
|
return _result;
|
2025-08-28 12:22:42 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-05 11:16:19 +01:00
|
|
|
// Enable RMT RX before RMT TX
|
2025-08-31 10:43:48 +01:00
|
|
|
if (_is_bidirectional)
|
|
|
|
|
{
|
2025-09-03 10:16:14 +01:00
|
|
|
// Performance reasons
|
|
|
|
|
rmt_symbol_word_t rx_symbols[DSHOT_BITS_PER_FRAME];
|
|
|
|
|
|
2025-09-05 11:16:19 +01:00
|
|
|
if (rmt_receive(_rmt_rx_channel, rx_symbols, sizeof(rx_symbols), &_receive_config) != DSHOT_OK)
|
|
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
_result.msg = RECEIVER_FAILED;
|
|
|
|
|
return _result;
|
2025-09-05 11:16:19 +01:00
|
|
|
}
|
2025-08-31 10:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-03 10:16:14 +01:00
|
|
|
// Local for performance
|
|
|
|
|
rmt_symbol_word_t tx_symbols[DSHOT_BITS_PER_FRAME];
|
|
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
// Encode DShot packet into RMT symbols
|
2025-09-03 10:16:14 +01:00
|
|
|
_encodeDShotFrame(packet, tx_symbols);
|
2025-08-05 23:32:03 +01:00
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
// Calculate transmission data size
|
2025-08-28 12:22:42 +01:00
|
|
|
size_t tx_size_bytes = DSHOT_BITS_PER_FRAME * sizeof(rmt_symbol_word_t);
|
2025-08-30 20:56:34 +01:00
|
|
|
|
2025-09-04 13:41:05 +01:00
|
|
|
// TODO: Find out, why this is needed
|
|
|
|
|
if (_is_bidirectional)
|
|
|
|
|
{
|
|
|
|
|
// Disable RMT RX for sending
|
|
|
|
|
if (rmt_disable(_rmt_rx_channel) != DSHOT_OK)
|
|
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
_result.msg = RECEIVER_FAILED;
|
|
|
|
|
return _result;
|
2025-09-04 13:41:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-05 11:16:19 +01:00
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Perform RMT transmission
|
2025-09-04 13:41:05 +01:00
|
|
|
if (rmt_transmit(_rmt_tx_channel, _dshot_encoder, tx_symbols, tx_size_bytes, &_transmit_config) != DSHOT_OK)
|
2025-08-28 12:22:42 +01:00
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
_result.msg = TRANSMISSION_FAILED;
|
|
|
|
|
return _result;
|
2025-07-30 23:36:58 +01:00
|
|
|
}
|
2025-08-28 12:22:42 +01:00
|
|
|
|
2025-08-31 19:23:04 +01:00
|
|
|
// Re-enable RMT RX
|
|
|
|
|
if (_is_bidirectional)
|
|
|
|
|
{
|
|
|
|
|
if (rmt_enable(_rmt_rx_channel) != DSHOT_OK)
|
|
|
|
|
{
|
2025-09-08 13:40:32 +01:00
|
|
|
_result.msg = RECEIVER_FAILED;
|
|
|
|
|
return _result;
|
2025-08-31 19:23:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 11:16:19 +01:00
|
|
|
// Update timestamp and calculate execution time
|
2025-08-31 19:23:04 +01:00
|
|
|
_timer_reset();
|
2025-09-04 13:41:05 +01:00
|
|
|
|
2025-09-08 13:40:32 +01:00
|
|
|
_result.success = true;
|
|
|
|
|
_result.msg = TRANSMISSION_SUCCESS;
|
2025-09-05 11:16:19 +01:00
|
|
|
|
2025-09-08 13:40:32 +01:00
|
|
|
return _result;
|
2025-07-30 23:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
// Encode DShot packet into RMT symbol format (placed in IRAM for performance)
|
2025-08-31 10:43:48 +01:00
|
|
|
bool IRAM_ATTR DShotRMT::_encodeDShotFrame(const dshot_packet_t &packet, rmt_symbol_word_t *symbols)
|
2025-07-30 23:36:58 +01:00
|
|
|
{
|
2025-08-30 20:56:34 +01:00
|
|
|
_parsed_packet = _parseDShotPacket(packet);
|
2025-09-07 22:21:23 +01:00
|
|
|
|
2025-09-07 13:45:11 +01:00
|
|
|
// Decode MSB
|
2025-09-07 22:21:23 +01:00
|
|
|
for (int i = 0; i < DSHOT_BITS_PER_FRAME; ++i)
|
|
|
|
|
{
|
2025-09-07 13:45:11 +01:00
|
|
|
// Use precalculated bit positions - Performace optimized
|
|
|
|
|
int bit_position = _bitPositions[i];
|
|
|
|
|
|
2025-09-07 22:21:23 +01:00
|
|
|
bool bit = (_parsed_packet >> bit_position) & 0b0000000000000001;
|
2025-09-07 14:19:52 +01:00
|
|
|
symbols[i].level0 = _level0;
|
2025-09-03 10:16:14 +01:00
|
|
|
symbols[i].duration0 = bit ? _timing_config.ticks_one_high : _timing_config.ticks_zero_high;
|
2025-09-07 14:19:52 +01:00
|
|
|
symbols[i].level1 = _level1;
|
2025-09-03 10:16:14 +01:00
|
|
|
symbols[i].duration1 = bit ? _timing_config.ticks_one_low : _timing_config.ticks_zero_low;
|
2023-04-15 06:45:14 +01:00
|
|
|
}
|
2025-08-30 20:56:34 +01:00
|
|
|
|
2025-08-03 21:15:38 +01:00
|
|
|
return DSHOT_OK;
|
2021-06-29 19:05:20 +01:00
|
|
|
}
|
2025-07-17 22:24:28 +01:00
|
|
|
|
2025-09-04 13:41:05 +01:00
|
|
|
// Decodes a DShot telemetry frame from received RMT symbols.
|
2025-08-05 23:32:03 +01:00
|
|
|
uint16_t DShotRMT::_decodeDShotFrame(const rmt_symbol_word_t *symbols)
|
2025-07-17 22:24:28 +01:00
|
|
|
{
|
2025-09-04 13:41:05 +01:00
|
|
|
uint32_t gcr_value = 0;
|
|
|
|
|
|
|
|
|
|
// Decode GCR symbols into a 21-bit value.
|
|
|
|
|
// '1' has a longer low pulse (duration0 > duration1).
|
|
|
|
|
// '0' has a longer high pulse (duration1 > duration0).
|
2025-09-03 10:16:14 +01:00
|
|
|
for (size_t i = 0; i < GCR_BITS_PER_FRAME; ++i)
|
2025-07-17 22:24:28 +01:00
|
|
|
{
|
2025-09-01 13:41:48 +01:00
|
|
|
bool bit_is_one = symbols[i].duration0 > symbols[i].duration1;
|
2025-09-04 13:41:05 +01:00
|
|
|
gcr_value = (gcr_value << 1) | bit_is_one;
|
2025-09-01 13:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-04 13:41:05 +01:00
|
|
|
// Perform GCR decoding: data = gcr ^ (gcr >> 1).
|
|
|
|
|
uint32_t decoded_frame = gcr_value ^ (gcr_value >> 1);
|
|
|
|
|
|
|
|
|
|
// Extract 16 data bits and 4 CRC bits from 20-bit frame.
|
|
|
|
|
// The first bit of the GCR frame is a start bit and is discarded.
|
2025-09-08 13:40:32 +01:00
|
|
|
uint16_t data_and_crc = (decoded_frame & DSHOT_FULL_PACKET);
|
2025-09-03 10:16:14 +01:00
|
|
|
|
2025-09-04 13:41:05 +01:00
|
|
|
// Cutting 4 bits?
|
|
|
|
|
uint16_t received_data = data_and_crc >> 4;
|
2025-07-17 22:24:28 +01:00
|
|
|
|
2025-09-04 13:41:05 +01:00
|
|
|
// Masking CRC
|
2025-09-08 13:40:32 +01:00
|
|
|
uint16_t received_crc = data_and_crc & DSHOT_CRC_MASK;
|
2025-09-01 13:41:48 +01:00
|
|
|
|
2025-09-04 13:41:05 +01:00
|
|
|
// Telemetry request bit is always 1.
|
|
|
|
|
if (!(received_data & (1 << 11)))
|
|
|
|
|
{
|
|
|
|
|
return DSHOT_NULL_PACKET;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate expected CRC
|
|
|
|
|
uint16_t data_for_crc = received_data;
|
2025-09-01 15:31:50 +01:00
|
|
|
uint16_t calculated_crc = _calculateCRC(data_for_crc);
|
2025-09-04 13:41:05 +01:00
|
|
|
|
2025-09-01 15:31:50 +01:00
|
|
|
// Validate CRC
|
|
|
|
|
if (received_crc != calculated_crc)
|
2025-07-19 15:41:04 +01:00
|
|
|
{
|
2025-08-30 20:56:34 +01:00
|
|
|
return DSHOT_NULL_PACKET;
|
2025-07-19 15:41:04 +01:00
|
|
|
}
|
2025-07-17 22:24:28 +01:00
|
|
|
|
2025-09-04 13:41:05 +01:00
|
|
|
// Return the eRPM value (first 11 bits of received data).
|
2025-09-08 13:40:32 +01:00
|
|
|
return received_data & DSHOT_THROTTLE_MAX;
|
2025-07-30 23:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Check if enough time has passed for next transmission
|
2025-08-31 10:43:48 +01:00
|
|
|
bool IRAM_ATTR DShotRMT::_timer_signal()
|
2025-08-30 23:21:03 +01:00
|
|
|
{
|
2025-09-03 10:16:14 +01:00
|
|
|
uint64_t current_time = esp_timer_get_time();
|
2025-08-30 23:21:03 +01:00
|
|
|
|
|
|
|
|
// Handle potential overflow
|
2025-09-03 10:16:14 +01:00
|
|
|
uint64_t elapsed = current_time - _last_transmission_time;
|
2025-08-30 23:21:03 +01:00
|
|
|
|
|
|
|
|
return elapsed >= _frame_timer_us;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset transmission timer to current time
|
|
|
|
|
bool DShotRMT::_timer_reset()
|
|
|
|
|
{
|
2025-09-03 10:16:14 +01:00
|
|
|
_last_transmission_time = esp_timer_get_time();
|
2025-08-30 23:21:03 +01:00
|
|
|
return DSHOT_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print timing diagnostic information to specified stream
|
2025-09-04 13:41:05 +01:00
|
|
|
void DShotRMT::printDShotInfo(Stream &output) const
|
2025-08-28 12:22:42 +01:00
|
|
|
{
|
2025-09-01 15:08:05 +01:00
|
|
|
output.println(" ");
|
2025-08-30 20:56:34 +01:00
|
|
|
output.println(" === DShot Signal Info === ");
|
|
|
|
|
|
|
|
|
|
// Current DShot mode
|
2025-09-07 22:21:23 +01:00
|
|
|
output.printf("Current Mode: DSHOT%d\n",
|
|
|
|
|
_mode == DSHOT150 ? 150 :
|
|
|
|
|
_mode == DSHOT300 ? 300 :
|
|
|
|
|
_mode == DSHOT600 ? 600 :
|
|
|
|
|
_mode == DSHOT1200 ? 1200 : 0);
|
2025-08-30 23:21:03 +01:00
|
|
|
|
2025-08-30 20:56:34 +01:00
|
|
|
output.printf("Bidirectional: %s\n", _is_bidirectional ? "YES" : "NO");
|
|
|
|
|
|
|
|
|
|
// Timing Info
|
|
|
|
|
output.printf("Frame Length: %u us\n", _timing_config.frame_length_us);
|
|
|
|
|
|
|
|
|
|
// Packet Info
|
|
|
|
|
output.printf("Current Packet: ");
|
|
|
|
|
|
|
|
|
|
// Print bit by bit
|
2025-09-07 22:21:23 +01:00
|
|
|
for (int i = DSHOT_BITS_PER_FRAME - 1; i >= 0; --i)
|
2025-08-30 20:56:34 +01:00
|
|
|
{
|
|
|
|
|
if ((_parsed_packet >> i) & 1)
|
|
|
|
|
{
|
|
|
|
|
output.print("1");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
output.print("0");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
output.printf("\n");
|
|
|
|
|
|
|
|
|
|
output.printf("Current Value: %u\n", _packet.throttle_value);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-30 23:21:03 +01:00
|
|
|
// Print CPU information
|
2025-08-30 20:56:34 +01:00
|
|
|
void DShotRMT::printCpuInfo(Stream &output) const
|
|
|
|
|
{
|
2025-09-01 15:08:05 +01:00
|
|
|
output.println(" ");
|
2025-08-30 20:56:34 +01:00
|
|
|
output.println(" === CPU Info === ");
|
|
|
|
|
output.printf("Chip Model: %s\n", ESP.getChipModel());
|
|
|
|
|
output.printf("Chip Revision: %d\n", ESP.getChipRevision());
|
|
|
|
|
output.printf("CPU Freq = %lu MHz\n", ESP.getCpuFreqMHz());
|
|
|
|
|
output.printf("XTAL Freq = %lu MHz\n", getXtalFrequencyMhz());
|
|
|
|
|
output.printf("APB Freq = %lu Hz\n", getApbFrequency());
|
2025-08-28 12:22:42 +01:00
|
|
|
}
|
2025-09-08 08:05:54 +01:00
|
|
|
|
|
|
|
|
// --- HELPERS ---
|
|
|
|
|
void printDShotResult(dshot_result_t &result, Stream &output)
|
|
|
|
|
{
|
|
|
|
|
if (result.success)
|
|
|
|
|
{
|
|
|
|
|
output.printf("Staus: SUCCESS - %s\n", result.msg);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
output.printf("Status: FAILED - %s\n", result.msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output.println(" ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
void printDShotTelemetry(dshot_telemetry_result_t &result, Stream &output)
|
|
|
|
|
{
|
|
|
|
|
if (result.success)
|
|
|
|
|
{
|
|
|
|
|
output.printf("Telemetry: eRPM=%u, Motor RPM=%u \n", result.erpm, result.motor_rpm);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
output.printf("Telemetry: FAILED - %s\n", result.msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output.println(" ");
|
|
|
|
|
}
|