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-28 12:22:42 +01:00
|
|
|
#include <driver/rmt_tx.h>
|
2025-07-30 23:36:58 +01:00
|
|
|
|
|
|
|
|
// --- DShot Timings ---
|
2025-08-17 17:58:40 +01:00
|
|
|
// frame_length_us, ticks_per_bit, ticks_one_high, ticks_one_low, ticks_zero_high, ticks_zero_low
|
2025-08-03 21:15:38 +01:00
|
|
|
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-08-17 22:00:43 +01:00
|
|
|
// --- DShot Config Constructor ---
|
2025-08-28 12:22:42 +01:00
|
|
|
DShotRMT::DShotRMT(gpio_num_t gpio, dshot_mode_t mode, bool is_bidirectional) : _gpio(gpio),
|
|
|
|
|
_mode(mode),
|
|
|
|
|
_is_bidirectional(is_bidirectional),
|
|
|
|
|
_timing_config(DSHOT_TIMINGS[mode]),
|
|
|
|
|
_rmt_tx_channel(nullptr),
|
|
|
|
|
_rmt_rx_channel(nullptr),
|
|
|
|
|
_dshot_encoder(nullptr),
|
|
|
|
|
_last_erpm(0),
|
|
|
|
|
_current_packet(0),
|
|
|
|
|
_packet{0},
|
|
|
|
|
_last_transmission_time(0)
|
2025-07-25 16:41:38 +01:00
|
|
|
{
|
2025-08-17 22:00:43 +01:00
|
|
|
// Calculates frame time and adds switch/pause time
|
|
|
|
|
_frame_timer_us = _timing_config.frame_length_us + DSHOT_SWITCH_TIME;
|
|
|
|
|
|
|
|
|
|
// Doubles up frame time for bidirectional mode
|
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
|
|
|
// DSHOT_SWITCH_TIME also needed two times
|
|
|
|
|
_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-06 22:57:26 +01:00
|
|
|
// Easy Constructor
|
2025-08-28 12:22:42 +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-17 17:58:40 +01:00
|
|
|
// ...just to accept pin numbers and GPIO_NUMs
|
2025-08-06 22:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 17:58:40 +01:00
|
|
|
// Setup and configure DShotRMT
|
2025-08-28 13:37:02 +01:00
|
|
|
uint16_t DShotRMT::begin()
|
2022-11-25 15:08:58 +00:00
|
|
|
{
|
2025-08-17 22:00:43 +01:00
|
|
|
// Inits TX Channel
|
2025-07-30 23:36:58 +01:00
|
|
|
if (!_initTXChannel())
|
2025-06-12 23:45:48 +01:00
|
|
|
{
|
2025-08-05 23:32:03 +01:00
|
|
|
Serial.println(DSHOT_MSG_01);
|
2025-07-30 23:36:58 +01:00
|
|
|
return DSHOT_ERROR;
|
2025-06-12 23:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
// Inits RX Channel
|
2025-07-30 23:36:58 +01:00
|
|
|
if (!_initRXChannel() && _is_bidirectional)
|
2025-07-19 12:26:54 +01:00
|
|
|
{
|
2025-08-05 23:32:03 +01:00
|
|
|
Serial.println(DSHOT_MSG_02);
|
2025-07-30 23:36:58 +01:00
|
|
|
return DSHOT_ERROR;
|
2025-07-19 12:26:54 +01:00
|
|
|
}
|
2025-07-29 23:40:09 +01:00
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
// Inits DShot Decoder
|
2025-07-30 23:36:58 +01:00
|
|
|
if (!_initDShotEncoder())
|
2025-07-19 12:26:54 +01:00
|
|
|
{
|
2025-08-17 22:00:43 +01:00
|
|
|
// Serial.println(DSHOT_MSG_03);
|
2025-07-30 23:36:58 +01:00
|
|
|
return DSHOT_ERROR;
|
2025-07-19 12:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-05 21:01:00 +01:00
|
|
|
// All good, ready
|
2025-07-30 23:36:58 +01:00
|
|
|
return DSHOT_OK;
|
2021-06-29 19:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 17:58:40 +01:00
|
|
|
// Deprecated, use "sendThrottle()"" instead
|
2025-07-30 23:36:58 +01:00
|
|
|
bool DShotRMT::setThrottle(uint16_t throttle)
|
2022-11-25 15:08:58 +00:00
|
|
|
{
|
2025-08-06 22:57:26 +01:00
|
|
|
return sendThrottle(throttle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sends a valid throttle value
|
|
|
|
|
bool DShotRMT::sendThrottle(uint16_t throttle)
|
|
|
|
|
{
|
2025-08-28 12:22:42 +01:00
|
|
|
// Validate throttle value
|
|
|
|
|
if (throttle < DSHOT_THROTTLE_MIN || throttle > DSHOT_THROTTLE_MAX)
|
|
|
|
|
{
|
|
|
|
|
Serial.println(DSHOT_MSG_05);
|
|
|
|
|
return DSHOT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Constrain throttle value
|
2025-08-17 17:58:40 +01:00
|
|
|
auto value = constrain(throttle, DSHOT_THROTTLE_MIN, DSHOT_THROTTLE_MAX);
|
2025-08-06 22:57:26 +01:00
|
|
|
|
2025-08-28 12:22:42 +01:00
|
|
|
// Build and send packet
|
2025-08-17 17:58:40 +01:00
|
|
|
_packet = _buildDShotPacket(value);
|
2025-08-28 12:22:42 +01:00
|
|
|
return _sendDShotFrame(_packet);
|
2021-06-29 19:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 17:58:40 +01:00
|
|
|
// Deprecated, use "sendCommand()"" instead
|
2025-08-02 16:20:32 +01:00
|
|
|
bool DShotRMT::sendDShotCommand(uint16_t command)
|
|
|
|
|
{
|
2025-08-06 22:57:26 +01:00
|
|
|
return sendCommand(command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DShotRMT::sendCommand(uint16_t command)
|
|
|
|
|
{
|
|
|
|
|
// Check for valid command
|
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-08-17 22:00:43 +01:00
|
|
|
Serial.println(DSHOT_MSG_06);
|
2025-08-02 16:20:32 +01:00
|
|
|
return DSHOT_ERROR;
|
|
|
|
|
}
|
2025-08-05 21:01:00 +01:00
|
|
|
|
2025-08-06 22:57:26 +01:00
|
|
|
_packet = _buildDShotPacket(command);
|
2025-08-28 12:22:42 +01:00
|
|
|
return _sendDShotFrame(_packet);
|
2025-08-02 16:20:32 +01:00
|
|
|
}
|
|
|
|
|
|
2025-07-30 23:36:58 +01:00
|
|
|
//
|
2025-08-05 23:32:03 +01:00
|
|
|
uint16_t DShotRMT::getERPM()
|
2023-03-27 18:47:23 +01:00
|
|
|
{
|
2025-07-30 23:36:58 +01:00
|
|
|
if (!_is_bidirectional || !_rmt_rx_channel)
|
2025-06-14 17:16:45 +01:00
|
|
|
{
|
2025-08-17 22:00:43 +01:00
|
|
|
Serial.println(DSHOT_MSG_07);
|
2025-07-30 23:36:58 +01:00
|
|
|
return _last_erpm;
|
|
|
|
|
}
|
2025-06-13 20:50:00 +01:00
|
|
|
|
2025-07-30 23:36:58 +01:00
|
|
|
// Try to receive telemetry data
|
|
|
|
|
if (!rmt_receive(_rmt_rx_channel, _rx_symbols, DSHOT_SYMBOLS_SIZE, &_receive_config))
|
|
|
|
|
{
|
2025-08-17 22:00:43 +01:00
|
|
|
Serial.println(DSHOT_MSG_08);
|
2025-07-17 22:24:28 +01:00
|
|
|
return _last_erpm;
|
2025-06-14 17:16:45 +01:00
|
|
|
}
|
2025-07-17 22:24:28 +01:00
|
|
|
|
2025-08-08 13:43:26 +01:00
|
|
|
// Decodes the response
|
2025-08-05 23:32:03 +01:00
|
|
|
uint16_t new_erpm = _decodeDShotFrame(_rx_symbols);
|
2025-07-30 23:36:58 +01:00
|
|
|
if (new_erpm != 0)
|
|
|
|
|
{
|
|
|
|
|
_last_erpm = new_erpm;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 20:50:00 +01:00
|
|
|
return _last_erpm;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-30 23:36:58 +01:00
|
|
|
//
|
2025-06-14 17:16:45 +01:00
|
|
|
uint32_t DShotRMT::getMotorRPM(uint8_t magnet_count)
|
|
|
|
|
{
|
2025-07-30 23:36:58 +01:00
|
|
|
uint8_t pole_pairs = max(1, magnet_count / 2);
|
|
|
|
|
return getERPM() / pole_pairs;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
// --- RMT TX Config ---
|
2025-07-30 23:36:58 +01:00
|
|
|
bool DShotRMT::_initTXChannel()
|
|
|
|
|
{
|
|
|
|
|
_tx_channel_config.gpio_num = _gpio;
|
|
|
|
|
_tx_channel_config.clk_src = DSHOT_CLOCK_SRC_DEFAULT;
|
|
|
|
|
_tx_channel_config.resolution_hz = DSHOT_RMT_RESOLUTION;
|
|
|
|
|
_tx_channel_config.mem_block_symbols = DSHOT_SYMBOLS_SIZE;
|
|
|
|
|
_tx_channel_config.trans_queue_depth = TX_BUFFER_SIZE;
|
|
|
|
|
|
2025-08-08 13:43:26 +01:00
|
|
|
// No loops, real time calculation for each frame
|
2025-07-30 23:36:58 +01:00
|
|
|
_transmit_config.loop_count = 0;
|
|
|
|
|
|
|
|
|
|
// ...it's a trap
|
|
|
|
|
_transmit_config.flags.eot_level = _is_bidirectional ? 1 : 0;
|
2025-06-14 17:16:45 +01:00
|
|
|
|
2025-08-05 23:32:03 +01:00
|
|
|
// Creates and activates RMT TX Channel
|
2025-07-30 23:36:58 +01:00
|
|
|
if (rmt_new_tx_channel(&_tx_channel_config, &_rmt_tx_channel) != DSHOT_OK)
|
|
|
|
|
{
|
2025-08-17 22:00:43 +01:00
|
|
|
Serial.println(DSHOT_MSG_01);
|
2025-07-30 23:36:58 +01:00
|
|
|
return DSHOT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
return (rmt_enable(_rmt_tx_channel) == DSHOT_OK);
|
2025-06-14 17:16:45 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
// --- RMT RX Config ---
|
2025-07-30 23:36:58 +01:00
|
|
|
bool DShotRMT::_initRXChannel()
|
2025-06-17 19:56:50 +01:00
|
|
|
{
|
2025-07-30 23:36:58 +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;
|
|
|
|
|
_rx_channel_config.mem_block_symbols = DSHOT_SYMBOLS_SIZE;
|
2025-06-17 19:56:50 +01:00
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
// TODO: figure out ranges
|
2025-08-17 17:58:40 +01:00
|
|
|
_receive_config.signal_range_min_ns = 2;
|
|
|
|
|
_receive_config.signal_range_max_ns = 128;
|
2025-07-17 22:24:28 +01:00
|
|
|
|
2025-08-05 23:32:03 +01:00
|
|
|
// Creates and activates RMT TX Channel
|
2025-07-30 23:36:58 +01:00
|
|
|
if (rmt_new_rx_channel(&_rx_channel_config, &_rmt_rx_channel) != DSHOT_OK)
|
|
|
|
|
{
|
2025-08-17 22:00:43 +01:00
|
|
|
Serial.println(DSHOT_MSG_02);
|
2025-07-30 23:36:58 +01:00
|
|
|
return DSHOT_ERROR;
|
|
|
|
|
}
|
2025-07-29 23:40:09 +01:00
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
return (rmt_enable(_rmt_rx_channel) == DSHOT_OK);
|
2025-06-17 19:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
// --- RMT Encoder Config ---
|
2025-07-30 23:36:58 +01:00
|
|
|
bool DShotRMT::_initDShotEncoder()
|
2025-06-17 19:56:50 +01:00
|
|
|
{
|
2025-08-17 22:00:43 +01:00
|
|
|
// Encoder "config"
|
2025-07-30 23:36:58 +01:00
|
|
|
rmt_copy_encoder_config_t encoder_config = {};
|
2025-08-17 22:00:43 +01:00
|
|
|
|
|
|
|
|
// Creates a dummy encoder
|
|
|
|
|
if (rmt_new_copy_encoder(&encoder_config, &_dshot_encoder) != DSHOT_OK)
|
|
|
|
|
{
|
|
|
|
|
Serial.println(DSHOT_MSG_03);
|
|
|
|
|
return DSHOT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DSHOT_OK;
|
2025-06-17 19:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
// Uses RMT to transmit a prepared DShot packet and returns it
|
2025-08-05 00:43:19 +01:00
|
|
|
bool DShotRMT::_sendDShotFrame(const dshot_packet_t &packet)
|
2025-06-13 20:50:00 +01:00
|
|
|
{
|
2025-08-28 12:22:42 +01:00
|
|
|
// Check if we can send (timing check)
|
|
|
|
|
if (!_timer_signal())
|
|
|
|
|
{
|
|
|
|
|
return DSHOT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Encode the frame
|
2025-08-18 21:11:32 +01:00
|
|
|
_encodeDShotFrame(packet, _tx_symbols);
|
2025-08-05 23:32:03 +01:00
|
|
|
|
2025-08-28 12:22:42 +01:00
|
|
|
// Attempt to transmit
|
|
|
|
|
size_t tx_size_bytes = DSHOT_BITS_PER_FRAME * sizeof(rmt_symbol_word_t);
|
|
|
|
|
bool result = rmt_transmit(_rmt_tx_channel, _dshot_encoder, _tx_symbols, tx_size_bytes, &_transmit_config);
|
2025-08-05 23:32:03 +01:00
|
|
|
|
2025-08-28 12:22:42 +01:00
|
|
|
if (result != DSHOT_OK)
|
|
|
|
|
{
|
|
|
|
|
return DSHOT_ERROR;
|
2025-07-30 23:36:58 +01:00
|
|
|
}
|
2025-07-29 23:40:09 +01:00
|
|
|
|
2025-08-28 12:22:42 +01:00
|
|
|
// Update timestamp
|
|
|
|
|
_timer_reset();
|
|
|
|
|
|
|
|
|
|
return DSHOT_OK;
|
2025-07-30 23:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-04 22:29:49 +01:00
|
|
|
// Calculates checksum for given package
|
2025-07-30 23:36:58 +01:00
|
|
|
uint16_t DShotRMT::_calculateCRC(const dshot_packet_t &packet)
|
|
|
|
|
{
|
|
|
|
|
uint16_t data = (packet.throttle_value << 1) | packet.telemetric_request;
|
|
|
|
|
uint16_t crc = (data ^ (data >> 4) ^ (data >> 8)) & 0b0000000000001111;
|
|
|
|
|
|
2025-08-08 13:43:26 +01:00
|
|
|
// Inverts CRC for bidirectional DShot
|
2025-08-04 22:29:49 +01:00
|
|
|
if (_is_bidirectional)
|
2025-07-30 23:36:58 +01:00
|
|
|
{
|
|
|
|
|
crc = (~crc) & 0b0000000000001111;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return crc;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 23:32:03 +01:00
|
|
|
// Returns bitwise parsed DShot packet
|
2025-08-04 22:29:49 +01:00
|
|
|
uint16_t DShotRMT::_parseDShotPacket(const dshot_packet_t &packet)
|
2025-07-30 23:36:58 +01:00
|
|
|
{
|
|
|
|
|
uint16_t data = (packet.throttle_value << 1) | packet.telemetric_request;
|
2025-08-05 00:43:19 +01:00
|
|
|
return (data << 4) | _calculateCRC(packet);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 23:32:03 +01:00
|
|
|
// Returns a "true" DShot Packet ready to roll
|
2025-08-05 00:43:19 +01:00
|
|
|
dshot_packet_t DShotRMT::_buildDShotPacket(const uint16_t value)
|
|
|
|
|
{
|
|
|
|
|
// DShot Frame Container
|
|
|
|
|
dshot_packet_t packet = {};
|
|
|
|
|
|
2025-08-08 13:43:26 +01:00
|
|
|
// Creates DShot packet
|
2025-08-05 00:43:19 +01:00
|
|
|
packet.throttle_value = value;
|
2025-08-28 12:22:42 +01:00
|
|
|
packet.telemetric_request = _is_bidirectional ? 1 : 0;
|
2025-08-05 00:43:19 +01:00
|
|
|
packet.checksum = _calculateCRC(packet);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
return packet;
|
2025-07-30 23:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 22:00:43 +01:00
|
|
|
// Encodes DShot packet into RMT buffer and places code into IRAM instead of flash
|
2025-08-25 12:46:46 +01:00
|
|
|
bool DShotRMT::_encodeDShotFrame(const dshot_packet_t &packet, rmt_symbol_word_t *symbols)
|
2025-07-30 23:36:58 +01:00
|
|
|
{
|
2025-08-05 23:32:03 +01:00
|
|
|
// Parse actual packet into buffer
|
2025-08-05 00:43:19 +01:00
|
|
|
_current_packet = _parseDShotPacket(packet);
|
2025-08-02 15:11:10 +01:00
|
|
|
|
2025-08-08 13:43:26 +01:00
|
|
|
// Converts the parsed dshot frame to rmt_tx data
|
2025-08-03 21:15:38 +01:00
|
|
|
for (int i = 0; i < DSHOT_BITS_PER_FRAME; i++)
|
|
|
|
|
{
|
2025-08-08 13:43:26 +01:00
|
|
|
// Encoded RMT symbols bitwise (MSB first) - tricky
|
2025-08-05 00:43:19 +01:00
|
|
|
bool bit = (_current_packet >> (DSHOT_BITS_PER_FRAME - 1 - i)) & 0b0000000000000001;
|
2025-08-03 21:15:38 +01:00
|
|
|
if (_is_bidirectional)
|
|
|
|
|
{
|
|
|
|
|
symbols[i].level0 = 0;
|
|
|
|
|
symbols[i].duration0 = bit ? _timing_config.ticks_one_high : _timing_config.ticks_zero_high;
|
|
|
|
|
symbols[i].level1 = 1;
|
|
|
|
|
symbols[i].duration1 = bit ? _timing_config.ticks_one_low : _timing_config.ticks_zero_low;
|
|
|
|
|
}
|
|
|
|
|
else
|
2025-07-31 12:16:58 +01:00
|
|
|
{
|
2025-08-03 21:15:38 +01:00
|
|
|
symbols[i].level0 = 1;
|
|
|
|
|
symbols[i].duration0 = bit ? _timing_config.ticks_one_high : _timing_config.ticks_zero_high;
|
|
|
|
|
symbols[i].level1 = 0;
|
|
|
|
|
symbols[i].duration1 = bit ? _timing_config.ticks_one_low : _timing_config.ticks_zero_low;
|
2025-07-17 22:24:28 +01:00
|
|
|
}
|
2023-04-15 06:45:14 +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-07-30 23:36:58 +01:00
|
|
|
//
|
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-07-30 23:36:58 +01:00
|
|
|
uint16_t received_frame = 0;
|
2025-07-17 22:24:28 +01:00
|
|
|
|
2025-08-08 13:43:26 +01:00
|
|
|
// Decodes each symbol to reconstruct the frame
|
2025-07-30 23:36:58 +01:00
|
|
|
for (size_t i = 0; i < DSHOT_BITS_PER_FRAME; ++i)
|
2025-07-17 22:24:28 +01:00
|
|
|
{
|
2025-08-28 13:37:02 +01:00
|
|
|
bool bit = symbols[i].duration0 > symbols[i].duration1;
|
2025-07-19 15:41:04 +01:00
|
|
|
received_frame = (received_frame << 1) | bit;
|
2025-07-17 22:24:28 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-08 13:43:26 +01:00
|
|
|
// Extracts payload and CRC
|
2025-07-30 23:36:58 +01:00
|
|
|
uint16_t data = received_frame >> 4;
|
2025-08-05 23:32:03 +01:00
|
|
|
uint16_t received_crc = received_frame & 0b0000000000001111;
|
2025-07-17 22:24:28 +01:00
|
|
|
|
2025-08-08 13:43:26 +01:00
|
|
|
// Calculates CRC for received frame
|
2025-08-05 23:32:03 +01:00
|
|
|
uint16_t calculated_crc = (data ^ (data >> 4) ^ (data >> 8)) & 0b0000000000001111;
|
2025-07-30 23:36:58 +01:00
|
|
|
if (_is_bidirectional)
|
|
|
|
|
{
|
|
|
|
|
calculated_crc = (~calculated_crc) & 0b0000000000001111;
|
|
|
|
|
}
|
2025-07-17 22:24:28 +01:00
|
|
|
|
2025-08-08 13:43:26 +01:00
|
|
|
// Compares CRC
|
2025-07-30 23:36:58 +01:00
|
|
|
if (received_crc != calculated_crc)
|
2025-07-19 15:41:04 +01:00
|
|
|
{
|
2025-08-05 23:32:03 +01:00
|
|
|
Serial.println(DSHOT_MSG_04);
|
2025-07-30 23:36:58 +01:00
|
|
|
return 0b0000000000000000;
|
2025-07-19 15:41:04 +01:00
|
|
|
}
|
2025-07-17 22:24:28 +01:00
|
|
|
|
2025-08-05 23:32:03 +01:00
|
|
|
// Removes telemetry bit and returns 10bit value
|
2025-07-30 23:36:58 +01:00
|
|
|
return data >> 1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-28 12:22:42 +01:00
|
|
|
//
|
|
|
|
|
void DShotRMT::printTimingDiagnostics() const
|
|
|
|
|
{
|
|
|
|
|
uint32_t current_time = micros();
|
|
|
|
|
Serial.println("\n=== DShot Timing Diagnostics ===");
|
|
|
|
|
Serial.printf("Current mode: DSHOT%d\n", _mode == DSHOT150 ? 150 : _mode == DSHOT300 ? 300
|
|
|
|
|
: _mode == DSHOT600 ? 600
|
|
|
|
|
: _mode == DSHOT1200);
|
|
|
|
|
Serial.printf("Protocol Frame length: %u µs\n", _timing_config.frame_length_us);
|
|
|
|
|
Serial.printf("Frame to Frame: %u µs\n", _frame_timer_us);
|
|
|
|
|
Serial.printf("Bidirectional: %s\n", _is_bidirectional ? "Yes" : "No");
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 23:32:03 +01:00
|
|
|
// Timer triggered
|
2025-08-25 12:46:46 +01:00
|
|
|
bool DShotRMT::_timer_signal()
|
2025-07-30 23:36:58 +01:00
|
|
|
{
|
2025-08-28 12:22:42 +01:00
|
|
|
// fixing possible overflow
|
|
|
|
|
uint32_t current_time = micros();
|
|
|
|
|
uint32_t elapsed = current_time - _last_transmission_time;
|
|
|
|
|
return elapsed >= _frame_timer_us;
|
2025-07-30 23:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-05 23:32:03 +01:00
|
|
|
// Updates timestamp
|
2025-08-03 21:15:38 +01:00
|
|
|
bool DShotRMT::_timer_reset()
|
2025-07-30 23:36:58 +01:00
|
|
|
{
|
|
|
|
|
_last_transmission_time = micros();
|
2025-08-03 21:15:38 +01:00
|
|
|
return DSHOT_OK;
|
2025-07-18 10:13:43 +01:00
|
|
|
}
|