Paparazzi UAS  v6.3_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>
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 {
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
117  DSHOT_CMD_MAX = 47
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
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 
void dshotStart(DSHOTDriver *driver, const DSHOTConfig *config)
Configures and activates the DSHOT peripheral.
Definition: esc_dshot.c:122
void dshotSetThrottle(DSHOTDriver *driver, const uint8_t index, const uint16_t throttle)
prepare throttle order for specified ESC
Definition: esc_dshot.c:210
#define DSHOT_CHANNELS
Definition: esc_dshot.h:56
uint32_t dshotGetCrcErrorsCount(DSHOTDriver *driver)
return number of telemetry crc error since dshotStart
Definition: esc_dshot.c:329
SerialDriver * tlm_sd
if non null : dshot telemetry serial driver
Definition: esc_dshot.h:174
volatile bool onGoingQry
Definition: esc_dshot.h:221
PWMDriver * pwmp
PWM driver that feed up to 4 dshot lines.
Definition: esc_dshot.h:169
dshotstate_t
Driver state machine possible states.
Definition: esc_dshot.h:68
@ DSHOT_UNINIT
Not initialized.
Definition: esc_dshot.h:69
@ DSHOT_ERROR
Transfert error.
Definition: esc_dshot.h:73
@ DSHOT_READY
Ready.
Definition: esc_dshot.h:71
@ DSHOT_STOP
Stopped.
Definition: esc_dshot.h:70
@ DSHOT_ONGOING_TELEMETRY_QUERY
Transfering.
Definition: esc_dshot.h:72
#define DSHOT_DMA_BUFFER_SIZE
Definition: esc_dshot.h:52
uint8_t dma_channel
: dma channel associated with pwm timer used to generate dshot output
Definition: esc_dshot.h:164
void dshotSendThrottles(DSHOTDriver *driver, const uint16_t throttles[DSHOT_CHANNELS])
send throttle packed order to all of the ESCs
Definition: esc_dshot.c:286
uint32_t dma_stream
: dma stream associated with pwm timer used to generate dshot output
Definition: esc_dshot.h:159
uint16_t rawFrame
Definition: esc_dshot.h:214
uint8_t crc8
Definition: esc_dshot.h:136
void dshotSendFrame(DSHOTDriver *driver)
send throttle order
Definition: esc_dshot.c:302
DshotDmaBuffer * dma_buf
dshot dma buffer, sgould be defined in a non Dcached region
Definition: esc_dshot.h:179
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
dshot_special_commands_t
DSHOT special commands (0-47) for KISS and BLHELI ESC.
Definition: esc_dshot.h:91
@ DSHOT_CMD_SPIN_DIRECTION_REVERSED
Definition: esc_dshot.h:106
@ DSHOT_CMD_SILENT_MODE_ON_OFF
Definition: esc_dshot.h:116
@ DSHOT_CMD_SAVE_SETTINGS
Definition: esc_dshot.h:104
@ DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF
Definition: esc_dshot.h:115
@ DSHOT_CMD_LED2_ON
Definition: esc_dshot.h:109
@ DSHOT_CMD_LED3_ON
Definition: esc_dshot.h:110
@ DSHOT_CMD_BEACON1
Definition: esc_dshot.h:93
@ DSHOT_CMD_BEACON5
Definition: esc_dshot.h:97
@ DSHOT_CMD_SETTINGS_REQUEST
Definition: esc_dshot.h:103
@ DSHOT_CMD_LED1_OFF
Definition: esc_dshot.h:112
@ DSHOT_CMD_BEACON4
Definition: esc_dshot.h:96
@ DSHOT_CMD_LED0_OFF
Definition: esc_dshot.h:111
@ DSHOT_CMD_SPIN_DIRECTION_NORMAL
Definition: esc_dshot.h:105
@ DSHOT_CMD_SPIN_DIRECTION_1
Definition: esc_dshot.h:99
@ DSHOT_CMD_SPIN_DIRECTION_2
Definition: esc_dshot.h:100
@ DSHOT_CMD_3D_MODE_OFF
Definition: esc_dshot.h:101
@ DSHOT_CMD_MOTOR_STOP
Definition: esc_dshot.h:92
@ DSHOT_CMD_LED1_ON
Definition: esc_dshot.h:108
@ DSHOT_CMD_BEACON3
Definition: esc_dshot.h:95
@ DSHOT_CMD_BEACON2
Definition: esc_dshot.h:94
@ DSHOT_CMD_LED3_OFF
Definition: esc_dshot.h:114
@ DSHOT_CMD_LED0_ON
Definition: esc_dshot.h:107
@ DSHOT_CMD_LED2_OFF
Definition: esc_dshot.h:113
@ DSHOT_CMD_ESC_INFO
Definition: esc_dshot.h:98
@ DSHOT_CMD_MAX
Definition: esc_dshot.h:117
@ DSHOT_CMD_3D_MODE_ON
Definition: esc_dshot.h:102
const DshotTelemetry * dshotGetTelemetry(const DSHOTDriver *driver, const uint32_t index)
return last received telemetry data
Definition: esc_dshot.c:342
volatile uint8_t currentTlmQry
Definition: esc_dshot.h:220
DSHOT Driver configuration structure.
Definition: esc_dshot.h:155
telemetry packed as sent by some KISS ESC
Definition: esc_dshot.h:125
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:228
uint32_t crc_errors
number of crc errors
Definition: esc_dshot.h:262
DshotPackets dshotMotors
Definition: esc_dshot.h:269
mailbox_t mb
mailbox for dshot telemetry thread
Definition: esc_dshot.h:257
DMADriver dmap
DMA driver associated with pwm timer.
Definition: esc_dshot.h:247
PWMConfig pwm_conf
PWM config associated with pwm timer.
Definition: esc_dshot.h:242
const DSHOTConfig * config
DMA config associated with pwm timer.
Definition: esc_dshot.h:232
msg_t _mbBuf[1]
mailbox buffer for dshot telemetry thread
Definition: esc_dshot.h:252
THD_WORKING_AREA(waDshotTlmRec, 512)
stack working area for dshot telemetry thread
DMAConfig dma_conf
DMA config associated with pwm timer.
Definition: esc_dshot.h:237
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