Merge pull request #11939 from blckmn/flash-fix
[betaflight.git] / src / main / drivers / dshot_dpwm.h
blobdc1b19007211d1124a2f6ebec867874dc2bcadcf
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 #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)
32 #define MOTOR_BIT_0 7
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
64 #if defined(STM32H7)
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
70 #else
71 #define DSHOT_DMA_BUFFER_ATTRIBUTE // None
72 #endif
74 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
75 #define DSHOT_DMA_BUFFER_UNIT uint32_t
76 #else
77 #define DSHOT_DMA_BUFFER_UNIT uint8_t
78 #endif
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
83 #else
84 #define DSHOT_DMA_BUFFER_ALLOC_SIZE DSHOT_DMA_BUFFER_SIZE
85 #endif
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];
90 #ifdef USE_DSHOT_DMAR
91 extern DSHOT_DMA_BUFFER_UNIT dshotBurstDmaBuffer[MAX_DMA_TIMERS][DSHOT_DMA_BUFFER_SIZE * 4];
92 #endif
94 typedef struct {
95 TIM_TypeDef *timer;
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;
102 #endif
103 dmaResource_t *dmaBurstRef;
104 uint16_t dmaBurstLength;
105 uint32_t *dmaBurstBuffer;
106 #endif
107 #endif
108 uint16_t timerDmaSources;
109 } motorDmaTimer_t;
111 typedef struct motorDmaOutput_s {
112 dshotProtocolControl_t protocolControl;
113 ioTag_t ioTag;
114 const timerHardware_t *timerHardware;
115 #ifdef USE_DSHOT
116 uint16_t timerDmaSource;
117 uint8_t timerDmaIndex;
118 bool configured;
119 #if defined(STM32H7) || defined(STM32G4)
120 TIM_HandleTypeDef TimHandle;
121 DMA_HandleTypeDef hdma_tim;
122 IO_t io;
123 #endif
124 uint8_t output;
125 uint8_t index;
126 uint32_t iocfg;
128 #if defined(USE_HAL_DRIVER) && defined(USE_FULL_LL_DRIVER)
129 LL_DMA_InitTypeDef dmaInitStruct;
130 uint32_t llChannel;
131 #else
132 DMA_InitTypeDef dmaInitStruct;
133 #endif
135 #ifdef USE_DSHOT_TELEMETRY
136 volatile bool isInput;
137 timeDelta_t dshotTelemetryDeadtimeUs;
138 uint8_t dmaInputLen;
140 #ifdef USE_HAL_DRIVER
141 LL_TIM_OC_InitTypeDef ocInitStruct;
142 LL_TIM_IC_InitTypeDef icInitStruct;
143 #else
144 TIM_OCInitTypeDef ocInitStruct;
145 TIM_ICInitTypeDef icInitStruct;
146 #endif
148 #endif // USE_DSHOT_TELEMETRY
150 dmaResource_t *dmaRef;
151 #endif // USE_DSHOT
153 motorDmaTimer_t *timer;
154 DSHOT_DMA_BUFFER_UNIT *dmaBuffer;
155 } motorDmaOutput_t;
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 pwmStartDshotMotorUpdate(void);
163 #endif
164 void pwmCompleteDshotMotorUpdate(void);
166 extern bool useBurstDshot;
168 extern motorDevice_t dshotPwmDevice;