diff --git a/radio/src/mixer_scheduler.cpp b/radio/src/mixer_scheduler.cpp index dcf95383b..fef9034c0 100644 --- a/radio/src/mixer_scheduler.cpp +++ b/radio/src/mixer_scheduler.cpp @@ -68,9 +68,13 @@ void mixerSchedulerSetPeriod(uint8_t moduleIdx, uint16_t periodUs) mixerSchedules[moduleIdx].period = periodUs; } -bool mixerSchedulerWaitForTrigger(uint8_t timeoutMs) +void mixerSchedulerClearTrigger() { RTOS_CLEAR_FLAG(mixerFlag); +} + +bool mixerSchedulerWaitForTrigger(uint8_t timeoutMs) +{ return RTOS_WAIT_FLAG(mixerFlag, timeoutMs); } diff --git a/radio/src/mixer_scheduler.h b/radio/src/mixer_scheduler.h index 601f3fcd7..75a6ad1aa 100644 --- a/radio/src/mixer_scheduler.h +++ b/radio/src/mixer_scheduler.h @@ -43,6 +43,9 @@ void mixerSchedulerResetTimer(); // Set the scheduling period for a given module void mixerSchedulerSetPeriod(uint8_t moduleIdx, uint16_t periodUs); +// Clear the flag before waiting +void mixerSchedulerClearTrigger(); + // Wait for the scheduler timer to trigger // returns true if timeout, false otherwise bool mixerSchedulerWaitForTrigger(uint8_t timeoutMs); @@ -66,6 +69,7 @@ void mixerSchedulerISRTrigger(); #define mixerSchedulerStop() #define mixerSchedulerResetTimer() #define mixerSchedulerSetPeriod(m,p) +#define mixerSchedulerClearTrigger() static inline bool mixerSchedulerWaitForTrigger(uint8_t timeout) { diff --git a/radio/src/tasks.cpp b/radio/src/tasks.cpp index 4878535fd..9beb3f195 100644 --- a/radio/src/tasks.cpp +++ b/radio/src/tasks.cpp @@ -140,21 +140,33 @@ TASK_FUNCTION(mixerTask) mixerSchedulerStart(); #endif + // clear the flag before first loop + mixerSchedulerClearTrigger(); + while (true) { - for (int timeout = 0; timeout < MIXER_MAX_PERIOD; timeout += MIXER_FREQUENT_ACTIONS_PERIOD) { - bool interruptedByTimeout = mixerSchedulerWaitForTrigger(MIXER_FREQUENT_ACTIONS_PERIOD); + int timeout = 0; + for (; timeout < MIXER_MAX_PERIOD; timeout += MIXER_FREQUENT_ACTIONS_PERIOD) { + + // run periodicals before waiting for the trigger + // to keep the delay short execMixerFrequentActions(); - if (!interruptedByTimeout) { + + // mixer flag triggered? + if (!mixerSchedulerWaitForTrigger(MIXER_FREQUENT_ACTIONS_PERIOD)) { break; } } + #if defined(DEBUG_MIXER_SCHEDULER) GPIO_SetBits(EXTMODULE_TX_GPIO, EXTMODULE_TX_GPIO_PIN); GPIO_ResetBits(EXTMODULE_TX_GPIO, EXTMODULE_TX_GPIO_PIN); #endif #if !defined(PCBSKY9X) + // clear the flag ASAP to avoid missing a tick + mixerSchedulerClearTrigger(); + // re-enable trigger mixerSchedulerEnableTrigger(); #endif