Skip to content

Commit

Permalink
fix: prevent endless loop with SBUS trainer on model change (#2515)
Browse files Browse the repository at this point in the history
* fix: prevent generic module de-init if protocol is NONE

Fixes #1898

De-initialising the external module while SBUS trainer is used on external module RX leads to the following sequence:
- `extmoduleStop()` is called, thus shutting down the timer DMA
- on certain targets, the timer DMA stream is the same as the external module RX DMA stream
- when this DMA stream is used by the SBUS trainer input, this leads to DMAFifo to return random bytes continuously
- thus driving `processSbusInput()` into an endless loop, which is run in the mixer task
- as the mixer task has the highest priority, it runs forever
- after some time the watchdog timer expires, and causes a reboot

* DMAFifo: check if the stream is enabled

This provides some hardening to avoid the conditions described in previous commit.
  • Loading branch information
raphaelcoeffic authored Oct 12, 2022
1 parent 0d5ef0b commit 655965d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion radio/src/dmafifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DMAFifo
#if defined(SIMU)
return true;
#endif
return (ridx == N - stream->NDTR);
return !(stream->CR & DMA_SxCR_EN) || (ridx == N - stream->NDTR);
}

bool pop(uint8_t & element)
Expand Down
13 changes: 8 additions & 5 deletions radio/src/pulses/pulses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ bool setupPulsesInternalModule(uint8_t protocol)

void stopPulsesInternalModule()
{
if (moduleState[INTERNAL_MODULE].protocol != PROTOCOL_CHANNELS_UNINITIALIZED) {
auto& proto = moduleState[INTERNAL_MODULE].protocol;
if (proto != PROTOCOL_CHANNELS_UNINITIALIZED &&
proto != PROTOCOL_CHANNELS_NONE) {
if (internalModuleDriver) {
internalModuleDriver->deinit(internalModuleContext);
internalModuleDriver = nullptr;
Expand All @@ -467,7 +469,7 @@ void stopPulsesInternalModule()
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
intmoduleStop();
}
moduleState[INTERNAL_MODULE].protocol = PROTOCOL_CHANNELS_NONE;
proto = PROTOCOL_CHANNELS_NONE;
}
}

Expand Down Expand Up @@ -766,8 +768,9 @@ void extmoduleSendNextFrame()

void stopPulsesExternalModule()
{
if (moduleState[EXTERNAL_MODULE].protocol !=
PROTOCOL_CHANNELS_UNINITIALIZED) {
auto& proto = moduleState[EXTERNAL_MODULE].protocol;
if (proto != PROTOCOL_CHANNELS_UNINITIALIZED &&
proto != PROTOCOL_CHANNELS_NONE) {
if (externalModuleDriver) {
externalModuleDriver->deinit(externalModuleContext);
externalModuleDriver = nullptr;
Expand All @@ -776,7 +779,7 @@ void stopPulsesExternalModule()
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
extmoduleStop();
}
moduleState[EXTERNAL_MODULE].protocol = PROTOCOL_CHANNELS_NONE;
proto = PROTOCOL_CHANNELS_NONE;
}
}

Expand Down

0 comments on commit 655965d

Please sign in to comment.