Paparazzi UAS  v5.15_devel-88-gb3ad7fe
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 dependency graph for esc_dshot.c:

Go to the source code of this file.

Macros

#define DSHOT_SPEED   600
 Base freq of DSHOT signal (in kHz) Possible values are: 150, 300, 600, 1200. More...
 
#define DSHOT_TELEMETRY_BAUD   115200
 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_TELEMETRY_TIMEOUT_MS   3
 Telemetry timeout in ms. More...
 
#define PWM_FREQ   (STM32_SYSCLK/2000)
 the timer will beat @84Mhz on STM32F4 // TODO check on F7 More...
 
#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_FREQ   (DSHOT_SPEED*1000)
 
#define TICK_FREQ   (PWM_FREQ * TICKS_PER_PERIOD)
 
#define DSHOT_PWM_PERIOD   (TICK_FREQ/DSHOT_FREQ)
 
#define DSHOT_BIT0_DUTY   (DSHOT_PWM_PERIOD * 373 / 1000)
 
#define DSHOT_BIT1_DUTY   (DSHOT_BIT0_DUTY*2)
 
#define DCR_DBL   ((DSHOT_CHANNELS-1) << 8)
 
#define DCR_DBA(pwmd)   (((uint32_t *) (&pwmd->tim->CCR) - ((uint32_t *) pwmd->tim)))
 
#define ARRAY_LEN(a)   (sizeof(a)/sizeof(a[0]))
 
#define Min(x, y)   (x < y ? x : y)
 
#define DSHOT_MAX_VALUE   ((1<<11)-1)
 

Functions

static DshotPacket makeDshotPacket (const uint16_t throttle, const bool tlmRequest)
 
static void setDshotPacketThrottle (DshotPacket *const dp, const uint16_t throttle)
 
static void setDshotPacketTlm (DshotPacket *const dp, const bool tlmRequest)
 
static void buildDshotDmaBuffer (DshotPackets *const dsp, DshotDmaBuffer *const dma, const size_t timerWidth)
 
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 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 dshotGetCrcErrorsCount (DSHOTDriver *driver)
 return number of telemetry crc error since dshotStart More...
 
const DshotTelemetrydshotGetTelemetry (const DSHOTDriver *driver, const uint32_t index)
 return last received telemetry data More...
 

Detailed Description

DSHOT driver based on ChibiOS.

Author
Alexandre Bustico 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

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

Definition at line 84 of file esc_dshot.c.

Referenced by dshotStart().

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

Definition at line 82 of file esc_dshot.c.

Referenced by dshotStart().

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

Definition at line 81 of file esc_dshot.c.

Referenced by dshotStart().

#define DSHOT_BIT0_DUTY   (DSHOT_PWM_PERIOD * 373 / 1000)

Definition at line 79 of file esc_dshot.c.

Referenced by buildDshotDmaBuffer().

#define DSHOT_BIT1_DUTY   (DSHOT_BIT0_DUTY*2)

Definition at line 80 of file esc_dshot.c.

Referenced by buildDshotDmaBuffer().

#define DSHOT_FREQ   (DSHOT_SPEED*1000)

Definition at line 76 of file esc_dshot.c.

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

Definition at line 87 of file esc_dshot.c.

Referenced by dshotSetThrottle().

#define DSHOT_PWM_PERIOD   (TICK_FREQ/DSHOT_FREQ)

Definition at line 78 of file esc_dshot.c.

Referenced by dshotStart().

#define DSHOT_SPEED   600

Base freq of DSHOT signal (in kHz) Possible values are: 150, 300, 600, 1200.

Definition at line 48 of file esc_dshot.c.

#define DSHOT_TELEMETRY_BAUD   115200

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 55 of file esc_dshot.c.

Referenced by dshotStart().

#define DSHOT_TELEMETRY_TIMEOUT_MS   3

Telemetry timeout in ms.

Definition at line 61 of file esc_dshot.c.

Referenced by dshotTlmRec().

#define PWM_FREQ   (STM32_SYSCLK/2000)

the timer will beat @84Mhz on STM32F4 // TODO check on F7

Definition at line 65 of file esc_dshot.c.

#define TICK_FREQ   (PWM_FREQ * TICKS_PER_PERIOD)

Definition at line 77 of file esc_dshot.c.

Referenced by dshotStart().

#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 73 of file esc_dshot.c.

Referenced by dshotStart().

Function Documentation

static void buildDshotDmaBuffer ( DshotPackets *const  dsp,
DshotDmaBuffer *const  dma,
const size_t  timerWidth 
)
static

Definition at line 386 of file esc_dshot.c.

References DshotPackets::dp, DSHOT_BIT0_DUTY, DSHOT_BIT1_DUTY, DSHOT_BIT_WIDTHS, DSHOT_CHANNELS, DshotPacket::rawFrame, and DshotDmaBuffer::widths16.

Referenced by dshotSendFrame().

+ Here is the caller graph for this function:

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

Definition at line 428 of file esc_dshot.c.

References crc, and updateCrc8().

Referenced by dshotTlmRec().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

uint32_t dshotGetCrcErrorsCount ( DSHOTDriver driver)

return number of telemetry crc error since dshotStart

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

Definition at line 330 of file esc_dshot.c.

References DSHOTDriver::crc_errors.

const DshotTelemetry* dshotGetTelemetry ( const DSHOTDriver driver,
const 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
pointer on a telemetry structure

Definition at line 343 of file esc_dshot.c.

References DSHOTDriver::dshotMotors, and DshotPackets::dt.

void dshotSendFrame ( DSHOTDriver driver)

send throttle order

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

Definition at line 303 of file esc_dshot.c.

References buildDshotDmaBuffer(), DSHOTDriver::config, DshotPackets::currentTlmQry, DMA_READY, DSHOTDriver::dmap, dmaStartTransfert(), DshotPackets::dp, DSHOTDriver::dsdb, DSHOT_CHANNELS, DSHOT_DMA_BUFFER_SIZE, DSHOTDriver::dshotMotors, getTimerWidth(), 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:

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

Definition at line 240 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_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:

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

Definition at line 287 of file esc_dshot.c.

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

+ Here is the call graph for this function:

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

Definition at line 215 of file esc_dshot.c.

References DshotPackets::dp, DSHOT_ALL_MOTORS, 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:

static noreturn void dshotTlmRec ( void *  arg)
static

Definition at line 463 of file esc_dshot.c.

References calculateCrc8(), DSHOTDriver::config, DshotTelemetry::crc8, DSHOTDriver::crc_errors, DSHOT_TELEMETRY_TIMEOUT_MS, DSHOTDriver::dshotMotors, DshotPackets::dt, idx, DSHOTDriver::mb, DshotPackets::onGoingQry, and DSHOTConfig::tlm_sd.

Referenced by dshotStart().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static size_t getTimerWidth ( const PWMDriver *  pwmp)
static

Definition at line 439 of file esc_dshot.c.

References STM32_PWM_USE_TIM2.

Referenced by dshotSendFrame(), and dshotStart().

+ Here is the caller graph for this function:

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

Definition at line 358 of file esc_dshot.c.

References crc.

Referenced by dshotStart().

+ Here is the caller graph for this function:

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

Definition at line 375 of file esc_dshot.c.

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

+ Here is the caller graph for this function:

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

Definition at line 381 of file esc_dshot.c.

Referenced by dshotSendFrame(), and dshotSendSpecialCommand().

+ Here is the caller graph for this function:

static uint8_t updateCrc8 ( uint8_t  crc,
uint8_t  crc_seed 
)
inlinestatic

Definition at line 416 of file esc_dshot.c.

References crc.

Referenced by calculateCrc8().

+ Here is the caller graph for this function: