DShotRMT/DShotRMT.h

136 lines
3.5 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-08-02 16:20:32 +01:00
#include <hw_defaults.h>
2022-11-26 12:52:46 +00:00
2025-07-29 23:40:09 +01:00
static constexpr bool DSHOT_OK = 0;
2025-07-30 23:36:58 +01:00
static constexpr bool DSHOT_ERROR = 1;
2025-07-29 23:40:09 +01:00
2025-06-13 20:50:00 +01:00
// --- DShot Protocol Constants ---
2025-07-30 23:36:58 +01:00
static constexpr uint16_t DSHOT_THROTTLE_FAILSAFE = 0;
static constexpr uint16_t DSHOT_THROTTLE_MIN = 48;
static constexpr uint16_t DSHOT_THROTTLE_MAX = 2047;
2021-06-29 19:05:20 +01:00
2025-07-30 23:36:58 +01:00
static constexpr uint8_t DSHOT_BITS_PER_FRAME = 16;
static constexpr uint8_t DSHOT_SWITCH_TIME = 300; // 30us
2025-07-30 23:36:58 +01:00
static constexpr uint16_t DSHOT_NULL_PACKET = 0b0000000000000000;
// --- RMT Config Constants ---
static constexpr rmt_clock_source_t DSHOT_CLOCK_SRC_DEFAULT = RMT_CLK_SRC_DEFAULT;
static constexpr uint32_t DSHOT_RMT_RESOLUTION = 10 * 1000 * 1000; // 10 MHz
static constexpr size_t TX_BUFFER_SIZE = DSHOT_BITS_PER_FRAME;
static constexpr size_t RX_BUFFER_SIZE = 32;
static constexpr size_t DSHOT_SYMBOLS_SIZE = 64;
2025-07-30 23:36:58 +01:00
// --- DShot Mode Select ---
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;
2025-07-30 23:36:58 +01:00
// --- DShot Packet Structure ---
typedef struct
2025-07-30 13:44:01 +01:00
{
2025-07-30 23:36:58 +01:00
uint16_t throttle_value : 11;
bool telemetric_request : 1;
uint16_t checksum : 4;
} dshot_packet_t;
// --- DShot Timing Config ---
typedef struct
{
uint16_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_zero_high;
uint16_t ticks_zero_low;
uint16_t ticks_one_low;
} dshot_timing_t;
2025-07-30 23:36:58 +01:00
// --- DShot Timing Config ---
extern const dshot_timing_t DSHOT_TIMINGS[];
2025-07-30 13:44:01 +01:00
//
2022-11-25 15:08:58 +00:00
class DShotRMT
{
2023-03-24 12:44:49 +00:00
public:
2025-07-30 23:36:58 +01:00
//
DShotRMT(gpio_num_t gpio, dshot_mode_t mode = DSHOT300, bool is_bidirectional = false);
// --- Init RMT Module ---
bool begin();
2025-07-30 23:36:58 +01:00
// Sets the throttle value and transmits
bool setThrottle(uint16_t throttle);
2025-06-13 20:50:00 +01:00
// Sends a DShot Command
2025-07-30 23:36:58 +01:00
bool sendDShotCommand(uint16_t command);
2021-06-29 19:05:20 +01:00
2025-07-30 23:36:58 +01:00
// Gets eRPM from ESC telemetry
2025-06-13 20:50:00 +01:00
uint32_t getERPM();
2025-07-30 23:36:58 +01:00
// Converts eRPM to motor RPM
2025-06-14 17:16:45 +01:00
uint32_t getMotorRPM(uint8_t magnet_count);
2025-07-30 23:36:58 +01:00
//
gpio_num_t getGPIO() const { return _gpio; }
dshot_mode_t getDShotMode() const { return _mode; }
bool is_bidirectional() const { return _is_bidirectional; }
2021-06-29 19:05:20 +01:00
2025-07-25 21:00:26 +01:00
private:
2025-07-30 23:36:58 +01:00
// --- Config ---
gpio_num_t _gpio;
dshot_mode_t _mode;
bool _is_bidirectional;
uint32_t _frame_time_us;
// --- DShot Timings ---
const dshot_timing_t &_timing_config;
// --- RMT Handles ---
rmt_channel_handle_t _rmt_tx_channel;
rmt_channel_handle_t _rmt_rx_channel;
rmt_encoder_handle_t _dshot_encoder;
// --- RMT Config ---
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;
// --- Buffers ---
rmt_symbol_word_t _rx_symbols[RX_BUFFER_SIZE];
uint16_t _last_erpm;
unsigned long _last_transmission_time;
//
2025-07-30 23:36:58 +01:00
bool _initTXChannel();
bool _initRXChannel();
bool _initDShotEncoder();
uint16_t _sendDShotFrame(const dshot_packet_t &packet);
2025-07-30 23:36:58 +01:00
uint16_t _calculateCRC(const dshot_packet_t &packet);
uint16_t _assembleDShotFrame(const dshot_packet_t &packet);
bool _encodeDShotFrame(const dshot_packet_t &packet, rmt_symbol_word_t *symbols);
2025-07-30 23:36:58 +01:00
uint16_t _decodeDShotFrame(const rmt_symbol_word_t *symbols, size_t symbol_count);
bool _timer_signal();
bool _timer_reset();
};