DShotRMT/DShotRMT.h

186 lines
6.0 KiB
C
Raw Normal View History

/**
* @file DShotRMT.h
2025-07-30 23:36:58 +01:00
* @brief DShot signal generation using ESP32 RMT with bidirectional support
* @author Wastl Kraus
* @date 2025-06-11
* @license MIT
*/
2021-06-29 19:05:20 +01:00
#pragma once
2021-06-29 19:05:20 +01:00
#include <Arduino.h>
2025-06-18 17:40:16 +01:00
#include <dshot_commands.h>
2025-06-13 20:50:00 +01:00
#include <driver/gpio.h>
#include <driver/rmt_tx.h>
2025-06-12 23:45:48 +01:00
#include <driver/rmt_rx.h>
2025-09-01 15:08:05 +01:00
#include <iostream>
#include <string_view>
2025-07-29 23:40:09 +01:00
// DShot Protocol Constants
2025-08-28 13:37:02 +01:00
constexpr auto DSHOT_THROTTLE_FAILSAFE = 0;
constexpr auto DSHOT_THROTTLE_MIN = 48;
constexpr auto DSHOT_THROTTLE_MAX = 2047;
constexpr auto DSHOT_BITS_PER_FRAME = 16;
constexpr auto DSHOT_SWITCH_TIME = 30; // Time in us between TX and RX
2025-08-28 13:37:02 +01:00
constexpr auto DSHOT_NULL_PACKET = 0b0000000000000000;
2025-08-31 10:43:48 +01:00
constexpr auto DSHOT_RX_TIMEOUT_MS = 2;
2025-07-30 23:36:58 +01:00
// RMT Configuration Constants
2025-08-28 13:37:02 +01:00
constexpr auto DSHOT_CLOCK_SRC_DEFAULT = RMT_CLK_SRC_DEFAULT;
2025-08-30 23:21:03 +01:00
constexpr auto DSHOT_RMT_RESOLUTION = 10 * 1000 * 1000; // 10 MHz resolution
constexpr auto RMT_BUFFER_SIZE = DSHOT_BITS_PER_FRAME;
constexpr auto RMT_BUFFER_SYMBOLS = 64;
2025-09-02 14:18:37 +01:00
constexpr auto RMT_QUEUE_DEPTH = 1;
2025-08-31 19:23:04 +01:00
// 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;
2025-08-30 23:21:03 +01:00
// DShot Mode Enumeration
typedef enum
2022-11-25 15:08:58 +00:00
{
DSHOT_OFF,
2023-03-24 12:44:49 +00:00
DSHOT150,
DSHOT300,
DSHOT600,
2025-07-30 13:44:01 +01:00
DSHOT1200
2021-06-29 19:05:20 +01:00
} dshot_mode_t;
// DShot Packet Structure
typedef struct
2025-07-30 13:44:01 +01:00
{
2025-08-30 23:21:03 +01:00
uint16_t throttle_value : 11;
uint16_t telemetric_request : 1;
uint16_t checksum : 4;
2025-07-30 23:36:58 +01:00
} dshot_packet_t;
2025-08-30 23:21:03 +01:00
// DShot Timing Configuration Structure
typedef struct
2025-07-30 23:36:58 +01:00
{
uint32_t frame_length_us;
2025-07-30 13:44:01 +01:00
uint16_t ticks_per_bit;
uint16_t ticks_one_high;
uint16_t ticks_one_low;
2025-07-30 13:44:01 +01:00
uint16_t ticks_zero_high;
uint16_t ticks_zero_low;
} dshot_timing_t;
2025-08-30 23:21:03 +01:00
//
2022-11-25 15:08:58 +00:00
class DShotRMT
{
2023-03-24 12:44:49 +00:00
public:
// Primary constructor with GPIO enum
explicit DShotRMT(gpio_num_t gpio = GPIO_NUM_16,
dshot_mode_t mode = DSHOT300,
bool is_bidirectional = false);
2025-08-30 23:21:03 +01:00
// Constructor with pin number
2025-08-06 22:57:26 +01:00
DShotRMT(uint16_t pin_nr, dshot_mode_t mode, bool is_bidirectional);
2025-07-30 23:36:58 +01:00
// Destructor for "better" code
~DShotRMT();
// Initialize the RMT module and DShot config
2025-08-28 13:37:02 +01:00
uint16_t begin();
// Send throttle value (48-2047)
2025-08-06 22:57:26 +01:00
bool sendThrottle(uint16_t throttle);
2025-06-13 20:50:00 +01:00
// Send DShot command (0-47)
2025-08-06 22:57:26 +01:00
bool sendCommand(uint16_t command);
2021-06-29 19:05:20 +01:00
// Get telemetry data (bidirectional mode only)
2025-08-05 23:32:03 +01:00
uint16_t getERPM();
2025-07-30 23:36:58 +01:00
2025-08-30 23:21:03 +01:00
// Convert eRPM to motor RPM
uint32_t getMotorRPM(uint8_t magnet_count);
2025-08-05 23:32:03 +01:00
2025-08-30 23:21:03 +01:00
//
gpio_num_t getGPIO() const { return _gpio; }
uint16_t getDShotPacket() const { return _parsed_packet; }
bool is_bidirectional() const { return _is_bidirectional; }
// --- INFO ---
void printDshotInfo(Stream &output = Serial0) const;
void printCpuInfo(Stream &output = Serial0) const;
2025-08-30 23:21:03 +01:00
// --- DEPRECATED METHODS ---
[[deprecated("Use sendThrottle() instead")]]
2025-08-31 19:23:04 +01:00
bool setThrottle(uint16_t throttle) { return sendThrottle(throttle); }
2025-08-30 23:21:03 +01:00
[[deprecated("Use sendCommand() instead")]]
2025-08-31 19:23:04 +01:00
bool sendDShotCommand(uint16_t command) { return sendCommand(command); }
2025-08-30 23:21:03 +01:00
2025-07-25 21:00:26 +01:00
private:
2025-08-30 23:21:03 +01:00
// --- CONFIG ---
2025-07-30 23:36:58 +01:00
gpio_num_t _gpio;
dshot_mode_t _mode;
bool _is_bidirectional;
uint32_t _frame_timer_us;
2025-07-30 23:36:58 +01:00
const dshot_timing_t &_timing_config;
2025-08-30 23:21:03 +01:00
// --- TIMING & STATE VARIABLES ---
uint32_t _last_transmission_time;
uint16_t _last_erpm;
uint16_t _parsed_packet;
dshot_packet_t _packet;
// --- RMT HARDWARE HANDLES ---
2025-07-30 23:36:58 +01:00
rmt_channel_handle_t _rmt_tx_channel;
rmt_channel_handle_t _rmt_rx_channel;
rmt_encoder_handle_t _dshot_encoder;
2025-08-30 23:21:03 +01:00
// --- RMT CONFIG STRUCTURES ---
2025-07-30 23:36:58 +01:00
rmt_tx_channel_config_t _tx_channel_config;
rmt_rx_channel_config_t _rx_channel_config;
rmt_transmit_config_t _transmit_config;
rmt_receive_config_t _receive_config;
2025-08-30 23:21:03 +01:00
// --- RMT DATA BUFFERS ---
rmt_symbol_word_t _tx_symbols[RMT_BUFFER_SYMBOLS];
rmt_symbol_word_t _rx_symbols[RMT_BUFFER_SYMBOLS];
2025-07-30 23:36:58 +01:00
2025-08-30 23:21:03 +01:00
// --- INITS ---
2025-07-30 23:36:58 +01:00
bool _initTXChannel();
bool _initRXChannel();
bool _initDShotEncoder();
2025-08-30 23:21:03 +01:00
// --- PACKET MANAGEMENT ---
2025-08-05 00:43:19 +01:00
dshot_packet_t _buildDShotPacket(const uint16_t value);
uint16_t _parseDShotPacket(const dshot_packet_t &packet);
uint16_t _calculateCRC(const uint16_t data);
2025-08-30 23:21:03 +01:00
// --- FRAME PROCESSING ---
uint16_t _sendDShotFrame(const dshot_packet_t &packet);
2025-08-18 21:11:32 +01:00
bool IRAM_ATTR _encodeDShotFrame(const dshot_packet_t &packet, rmt_symbol_word_t *symbols);
2025-08-05 23:32:03 +01:00
uint16_t _decodeDShotFrame(const rmt_symbol_word_t *symbols);
2025-07-30 23:36:58 +01:00
2025-08-30 23:21:03 +01:00
// --- TIMING CONTROL ---
2025-08-18 21:11:32 +01:00
bool IRAM_ATTR _timer_signal();
bool _timer_reset();
2025-08-05 21:01:00 +01:00
2025-08-31 10:43:48 +01:00
// -- CALLBACKS ---
QueueHandle_t _rx_queue;
rmt_rx_event_callbacks_t _rx_event_cbs;
static bool IRAM_ATTR _rmt_rx_done_callback(rmt_channel_handle_t rx_chan, const rmt_rx_done_event_data_t *edata, void *user_data);
2025-08-30 23:21:03 +01:00
// --- ERROR HANDLING & LOGGING ---
2025-09-01 15:08:05 +01:00
void _dshot_log(std::string_view msg) { std::cerr << msg << '\n'; }
2025-08-30 23:21:03 +01:00
// --- CONSTANTS & ERROR MESSAGES ---
static constexpr uint16_t DSHOT_OK = 0;
static constexpr uint16_t DSHOT_ERROR = 1;
2025-09-01 15:08:05 +01:00
static constexpr std::string_view NEW_LINE = " ";
static constexpr std::string_view TX_INIT_FAILED = "Failed to initialize TX channel!";
static constexpr std::string_view RX_INIT_FAILED = "Failed to initialize RX channel!";
static constexpr std::string_view ENCODER_INIT_FAILED = "Failed to initialize DShot encoder!";
static constexpr std::string_view CRC_CHECK_FAILED = "RX CRC Check failed!";
static constexpr std::string_view THROTTLE_NOT_IN_RANGE = "Throttle value not in range (48 - 2047)!";
static constexpr std::string_view COMMAND_NOT_VALID = "Not a valid DShot Command (0 - 47)!";
static constexpr std::string_view BIDIR_NOT_ENABLED = "Bidirectional DShot support not enabled!";
static constexpr std::string_view RX_RMT_RECEIVER_ERROR = "RX RMT receiver error!";
2025-09-01 20:26:22 +01:00
static constexpr std::string_view PACKET_BUILD_ERROR = "Value too big for DShot Packet!";
};