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_SPEED   300U
 Base freq of DSHOT signal (in kHz) Possible values are: 150, 300, 600. More...
 
#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_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   (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 * DSHOT_BIT0_DUTY_RATIO / 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 @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 100 of file esc_dshot.c.

◆ DCR_DBA

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

Definition at line 98 of file esc_dshot.c.

◆ DCR_DBL

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

Definition at line 97 of file esc_dshot.c.

◆ DSHOT_BIT0_DUTY

#define DSHOT_BIT0_DUTY   (DSHOT_PWM_PERIOD * DSHOT_BIT0_DUTY_RATIO / 1000)

Definition at line 95 of file esc_dshot.c.

◆ DSHOT_BIT0_DUTY_RATIO

#define DSHOT_BIT0_DUTY_RATIO   373U

Definition at line 88 of file esc_dshot.c.

◆ DSHOT_BIT1_DUTY

#define DSHOT_BIT1_DUTY   (DSHOT_BIT0_DUTY*2)

Definition at line 96 of file esc_dshot.c.

◆ DSHOT_FREQ

#define DSHOT_FREQ   (DSHOT_SPEED*1000)

Definition at line 92 of file esc_dshot.c.

◆ DSHOT_MAX_VALUE

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

Definition at line 103 of file esc_dshot.c.

◆ DSHOT_PWM_PERIOD

#define DSHOT_PWM_PERIOD   (TICK_FREQ/DSHOT_FREQ)

Definition at line 94 of file esc_dshot.c.

◆ DSHOT_SPEED

#define DSHOT_SPEED   300U

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

Definition at line 49 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 56 of file esc_dshot.c.

◆ DSHOT_TELEMETRY_TIMEOUT_MS

#define DSHOT_TELEMETRY_TIMEOUT_MS   3

Telemetry timeout in ms.

Definition at line 62 of file esc_dshot.c.

◆ Min

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

Definition at line 101 of file esc_dshot.c.

◆ PWM_FREQ

#define PWM_FREQ   (STM32_SYSCLK / 2000U)

Definition at line 73 of file esc_dshot.c.

◆ TICK_FREQ

#define TICK_FREQ   (PWM_FREQ * TICKS_PER_PERIOD)

Definition at line 93 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 83 of file esc_dshot.c.

Function Documentation

◆ buildDshotDmaBuffer()

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

Definition at line 405 of file esc_dshot.c.

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

Referenced by dshotSendFrame().

+ Here is the caller graph for this function:

◆ calculateCrc8()

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

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

◆ dshotGetCrcErrorsCount()

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 @api

Definition at line 349 of file esc_dshot.c.

References DSHOTDriver::crc_errors.

◆ dshotGetTelemetry()

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 @api

Definition at line 362 of file esc_dshot.c.

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

Referenced by actuators_dshot_arch_commit(), and esc_msg_send().

+ Here is the caller graph for this function:

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

References buildDshotDmaBuffer(), DSHOTDriver::config, DshotPackets::currentTlmQry, DSHOTConfig::dma_buf, DMA_READY, DSHOTDriver::dmap, dmaStartTransfert(), DshotPackets::dp, 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:

◆ 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 259 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:

◆ 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 306 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 230 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()

void dshotStart ( DSHOTDriver driver,
const DSHOTConfig config 
)

◆ dshotTlmRec()

static noreturn void dshotTlmRec ( void *  arg)
static

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

◆ getTimerWidth()

static size_t getTimerWidth ( const PWMDriver *  pwmp)
static

Definition at line 458 of file esc_dshot.c.

References STM32_PWM_USE_TIM2.

Referenced by dshotSendFrame(), 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 377 of file esc_dshot.c.

References logger_uart_parse::crc.

Referenced by dshotStart().

+ Here is the caller graph for this function:

◆ setDshotPacketThrottle()

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

Definition at line 394 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 400 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 435 of file esc_dshot.c.

References logger_uart_parse::crc.

Referenced by calculateCrc8().

+ Here is the caller graph for this function: