Paparazzi UAS  v7.0_unstable
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>
36 #if DSHOT_BIDIR
39 #endif
40 
41 
42 #ifndef DSHOT_CHANNEL_FIRST_INDEX
43 #define DSHOT_CHANNEL_FIRST_INDEX 0U
44 #endif
45 
48 #define DSHOT_BIT_WIDTHS 16U
49 #define DSHOT_PRE_FRAME_SILENT_SYNC_BITS 2U
50 #define DSHOT_POST_FRAME_SILENT_SYNC_BITS 4U
51 #define DSHOT_DMA_BUFFER_SIZE (DSHOT_BIT_WIDTHS + \
52  DSHOT_PRE_FRAME_SILENT_SYNC_BITS + \
53  DSHOT_POST_FRAME_SILENT_SYNC_BITS )
54 
55 
56 #if STM32_DMA_SUPPORTS_DMAMUX
57 #define DSHOT_EMIT_STREAM_NX(tim) STM32_DMAMUX1_TIM ## tim ## _UP
58 #define DSHOT_EMIT_STREAM(tim) DSHOT_EMIT_STREAM_NX(tim)
59 #endif
60 
65 #define DSHOT_BIDIR_ERR_CRC UINT32_MAX
66 #define DSHOT_BIDIR_TLM_EDT (UINT32_MAX-1U)
67 
68 
74 #define DSHOT_ALL_MOTORS 255U
75 
76 // /**
77 // * @brief Driver state machine possible states.
78 // */
79 // typedef enum {
80 // DSHOT_UNINIT = 0, /**< Not initialized. */
81 // DSHOT_STOP, /**< Stopped. */
82 // DSHOT_READY, /**< Ready. */
83 // DSHOT_ONGOING_TELEMETRY_QUERY, /**< Transfering. */
84 // DSHOT_ERROR /**< Transfert error. */
85 // } dshotstate_t;
86 
87 /*
88  DshotSettingRequest (KISS24). Spin direction,
89  3d and save Settings require 10 requests.. and the
90  TLM Byte must always be high if 1-47 are used to send settings
91 
92  3D Mode:
93  0 = stop
94  48 (low) - 1047 (high) -> negative direction
95  1048 (low) - 2047 (high) -> positive direction
96  */
97 
102 typedef enum {
109  DSHOT_CMD_ESC_INFO, // V2 includes settings
114  DSHOT_CMD_SETTINGS_REQUEST, // Currently not implemented
120  DSHOT_CMD_LED0_ON, // BLHeli32 only
121  DSHOT_CMD_LED1_ON, // BLHeli32 only
122  DSHOT_CMD_LED2_ON, // BLHeli32 only
123  DSHOT_CMD_LED3_ON, // BLHeli32 only
124  DSHOT_CMD_LED0_OFF, // BLHeli32 only
125  DSHOT_CMD_LED1_OFF, // BLHeli32 only
126  DSHOT_CMD_LED2_OFF, // BLHeli32 only
127  DSHOT_CMD_LED3_OFF, // BLHeli32 only
128  DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF = 30U, // KISS audio Stream mode on/Off
129  DSHOT_CMD_SILENT_MODE_ON_OFF = 31U, // KISS silent Mode on/Off
133 
139 typedef struct {
140  union {
141  struct {
142  uint8_t temp;
143  uint16_t voltage;
144  uint16_t current;
145  uint16_t consumption;
146  uint16_t rpm;
147  }
148  __attribute__ ((__packed__));
149  uint8_t rawData[9];
150  };
152 } __attribute__ ((__packed__)) DshotTelemetryFrame ;
153 
154 
158 typedef struct {
159  DshotTelemetryFrame frame; // fields shared by serial telemetry and EDT
160  uint8_t stress; // EDT additionnal field
161  uint8_t status; // EDT additionnal field
162  systime_t ts; // timestamp of last succesfull received frame
163 } DshotTelemetry ;
164 
165 
166 typedef union {
167 #if DSHOT_AT_LEAST_ONE_32B_TIMER
169 #endif
171 } DshotDmaBuffer; // alignment to satisfy dma requirement
172 
176 typedef struct DSHOTDriver DSHOTDriver;
177 
178 
182 typedef struct {
187 
191 #if STM32_DMA_SUPPORTS_DMAMUX
192  uint8_t dmamux;
193 #else
195 #endif
196 
200  PWMDriver *pwmp;
201 
205  SerialDriver *tlm_sd;
206 
211 
212 #if DSHOT_BIDIR
216  DshotRpmCaptureConfig dma_capt_cfg;
217 #endif
218 
219 #if DSHOT_SPEED == 0
225 #endif
226 
227 #if __DCACHE_PRESENT
231  bool dcache_memory_in_use;
232 #endif
233 } DSHOTConfig;
234 
235 
236 
237 
238 
239 
240 void dshotStart(DSHOTDriver *driver, const DSHOTConfig *config);
241 void dshotStop(DSHOTDriver *driver);
242 void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle);
243 void dshotSendFrame(DSHOTDriver *driver);
244 void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS]);
245 void dshotSendSpecialCommand(DSHOTDriver *driver, const uint8_t index, const dshot_special_commands_t specmd);
246 
250 #if DSHOT_BIDIR
251 uint32_t dshotGetEperiod(DSHOTDriver *driver, const uint32_t index);
252 uint32_t dshotGetRpm(DSHOTDriver *driver, const uint32_t index);
253 #endif
254 
255 
256 /*
257 # _ __ _ _
258 # | '_ \ (_) | |
259 # | |_) | _ __ _ __ __ __ _ | |_ ___
260 # | .__/ | '__| | | \ \ / / / _` | | __| / _ \
261 # | | | | | | \ V / | (_| | \ |_ | __/
262 # |_| |_| |_| \_/ \__,_| \__| \___|
263 */
264 
265 typedef union {
266  struct {
267  uint16_t crc:4;
268  uint16_t telemetryRequest:1;
269  uint16_t throttle:11;
270  };
272 } DshotPacket;
273 
274 
275 typedef struct {
278  mutex_t tlmMtx[DSHOT_CHANNELS];
279  volatile bool onGoingQry;
281 } DshotPackets;
282 
283 
287 struct DSHOTDriver {
292 
297 
301  PWMConfig pwm_conf;
302 
307 
311  msg_t _mbBuf[1];
312 
316  mailbox_t mb;
317 
322 
327 
328 #if DSHOT_SPEED == 0
331 #endif
332 #if DSHOT_BIDIR
338  DshotRpmCapture rpm_capture;
342  DshotErps erps;
346  uint32_t rpms_frame[DSHOT_CHANNELS];
347 #endif
351  THD_WORKING_AREA(waDshotTlmRec, 512);
352 
357 };
358 
ERPS complete frame, raw and decoded.
Definition: dshot_erps.h:58
: DSHOT Rpm Capture Driver configuration structure.
void dshotStart(DSHOTDriver *driver, const DSHOTConfig *config)
Configures and activates the DSHOT peripheral.
Definition: esc_dshot.c:154
void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle)
prepare throttle order for specified ESC
Definition: esc_dshot.c:301
DshotTelemetry dshotGetTelemetry(DSHOTDriver *driver, const uint32_t index)
return last received telemetry data
Definition: esc_dshot.c:468
uint32_t dshotGetTelemetryFrameCount(const DSHOTDriver *driver)
return number of telemetry succesfull frame since dshotStart
Definition: esc_dshot.c:454
SerialDriver * tlm_sd
if non null : dshot telemetry serial driver
Definition: esc_dshot.h:205
volatile bool onGoingQry
Definition: esc_dshot.h:279
PWMDriver * pwmp
PWM driver that feed up to 4 dshot lines.
Definition: esc_dshot.h:200
#define DSHOT_DMA_BUFFER_SIZE
Definition: esc_dshot.h:51
uint8_t dma_channel
: dma channel associated with pwm timer used to generate dshot output
Definition: esc_dshot.h:194
uint16_t speed_khz
dynamic dshot speed, when speed id not known at compilation
Definition: esc_dshot.h:224
void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS])
send throttle packed order to all of the ESCs
Definition: esc_dshot.c:385
uint8_t status
Definition: esc_dshot.h:161
uint8_t stress
Definition: esc_dshot.h:160
DshotTelemetryFrame frame
Definition: esc_dshot.h:159
uint32_t dma_stream
: dma stream associated with pwm timer used to generate dshot output
Definition: esc_dshot.h:186
systime_t ts
Definition: esc_dshot.h:162
uint32_t dshotGetCrcErrorCount(const DSHOTDriver *driver)
return number of telemetry crc error since dshotStart
Definition: esc_dshot.c:442
void dshotStop(DSHOTDriver *driver)
stop the DSHOT driver and free the related resources : pwm driver and dma driver.
Definition: esc_dshot.c:260
uint16_t rawFrame
Definition: esc_dshot.h:271
void dshotSendFrame(DSHOTDriver *driver)
send throttle order
Definition: esc_dshot.c:404
DshotDmaBuffer * dma_buf
dshot dma buffer, should be defined in a non Dcached region
Definition: esc_dshot.h:210
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:330
dshot_special_commands_t
DSHOT special commands (0-47) for KISS and BLHELI ESC.
Definition: esc_dshot.h:102
@ DSHOT_CMD_SPIN_DIRECTION_REVERSED
Definition: esc_dshot.h:119
@ DSHOT_CMD_SILENT_MODE_ON_OFF
Definition: esc_dshot.h:129
@ DSHOT_CMD_SAVE_SETTINGS
Definition: esc_dshot.h:115
@ DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF
Definition: esc_dshot.h:128
@ DSHOT_CMD_LED2_ON
Definition: esc_dshot.h:122
@ DSHOT_CMD_LED3_ON
Definition: esc_dshot.h:123
@ DSHOT_CMD_BEACON1
Definition: esc_dshot.h:104
@ DSHOT_CMD_BEACON5
Definition: esc_dshot.h:108
@ DSHOT_CMD_SETTINGS_REQUEST
Definition: esc_dshot.h:114
@ DSHOT_CMD_LED1_OFF
Definition: esc_dshot.h:125
@ DSHOT_CMD_BEACON4
Definition: esc_dshot.h:107
@ DSHOT_CMD_BIDIR_EDT_MODE_OFF
Definition: esc_dshot.h:117
@ DSHOT_CMD_LED0_OFF
Definition: esc_dshot.h:124
@ DSHOT_CMD_SPIN_DIRECTION_NORMAL
Definition: esc_dshot.h:118
@ DSHOT_CMD_SPIN_DIRECTION_1
Definition: esc_dshot.h:110
@ DSHOT_MIN_THROTTLE
Definition: esc_dshot.h:131
@ DSHOT_CMD_SPIN_DIRECTION_2
Definition: esc_dshot.h:111
@ DSHOT_CMD_3D_MODE_OFF
Definition: esc_dshot.h:112
@ DSHOT_CMD_MOTOR_STOP
Definition: esc_dshot.h:103
@ DSHOT_CMD_LED1_ON
Definition: esc_dshot.h:121
@ DSHOT_CMD_BEACON3
Definition: esc_dshot.h:106
@ DSHOT_CMD_BEACON2
Definition: esc_dshot.h:105
@ DSHOT_CMD_LED3_OFF
Definition: esc_dshot.h:127
@ DSHOT_CMD_LED0_ON
Definition: esc_dshot.h:120
@ DSHOT_CMD_LED2_OFF
Definition: esc_dshot.h:126
@ DSHOT_CMD_ESC_INFO
Definition: esc_dshot.h:109
@ DSHOT_CMD_MAX
Definition: esc_dshot.h:130
@ DSHOT_CMD_3D_MODE_ON
Definition: esc_dshot.h:113
@ DSHOT_CMD_BIDIR_EDT_MODE_ON
Definition: esc_dshot.h:116
uint8_t currentTlmQry
Definition: esc_dshot.h:280
DSHOT Driver configuration structure.
Definition: esc_dshot.h:182
telemetry with timestamp
Definition: esc_dshot.h:158
telemetry packed as sent by some KISS ESC
Definition: esc_dshot.h:139
#define DSHOT_CHANNELS
STM32 DMA subsystem driver header.
DMA stream configuration structure.
Structure representing a DMA driver.
uint16_t rpm
Definition: rpm_sensor.c:33
static const struct usb_config_descriptor config
Definition: usb_ser_hw.c:200
DSHOT driver structure.
Definition: esc_dshot.h:287
uint32_t crc_errors
number of crc errors
Definition: esc_dshot.h:321
DshotPackets dshotMotors
object managing dma control frame for outgoing command
Definition: esc_dshot.h:356
mailbox_t mb
mailbox for dshot telemetry thread
Definition: esc_dshot.h:316
DMADriver dmap
DMA driver associated with pwm timer.
Definition: esc_dshot.h:306
PWMConfig pwm_conf
PWM config associated with pwm timer.
Definition: esc_dshot.h:301
uint16_t bit1Duty
Definition: esc_dshot.h:330
const DSHOTConfig * config
DMA config associated with pwm timer.
Definition: esc_dshot.h:291
msg_t _mbBuf[1]
mailbox buffer for dshot telemetry thread
Definition: esc_dshot.h:311
THD_WORKING_AREA(waDshotTlmRec, 512)
stack working area for dshot telemetry thread
uint32_t tlm_frame_nb
number of sucessful telemetry frame received
Definition: esc_dshot.h:326
DMAConfig dma_conf
DMA config associated with pwm timer.
Definition: esc_dshot.h:296
uint16_t bit0Duty
Definition: esc_dshot.h:329
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98