Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
esc_dshot.c File Reference

DSHOT driver based on ChibiOS. More...

#include "modules/actuators/esc_dshot.h"
#include <stdnoreturn.h>
#include <math.h>
#include <string.h>
#include "mcu_periph/sys_time_arch.h"
+ Include dependency graph for esc_dshot.c:

Go to the source code of this file.

Macros

#define DSHOT_TELEMETRY_BAUD   115200U
 Baudrate of the serial link used for telemetry data Can depend on the ESC, but only 115k have been used so far. More...
 
#define DSHOT_BIDIR_EXTENTED_TELEMETRY   FALSE
 
#define DSHOT_TELEMETRY_TIMEOUT_MS   3
 Telemetry timeout in ms. More...
 
#define PWM_FREQ   (STM32_SYSCLK / 2000U)
 
#define TICKS_PER_PERIOD   1000
 Ticks per period that let use any timer: does not care if linked to PCLK1 or PCLK2 tick_per_period will be dynamically calculated after pwm init. More...
 
#define DSHOT_BIT0_DUTY_RATIO   373U
 
#define DSHOT_FREQ   (driver->config->speed_khz * 1000U)
 
#define DSHOT_BIT0_DUTY   (driver->bit0Duty)
 
#define DSHOT_BIT1_DUTY   (driver->bit1Duty)
 
#define TICK_FREQ   (PWM_FREQ * TICKS_PER_PERIOD)
 
#define DSHOT_PWM_PERIOD   (TICK_FREQ/DSHOT_FREQ)
 
#define DCR_DBL   ((DSHOT_CHANNELS-1) << 8)
 
#define DCR_DBA(pwmd)   (((uint32_t *) (&pwmd->tim->CCR) - ((uint32_t *) pwmd->tim)))
 
#define DSHOT_MAX_VALUE   ((1U<<11U)-1U)
 
#define ARRAY_LEN(a)   (sizeof(a)/sizeof(a[0]))
 
#define Min(x, y)   (x < y ? x : y)
 

Functions

static DshotPacket makeDshotPacket (const uint16_t throttle, const bool tlmRequest)
 
static void setCrc4 (DshotPacket *dp)
 
static void setDshotPacketThrottle (DshotPacket *const dp, const uint16_t throttle)
 
static void setDshotPacketTlm (DshotPacket *const dp, const bool tlmRequest)
 
static void buildDshotDmaBuffer (DSHOTDriver *driver)
 
static uint8_t updateCrc8 (uint8_t crc, uint8_t crc_seed)
 
static uint8_t calculateCrc8 (const uint8_t *Buf, const uint8_t BufLen)
 
static noreturn void dshotTlmRec (void *arg)
 
static size_t getTimerWidth (const PWMDriver *pwmp)
 
void dshotStart (DSHOTDriver *driver, const DSHOTConfig *config)
 Configures and activates the DSHOT peripheral. More...
 
void dshotStop (DSHOTDriver *driver)
 stop the DSHOT driver and free the related resources : pwm driver and dma driver. More...
 
void dshotSetThrottle (DSHOTDriver *driver, const uint8_t index, const uint16_t throttle)
 prepare throttle order for specified ESC More...
 
void dshotSendSpecialCommand (DSHOTDriver *driver, const uint8_t index, const dshot_special_commands_t specmd)
 send special order to one of the ESC (BHELIX, KISS, ...) More...
 
void dshotSendThrottles (DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS])
 send throttle packed order to all of the ESCs More...
 
void dshotSendFrame (DSHOTDriver *driver)
 send throttle order More...
 
uint32_t dshotGetCrcErrorCount (const DSHOTDriver *driver)
 return number of telemetry crc error since dshotStart More...
 
uint32_t dshotGetTelemetryFrameCount (const DSHOTDriver *driver)
 return number of telemetry succesfull frame since dshotStart More...
 
DshotTelemetry dshotGetTelemetry (DSHOTDriver *driver, uint32_t index)
 return last received telemetry data More...
 

Detailed Description

DSHOT driver based on ChibiOS.

Author
Alexandre Bustico @maintainer Gautier Hattenberger gauti.nosp@m.er.h.nosp@m.atten.nosp@m.berg.nosp@m.er@en.nosp@m.ac.f.nosp@m.r

Definition in file esc_dshot.c.

Macro Definition Documentation

◆ ARRAY_LEN

#define ARRAY_LEN (   a)    (sizeof(a)/sizeof(a[0]))

Definition at line 108 of file esc_dshot.c.

◆ DCR_DBA

#define DCR_DBA (   pwmd)    (((uint32_t *) (&pwmd->tim->CCR) - ((uint32_t *) pwmd->tim)))

Definition at line 104 of file esc_dshot.c.

◆ DCR_DBL

#define DCR_DBL   ((DSHOT_CHANNELS-1) << 8)

Definition at line 102 of file esc_dshot.c.

◆ DSHOT_BIDIR_EXTENTED_TELEMETRY

#define DSHOT_BIDIR_EXTENTED_TELEMETRY   FALSE

Definition at line 54 of file esc_dshot.c.

◆ DSHOT_BIT0_DUTY

#define DSHOT_BIT0_DUTY   (driver->bit0Duty)

Definition at line 95 of file esc_dshot.c.

◆ DSHOT_BIT0_DUTY_RATIO

#define DSHOT_BIT0_DUTY_RATIO   373U

Definition at line 86 of file esc_dshot.c.

◆ DSHOT_BIT1_DUTY

#define DSHOT_BIT1_DUTY   (driver->bit1Duty)

Definition at line 96 of file esc_dshot.c.

◆ DSHOT_FREQ

#define DSHOT_FREQ   (driver->config->speed_khz * 1000U)

Definition at line 94 of file esc_dshot.c.

◆ DSHOT_MAX_VALUE

#define DSHOT_MAX_VALUE   ((1U<<11U)-1U)

Definition at line 106 of file esc_dshot.c.

◆ DSHOT_PWM_PERIOD

#define DSHOT_PWM_PERIOD   (TICK_FREQ/DSHOT_FREQ)

Definition at line 99 of file esc_dshot.c.

◆ DSHOT_TELEMETRY_BAUD

#define DSHOT_TELEMETRY_BAUD   115200U

Baudrate of the serial link used for telemetry data Can depend on the ESC, but only 115k have been used so far.

Definition at line 50 of file esc_dshot.c.

◆ DSHOT_TELEMETRY_TIMEOUT_MS

#define DSHOT_TELEMETRY_TIMEOUT_MS   3

Telemetry timeout in ms.

Definition at line 60 of file esc_dshot.c.

◆ Min

#define Min (   x,
 
)    (x < y ? x : y)

Definition at line 109 of file esc_dshot.c.

◆ PWM_FREQ

#define PWM_FREQ   (STM32_SYSCLK / 2000U)

Definition at line 71 of file esc_dshot.c.

◆ TICK_FREQ

#define TICK_FREQ   (PWM_FREQ * TICKS_PER_PERIOD)

Definition at line 98 of file esc_dshot.c.

◆ TICKS_PER_PERIOD

#define TICKS_PER_PERIOD   1000

Ticks per period that let use any timer: does not care if linked to PCLK1 or PCLK2 tick_per_period will be dynamically calculated after pwm init.

Definition at line 81 of file esc_dshot.c.

Function Documentation

◆ buildDshotDmaBuffer()

static void buildDshotDmaBuffer ( DSHOTDriver driver)
static

◆ calculateCrc8()

static uint8_t calculateCrc8 ( const uint8_t Buf,
const uint8_t  BufLen 
)
static

Definition at line 665 of file esc_dshot.c.

References logger_uart_parse::crc, and updateCrc8().

Referenced by dshotTlmRec().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dshotGetCrcErrorCount()

uint32_t dshotGetCrcErrorCount ( const DSHOTDriver driver)

return number of telemetry crc error since dshotStart

Parameters
[in]driverpointer to the DSHOTDriver object
Returns
number of CRC errors @api

Definition at line 442 of file esc_dshot.c.

References DSHOTDriver::crc_errors.

◆ dshotGetTelemetry()

DshotTelemetry dshotGetTelemetry ( DSHOTDriver driver,
uint32_t  index 
)

return last received telemetry data

Parameters
[in]driverpointer to the DSHOTDriver object
[in]indexchannel : [0..3] or [0..1] depending on driver used
Returns
telemetry structure by copy @api

Definition at line 468 of file esc_dshot.c.

References DSHOT_CHANNEL_FIRST_INDEX, DSHOT_CHANNELS, DSHOTDriver::dshotMotors, DshotPackets::dt, and DshotPackets::tlmMtx.

Referenced by actuators_dshot_arch_commit(), and esc_msg_send().

+ Here is the caller graph for this function:

◆ dshotGetTelemetryFrameCount()

uint32_t dshotGetTelemetryFrameCount ( const DSHOTDriver driver)

return number of telemetry succesfull frame since dshotStart

Parameters
[in]driverpointer to the DSHOTDriver object
Returns
number of frames @api

Definition at line 454 of file esc_dshot.c.

References DSHOTDriver::tlm_frame_nb.

◆ dshotSendFrame()

void dshotSendFrame ( DSHOTDriver driver)

send throttle order

Parameters
[in]driverpointer to the DSHOTDriver object
Note
dshotSetXXX api should be called prior to this function @api

Definition at line 404 of file esc_dshot.c.

References buildDshotDmaBuffer(), DSHOTDriver::config, DshotPackets::currentTlmQry, DSHOTConfig::dma_buf, DMA_READY, DSHOTDriver::dmap, dmaTransfert(), DshotPackets::dp, DSHOT_CHANNELS, DSHOT_DMA_BUFFER_SIZE, DSHOTDriver::dshotMotors, dshotRpmCatchErps(), dshotStop(), DSHOTDriver::mb, DshotPackets::onGoingQry, DSHOTConfig::pwmp, setDshotPacketTlm(), DMADriver::state, and DSHOTConfig::tlm_sd.

Referenced by actuators_dshot_arch_commit(), dshotSendSpecialCommand(), and dshotSendThrottles().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dshotSendSpecialCommand()

void dshotSendSpecialCommand ( DSHOTDriver driver,
const uint8_t  index,
const dshot_special_commands_t  specmd 
)

send special order to one of the ESC (BHELIX, KISS, ...)

Parameters
[in]driverpointer to the DSHOTDriver object
[in]indexchannel : [0..3] or [0..1] depending on driver used
[in]specmdspecial commands, see enum @api

Definition at line 330 of file esc_dshot.c.

References DSHOTDriver::config, DshotPackets::dp, DSHOT_ALL_MOTORS, DSHOT_CHANNELS, DSHOT_CMD_3D_MODE_OFF, DSHOT_CMD_3D_MODE_ON, DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF, DSHOT_CMD_BIDIR_EDT_MODE_OFF, DSHOT_CMD_BIDIR_EDT_MODE_ON, DSHOT_CMD_MAX, DSHOT_CMD_SAVE_SETTINGS, DSHOT_CMD_SETTINGS_REQUEST, DSHOT_CMD_SILENT_MODE_ON_OFF, DSHOT_CMD_SPIN_DIRECTION_1, DSHOT_CMD_SPIN_DIRECTION_2, DSHOTDriver::dshotMotors, dshotSendFrame(), setDshotPacketThrottle(), setDshotPacketTlm(), and DSHOTConfig::tlm_sd.

+ Here is the call graph for this function:

◆ dshotSendThrottles()

void dshotSendThrottles ( DSHOTDriver driver,
const uint16_t  throttles[DSHOT_CHANNELS] 
)

send throttle packed order to all of the ESCs

Parameters
[in]driverpointer to the DSHOTDriver object
[in]throttle[DSHOT_CHANNELS][48 .. 2047]
Note
dshotSendFrame is called by this function
telemetry bit is set in turn for each ESC of the ESCs @api

Definition at line 385 of file esc_dshot.c.

References DshotPackets::dp, DSHOT_CHANNELS, DSHOTDriver::dshotMotors, dshotSendFrame(), and setDshotPacketThrottle().

+ Here is the call graph for this function:

◆ dshotSetThrottle()

void dshotSetThrottle ( DSHOTDriver driver,
const uint8_t  index,
const uint16_t  throttle 
)

prepare throttle order for specified ESC

Parameters
[in]driverpointer to the DSHOTDriver object
[in]indexchannel : [0..3] or [0..1] depending on driver used
[in]throttle[48 .. 2047]
Note
dshotSendFrame has to be called after using this function
see also dshotSendThrottles @api

Definition at line 301 of file esc_dshot.c.

References DshotPackets::dp, DSHOT_ALL_MOTORS, DSHOT_CHANNEL_FIRST_INDEX, DSHOT_CHANNELS, DSHOT_CMD_MAX, DSHOT_MAX_VALUE, DSHOTDriver::dshotMotors, Min, and setDshotPacketThrottle().

Referenced by actuators_dshot_arch_commit().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dshotStart()

◆ dshotStop()

void dshotStop ( DSHOTDriver driver)

stop the DSHOT driver and free the related resources : pwm driver and dma driver.

Parameters
[in]driverpointer to the DSHOTDriver object @api

Definition at line 260 of file esc_dshot.c.

References DSHOTDriver::config, DSHOTDriver::dmap, dmaStop(), dmaStopTransfert(), and DSHOTConfig::pwmp.

Referenced by dshotSendFrame().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dshotTlmRec()

static noreturn void dshotTlmRec ( void *  arg)
static

◆ getTimerWidth()

static size_t getTimerWidth ( const PWMDriver *  pwmp)
static

Definition at line 687 of file esc_dshot.c.

References STM32_PWM_USE_TIM2.

Referenced by buildDshotDmaBuffer(), and dshotStart().

+ Here is the caller graph for this function:

◆ makeDshotPacket()

static DshotPacket makeDshotPacket ( const uint16_t  throttle,
const bool  tlmRequest 
)
static

Definition at line 601 of file esc_dshot.c.

References logger_uart_parse::crc, and setCrc4().

Referenced by dshotStart().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCrc4()

static void setCrc4 ( DshotPacket dp)
static

Definition at line 587 of file esc_dshot.c.

Referenced by buildDshotDmaBuffer(), and makeDshotPacket().

+ Here is the caller graph for this function:

◆ setDshotPacketThrottle()

static void setDshotPacketThrottle ( DshotPacket *const  dp,
const uint16_t  throttle 
)
inlinestatic

Definition at line 612 of file esc_dshot.c.

Referenced by dshotSendSpecialCommand(), dshotSendThrottles(), and dshotSetThrottle().

+ Here is the caller graph for this function:

◆ setDshotPacketTlm()

static void setDshotPacketTlm ( DshotPacket *const  dp,
const bool  tlmRequest 
)
inlinestatic

Definition at line 618 of file esc_dshot.c.

Referenced by dshotSendFrame(), and dshotSendSpecialCommand().

+ Here is the caller graph for this function:

◆ updateCrc8()

static uint8_t updateCrc8 ( uint8_t  crc,
uint8_t  crc_seed 
)
inlinestatic

Definition at line 653 of file esc_dshot.c.

References logger_uart_parse::crc.

Referenced by calculateCrc8().

+ Here is the caller graph for this function: