Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
esc_dshot.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Alexandre Bustico <alexandre.bustico@enac.fr>
3  * Gautier Hattenberger <gautier.hattenberger@enac.fr>
4  *
5  * This file is part of paparazzi
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
30 #pragma once
31 
32 #include <ch.h>
33 #include <hal.h>
35 
39 #ifndef DSHOT_AT_LEAST_ONE_32B_TIMER
40 #define DSHOT_AT_LEAST_ONE_32B_TIMER TRUE
41 #endif
42 
43 #ifndef DSHOT_CHANNEL_FIRST_INDEX
44 #define DSHOT_CHANNEL_FIRST_INDEX 0U
45 #endif
46 
49 #define DSHOT_BIT_WIDTHS 16U
50 #define DSHOT_PRE_FRAME_SILENT_SYNC_BITS 2U
51 #define DSHOT_POST_FRAME_SILENT_SYNC_BITS 2U
52 #define DSHOT_DMA_BUFFER_SIZE (DSHOT_BIT_WIDTHS + \
53  DSHOT_PRE_FRAME_SILENT_SYNC_BITS + \
54  DSHOT_POST_FRAME_SILENT_SYNC_BITS )
55 
56 #define DSHOT_CHANNELS 4U // depend on the number of channels per timer
57 
63 #define DSHOT_ALL_MOTORS 255U
64 
68 typedef enum {
74 } dshotstate_t;
75 
76 /*
77  DshotSettingRequest (KISS24). Spin direction,
78  3d and save Settings require 10 requests.. and the
79  TLM Byte must always be high if 1-47 are used to send settings
80 
81  3D Mode:
82  0 = stop
83  48 (low) - 1047 (high) -> negative direction
84  1048 (low) - 2047 (high) -> positive direction
85  */
86 
91 typedef enum {
98  DSHOT_CMD_ESC_INFO, // V2 includes settings
103  DSHOT_CMD_SETTINGS_REQUEST, // Currently not implemented
107  DSHOT_CMD_LED0_ON, // BLHeli32 only
108  DSHOT_CMD_LED1_ON, // BLHeli32 only
109  DSHOT_CMD_LED2_ON, // BLHeli32 only
110  DSHOT_CMD_LED3_ON, // BLHeli32 only
111  DSHOT_CMD_LED0_OFF, // BLHeli32 only
112  DSHOT_CMD_LED1_OFF, // BLHeli32 only
113  DSHOT_CMD_LED2_OFF, // BLHeli32 only
114  DSHOT_CMD_LED3_OFF, // BLHeli32 only
115  DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF = 30, // KISS audio Stream mode on/Off
116  DSHOT_CMD_SILENT_MODE_ON_OFF = 31, // KISS silent Mode on/Off
119 
125 typedef struct {
126  union {
127  struct {
128  uint8_t temp;
129  uint16_t voltage;
130  uint16_t current;
131  uint16_t consumption;
132  uint16_t rpm;
133  } __attribute__((__packed__, scalar_storage_order("big-endian")));
134  uint8_t rawData[9];
135  };
137 } __attribute__((__packed__)) DshotTelemetry ;
138 
139 typedef union {
140 #if DSHOT_AT_LEAST_ONE_32B_TIMER
142 #endif
144 } DshotDmaBuffer; // alignment to satisfy dma requirement
145 
149 typedef struct DSHOTDriver DSHOTDriver;
150 
151 
155 typedef struct {
160 
165 
169  PWMDriver *pwmp;
170 
174  SerialDriver *tlm_sd;
175 
180 
181 #if __DCACHE_PRESENT
182 
185  bool dcache_memory_in_use;
186 #endif
187 } DSHOTConfig;
188 
189 void dshotStart(DSHOTDriver *driver, const DSHOTConfig *config);
190 void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle);
191 void dshotSendFrame(DSHOTDriver *driver);
192 void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS]);
193 void dshotSendSpecialCommand(DSHOTDriver *driver, const uint8_t index, const dshot_special_commands_t specmd);
194 
196 const DshotTelemetry *dshotGetTelemetry(const DSHOTDriver *driver, const uint32_t index);
197 
198 
199 /*
200 # _ __ _ _
201 # | '_ \ (_) | |
202 # | |_) | _ __ _ __ __ __ _ | |_ ___
203 # | .__/ | '__| | | \ \ / / / _` | | __| / _ \
204 # | | | | | | \ V / | (_| | \ |_ | __/
205 # |_| |_| |_| \_/ \__,_| \__| \___|
206 */
207 
208 typedef union {
209  struct {
210  uint16_t crc: 4;
211  uint16_t telemetryRequest: 1;
212  uint16_t throttle: 11;
213  };
215 } DshotPacket;
216 
217 typedef struct {
221  volatile bool onGoingQry;
222 } DshotPackets;
223 
224 
228 struct DSHOTDriver {
233 
238 
242  PWMConfig pwm_conf;
243 
248 
252  msg_t _mbBuf[1];
253 
257  mailbox_t mb;
258 
263 
267  THD_WORKING_AREA(waDshotTlmRec, 512);
268 
270 };
271 
DSHOT_CMD_SAVE_SETTINGS
@ DSHOT_CMD_SAVE_SETTINGS
Definition: esc_dshot.h:104
DSHOT_CMD_BEACON1
@ DSHOT_CMD_BEACON1
Definition: esc_dshot.h:93
DshotPacket::rawFrame
uint16_t rawFrame
Definition: esc_dshot.h:214
uint16_t
unsigned short uint16_t
Definition: types.h:16
DSHOT_CMD_SPIN_DIRECTION_2
@ DSHOT_CMD_SPIN_DIRECTION_2
Definition: esc_dshot.h:100
DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF
@ DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF
Definition: esc_dshot.h:115
DSHOT_STOP
@ DSHOT_STOP
Stopped.
Definition: esc_dshot.h:70
DSHOTConfig::dma_channel
uint8_t dma_channel
: dma channel associated with pwm timer used to generate dshot output
Definition: esc_dshot.h:164
DSHOTDriver::pwm_conf
PWMConfig pwm_conf
PWM config associated with pwm timer.
Definition: esc_dshot.h:242
DSHOTConfig::pwmp
PWMDriver * pwmp
PWM driver that feed up to 4 dshot lines.
Definition: esc_dshot.h:169
DSHOTConfig::dma_stream
uint32_t dma_stream
: dma stream associated with pwm timer used to generate dshot output
Definition: esc_dshot.h:159
DshotTelemetry::crc8
uint8_t crc8
Definition: esc_dshot.h:136
DSHOT_READY
@ DSHOT_READY
Ready.
Definition: esc_dshot.h:71
DSHOTDriver::dshotMotors
DshotPackets dshotMotors
Definition: esc_dshot.h:269
uint32_t
unsigned long uint32_t
Definition: types.h:18
DSHOT_UNINIT
@ DSHOT_UNINIT
Not initialized.
Definition: esc_dshot.h:69
DSHOTDriver::config
const DSHOTConfig * config
DMA config associated with pwm timer.
Definition: esc_dshot.h:232
DSHOT_CMD_3D_MODE_ON
@ DSHOT_CMD_3D_MODE_ON
Definition: esc_dshot.h:102
DshotPackets::currentTlmQry
volatile uint8_t currentTlmQry
Definition: esc_dshot.h:220
DSHOT_CMD_SPIN_DIRECTION_1
@ DSHOT_CMD_SPIN_DIRECTION_1
Definition: esc_dshot.h:99
DSHOT_CMD_LED2_ON
@ DSHOT_CMD_LED2_ON
Definition: esc_dshot.h:109
DSHOTDriver::THD_WORKING_AREA
THD_WORKING_AREA(waDshotTlmRec, 512)
stack working area for dshot telemetry thread
rpm
uint16_t rpm
Definition: rpm_sensor.c:33
DSHOTDriver::crc_errors
uint32_t crc_errors
number of crc errors
Definition: esc_dshot.h:262
DSHOT_CMD_BEACON2
@ DSHOT_CMD_BEACON2
Definition: esc_dshot.h:94
DSHOT_CMD_LED1_OFF
@ DSHOT_CMD_LED1_OFF
Definition: esc_dshot.h:112
dshotGetCrcErrorsCount
uint32_t dshotGetCrcErrorsCount(DSHOTDriver *driver)
return number of telemetry crc error since dshotStart
Definition: esc_dshot.c:329
DSHOT_CHANNELS
#define DSHOT_CHANNELS
Definition: esc_dshot.h:56
DSHOT_CMD_LED1_ON
@ DSHOT_CMD_LED1_ON
Definition: esc_dshot.h:108
DSHOT_ONGOING_TELEMETRY_QUERY
@ DSHOT_ONGOING_TELEMETRY_QUERY
Transfering.
Definition: esc_dshot.h:72
DSHOTDriver::dmap
DMADriver dmap
DMA driver associated with pwm timer.
Definition: esc_dshot.h:247
DshotPackets::onGoingQry
volatile bool onGoingQry
Definition: esc_dshot.h:221
config
static const struct usb_config_descriptor config
Definition: usb_ser_hw.c:200
DSHOT_CMD_SETTINGS_REQUEST
@ DSHOT_CMD_SETTINGS_REQUEST
Definition: esc_dshot.h:103
uint8_t
unsigned char uint8_t
Definition: types.h:14
DSHOT_CMD_LED3_OFF
@ DSHOT_CMD_LED3_OFF
Definition: esc_dshot.h:114
DSHOTConfig::dma_buf
DshotDmaBuffer * dma_buf
dshot dma buffer, sgould be defined in a non Dcached region
Definition: esc_dshot.h:179
dshotstate_t
dshotstate_t
Driver state machine possible states.
Definition: esc_dshot.h:68
DSHOTDriver::mb
mailbox_t mb
mailbox for dshot telemetry thread
Definition: esc_dshot.h:257
hal_stm32_dma.h
STM32 DMA subsystem driver header.
DSHOT_CMD_ESC_INFO
@ DSHOT_CMD_ESC_INFO
Definition: esc_dshot.h:98
DSHOT_CMD_LED0_OFF
@ DSHOT_CMD_LED0_OFF
Definition: esc_dshot.h:111
DshotDmaBuffer
Definition: esc_dshot.h:139
DSHOTDriver::_mbBuf
msg_t _mbBuf[1]
mailbox buffer for dshot telemetry thread
Definition: esc_dshot.h:252
DSHOTConfig::tlm_sd
SerialDriver * tlm_sd
if non null : dshot telemetry serial driver
Definition: esc_dshot.h:174
DSHOTDriver::dma_conf
DMAConfig dma_conf
DMA config associated with pwm timer.
Definition: esc_dshot.h:237
DSHOT_CMD_BEACON3
@ DSHOT_CMD_BEACON3
Definition: esc_dshot.h:95
dshotStart
void dshotStart(DSHOTDriver *driver, const DSHOTConfig *config)
Configures and activates the DSHOT peripheral.
Definition: esc_dshot.c:122
DSHOT_CMD_MAX
@ DSHOT_CMD_MAX
Definition: esc_dshot.h:117
DshotPackets
Definition: esc_dshot.h:217
DMADriver
Structure representing a DMA driver.
Definition: hal_stm32_dma.h:365
DSHOT_CMD_SILENT_MODE_ON_OFF
@ DSHOT_CMD_SILENT_MODE_ON_OFF
Definition: esc_dshot.h:116
DshotTelemetry
telemetry packed as sent by some KISS ESC
Definition: esc_dshot.h:125
DSHOT_CMD_BEACON5
@ DSHOT_CMD_BEACON5
Definition: esc_dshot.h:97
dshot_special_commands_t
dshot_special_commands_t
DSHOT special commands (0-47) for KISS and BLHELI ESC.
Definition: esc_dshot.h:91
DSHOT_CMD_BEACON4
@ DSHOT_CMD_BEACON4
Definition: esc_dshot.h:96
DSHOT_ERROR
@ DSHOT_ERROR
Transfert error.
Definition: esc_dshot.h:73
dshotSetThrottle
void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle)
prepare throttle order for specified ESC
Definition: esc_dshot.c:210
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, ...)
Definition: esc_dshot.c:239
rawData
Definition: actuators_ostrich.c:48
DMAConfig
DMA stream configuration structure.
Definition: hal_stm32_dma.h:241
DshotPacket
Definition: esc_dshot.h:208
DSHOT_CMD_MOTOR_STOP
@ DSHOT_CMD_MOTOR_STOP
Definition: esc_dshot.h:92
DSHOT_CMD_SPIN_DIRECTION_NORMAL
@ DSHOT_CMD_SPIN_DIRECTION_NORMAL
Definition: esc_dshot.h:105
DSHOTDriver
DSHOT driver structure.
Definition: esc_dshot.h:228
DSHOT_CMD_SPIN_DIRECTION_REVERSED
@ DSHOT_CMD_SPIN_DIRECTION_REVERSED
Definition: esc_dshot.h:106
dshotSendThrottles
void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS])
send throttle packed order to all of the ESCs
Definition: esc_dshot.c:286
DSHOT_DMA_BUFFER_SIZE
#define DSHOT_DMA_BUFFER_SIZE
Definition: esc_dshot.h:52
dshotGetTelemetry
const DshotTelemetry * dshotGetTelemetry(const DSHOTDriver *driver, const uint32_t index)
return last received telemetry data
Definition: esc_dshot.c:342
DSHOT_CMD_LED2_OFF
@ DSHOT_CMD_LED2_OFF
Definition: esc_dshot.h:113
DSHOT_CMD_3D_MODE_OFF
@ DSHOT_CMD_3D_MODE_OFF
Definition: esc_dshot.h:101
DSHOT_CMD_LED3_ON
@ DSHOT_CMD_LED3_ON
Definition: esc_dshot.h:110
DSHOT_CMD_LED0_ON
@ DSHOT_CMD_LED0_ON
Definition: esc_dshot.h:107
dshotSendFrame
void dshotSendFrame(DSHOTDriver *driver)
send throttle order
Definition: esc_dshot.c:302
DSHOTConfig
DSHOT Driver configuration structure.
Definition: esc_dshot.h:155