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
45 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
46 extern FAST_DATA_ZERO_INIT loadDmaBufferFn
*loadDmaBuffer
;
47 uint8_t loadDmaBufferDshot(uint32_t *dmaBuffer
, int stride
, uint16_t packet
);
48 uint8_t loadDmaBufferProshot(uint32_t *dmaBuffer
, int stride
, uint16_t packet
);
50 uint32_t getDshotHz(motorPwmProtocolTypes_e pwmProtocolType
);
52 struct motorDevConfig_s
;
53 motorDevice_t
*dshotPwmDevInit(const struct motorDevConfig_s
*motorConfig
, uint16_t idlePulse
, uint8_t motorCount
, bool useUnsyncedPwm
);
55 /* Motor DMA related, moved from pwm_output.h */
57 #define MAX_DMA_TIMERS 8
59 #define DSHOT_DMA_BUFFER_SIZE 18 /* resolution + frame reset (2us) */
60 #define PROSHOT_DMA_BUFFER_SIZE 6 /* resolution + frame reset (2us) */
62 #define GCR_TELEMETRY_INPUT_LEN MAX_GCR_EDGES
64 // For H7, DMA buffer is placed in a dedicated segment for coherency management
65 // XXX Do we need any special qualifier for AT32?
67 #define DSHOT_DMA_BUFFER_ATTRIBUTE DMA_RAM
68 #elif defined(STM32G4)
69 #define DSHOT_DMA_BUFFER_ATTRIBUTE DMA_RAM_W
70 #elif defined(STM32F7)
71 #define DSHOT_DMA_BUFFER_ATTRIBUTE FAST_DATA_ZERO_INIT
73 #define DSHOT_DMA_BUFFER_ATTRIBUTE // None
76 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4) || defined(AT32F435) || defined(APM32F4)
77 #define DSHOT_DMA_BUFFER_UNIT uint32_t
79 #define DSHOT_DMA_BUFFER_UNIT uint8_t
82 #ifdef USE_DSHOT_TELEMETRY
83 STATIC_ASSERT(GCR_TELEMETRY_INPUT_LEN
>= DSHOT_DMA_BUFFER_SIZE
, dshotBufferSizeConstrait
);
84 #define DSHOT_DMA_BUFFER_ALLOC_SIZE GCR_TELEMETRY_INPUT_LEN
86 #define DSHOT_DMA_BUFFER_ALLOC_SIZE DSHOT_DMA_BUFFER_SIZE
89 extern DSHOT_DMA_BUFFER_UNIT dshotDmaBuffer
[MAX_SUPPORTED_MOTORS
][DSHOT_DMA_BUFFER_ALLOC_SIZE
];
90 extern DSHOT_DMA_BUFFER_UNIT dshotDmaInputBuffer
[MAX_SUPPORTED_MOTORS
][DSHOT_DMA_BUFFER_ALLOC_SIZE
];
93 extern DSHOT_DMA_BUFFER_UNIT dshotBurstDmaBuffer
[MAX_DMA_TIMERS
][DSHOT_DMA_BUFFER_SIZE
* 4];
98 #if defined(USE_DSHOT)
99 uint16_t outputPeriod
;
100 #if defined(USE_DSHOT_DMAR)
101 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
102 TIM_HandleTypeDef timHandle
;
103 DMA_HandleTypeDef hdma_tim
;
105 dmaResource_t
*dmaBurstRef
;
106 uint16_t dmaBurstLength
;
107 uint32_t *dmaBurstBuffer
;
110 uint16_t timerDmaSources
;
113 typedef struct motorDmaOutput_s
{
114 dshotProtocolControl_t protocolControl
;
116 const timerHardware_t
*timerHardware
;
118 uint16_t timerDmaSource
;
119 uint8_t timerDmaIndex
;
121 #if defined(STM32H7) || defined(STM32G4)
122 TIM_HandleTypeDef TimHandle
;
123 DMA_HandleTypeDef hdma_tim
;
130 #if defined(USE_HAL_DRIVER) && defined(USE_FULL_LL_DRIVER)
131 LL_DMA_InitTypeDef dmaInitStruct
;
134 DMA_InitTypeDef dmaInitStruct
;
137 #ifdef USE_DSHOT_TELEMETRY
138 volatile bool isInput
;
139 timeDelta_t dshotTelemetryDeadtimeUs
;
142 #ifdef USE_HAL_DRIVER
143 LL_TIM_OC_InitTypeDef ocInitStruct
;
144 LL_TIM_IC_InitTypeDef icInitStruct
;
146 TIM_OCInitTypeDef ocInitStruct
;
147 TIM_ICInitTypeDef icInitStruct
;
150 #endif // USE_DSHOT_TELEMETRY
152 dmaResource_t
*dmaRef
;
155 motorDmaTimer_t
*timer
;
156 DSHOT_DMA_BUFFER_UNIT
*dmaBuffer
;
159 motorDmaOutput_t
*getMotorDmaOutput(uint8_t index
);
161 void pwmWriteDshotInt(uint8_t index
, uint16_t value
);
162 bool pwmDshotMotorHardwareConfig(const timerHardware_t
*timerHardware
, uint8_t motorIndex
, uint8_t reorderedMotorIndex
, motorPwmProtocolTypes_e pwmProtocolType
, uint8_t output
);
163 #ifdef USE_DSHOT_TELEMETRY
164 bool pwmTelemetryDecode(void);
166 void pwmCompleteDshotMotorUpdate(void);
168 extern bool useBurstDshot
;
170 extern motorDevice_t dshotPwmDevice
;