From dd5d1462e3f679c5664781440f0e05d185c400f3 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sat, 13 Sep 2014 13:02:53 -0500 Subject: [PATCH] auto-sync --- .../system/SingleTimerExecutor.cpp | 32 ++++++++++++------- .../controllers/system/SingleTimerExecutor.h | 1 + .../system/pwm_generator_logic.cpp | 32 +++++++++---------- firmware/controllers/system/scheduler.h | 1 + 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/firmware/controllers/system/SingleTimerExecutor.cpp b/firmware/controllers/system/SingleTimerExecutor.cpp index 6ebfa886dd..01ea0bf3d5 100644 --- a/firmware/controllers/system/SingleTimerExecutor.cpp +++ b/firmware/controllers/system/SingleTimerExecutor.cpp @@ -43,26 +43,31 @@ void Executor::unlock(void) { unlockAnyContext(); } -void Executor::schedule(const char *prefix, scheduling_s *scheduling, uint64_t nowUs, int delayUs, schfunc_t callback, void *param) { - if (delayUs < 0) { - firmwareError("Negative delayUs %s: %d", prefix, delayUs); - return; - } - if (delayUs == 0) { - callback(param); - return; - } +void Executor::schedule2(const char *prefix, scheduling_s *scheduling, uint64_t timeUs, schfunc_t callback, void *param) { +// if (delayUs < 0) { +// firmwareError("Negative delayUs %s: %d", prefix, delayUs); +// return; +// } +// if (delayUs == 0) { +// callback(param); +// return; +// } if (!reentrantLock) { // this would guard the queue and disable interrupts lock(); } - queue.insertTask(scheduling, nowUs + delayUs, callback, param); + queue.insertTask(scheduling, timeUs, callback, param); if (!reentrantLock) { - doExecute(nowUs); + doExecute(getTimeNowUs()); unlock(); } } + +void Executor::schedule(const char *prefix, scheduling_s *scheduling, uint64_t nowUs, int delayUs, schfunc_t callback, void *param) { + schedule2(prefix, scheduling, nowUs + delayUs, callback, param); +} + void Executor::execute(uint64_t nowUs) { lock(); doExecute(nowUs); @@ -108,6 +113,11 @@ void scheduleTask(const char *prefix, scheduling_s *scheduling, int delayUs, sch instance.schedule(prefix, scheduling, getTimeNowUs(), delayUs, callback, param); } +void scheduleTask2(const char *prefix, scheduling_s *scheduling, uint64_t time, schfunc_t callback, void *param) { + instance.schedule2(prefix, scheduling, time, callback, param); +} + + void initSignalExecutorImpl(void) { globalTimerCallback = executorCallback; #if EFI_PROD_CODE diff --git a/firmware/controllers/system/SingleTimerExecutor.h b/firmware/controllers/system/SingleTimerExecutor.h index c2aea04ec7..9a32e3ce90 100644 --- a/firmware/controllers/system/SingleTimerExecutor.h +++ b/firmware/controllers/system/SingleTimerExecutor.h @@ -15,6 +15,7 @@ class Executor { public: Executor(); void schedule(const char *prefix, scheduling_s *scheduling, uint64_t nowUs, int delayUs, schfunc_t callback, void *param); + void schedule2(const char *prefix, scheduling_s *scheduling, uint64_t timeUs, schfunc_t callback, void *param); void execute(uint64_t nowUs); private: EventQueue queue; diff --git a/firmware/controllers/system/pwm_generator_logic.cpp b/firmware/controllers/system/pwm_generator_logic.cpp index bc61f057f2..509dc00457 100644 --- a/firmware/controllers/system/pwm_generator_logic.cpp +++ b/firmware/controllers/system/pwm_generator_logic.cpp @@ -114,34 +114,34 @@ static uint64_t togglePwmState(PwmConfig *state) { scheduleMsg(&logger, "%s: nextSwitchTime %d", state->name, nextSwitchTime); #endif // signed value is needed here - int64_t timeToSwitch = nextSwitchTimeUs - getTimeNowUs(); - if (timeToSwitch < 1) { - /** - * We are here if we are late for a state transition. - * At 12000RPM=200Hz with a 60 toothed wheel we need to change state every - * 1000000 / 200 / 120 = ~41 uS. We are kind of OK. - * - * We are also here after a flash write. Flash write freezes the whole chip for a couple of seconds, - * so PWM generation and trigger simulation generation would have to recover from this time lag. - */ - //todo: introduce error and test this error handling warning(OBD_PCM_Processor_Fault, "PWM: negative switch time"); - timeToSwitch = 10; - } +// int64_t timeToSwitch = nextSwitchTimeUs - getTimeNowUs(); +// if (timeToSwitch < 1) { +// /** +// * We are here if we are late for a state transition. +// * At 12000RPM=200Hz with a 60 toothed wheel we need to change state every +// * 1000000 / 200 / 120 = ~41 uS. We are kind of OK. +// * +// * We are also here after a flash write. Flash write freezes the whole chip for a couple of seconds, +// * so PWM generation and trigger simulation generation would have to recover from this time lag. +// */ +// //todo: introduce error and test this error handling warning(OBD_PCM_Processor_Fault, "PWM: negative switch time"); +// timeToSwitch = 10; +// } state->safe.phaseIndex++; if (state->safe.phaseIndex == state->phaseCount) { state->safe.phaseIndex = 0; // restart state->safe.iteration++; } - return timeToSwitch; + return nextSwitchTimeUs; } /** * Main PWM loop: toggle pin & schedule next invocation */ static void timerCallback(PwmConfig *state) { - time_t timeToSleepUs = togglePwmState(state); - scheduleTask("pwm", &state->scheduling, timeToSleepUs, (schfunc_t) timerCallback, state); + uint64_t switchTimeUs = togglePwmState(state); + scheduleTask2("pwm", &state->scheduling, switchTimeUs, (schfunc_t) timerCallback, state); } /** diff --git a/firmware/controllers/system/scheduler.h b/firmware/controllers/system/scheduler.h index 4651924a44..cad451dcdb 100644 --- a/firmware/controllers/system/scheduler.h +++ b/firmware/controllers/system/scheduler.h @@ -30,6 +30,7 @@ extern "C" #endif /* __cplusplus */ void scheduleTask(const char *prefix, scheduling_s *scheduling, int delayUs, schfunc_t callback, void *param); +void scheduleTask2(const char *prefix, scheduling_s *scheduling, uint64_t time, schfunc_t callback, void *param); #ifdef __cplusplus }