31#include <stdnoreturn.h>
50#ifndef DSHOT_TELEMETRY_BAUD
51#define DSHOT_TELEMETRY_BAUD 115200U
54#ifndef DSHOT_BIDIR_EXTENTED_TELEMETRY
55#define DSHOT_BIDIR_EXTENTED_TELEMETRY FALSE
60#ifndef DSHOT_TELEMETRY_TIMEOUT_MS
61#define DSHOT_TELEMETRY_TIMEOUT_MS 3
67#define PWM_FREQ (STM32_TIMCLK1 / 1000U)
72#define PWM_FREQ (STM32_SYSCLK / 2000U)
82#define TICKS_PER_PERIOD 1000
86#ifndef DSHOT_BIT0_DUTY_RATIO
87#define DSHOT_BIT0_DUTY_RATIO 373U
91# define DSHOT_FREQ (DSHOT_SPEED*1000)
92# define DSHOT_BIT0_DUTY (DSHOT_PWM_PERIOD * DSHOT_BIT0_DUTY_RATIO / 1000U)
93# define DSHOT_BIT1_DUTY (DSHOT_BIT0_DUTY*2)
95# define DSHOT_FREQ (driver->config->speed_khz * 1000U)
96# define DSHOT_BIT0_DUTY (driver->bit0Duty)
97# define DSHOT_BIT1_DUTY (driver->bit1Duty)
99#define TICK_FREQ (PWM_FREQ * TICKS_PER_PERIOD)
100#define DSHOT_PWM_PERIOD (TICK_FREQ/DSHOT_FREQ)
103#define DCR_DBL ((DSHOT_CHANNELS-1) << 8)
105#define DCR_DBA(pwmd) (((uint32_t *) (&pwmd->tim->CCR) - ((uint32_t *) pwmd->tim)))
107#define DSHOT_MAX_VALUE ((1U<<11U)-1U)
109#define ARRAY_LEN(a) (sizeof(a)/sizeof(a[0]))
110#define Min(x,y) (x < y ? x : y)
134#if DSHOT_BIDIR_EXTENTED_TELEMETRY
157 chDbgAssert(config->
dma_buf != NULL,
".dma_buf must reference valid DshotDmaBuffer object");
166 static const SerialConfig tlmcfg = {
169 .cr2 = USART_CR2_STOP1_BITS,
173 static const uint32_t pwmPolarity =
DSHOT_BIDIR ? PWM_OUTPUT_ACTIVE_LOW : PWM_OUTPUT_ACTIVE_HIGH;
179#if STM32_DMA_SUPPORTS_DMAMUX
180 .dmamux = config->dmamux,
185 .irq_priority = CORTEX_MAX_KERNEL_PRIORITY + 1,
187 .psize = timerWidthInBytes,
188 .msize = timerWidthInBytes,
190 .dcache_memory_in_use = config->dcache_memory_in_use,
192 .inc_peripheral_addr =
false,
193 .inc_memory_addr =
true,
207 {.mode = pwmPolarity,
216 .cr2 = STM32_TIM_CR2_CCDS,
217 .dier = STM32_TIM_DIER_UDE
233 chDbgAssert(dmaOk ==
true,
"dshot dma start error");
237 chThdCreateStatic(driver->waDshotTlmRec,
sizeof(driver->waDshotTlmRec), NORMALPRIO,
279 chDbgAssert(dmaOk ==
true,
"dshot dma start error");
306 chDbgAssert(
false,
"dshotSetThrottle throttle error");
318 chDbgAssert(
false,
"dshotSetThrottle index error");
359 systime_t now = chVTGetSystemTimeX();
369 chDbgAssert(
false,
"dshotSetThrottle index error");
373 chThdSleepUntilWindowed(now, now + TIME_US2I(500));
409 const tprio_t currentPrio = chThdSetPriority(HIGHPRIO);
417 chMBPostTimeout(&driver->
mb, index, TIME_IMMEDIATE);
429 processBidirErpm(driver);
430 dshotRestart(driver);
431 chThdSetPriority(currentPrio);
490#if DSHOT_BIDIR && DSHOT_BIDIR_EXTENTED_TELEMETRY
511 tlm->
ts = chVTGetSystemTimeX();
531#if DSHOT_BIDIR_EXTENTED_TELEMETRY
535 updateTelemetryFromBidirEdt(&driver->erps, tlm);
561#if DSHOT_BIDIR_EXTENTED_TELEMETRY
565 updateTelemetryFromBidirEdt(&driver->erps, tlm);
592 uint16_t csum = (dp->throttle << 1) | dp->telemetryRequest;
593 for (
int i = 0; i < 3; i++) {
598 dp->crc = ~(dp->crc);
605 .telemetryRequest = (tlmRequest ? 1 : 0),
615 dp->throttle = throttle;
616 dp->telemetryRequest = 0;
621 dp->telemetryRequest = tlmRequest ? 1 : 0;
639 if (timerWidth == 2) {
642#if DSHOT_AT_LEAST_ONE_32B_TIMER
645 chSysHalt(
"use of 32 bit timer implies to define DSHOT_AT_LEAST_ONE_32B_TIMER to TRUE");
659 for (
int i = 0; i < 8; i++) {
660 crc_u = (crc_u & 0x80) ? 0x7 ^ (crc_u << 1) : (crc_u << 1);
669 for (
int i = 0; i < BufLen; i++) {
687__attribute__((
const))
720 chRegSetThreadName(
"dshotTlmRec");
722 chMBFetchTimeout(&driver->
mb, &escIdx, TIME_INFINITE);
730 while (sdGetTimeout(driver->
config->
tlm_sd, TIME_IMMEDIATE) >= 0) {};
737 tlm.
frame.voltage = __builtin_bswap16(tlm.
frame.voltage);
738 tlm.
frame.current = __builtin_bswap16(tlm.
frame.current);
739 tlm.
frame.consumption = __builtin_bswap16(tlm.
frame.consumption);
740 tlm.
frame.rpm = __builtin_bswap16(tlm.
frame.rpm);
741 tlm.
ts = chVTGetSystemTimeX();
uint32_t DshotErpsGetRpm(const DshotErps *derpsp)
calculate and return rpm
uint32_t DshotErpsGetEperiod(const DshotErps *derpsp)
return eperiod from mantisse and exponent
const DshotErps * DshotErpsSetFromFrame(DshotErps *derpsp, uint32_t frame)
initialise from GCR encoded frame
bool DshotErpsCheckCrc4(const DshotErps *derpsp)
check packed validity
static uint16_t DshotErpsEdtStatus(const DshotErps *derpsp)
return status value
static uint8_t DshotErpsEdtTempCentigrade(const DshotErps *derpsp)
return temperature for a temperature telemetry frame
static uint16_t DshotErpsEdtStress(const DshotErps *derpsp)
return stress value
static EdtType DshotErpsEdtType(const DshotErps *derpsp)
return type of a telemetry frame
static uint16_t DshotErpsEdtCentiVolts(const DshotErps *derpsp)
return voltage for a voltage telemetry frame
static uint16_t DshotErpsEdtCurrentAmp(const DshotErps *derpsp)
return current intensity for a current telemetry frame
static bool DshotErpsIsEdt(const DshotErps *derpsp)
return true if current frame is a telemetry frame
ERPS complete frame, raw and decoded.
void dshotRpmCaptureStart(DshotRpmCapture *drcp, const DshotRpmCaptureConfig *cfg, stm32_tim_t *timer)
Configures and activates the DSHOT ERPS CAPTURE driver.
void dshotRpmCatchErps(DshotRpmCapture *drcp)
capture the DSHOT ERPS frame(s) : one frame for each DSHOT_CHANNELS
static uint32_t dshotRpmGetFrame(const DshotRpmCapture *drcp, uint8_t index)
return last collected erps frame
static void buildDshotDmaBuffer(DSHOTDriver *driver)
void dshotStart(DSHOTDriver *driver, const DSHOTConfig *config)
Configures and activates the DSHOT peripheral.
void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle)
prepare throttle order for specified ESC
static size_t getTimerWidth(const PWMDriver *pwmp)
#define TICKS_PER_PERIOD
Ticks per period that let use any timer: does not care if linked to PCLK1 or PCLK2 tick_per_period wi...
uint32_t dshotGetTelemetryFrameCount(const DSHOTDriver *driver)
return number of telemetry succesfull frame since dshotStart
static uint8_t calculateCrc8(const uint8_t *Buf, const uint8_t BufLen)
DshotTelemetry dshotGetTelemetry(DSHOTDriver *driver, uint32_t index)
return last received telemetry data
static void setDshotPacketThrottle(DshotPacket *const dp, const uint16_t throttle)
void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS])
send throttle packed order to all of the ESCs
static void setCrc4(DshotPacket *dp)
uint32_t dshotGetCrcErrorCount(const DSHOTDriver *driver)
return number of telemetry crc error since dshotStart
void dshotStop(DSHOTDriver *driver)
stop the DSHOT driver and free the related resources : pwm driver and dma driver.
#define DSHOT_TELEMETRY_TIMEOUT_MS
Telemetry timeout in ms.
#define DSHOT_BIT0_DUTY_RATIO
void dshotSendFrame(DSHOTDriver *driver)
send throttle order
static noreturn void dshotTlmRec(void *arg)
static void setDshotPacketTlm(DshotPacket *const dp, const bool tlmRequest)
#define DSHOT_TELEMETRY_BAUD
Baudrate of the serial link used for telemetry data Can depend on the ESC, but only 115k have been us...
static DshotPacket makeDshotPacket(const uint16_t throttle, const bool tlmRequest)
void dshotSendSpecialCommand(DSHOTDriver *driver, const uint8_t index, const dshot_special_commands_t specmd)
send special order to one of the ESC (BHELIX, KISS, ...)
static uint8_t updateCrc8(uint8_t crc, uint8_t crc_seed)
DSHOT driver based on ChibiOS.
mutex_t tlmMtx[DSHOT_CHANNELS]
#define DSHOT_PRE_FRAME_SILENT_SYNC_BITS
SerialDriver * tlm_sd
if non null : dshot telemetry serial driver
#define DSHOT_BIT_WIDTHS
DMA buffer size and number of channels.
PWMDriver * pwmp
PWM driver that feed up to 4 dshot lines.
#define DSHOT_DMA_BUFFER_SIZE
uint8_t dma_channel
: dma channel associated with pwm timer used to generate dshot output
uint16_t widths16[DSHOT_DMA_BUFFER_SIZE][DSHOT_CHANNELS]
#define DSHOT_CHANNEL_FIRST_INDEX
DshotTelemetry dt[DSHOT_CHANNELS]
DshotTelemetryFrame frame
uint32_t dma_stream
: dma stream associated with pwm timer used to generate dshot output
#define DSHOT_ALL_MOTORS
special value for index : send order to all channels
#define DSHOT_BIDIR_TLM_EDT
DshotDmaBuffer * dma_buf
dshot dma buffer, should be defined in a non Dcached region
dshot_special_commands_t
Driver state machine possible states.
@ DSHOT_CMD_SILENT_MODE_ON_OFF
@ DSHOT_CMD_SAVE_SETTINGS
@ DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF
@ DSHOT_CMD_SETTINGS_REQUEST
@ DSHOT_CMD_BIDIR_EDT_MODE_OFF
@ DSHOT_CMD_SPIN_DIRECTION_1
@ DSHOT_CMD_SPIN_DIRECTION_2
@ DSHOT_CMD_BIDIR_EDT_MODE_ON
#define DSHOT_BIDIR_ERR_CRC
special values returned by dshotGetRpm function
DshotPacket dp[DSHOT_CHANNELS]
DSHOT Driver configuration structure.
telemetry packed as sent by some KISS ESC
void dmaObjectInit(DMADriver *dmap)
void dmaStop(DMADriver *dmap)
Deactivates the DMA peripheral.
void dmaStopTransfert(DMADriver *dmap)
Stops an ongoing transaction.
bool dmaStart(DMADriver *dmap, const DMAConfig *cfg)
Configures and activates the DMA peripheral.
@ DMA_ONESHOT
One transert then stop.
@ DMA_DIR_M2P
MEMORY to PERIPHERAL.
static msg_t dmaTransfert(DMADriver *dmap, volatile void *periphp, void *mem0p, const size_t size)
volatile dmastate_t state
Driver state.
DMA stream configuration structure.
#define STM32_PWM_USE_TIM2
#define STM32_PWM_USE_TIM5
uint32_t crc_errors
number of crc errors
DshotPackets dshotMotors
object managing dma control frame for outgoing command
mailbox_t mb
mailbox for dshot telemetry thread
DMADriver dmap
DMA driver associated with pwm timer.
PWMConfig pwm_conf
PWM config associated with pwm timer.
const DSHOTConfig * config
DMA config associated with pwm timer.
msg_t _mbBuf[1]
mailbox buffer for dshot telemetry thread
uint32_t tlm_frame_nb
number of sucessful telemetry frame received
DMAConfig dma_conf
DMA config associated with pwm timer.
Architecture independent timing functions.
Implementation of system time functions for ChibiOS arch.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.