Paparazzi UAS  v5.17_devel-3-g5993d39
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_FRAME_SILENT_SYNC_BITS 4
47 #define DSHOT_DMA_BUFFER_SIZE (DSHOT_BIT_WIDTHS+DSHOT_FRAME_SILENT_SYNC_BITS)
48 #define DSHOT_CHANNELS 4 // depend on the number of channels per timer
49 
55 #define DSHOT_ALL_MOTORS 255
56 
60 typedef enum {
66 } dshotstate_t;
67 
68 /*
69  DshotSettingRequest (KISS24). Spin direction,
70  3d and save Settings require 10 requests.. and the
71  TLM Byte must always be high if 1-47 are used to send settings
72 
73  3D Mode:
74  0 = stop
75  48 (low) - 1047 (high) -> negative direction
76  1048 (low) - 2047 (high) -> positive direction
77  */
78 
83 typedef enum {
90  DSHOT_CMD_ESC_INFO, // V2 includes settings
95  DSHOT_CMD_SETTINGS_REQUEST, // Currently not implemented
99  DSHOT_CMD_LED0_ON, // BLHeli32 only
100  DSHOT_CMD_LED1_ON, // BLHeli32 only
101  DSHOT_CMD_LED2_ON, // BLHeli32 only
102  DSHOT_CMD_LED3_ON, // BLHeli32 only
103  DSHOT_CMD_LED0_OFF, // BLHeli32 only
104  DSHOT_CMD_LED1_OFF, // BLHeli32 only
105  DSHOT_CMD_LED2_OFF, // BLHeli32 only
106  DSHOT_CMD_LED3_OFF, // BLHeli32 only
107  DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF = 30, // KISS audio Stream mode on/Off
108  DSHOT_CMD_SILENT_MODE_ON_OFF = 31, // KISS silent Mode on/Off
111 
117 typedef struct {
118  union {
119  struct {
120  uint8_t temp;
121  uint16_t voltage;
122  uint16_t current;
123  uint16_t consumption;
124  uint16_t rpm;
125  } __attribute__((__packed__, scalar_storage_order("big-endian")));
126  uint8_t rawData[9];
127  };
129 } __attribute__((__packed__)) DshotTelemetry ;
130 
131 
135 typedef struct DSHOTDriver DSHOTDriver;
136 
137 
141 typedef struct {
146 
151 
155  PWMDriver *pwmp;
156 
160  SerialDriver *tlm_sd;
161 } DSHOTConfig;
162 
163 void dshotStart(DSHOTDriver *driver, const DSHOTConfig *config);
164 void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle);
165 void dshotSendFrame(DSHOTDriver *driver);
166 void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS]);
167 void dshotSendSpecialCommand(DSHOTDriver *driver, const uint8_t index, const dshot_special_commands_t specmd);
168 
170 const DshotTelemetry *dshotGetTelemetry(const DSHOTDriver *driver, const uint32_t index);
171 
172 
173 /*
174 # _ __ _ _
175 # | '_ \ (_) | |
176 # | |_) | _ __ _ __ __ __ _ | |_ ___
177 # | .__/ | '__| | | \ \ / / / _` | | __| / _ \
178 # | | | | | | \ V / | (_| | \ |_ | __/
179 # |_| |_| |_| \_/ \__,_| \__| \___|
180 */
181 
182 typedef union {
183  struct {
184  uint16_t crc: 4;
185  uint16_t telemetryRequest: 1;
186  uint16_t throttle: 11;
187  };
189 } DshotPacket;
190 
191 typedef struct {
195  volatile bool onGoingQry;
196 } DshotPackets;
197 
198 typedef union {
200 #if DSHOT_AT_LEAST_ONE_32B_TIMER
202 #endif
203 } DshotDmaBuffer __attribute__((aligned(16))); // alignment to satisfy dma requirement
204 
208 struct DSHOTDriver {
213 
218 
222  PWMConfig pwm_conf;
223 
228 
232  msg_t _mbBuf[1];
233 
237  mailbox_t mb;
238 
243 
247  THD_WORKING_AREA(waDshotTlmRec, 512);
248 
251 };
252 
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:117
Not initialized.
Definition: esc_dshot.h:61
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:60
PWMConfig pwm_conf
PWM config associated with pwm timer.
Definition: esc_dshot.h:222
DSHOT Driver configuration structure.
Definition: esc_dshot.h:141
PWMDriver * pwmp
PWM driver that feed up to 4 dshot lines.
Definition: esc_dshot.h:155
Ready.
Definition: esc_dshot.h:63
DSHOT driver structure.
Definition: esc_dshot.h:208
DshotDmaBuffer dsdb
Definition: esc_dshot.h:250
const DSHOTConfig * config
DMA config associated with pwm timer.
Definition: esc_dshot.h:212
THD_WORKING_AREA(waDshotTlmRec, 512)
stack working area for dshot telemetry thread
uint8_t crc8
Definition: esc_dshot.h:128
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:343
SerialDriver * tlm_sd
if non null : dshot telemetry serial driver
Definition: esc_dshot.h:160
void dshotSendFrame(DSHOTDriver *driver)
send throttle order
Definition: esc_dshot.c:303
unsigned long uint32_t
Definition: types.h:18
volatile uint8_t currentTlmQry
Definition: esc_dshot.h:194
DshotPackets dshotMotors
Definition: esc_dshot.h:249
uint8_t dma_channel
: dma channel associated with pwm timer used to generate dshot output
Definition: esc_dshot.h:150
#define DSHOT_CHANNELS
Definition: esc_dshot.h:48
Transfert error.
Definition: esc_dshot.h:65
mailbox_t mb
mailbox for dshot telemetry thread
Definition: esc_dshot.h:237
uint32_t crc_errors
number of crc errors
Definition: esc_dshot.h:242
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:145
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:240
static const struct usb_config_descriptor config
Definition: usb_ser_hw.c:199
dshot_special_commands_t
DSHOT special commands (0-47) for KISS and BLHELI ESC.
Definition: esc_dshot.h:83
DMADriver dmap
DMA driver associated with pwm timer.
Definition: esc_dshot.h:227
DMAConfig dma_conf
DMA config associated with pwm timer.
Definition: esc_dshot.h:217
void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS])
send throttle packed order to all of the ESCs
Definition: esc_dshot.c:287
void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle)
prepare throttle order for specified ESC
Definition: esc_dshot.c:215
uint32_t dshotGetCrcErrorsCount(DSHOTDriver *driver)
return number of telemetry crc error since dshotStart
Definition: esc_dshot.c:330
uint16_t rawFrame
Definition: esc_dshot.h:188
volatile bool onGoingQry
Definition: esc_dshot.h:195
Stopped.
Definition: esc_dshot.h:62
msg_t _mbBuf[1]
mailbox buffer for dshot telemetry thread
Definition: esc_dshot.h:232
Structure representing a DMA driver.
#define DSHOT_DMA_BUFFER_SIZE
Definition: esc_dshot.h:47