./src/utils/ trim trailing whitestpaces (#14082)
[betaflight.git] / src / main / drivers / dshot_dpwm.h
blob1c6a7400fb1d0fb01ef0ae569724a35ee2d42ba0
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/>.
20 * Author: jflyper
23 #pragma once
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
34 #define MOTOR_BIT_0 7
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?
66 #if defined(STM32H7)
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
72 #else
73 #define DSHOT_DMA_BUFFER_ATTRIBUTE // None
74 #endif
76 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4) || defined(AT32F435) || defined(APM32F4)
77 #define DSHOT_DMA_BUFFER_UNIT uint32_t
78 #else
79 #define DSHOT_DMA_BUFFER_UNIT uint8_t
80 #endif
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
85 #else
86 #define DSHOT_DMA_BUFFER_ALLOC_SIZE DSHOT_DMA_BUFFER_SIZE
87 #endif
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];
92 #ifdef USE_DSHOT_DMAR
93 extern DSHOT_DMA_BUFFER_UNIT dshotBurstDmaBuffer[MAX_DMA_TIMERS][DSHOT_DMA_BUFFER_SIZE * 4];
94 #endif
96 typedef struct {
97 TIM_TypeDef *timer;
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;
104 #endif
105 dmaResource_t *dmaBurstRef;
106 uint16_t dmaBurstLength;
107 uint32_t *dmaBurstBuffer;
108 #endif
109 #endif
110 uint16_t timerDmaSources;
111 } motorDmaTimer_t;
113 typedef struct motorDmaOutput_s {
114 dshotProtocolControl_t protocolControl;
115 ioTag_t ioTag;
116 const timerHardware_t *timerHardware;
117 #ifdef USE_DSHOT
118 uint16_t timerDmaSource;
119 uint8_t timerDmaIndex;
120 bool configured;
121 #if defined(STM32H7) || defined(STM32G4)
122 TIM_HandleTypeDef TimHandle;
123 DMA_HandleTypeDef hdma_tim;
124 IO_t io;
125 #endif
126 uint8_t output;
127 uint8_t index;
128 uint32_t iocfg;
130 #if defined(USE_HAL_DRIVER) && defined(USE_FULL_LL_DRIVER)
131 LL_DMA_InitTypeDef dmaInitStruct;
132 uint32_t llChannel;
133 #else
134 DMA_InitTypeDef dmaInitStruct;
135 #endif
137 #ifdef USE_DSHOT_TELEMETRY
138 volatile bool isInput;
139 timeDelta_t dshotTelemetryDeadtimeUs;
140 uint8_t dmaInputLen;
142 #ifdef USE_HAL_DRIVER
143 LL_TIM_OC_InitTypeDef ocInitStruct;
144 LL_TIM_IC_InitTypeDef icInitStruct;
145 #else
146 TIM_OCInitTypeDef ocInitStruct;
147 TIM_ICInitTypeDef icInitStruct;
148 #endif
150 #endif // USE_DSHOT_TELEMETRY
152 dmaResource_t *dmaRef;
153 #endif // USE_DSHOT
155 motorDmaTimer_t *timer;
156 DSHOT_DMA_BUFFER_UNIT *dmaBuffer;
157 } motorDmaOutput_t;
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);
165 #endif
166 void pwmCompleteDshotMotorUpdate(void);
168 extern bool useBurstDshot;
170 extern motorDevice_t dshotPwmDevice;