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)
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/>.
25 #include "drivers/dshot.h"
26 #include "drivers/motor.h"
28 // Timer clock frequency for the dshot speeds
29 #define MOTOR_DSHOT600_HZ MHZ_TO_HZ(12)
30 #define MOTOR_DSHOT300_HZ MHZ_TO_HZ(6)
31 #define MOTOR_DSHOT150_HZ MHZ_TO_HZ(3)
33 // These three constants are times in timer clock ticks, e.g. with a 6 MHz clock 20 ticks for bitlength = 300kHz bit rate
35 #define MOTOR_BIT_1 14
36 #define MOTOR_BITLENGTH 20
38 #define MOTOR_PROSHOT1000_HZ MHZ_TO_HZ(24)
39 #define PROSHOT_BASE_SYMBOL 24 // 1uS
40 #define PROSHOT_BIT_WIDTH 3
41 #define MOTOR_NIBBLE_LENGTH_PROSHOT (PROSHOT_BASE_SYMBOL * 4) // 4uS
43 #define DSHOT_TELEMETRY_DEADTIME_US (30 + 5) // 30 to switch lines and 5 to switch lines back
46 typedef uint8_t loadDmaBufferFn(uint32_t *dmaBuffer
, int stride
, uint16_t packet
); // function pointer used to encode a digital motor value into the DMA buffer representation
47 extern FAST_DATA_ZERO_INIT loadDmaBufferFn
*loadDmaBuffer
;
48 uint8_t loadDmaBufferDshot(uint32_t *dmaBuffer
, int stride
, uint16_t packet
);
49 uint8_t loadDmaBufferProshot(uint32_t *dmaBuffer
, int stride
, uint16_t packet
);
51 uint32_t getDshotHz(motorPwmProtocolTypes_e pwmProtocolType
);
53 struct motorDevConfig_s
;
54 motorDevice_t
*dshotPwmDevInit(const struct motorDevConfig_s
*motorConfig
, uint16_t idlePulse
, uint8_t motorCount
, bool useUnsyncedPwm
);
56 /* Motor DMA related, moved from pwm_output.h */
58 #define MAX_DMA_TIMERS 8
60 #define DSHOT_DMA_BUFFER_SIZE 18 /* resolution + frame reset (2us) */
61 #define PROSHOT_DMA_BUFFER_SIZE 6 /* resolution + frame reset (2us) */
63 #define GCR_TELEMETRY_INPUT_LEN MAX_GCR_EDGES
65 // For H7, DMA buffer is placed in a dedicated segment for coherency management
66 // XXX Do we need any special qualifier for AT32?
68 #define DSHOT_DMA_BUFFER_ATTRIBUTE DMA_RAM
69 #elif defined(STM32G4)
70 #define DSHOT_DMA_BUFFER_ATTRIBUTE DMA_RAM_W
71 #elif defined(STM32F7)
72 #define DSHOT_DMA_BUFFER_ATTRIBUTE FAST_DATA_ZERO_INIT
74 #define DSHOT_DMA_BUFFER_ATTRIBUTE // None
77 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4) || defined(AT32F435) || defined(APM32F4)
78 #define DSHOT_DMA_BUFFER_UNIT uint32_t
80 #define DSHOT_DMA_BUFFER_UNIT uint8_t
83 #ifdef USE_DSHOT_TELEMETRY
84 STATIC_ASSERT(GCR_TELEMETRY_INPUT_LEN
>= DSHOT_DMA_BUFFER_SIZE
, dshotBufferSizeConstrait
);
85 #define DSHOT_DMA_BUFFER_ALLOC_SIZE GCR_TELEMETRY_INPUT_LEN
87 #define DSHOT_DMA_BUFFER_ALLOC_SIZE DSHOT_DMA_BUFFER_SIZE
90 extern DSHOT_DMA_BUFFER_UNIT dshotDmaBuffer
[MAX_SUPPORTED_MOTORS
][DSHOT_DMA_BUFFER_ALLOC_SIZE
];
91 extern DSHOT_DMA_BUFFER_UNIT dshotDmaInputBuffer
[MAX_SUPPORTED_MOTORS
][DSHOT_DMA_BUFFER_ALLOC_SIZE
];
94 extern DSHOT_DMA_BUFFER_UNIT dshotBurstDmaBuffer
[MAX_DMA_TIMERS
][DSHOT_DMA_BUFFER_SIZE
* 4];
99 #if defined(USE_DSHOT)
100 uint16_t outputPeriod
;
101 #if defined(USE_DSHOT_DMAR)
102 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
103 TIM_HandleTypeDef timHandle
;
104 DMA_HandleTypeDef hdma_tim
;
106 dmaResource_t
*dmaBurstRef
;
107 uint16_t dmaBurstLength
;
108 uint32_t *dmaBurstBuffer
;
111 uint16_t timerDmaSources
;
114 typedef struct motorDmaOutput_s
{
115 dshotProtocolControl_t protocolControl
;
117 const timerHardware_t
*timerHardware
;
119 uint16_t timerDmaSource
;
120 uint8_t timerDmaIndex
;
122 #if defined(STM32H7) || defined(STM32G4)
123 TIM_HandleTypeDef TimHandle
;
124 DMA_HandleTypeDef hdma_tim
;
131 #if defined(USE_HAL_DRIVER) && defined(USE_FULL_LL_DRIVER)
132 LL_DMA_InitTypeDef dmaInitStruct
;
135 DMA_InitTypeDef dmaInitStruct
;
138 #ifdef USE_DSHOT_TELEMETRY
139 volatile bool isInput
;
140 timeDelta_t dshotTelemetryDeadtimeUs
;
143 #ifdef USE_HAL_DRIVER
144 LL_TIM_OC_InitTypeDef ocInitStruct
;
145 LL_TIM_IC_InitTypeDef icInitStruct
;
147 TIM_OCInitTypeDef ocInitStruct
;
148 TIM_ICInitTypeDef icInitStruct
;
151 #endif // USE_DSHOT_TELEMETRY
153 dmaResource_t
*dmaRef
;
156 motorDmaTimer_t
*timer
;
157 DSHOT_DMA_BUFFER_UNIT
*dmaBuffer
;
160 motorDmaOutput_t
*getMotorDmaOutput(uint8_t index
);
162 void pwmWriteDshotInt(uint8_t index
, uint16_t value
);
163 bool pwmDshotMotorHardwareConfig(const timerHardware_t
*timerHardware
, uint8_t motorIndex
, uint8_t reorderedMotorIndex
, motorPwmProtocolTypes_e pwmProtocolType
, uint8_t output
);
164 #ifdef USE_DSHOT_TELEMETRY
165 bool pwmTelemetryDecode(void);
167 void pwmCompleteDshotMotorUpdate(void);
169 extern bool useBurstDshot
;
171 extern motorDevice_t dshotPwmDevice
;