Paparazzi UAS  v5.17_devel-145-ga57bdbf-dirty
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 
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 
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:125
Not initialized.
Definition: esc_dshot.h:69
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:68
PWMConfig pwm_conf
PWM config associated with pwm timer.
Definition: esc_dshot.h:242
DSHOT Driver configuration structure.
Definition: esc_dshot.h:155
PWMDriver * pwmp
PWM driver that feed up to 4 dshot lines.
Definition: esc_dshot.h:169
Ready.
Definition: esc_dshot.h:71
DSHOT driver structure.
Definition: esc_dshot.h:228
const DSHOTConfig * config
DMA config associated with pwm timer.
Definition: esc_dshot.h:232
THD_WORKING_AREA(waDshotTlmRec, 512)
stack working area for dshot telemetry thread
uint8_t crc8
Definition: esc_dshot.h:136
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:342
SerialDriver * tlm_sd
if non null : dshot telemetry serial driver
Definition: esc_dshot.h:174
void dshotSendFrame(DSHOTDriver *driver)
send throttle order
Definition: esc_dshot.c:302
unsigned long uint32_t
Definition: types.h:18
volatile uint8_t currentTlmQry
Definition: esc_dshot.h:220
DshotPackets dshotMotors
Definition: esc_dshot.h:269
uint8_t dma_channel
: dma channel associated with pwm timer used to generate dshot output
Definition: esc_dshot.h:164
#define DSHOT_CHANNELS
Definition: esc_dshot.h:56
Transfert error.
Definition: esc_dshot.h:73
mailbox_t mb
mailbox for dshot telemetry thread
Definition: esc_dshot.h:257
uint32_t crc_errors
number of crc errors
Definition: esc_dshot.h:262
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:159
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
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:91
DMADriver dmap
DMA driver associated with pwm timer.
Definition: esc_dshot.h:247
DshotDmaBuffer * dma_buf
dshot dma buffer, sgould be defined in a non Dcached region
Definition: esc_dshot.h:179
DMAConfig dma_conf
DMA config associated with pwm timer.
Definition: esc_dshot.h:237
void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS])
send throttle packed order to all of the ESCs
Definition: esc_dshot.c:286
void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle)
prepare throttle order for specified ESC
Definition: esc_dshot.c:210
uint32_t dshotGetCrcErrorsCount(DSHOTDriver *driver)
return number of telemetry crc error since dshotStart
Definition: esc_dshot.c:329
uint16_t rawFrame
Definition: esc_dshot.h:214
volatile bool onGoingQry
Definition: esc_dshot.h:221
Stopped.
Definition: esc_dshot.h:70
msg_t _mbBuf[1]
mailbox buffer for dshot telemetry thread
Definition: esc_dshot.h:252
Structure representing a DMA driver.
#define DSHOT_DMA_BUFFER_SIZE
Definition: esc_dshot.h:52