2021-07-04 02:01:26 +01:00
|
|
|
//
|
2023-03-10 16:45:18 +00:00
|
|
|
// Name: DShotRMT.h
|
2021-07-04 02:01:26 +01:00
|
|
|
// Created: 20.03.2021 00:49:15
|
|
|
|
|
// Author: derdoktor667
|
|
|
|
|
//
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-26 15:19:54 +01:00
|
|
|
#pragma once
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-10 16:45:18 +00:00
|
|
|
#include <Arduino.h>
|
2022-11-26 12:52:46 +00:00
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Use the RMT Module library for generating the DShot signal
|
2021-06-29 19:05:20 +01:00
|
|
|
#include <driver/rmt.h>
|
|
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define library version
|
2023-03-26 15:19:54 +01:00
|
|
|
constexpr auto DSHOT_LIB_VERSION = "0.2.3";
|
2023-03-10 16:45:18 +00:00
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define constants for generating the DShot signal
|
|
|
|
|
constexpr auto DSHOT_CLK_DIVIDER = 8; // Slow down RMT clock to 0.1 microseconds / 100 nanoseconds per cycle
|
|
|
|
|
constexpr auto DSHOT_PACKET_LENGTH = 17; // Last pack is the pause
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define constants for DShot throttle range and null packet
|
2021-06-29 19:05:20 +01:00
|
|
|
constexpr auto DSHOT_THROTTLE_MIN = 48;
|
|
|
|
|
constexpr auto DSHOT_THROTTLE_MAX = 2047;
|
|
|
|
|
constexpr auto DSHOT_NULL_PACKET = 0b0000000000000000;
|
|
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define constants for DShot pause
|
|
|
|
|
constexpr auto DSHOT_PAUSE = 21; // 21bit is recommended, but to be sure
|
2021-06-29 19:05:20 +01:00
|
|
|
constexpr auto DSHOT_PAUSE_BIT = 16;
|
|
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define constants for RMT timing
|
2021-07-17 05:40:21 +01:00
|
|
|
constexpr auto F_CPU_RMT = APB_CLK_FREQ;
|
2021-06-29 19:05:20 +01:00
|
|
|
constexpr auto RMT_CYCLES_PER_SEC = (F_CPU_RMT / DSHOT_CLK_DIVIDER);
|
|
|
|
|
constexpr auto RMT_CYCLES_PER_ESP_CYCLE = (F_CPU / RMT_CYCLES_PER_SEC);
|
|
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define enumeration for DShot mode
|
2022-11-25 15:08:58 +00:00
|
|
|
typedef enum dshot_mode_e
|
|
|
|
|
{
|
2023-03-24 12:44:49 +00:00
|
|
|
DSHOT_OFF,
|
|
|
|
|
DSHOT150,
|
|
|
|
|
DSHOT300,
|
|
|
|
|
DSHOT600,
|
|
|
|
|
DSHOT1200
|
2021-06-29 19:05:20 +01:00
|
|
|
} dshot_mode_t;
|
|
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define human-readable names for DShot modes
|
2023-03-24 12:44:49 +00:00
|
|
|
static const char *const dshot_mode_name[] = {
|
|
|
|
|
"DSHOT_OFF",
|
|
|
|
|
"DSHOT150",
|
|
|
|
|
"DSHOT300",
|
|
|
|
|
"DSHOT600",
|
2023-03-26 16:58:24 +01:00
|
|
|
"DSHOT1200"
|
|
|
|
|
};
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define type aliases for DShot name and telemetry request
|
2021-07-04 02:01:26 +01:00
|
|
|
typedef String dshot_name_t;
|
2022-11-25 15:08:58 +00:00
|
|
|
typedef enum telemetric_request_e
|
|
|
|
|
{
|
2023-03-24 12:44:49 +00:00
|
|
|
NO_TELEMETRIC,
|
|
|
|
|
ENABLE_TELEMETRIC,
|
2021-06-29 19:05:20 +01:00
|
|
|
} telemetric_request_t;
|
|
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define structure for DShot packet
|
2022-11-25 15:08:58 +00:00
|
|
|
typedef struct dshot_packet_s
|
|
|
|
|
{
|
2023-03-24 12:44:49 +00:00
|
|
|
uint16_t throttle_value : 11;
|
|
|
|
|
telemetric_request_t telemetric_request : 1;
|
|
|
|
|
uint16_t checksum : 4;
|
2021-06-29 19:05:20 +01:00
|
|
|
} dshot_packet_t;
|
|
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define structure for eRPM packet
|
2022-11-25 15:08:58 +00:00
|
|
|
typedef struct eRPM_packet_s
|
|
|
|
|
{
|
2023-03-24 12:44:49 +00:00
|
|
|
uint16_t eRPM_data : 12;
|
|
|
|
|
uint8_t checksum : 4;
|
2021-08-03 22:22:06 +01:00
|
|
|
} eRPM_packet_t;
|
|
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define structure for DShot configuration
|
2022-11-25 15:08:58 +00:00
|
|
|
typedef struct dshot_config_s
|
|
|
|
|
{
|
2023-03-24 12:44:49 +00:00
|
|
|
dshot_mode_t mode;
|
|
|
|
|
dshot_name_t name_str;
|
|
|
|
|
bool bidirectional;
|
|
|
|
|
gpio_num_t gpio_num;
|
|
|
|
|
uint8_t pin_num;
|
|
|
|
|
rmt_channel_t rmt_channel;
|
|
|
|
|
uint8_t mem_block_num;
|
|
|
|
|
uint16_t ticks_per_bit;
|
|
|
|
|
uint8_t clk_div;
|
|
|
|
|
uint16_t ticks_zero_high;
|
|
|
|
|
uint16_t ticks_zero_low;
|
|
|
|
|
uint16_t ticks_one_high;
|
|
|
|
|
uint16_t ticks_one_low;
|
2021-06-29 19:05:20 +01:00
|
|
|
} dshot_config_t;
|
|
|
|
|
|
2023-03-26 16:58:24 +01:00
|
|
|
// Define class for DShot generation using RMT
|
2022-11-25 15:08:58 +00:00
|
|
|
class DShotRMT
|
|
|
|
|
{
|
2023-03-24 12:44:49 +00:00
|
|
|
public:
|
2023-03-26 16:58:24 +01:00
|
|
|
DShotRMT(gpio_num_t gpio, rmt_channel_t rmtChannel, dshot_mode_t dshot_mode = DSHOT300, bool is_bidirectional = false);
|
|
|
|
|
DShotRMT(uint8_t pin, uint8_t channel, dshot_mode_t dshot_mode = DSHOT300, bool is_bidirectional = false);
|
2023-03-24 12:44:49 +00:00
|
|
|
~DShotRMT();
|
|
|
|
|
DShotRMT(DShotRMT const &);
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-24 12:44:49 +00:00
|
|
|
// ...safety first ...no parameters, no DShot
|
|
|
|
|
void send_dshot_value(uint16_t throttle_value, telemetric_request_t telemetric_request = NO_TELEMETRIC);
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-24 12:44:49 +00:00
|
|
|
dshot_config_t *get_dshot_info();
|
|
|
|
|
uint8_t *get_dshot_clock_div();
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-24 12:44:49 +00:00
|
|
|
private:
|
|
|
|
|
rmt_item32_t dshot_tx_rmt_item[DSHOT_PACKET_LENGTH];
|
|
|
|
|
rmt_config_t dshot_tx_rmt_config;
|
|
|
|
|
dshot_config_t dshot_config;
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-24 12:44:49 +00:00
|
|
|
rmt_item32_t *encode_dshot_to_rmt(uint16_t parsed_packet);
|
|
|
|
|
uint16_t calc_dshot_chksum(const dshot_packet_t &dshot_packet);
|
|
|
|
|
uint16_t prepare_rmt_data(const dshot_packet_t &dshot_packet);
|
2021-06-29 19:05:20 +01:00
|
|
|
|
2023-03-26 15:19:54 +01:00
|
|
|
void install_dshot_driver(dshot_mode_t dshot_mode, bool is_bidirectional);
|
2023-03-24 12:44:49 +00:00
|
|
|
void output_rmt_data(const dshot_packet_t &dshot_packet);
|
2021-06-29 19:05:20 +01:00
|
|
|
};
|