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 #define MOTOR_DSHOT600_HZ MHZ_TO_HZ(12)
29 #define MOTOR_DSHOT300_HZ MHZ_TO_HZ(6)
30 #define MOTOR_DSHOT150_HZ MHZ_TO_HZ(3)
33 #define MOTOR_BIT_1 14
34 #define MOTOR_BITLENGTH 20
36 #define MOTOR_PROSHOT1000_HZ MHZ_TO_HZ(24)
37 #define PROSHOT_BASE_SYMBOL 24 // 1uS
38 #define PROSHOT_BIT_WIDTH 3
39 #define MOTOR_NIBBLE_LENGTH_PROSHOT (PROSHOT_BASE_SYMBOL * 4) // 4uS
41 #define DSHOT_TELEMETRY_DEADTIME_US (30 + 5) // 30 to switch lines and 5 to switch lines back
44 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
45 extern FAST_DATA_ZERO_INIT loadDmaBufferFn
*loadDmaBuffer
;
46 uint8_t loadDmaBufferDshot(uint32_t *dmaBuffer
, int stride
, uint16_t packet
);
47 uint8_t loadDmaBufferProshot(uint32_t *dmaBuffer
, int stride
, uint16_t packet
);
49 uint32_t getDshotHz(motorPwmProtocolTypes_e pwmProtocolType
);
51 struct motorDevConfig_s
;
52 motorDevice_t
*dshotPwmDevInit(const struct motorDevConfig_s
*motorConfig
, uint16_t idlePulse
, uint8_t motorCount
, bool useUnsyncedPwm
);
54 /* Motor DMA related, moved from pwm_output.h */
56 #define MAX_DMA_TIMERS 8
58 #define DSHOT_DMA_BUFFER_SIZE 18 /* resolution + frame reset (2us) */
59 #define PROSHOT_DMA_BUFFER_SIZE 6 /* resolution + frame reset (2us) */
61 #define GCR_TELEMETRY_INPUT_LEN MAX_GCR_EDGES
63 // For H7, DMA buffer is placed in a dedicated segment for coherency management
65 #define DSHOT_DMA_BUFFER_ATTRIBUTE DMA_RAM
66 #elif defined(STM32G4)
67 #define DSHOT_DMA_BUFFER_ATTRIBUTE DMA_RAM_W
68 #elif defined(STM32F7)
69 #define DSHOT_DMA_BUFFER_ATTRIBUTE FAST_DATA_ZERO_INIT
71 #define DSHOT_DMA_BUFFER_ATTRIBUTE // None
74 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
75 #define DSHOT_DMA_BUFFER_UNIT uint32_t
77 #define DSHOT_DMA_BUFFER_UNIT uint8_t
80 #ifdef USE_DSHOT_TELEMETRY
81 STATIC_ASSERT(GCR_TELEMETRY_INPUT_LEN
>= DSHOT_DMA_BUFFER_SIZE
, dshotBufferSizeConstrait
);
82 #define DSHOT_DMA_BUFFER_ALLOC_SIZE GCR_TELEMETRY_INPUT_LEN
84 #define DSHOT_DMA_BUFFER_ALLOC_SIZE DSHOT_DMA_BUFFER_SIZE
87 extern DSHOT_DMA_BUFFER_UNIT dshotDmaBuffer
[MAX_SUPPORTED_MOTORS
][DSHOT_DMA_BUFFER_ALLOC_SIZE
];
88 extern DSHOT_DMA_BUFFER_UNIT dshotDmaInputBuffer
[MAX_SUPPORTED_MOTORS
][DSHOT_DMA_BUFFER_ALLOC_SIZE
];
91 extern DSHOT_DMA_BUFFER_UNIT dshotBurstDmaBuffer
[MAX_DMA_TIMERS
][DSHOT_DMA_BUFFER_SIZE
* 4];
96 #if defined(USE_DSHOT)
97 uint16_t outputPeriod
;
98 #if defined(USE_DSHOT_DMAR)
99 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
100 TIM_HandleTypeDef timHandle
;
101 DMA_HandleTypeDef hdma_tim
;
103 dmaResource_t
*dmaBurstRef
;
104 uint16_t dmaBurstLength
;
105 uint32_t *dmaBurstBuffer
;
108 uint16_t timerDmaSources
;
111 typedef struct motorDmaOutput_s
{
112 dshotProtocolControl_t protocolControl
;
114 const timerHardware_t
*timerHardware
;
116 uint16_t timerDmaSource
;
117 uint8_t timerDmaIndex
;
119 #if defined(STM32H7) || defined(STM32G4)
120 TIM_HandleTypeDef TimHandle
;
121 DMA_HandleTypeDef hdma_tim
;
128 #if defined(USE_HAL_DRIVER) && defined(USE_FULL_LL_DRIVER)
129 LL_DMA_InitTypeDef dmaInitStruct
;
132 DMA_InitTypeDef dmaInitStruct
;
135 #ifdef USE_DSHOT_TELEMETRY
136 volatile bool isInput
;
137 timeDelta_t dshotTelemetryDeadtimeUs
;
140 #ifdef USE_HAL_DRIVER
141 LL_TIM_OC_InitTypeDef ocInitStruct
;
142 LL_TIM_IC_InitTypeDef icInitStruct
;
144 TIM_OCInitTypeDef ocInitStruct
;
145 TIM_ICInitTypeDef icInitStruct
;
148 #endif // USE_DSHOT_TELEMETRY
150 dmaResource_t
*dmaRef
;
153 motorDmaTimer_t
*timer
;
154 DSHOT_DMA_BUFFER_UNIT
*dmaBuffer
;
157 motorDmaOutput_t
*getMotorDmaOutput(uint8_t index
);
159 void pwmWriteDshotInt(uint8_t index
, uint16_t value
);
160 bool pwmDshotMotorHardwareConfig(const timerHardware_t
*timerHardware
, uint8_t motorIndex
, uint8_t reorderedMotorIndex
, motorPwmProtocolTypes_e pwmProtocolType
, uint8_t output
);
161 #ifdef USE_DSHOT_TELEMETRY
162 bool pwmTelemetryDecode(void);
164 void pwmCompleteDshotMotorUpdate(void);
166 extern bool useBurstDshot
;
168 extern motorDevice_t dshotPwmDevice
;