Paparazzi UAS  v5.17_devel-24-g2ae834f
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
45 #define DSHOT_BIT_WIDTHS 16
46 #define DSHOT_PRE_FRAME_SILENT_SYNC_BITS 2
47 #define DSHOT_POST_FRAME_SILENT_SYNC_BITS 2
48 #define DSHOT_DMA_BUFFER_SIZE (DSHOT_BIT_WIDTHS + \
49  DSHOT_PRE_FRAME_SILENT_SYNC_BITS + \
50  DSHOT_POST_FRAME_SILENT_SYNC_BITS )
51 
52 #define DSHOT_CHANNELS 4 // depend on the number of channels per timer
53 
59 #define DSHOT_ALL_MOTORS 255
60 
64 typedef enum {
70 } dshotstate_t;
71 
72 /*
73  DshotSettingRequest (KISS24). Spin direction,
74  3d and save Settings require 10 requests.. and the
75  TLM Byte must always be high if 1-47 are used to send settings
76 
77  3D Mode:
78  0 = stop
79  48 (low) - 1047 (high) -> negative direction
80  1048 (low) - 2047 (high) -> positive direction
81  */
82 
87 typedef enum {
94  DSHOT_CMD_ESC_INFO, // V2 includes settings
99  DSHOT_CMD_SETTINGS_REQUEST, // Currently not implemented
103  DSHOT_CMD_LED0_ON, // BLHeli32 only
104  DSHOT_CMD_LED1_ON, // BLHeli32 only
105  DSHOT_CMD_LED2_ON, // BLHeli32 only
106  DSHOT_CMD_LED3_ON, // BLHeli32 only
107  DSHOT_CMD_LED0_OFF, // BLHeli32 only
108  DSHOT_CMD_LED1_OFF, // BLHeli32 only
109  DSHOT_CMD_LED2_OFF, // BLHeli32 only
110  DSHOT_CMD_LED3_OFF, // BLHeli32 only
111  DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF = 30, // KISS audio Stream mode on/Off
112  DSHOT_CMD_SILENT_MODE_ON_OFF = 31, // KISS silent Mode on/Off
115 
121 typedef struct {
122  union {
123  struct {
124  uint8_t temp;
125  uint16_t voltage;
126  uint16_t current;
127  uint16_t consumption;
128  uint16_t rpm;
129  } __attribute__((__packed__, scalar_storage_order("big-endian")));
130  uint8_t rawData[9];
131  };
133 } __attribute__((__packed__)) DshotTelemetry ;
134 
135 
139 typedef struct DSHOTDriver DSHOTDriver;
140 
141 
145 typedef struct {
150 
155 
159  PWMDriver *pwmp;
160 
164  SerialDriver *tlm_sd;
165 } DSHOTConfig;
166 
167 void dshotStart(DSHOTDriver *driver, const DSHOTConfig *config);
168 void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle);
169 void dshotSendFrame(DSHOTDriver *driver);
170 void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS]);
171 void dshotSendSpecialCommand(DSHOTDriver *driver, const uint8_t index, const dshot_special_commands_t specmd);
172 
174 const DshotTelemetry *dshotGetTelemetry(const DSHOTDriver *driver, const uint32_t index);
175 
176 
177 /*
178 # _ __ _ _
179 # | '_ \ (_) | |
180 # | |_) | _ __ _ __ __ __ _ | |_ ___
181 # | .__/ | '__| | | \ \ / / / _` | | __| / _ \
182 # | | | | | | \ V / | (_| | \ |_ | __/
183 # |_| |_| |_| \_/ \__,_| \__| \___|
184 */
185 
186 typedef union {
187  struct {
188  uint16_t crc: 4;
189  uint16_t telemetryRequest: 1;
190  uint16_t throttle: 11;
191  };
193 } DshotPacket;
194 
195 typedef struct {
199  volatile bool onGoingQry;
200 } DshotPackets;
201 
202 typedef union {
204 #if DSHOT_AT_LEAST_ONE_32B_TIMER
206 #endif
207 } DshotDmaBuffer __attribute__((aligned(16))); // alignment to satisfy dma requirement
208 
212 struct DSHOTDriver {
217 
222 
226  PWMConfig pwm_conf;
227 
232 
236  msg_t _mbBuf[1];
237 
241  mailbox_t mb;
242 
247 
251  THD_WORKING_AREA(waDshotTlmRec, 512);
252 
255 };
256 
unsigned short uint16_t
Definition: types.h:16
STM32 DMA subsystem driver header.
telemetry packed as sent by some KISS ESC
Definition: esc_dshot.h:121
Not initialized.
Definition: esc_dshot.h:65
void dshotStart(DSHOTDriver *driver, const DSHOTConfig *config)
Configures and activates the DSHOT peripheral.
Definition: esc_dshot.c:122
dshotstate_t
Driver state machine possible states.
Definition: esc_dshot.h:64
PWMConfig pwm_conf
PWM config associated with pwm timer.
Definition: esc_dshot.h:226
DSHOT Driver configuration structure.
Definition: esc_dshot.h:145
PWMDriver * pwmp
PWM driver that feed up to 4 dshot lines.
Definition: esc_dshot.h:159
Ready.
Definition: esc_dshot.h:67
DSHOT driver structure.
Definition: esc_dshot.h:212
DshotDmaBuffer dsdb
Definition: esc_dshot.h:254
const DSHOTConfig * config
DMA config associated with pwm timer.
Definition: esc_dshot.h:216
THD_WORKING_AREA(waDshotTlmRec, 512)
stack working area for dshot telemetry thread
uint8_t crc8
Definition: esc_dshot.h:132
DMA stream configuration structure.
uint16_t rpm
Definition: rpm_sensor.c:33
const DshotTelemetry * dshotGetTelemetry(const DSHOTDriver *driver, const uint32_t index)
return last received telemetry data
Definition: esc_dshot.c:344
SerialDriver * tlm_sd
if non null : dshot telemetry serial driver
Definition: esc_dshot.h:164
void dshotSendFrame(DSHOTDriver *driver)
send throttle order
Definition: esc_dshot.c:304
unsigned long uint32_t
Definition: types.h:18
volatile uint8_t currentTlmQry
Definition: esc_dshot.h:198
DshotPackets dshotMotors
Definition: esc_dshot.h:253
uint8_t dma_channel
: dma channel associated with pwm timer used to generate dshot output
Definition: esc_dshot.h:154
#define DSHOT_CHANNELS
Definition: esc_dshot.h:52
Transfert error.
Definition: esc_dshot.h:69
mailbox_t mb
mailbox for dshot telemetry thread
Definition: esc_dshot.h:241
uint32_t crc_errors
number of crc errors
Definition: esc_dshot.h:246
unsigned char uint8_t
Definition: types.h:14
uint32_t dma_stream
: dma stream associated with pwm timer used to generate dshot output
Definition: esc_dshot.h:149
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:241
static const struct usb_config_descriptor config
Definition: usb_ser_hw.c:200
dshot_special_commands_t
DSHOT special commands (0-47) for KISS and BLHELI ESC.
Definition: esc_dshot.h:87
DMADriver dmap
DMA driver associated with pwm timer.
Definition: esc_dshot.h:231
DMAConfig dma_conf
DMA config associated with pwm timer.
Definition: esc_dshot.h:221
void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS])
send throttle packed order to all of the ESCs
Definition: esc_dshot.c:288
void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle)
prepare throttle order for specified ESC
Definition: esc_dshot.c:216
uint32_t dshotGetCrcErrorsCount(DSHOTDriver *driver)
return number of telemetry crc error since dshotStart
Definition: esc_dshot.c:331
uint16_t rawFrame
Definition: esc_dshot.h:192
volatile bool onGoingQry
Definition: esc_dshot.h:199
Stopped.
Definition: esc_dshot.h:66
msg_t _mbBuf[1]
mailbox buffer for dshot telemetry thread
Definition: esc_dshot.h:236
Structure representing a DMA driver.
#define DSHOT_DMA_BUFFER_SIZE
Definition: esc_dshot.h:48