Merge pull request #11494 from haslinghuis/dshot_gpio
[betaflight.git] / src / main / drivers / dshot.h
blob15f665be1c2177acad27b75ac3801c8f88cf9379
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #pragma once
23 #include "common/time.h"
25 #include "pg/motor.h"
27 #define DSHOT_MIN_THROTTLE 48
28 #define DSHOT_MAX_THROTTLE 2047
29 #define DSHOT_3D_FORWARD_MIN_THROTTLE 1048
30 #define DSHOT_RANGE (DSHOT_MAX_THROTTLE - DSHOT_MIN_THROTTLE)
32 #define MIN_GCR_EDGES 7
33 #define MAX_GCR_EDGES 22
35 // comment out to see frame dump of corrupted frames in dshot_telemetry_info
36 //#define DEBUG_BBDECODE
38 #ifdef USE_DSHOT_TELEMETRY_STATS
39 #define DSHOT_TELEMETRY_QUALITY_WINDOW 1 // capture a rolling 1 second of packet stats
40 #define DSHOT_TELEMETRY_QUALITY_BUCKET_MS 100 // determines the granularity of the stats and the overall number of rolling buckets
41 #define DSHOT_TELEMETRY_QUALITY_BUCKET_COUNT (DSHOT_TELEMETRY_QUALITY_WINDOW * 1000 / DSHOT_TELEMETRY_QUALITY_BUCKET_MS)
43 typedef struct dshotTelemetryQuality_s {
44 uint32_t packetCountSum;
45 uint32_t invalidCountSum;
46 uint32_t packetCountArray[DSHOT_TELEMETRY_QUALITY_BUCKET_COUNT];
47 uint32_t invalidCountArray[DSHOT_TELEMETRY_QUALITY_BUCKET_COUNT];
48 uint8_t lastBucketIndex;
49 } dshotTelemetryQuality_t;
51 extern dshotTelemetryQuality_t dshotTelemetryQuality[MAX_SUPPORTED_MOTORS];
52 #endif // USE_DSHOT_TELEMETRY_STATS
54 typedef struct dshotProtocolControl_s {
55 uint16_t value;
56 bool requestTelemetry;
57 } dshotProtocolControl_t;
59 void dshotInitEndpoints(const motorConfig_t *motorConfig, float outputLimit, float *outputLow, float *outputHigh, float *disarm, float *deadbandMotor3dHigh, float *deadbandMotor3dLow);
60 float dshotConvertFromExternal(uint16_t externalValue);
61 uint16_t dshotConvertToExternal(float motorValue);
63 uint16_t prepareDshotPacket(dshotProtocolControl_t *pcb);
65 #ifdef USE_DSHOT_TELEMETRY
66 extern bool useDshotTelemetry;
68 typedef struct dshotTelemetryMotorState_s {
69 uint16_t telemetryValue;
70 bool telemetryActive;
71 } dshotTelemetryMotorState_t;
74 typedef struct dshotTelemetryState_s {
75 bool useDshotTelemetry;
76 uint32_t invalidPacketCount;
77 uint32_t readCount;
78 dshotTelemetryMotorState_t motorState[MAX_SUPPORTED_MOTORS];
79 uint32_t inputBuffer[MAX_GCR_EDGES];
80 } dshotTelemetryState_t;
82 extern dshotTelemetryState_t dshotTelemetryState;
84 #ifdef USE_DSHOT_TELEMETRY_STATS
85 void updateDshotTelemetryQuality(dshotTelemetryQuality_t *qualityStats, bool packetValid, timeMs_t currentTimeMs);
86 #endif
87 #endif
89 uint16_t getDshotTelemetry(uint8_t index);
90 bool isDshotMotorTelemetryActive(uint8_t motorIndex);
91 bool isDshotTelemetryActive(void);
93 int16_t getDshotTelemetryMotorInvalidPercent(uint8_t motorIndex);
95 void validateAndfixMotorOutputReordering(uint8_t *array, const unsigned size);