2025-09-09 16:19:57 +01:00
|
|
|
/**
|
|
|
|
|
* @file DShotRMT.h
|
|
|
|
|
* @brief DShot signal generation using ESP32 RMT with bidirectional support
|
|
|
|
|
* @author Wastl Kraus
|
|
|
|
|
* @date 2025-06-11
|
|
|
|
|
* @license MIT
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <dshot_commands.h>
|
|
|
|
|
#include <driver/gpio.h>
|
|
|
|
|
#include <driver/rmt_tx.h>
|
|
|
|
|
#include <driver/rmt_rx.h>
|
2025-09-13 10:54:30 +01:00
|
|
|
#include <atomic>
|
2025-09-09 16:19:57 +01:00
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// DShot Protocol Constants & Types
|
2025-09-09 16:19:57 +01:00
|
|
|
static constexpr auto DSHOT_THROTTLE_FAILSAFE = 0;
|
|
|
|
|
static constexpr auto DSHOT_THROTTLE_MIN = 48;
|
|
|
|
|
static constexpr auto DSHOT_THROTTLE_MAX = 2047;
|
|
|
|
|
static constexpr auto DSHOT_BITS_PER_FRAME = 16;
|
|
|
|
|
static constexpr auto DEFAULT_MOTOR_MAGNET_COUNT = 14;
|
|
|
|
|
|
|
|
|
|
// DShot Modes
|
2025-09-15 13:40:47 +01:00
|
|
|
typedef enum dshot_mode
|
2025-09-09 16:19:57 +01:00
|
|
|
{
|
|
|
|
|
DSHOT_OFF,
|
|
|
|
|
DSHOT150,
|
|
|
|
|
DSHOT300,
|
|
|
|
|
DSHOT600,
|
|
|
|
|
DSHOT1200
|
|
|
|
|
} dshot_mode_t;
|
|
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// DShot Packet Structure
|
2025-09-15 13:40:47 +01:00
|
|
|
typedef struct dshot_packet
|
2025-09-09 16:19:57 +01:00
|
|
|
{
|
|
|
|
|
uint16_t throttle_value : 11;
|
|
|
|
|
bool telemetric_request : 1;
|
|
|
|
|
uint16_t checksum : 4;
|
|
|
|
|
} dshot_packet_t;
|
|
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// DShot Timing Configuration
|
2025-09-15 13:40:47 +01:00
|
|
|
typedef struct dshot_timing
|
2025-09-12 22:14:34 +01:00
|
|
|
{
|
|
|
|
|
double bit_length_us;
|
|
|
|
|
double t1h_lenght_us;
|
|
|
|
|
} dshot_timing_us_t;
|
|
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// RMT Timing Configuration
|
2025-09-15 13:40:47 +01:00
|
|
|
typedef struct rmt_ticks
|
2025-09-09 16:19:57 +01:00
|
|
|
{
|
2025-09-15 13:40:47 +01:00
|
|
|
uint16_t bit_length_ticks;
|
2025-09-12 22:14:34 +01:00
|
|
|
uint16_t t1h_ticks;
|
|
|
|
|
uint16_t t1l_ticks;
|
|
|
|
|
uint16_t t0h_ticks;
|
|
|
|
|
uint16_t t0l_ticks;
|
|
|
|
|
} rmt_ticks_t;
|
2025-09-09 16:19:57 +01:00
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// Unified DShot Result Structure
|
2025-09-15 13:40:47 +01:00
|
|
|
typedef struct dshot_result
|
2025-09-09 16:19:57 +01:00
|
|
|
{
|
|
|
|
|
bool success;
|
|
|
|
|
const char *msg;
|
2025-09-11 12:58:22 +01:00
|
|
|
uint16_t erpm;
|
|
|
|
|
uint16_t motor_rpm;
|
2025-09-09 16:19:57 +01:00
|
|
|
} dshot_result_t;
|
|
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// Command Type Alias
|
2025-09-09 16:19:57 +01:00
|
|
|
typedef dshotCommands_e dshot_commands_t;
|
|
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// Helper Functions
|
2025-09-09 16:19:57 +01:00
|
|
|
void printDShotResult(dshot_result_t &result, Stream &output = Serial);
|
|
|
|
|
|
|
|
|
|
//
|
2025-09-13 10:54:30 +01:00
|
|
|
// DShotRMT Main Class
|
2025-09-09 16:19:57 +01:00
|
|
|
class DShotRMT
|
|
|
|
|
{
|
|
|
|
|
public:
|
2025-09-13 10:54:30 +01:00
|
|
|
// Constructors & Destructor
|
2025-09-09 16:19:57 +01:00
|
|
|
explicit DShotRMT(gpio_num_t gpio = GPIO_NUM_16, dshot_mode_t mode = DSHOT300, bool is_bidirectional = false);
|
|
|
|
|
DShotRMT(uint16_t pin_nr, dshot_mode_t mode, bool is_bidirectional);
|
2025-09-15 13:40:47 +01:00
|
|
|
|
2025-09-09 16:19:57 +01:00
|
|
|
~DShotRMT();
|
|
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// Public Core Functions
|
2025-09-09 16:19:57 +01:00
|
|
|
// Initialize the RMT module and DShot config
|
|
|
|
|
dshot_result_t begin();
|
2025-09-13 10:54:30 +01:00
|
|
|
|
2025-09-15 13:40:47 +01:00
|
|
|
// Send throttle value (48 - 2047)
|
2025-09-09 16:19:57 +01:00
|
|
|
dshot_result_t sendThrottle(uint16_t throttle);
|
2025-09-13 10:54:30 +01:00
|
|
|
|
2025-09-15 13:40:47 +01:00
|
|
|
// Send DShot command (0 - 47)
|
2025-09-09 16:19:57 +01:00
|
|
|
dshot_result_t sendCommand(uint16_t command);
|
2025-09-13 10:54:30 +01:00
|
|
|
|
|
|
|
|
// Get telemetry data (bidirectional mode only)
|
|
|
|
|
dshot_result_t getTelemetry(uint16_t magnet_count = DEFAULT_MOTOR_MAGNET_COUNT);
|
|
|
|
|
|
|
|
|
|
// Public Info & Debug Functions
|
2025-09-09 16:19:57 +01:00
|
|
|
void printDShotInfo(Stream &output = Serial) const;
|
|
|
|
|
void printCpuInfo(Stream &output = Serial) const;
|
2025-09-13 10:54:30 +01:00
|
|
|
|
|
|
|
|
// Deprecated Methods
|
2025-09-09 16:19:57 +01:00
|
|
|
[[deprecated("Use sendThrottle() instead")]]
|
|
|
|
|
bool setThrottle(uint16_t throttle)
|
|
|
|
|
{
|
|
|
|
|
auto result = sendThrottle(throttle);
|
|
|
|
|
return result.success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[deprecated("Use sendCommand() instead")]]
|
|
|
|
|
bool sendDShotCommand(uint16_t command)
|
|
|
|
|
{
|
|
|
|
|
auto result = sendCommand(command);
|
|
|
|
|
return result.success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[deprecated("Use getTelemetry() instead")]]
|
|
|
|
|
uint32_t getMotorRPM(uint8_t magnet_count)
|
|
|
|
|
{
|
|
|
|
|
auto result = getTelemetry(magnet_count);
|
2025-09-11 12:58:22 +01:00
|
|
|
return result.motor_rpm;
|
2025-09-09 16:19:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2025-09-13 10:54:30 +01:00
|
|
|
// Configuration Constants
|
|
|
|
|
static constexpr bool DSHOT_OK = 0;
|
|
|
|
|
static constexpr bool DSHOT_ERROR = 1;
|
|
|
|
|
|
|
|
|
|
static constexpr auto const DSHOT_NULL_PACKET = 0b0000000000000000;
|
|
|
|
|
static constexpr auto const DSHOT_FULL_PACKET = 0b1111111111111111;
|
|
|
|
|
static constexpr auto const DSHOT_CRC_MASK = 0b0000000000001111;
|
|
|
|
|
static constexpr auto const DSHOT_CLOCK_SRC_DEFAULT = RMT_CLK_SRC_DEFAULT;
|
|
|
|
|
static constexpr auto const DSHOT_RMT_RESOLUTION = 8 * 1000 * 1000; // 8 MHz resolution
|
|
|
|
|
static constexpr auto const RMT_TICKS_PER_US = DSHOT_RMT_RESOLUTION / (1 * 1000 * 1000); // RMT Ticks per microsecond
|
|
|
|
|
static constexpr auto const DSHOT_RX_TIMEOUT_MS = 2;
|
2025-09-15 13:40:47 +01:00
|
|
|
static constexpr auto const DSHOT_PADDING_US = 20; // Add to pause between frames for compatibility
|
2025-09-16 12:41:32 +01:00
|
|
|
static constexpr auto const RMT_BUFFER_SYMBOLS = 192;
|
|
|
|
|
static constexpr auto const RMT_QUEUE_DEPTH = 4;
|
2025-09-13 10:54:30 +01:00
|
|
|
static constexpr auto const GCR_BITS_PER_FRAME = 21; // Number of GCR bits in a DShot answer frame
|
|
|
|
|
static constexpr auto const POLE_PAIRS_MIN = 1;
|
|
|
|
|
static constexpr auto const MAGNETS_PER_POLE_PAIR = 2;
|
|
|
|
|
static constexpr auto const NO_DSHOT_TELEMETRY = 0;
|
2025-09-17 20:41:20 +01:00
|
|
|
static constexpr auto const DSHOT_PULSE_MIN = 1000; // 1.0us minimum pulse
|
|
|
|
|
static constexpr auto const DSHOT_PULSE_MAX = 8000; // 10.0us maximum pulse
|
2025-09-15 14:56:04 +01:00
|
|
|
static constexpr auto const DSHOT_TELEMETRY_INVALID = DSHOT_THROTTLE_MAX;
|
2025-09-13 10:54:30 +01:00
|
|
|
|
|
|
|
|
// Error Messages
|
|
|
|
|
static constexpr char const *NONE = "";
|
|
|
|
|
static constexpr char const *UNKNOWN_ERROR = "Unknown Error!";
|
|
|
|
|
static constexpr char const *INIT_SUCCESS = "SignalGeneratorRMT initialized successfully";
|
|
|
|
|
static constexpr char const *INIT_FAILED = "SignalGeneratorRMT init failed!";
|
|
|
|
|
static constexpr char const *TX_INIT_SUCCESS = "TX RMT channel initialized successfully";
|
|
|
|
|
static constexpr char const *TX_INIT_FAILED = "TX RMT channel init failed!";
|
|
|
|
|
static constexpr char const *RX_INIT_SUCCESS = "RX RMT channel initialized successfully";
|
|
|
|
|
static constexpr char const *RX_INIT_FAILED = "RX RMT channel init failed!";
|
|
|
|
|
static constexpr char const *ENCODER_INIT_SUCCESS = "RMT encoder initialized successfully";
|
|
|
|
|
static constexpr char const *ENCODER_INIT_FAILED = "RMT encoder init failed!";
|
2025-09-15 14:56:04 +01:00
|
|
|
static constexpr char const *ENCODING_SUCCESS = "Packet encoded successfully";
|
2025-09-13 10:54:30 +01:00
|
|
|
static constexpr char const *TRANSMISSION_SUCCESS = "Transmission successfully";
|
|
|
|
|
static constexpr char const *TRANSMISSION_FAILED = "Transmission failed!";
|
|
|
|
|
static constexpr char const *RECEIVER_FAILED = "RMT receiver failed!";
|
|
|
|
|
static constexpr char const *THROTTLE_NOT_IN_RANGE = "Throttle not in range! (48 - 2047)";
|
|
|
|
|
static constexpr char const *COMMAND_NOT_VALID = "Command not valid! (0 - 47)";
|
|
|
|
|
static constexpr char const *BIDIR_NOT_ENABLED = "Bidirectional DShot not enabled!";
|
|
|
|
|
static constexpr char const *TELEMETRY_SUCCESS = "Valid Telemetric Frame received!";
|
|
|
|
|
static constexpr char const *TELEMETRY_FAILED = "No valid Telemetric Frame received!";
|
|
|
|
|
static constexpr char const *INVALID_MAGNET_COUNT = "Invalid motor magnet count!";
|
|
|
|
|
static constexpr char const *TIMING_CORRECTION = "Timing correction!";
|
2025-09-17 20:41:20 +01:00
|
|
|
static constexpr char const *CALLBACK_REGISTERING_FAILED = "RMT RX Callback registering failed!";
|
|
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// Core Configuration Variables
|
2025-09-09 16:19:57 +01:00
|
|
|
gpio_num_t _gpio;
|
|
|
|
|
dshot_mode_t _mode;
|
|
|
|
|
bool _is_bidirectional;
|
2025-09-13 10:54:30 +01:00
|
|
|
const dshot_timing_us_t &_dshot_timing;
|
2025-09-15 14:56:04 +01:00
|
|
|
uint64_t _frame_timer_us;
|
2025-09-13 10:54:30 +01:00
|
|
|
|
|
|
|
|
// Timing & Packet Variables
|
2025-09-12 22:14:34 +01:00
|
|
|
rmt_ticks_t _rmt_ticks;
|
2025-09-09 16:19:57 +01:00
|
|
|
uint16_t _last_throttle;
|
2025-09-12 22:14:34 +01:00
|
|
|
uint64_t _last_transmission_time_us;
|
2025-09-09 16:19:57 +01:00
|
|
|
uint16_t _parsed_packet;
|
|
|
|
|
dshot_packet_t _packet;
|
|
|
|
|
uint8_t _bitPositions[DSHOT_BITS_PER_FRAME];
|
|
|
|
|
uint16_t _level0;
|
|
|
|
|
uint16_t _level1;
|
2025-09-13 10:54:30 +01:00
|
|
|
|
|
|
|
|
// RMT Hardware Handles
|
2025-09-09 16:19:57 +01:00
|
|
|
rmt_channel_handle_t _rmt_tx_channel;
|
|
|
|
|
rmt_channel_handle_t _rmt_rx_channel;
|
|
|
|
|
rmt_encoder_handle_t _dshot_encoder;
|
|
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// RMT Configuration Structures
|
2025-09-09 16:19:57 +01:00
|
|
|
rmt_tx_channel_config_t _tx_channel_config;
|
|
|
|
|
rmt_rx_channel_config_t _rx_channel_config;
|
2025-09-15 14:56:04 +01:00
|
|
|
rmt_transmit_config_t _rmt_tx_config;
|
|
|
|
|
rmt_receive_config_t _rmt_rx_config;
|
2025-09-09 16:19:57 +01:00
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// Bidirectional / Telemetry Variables
|
|
|
|
|
rmt_rx_event_callbacks_t _rx_event_callbacks;
|
|
|
|
|
std::atomic<uint16_t> _last_erpm_atomic;
|
|
|
|
|
std::atomic<bool> _telemetry_ready_flag_atomic;
|
|
|
|
|
|
|
|
|
|
// Private Initialization Functions
|
2025-09-09 16:19:57 +01:00
|
|
|
dshot_result_t _initTXChannel();
|
|
|
|
|
dshot_result_t _initRXChannel();
|
|
|
|
|
dshot_result_t _initDShotEncoder();
|
|
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// Private Packet Management Functions
|
2025-09-13 10:05:33 +01:00
|
|
|
dshot_packet_t _buildDShotPacket(const uint16_t &value);
|
2025-09-09 16:19:57 +01:00
|
|
|
uint16_t _parseDShotPacket(const dshot_packet_t &packet);
|
2025-09-15 13:40:47 +01:00
|
|
|
uint16_t _calculateCRC(const uint16_t &data);
|
2025-09-15 14:56:04 +01:00
|
|
|
void _preCalculateRMTTicks();
|
2025-09-09 16:19:57 +01:00
|
|
|
void _preCalculateBitPositions();
|
|
|
|
|
|
2025-09-13 10:54:30 +01:00
|
|
|
// Private Frame Processing Functions
|
2025-09-09 16:19:57 +01:00
|
|
|
dshot_result_t _sendDShotFrame(const dshot_packet_t &packet);
|
2025-09-15 14:56:04 +01:00
|
|
|
dshot_result_t _encodeDShotFrame(const dshot_packet_t &packet, rmt_symbol_word_t *symbols);
|
2025-09-09 16:19:57 +01:00
|
|
|
uint16_t _decodeDShotFrame(const rmt_symbol_word_t *symbols);
|
2025-09-13 10:54:30 +01:00
|
|
|
|
|
|
|
|
// Private Timing Control Functions
|
2025-09-15 10:58:56 +01:00
|
|
|
bool _timer_signal();
|
2025-09-09 16:19:57 +01:00
|
|
|
bool _timer_reset();
|
2025-09-13 10:54:30 +01:00
|
|
|
|
|
|
|
|
// Static Callback Functions
|
2025-09-15 10:58:56 +01:00
|
|
|
static bool _on_rx_done(rmt_channel_handle_t rmt_rx_channel, const rmt_rx_done_event_data_t *edata, void *user_data);
|
2025-09-09 16:19:57 +01:00
|
|
|
};
|