Skip to content

Commit 7504035

Browse files
committed
fix(motors): move DMA claim before DeInit/register touch
Local CodeRabbit review flagged: LL_EX_DMA_DeInit()/DMA_DeInit() (resets the DMA stream's registers) ran before the dmaAllocate() check in the OWNER_MOTOR path, and pwm_output_dshot.c had the same ordering issue in the OWNER_TIMUP burst path too. A failed claim on a stream already held by an unrelated owner would deinit it out from under that owner instead of leaving it untouched. Moved both claim checks (dshotDmaClaim() for OWNER_TIMUP, dmaAllocate() for OWNER_MOTOR) to before any DMA register access in both files, matching the ordering already established for LED strip and transponder.
1 parent c5a4958 commit 7504035

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

src/main/drivers/pwm_output_dshot.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,18 @@ void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t m
208208
motor->timer = &dmaMotorTimers[timerIndex];
209209
#ifdef USE_DSHOT_DMAR
210210
if (useBurstDshot) {
211+
if (!dshotDmaClaim(timerHardware->dmaTimUPIrqHandler, OWNER_TIMUP, timerGetTIMNumber(timerHardware->tim))) {
212+
return;
213+
}
214+
dmaEnable(timerHardware->dmaTimUPIrqHandler);
211215
motor->timer->dmaBurstRef = dmaRef;
212216
} else
213217
#endif
214218
{
219+
if (!dmaAllocate(timerHardware->dmaIrqHandler, OWNER_MOTOR, RESOURCE_INDEX(motorIndex))) {
220+
return;
221+
}
222+
dmaEnable(timerHardware->dmaIrqHandler);
215223
motor->timerDmaSource = timerDmaSource(timerHardware->channel);
216224
motor->timer->timerDmaSources &= ~motor->timerDmaSource;
217225
}
@@ -220,10 +228,6 @@ void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t m
220228
DMA_StructInit(&DMA_InitStructure);
221229
#ifdef USE_DSHOT_DMAR
222230
if (useBurstDshot) {
223-
if (!dshotDmaClaim(timerHardware->dmaTimUPIrqHandler, OWNER_TIMUP, timerGetTIMNumber(timerHardware->tim))) {
224-
return;
225-
}
226-
dmaEnable(timerHardware->dmaTimUPIrqHandler);
227231
dmaSetHandler(timerHardware->dmaTimUPIrqHandler, motor_DMA_IRQHandler, NVIC_BUILD_PRIORITY(1, 2), motorIndex);
228232
DMA_InitStructure.DMA_Channel = timerHardware->dmaTimUPChannel;
229233
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)motor->timer->dmaBurstBuffer;
@@ -243,10 +247,6 @@ void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t m
243247
} else
244248
#endif
245249
{
246-
if (!dmaAllocate(timerHardware->dmaIrqHandler, OWNER_MOTOR, RESOURCE_INDEX(motorIndex))) {
247-
return;
248-
}
249-
dmaEnable(timerHardware->dmaIrqHandler);
250250
dmaSetHandler(timerHardware->dmaIrqHandler, motor_DMA_IRQHandler, NVIC_BUILD_PRIORITY(1, 2), motorIndex);
251251
#if defined(STM32F4)
252252
DMA_InitStructure.DMA_Channel = timerHardware->dmaChannel;

src/main/drivers/pwm_output_dshot_hal.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t m
234234
} else
235235
#endif
236236
{
237+
if (!dmaAllocate(timerHardware->dmaIrqHandler, OWNER_MOTOR, RESOURCE_INDEX(motorIndex))) {
238+
return;
239+
}
240+
dmaEnable(timerHardware->dmaIrqHandler);
237241
motor->timerDmaSource = timerDmaSource(timerHardware->channel);
238242
motor->timer->timerDmaSources &= ~motor->timerDmaSource;
239243
}
@@ -253,10 +257,6 @@ void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t m
253257
} else
254258
#endif
255259
{
256-
if (!dmaAllocate(timerHardware->dmaIrqHandler, OWNER_MOTOR, RESOURCE_INDEX(motorIndex))) {
257-
return;
258-
}
259-
dmaEnable(timerHardware->dmaIrqHandler);
260260
dmaSetHandler(timerHardware->dmaIrqHandler, motor_DMA_IRQHandler, NVIC_BUILD_PRIORITY(1, 2), motorIndex);
261261
#if defined(STM32H7)
262262
dma_init.PeriphRequest = timerHardware->dmaChannel;

0 commit comments

Comments
 (0)