auto-sync

This commit is contained in:
rusEfi 2014-09-13 13:02:53 -05:00
parent 88e4a07234
commit dd5d1462e3
4 changed files with 39 additions and 27 deletions

View File

@ -43,26 +43,31 @@ void Executor::unlock(void) {
unlockAnyContext(); unlockAnyContext();
} }
void Executor::schedule(const char *prefix, scheduling_s *scheduling, uint64_t nowUs, int delayUs, schfunc_t callback, void *param) { void Executor::schedule2(const char *prefix, scheduling_s *scheduling, uint64_t timeUs, schfunc_t callback, void *param) {
if (delayUs < 0) { // if (delayUs < 0) {
firmwareError("Negative delayUs %s: %d", prefix, delayUs); // firmwareError("Negative delayUs %s: %d", prefix, delayUs);
return; // return;
} // }
if (delayUs == 0) { // if (delayUs == 0) {
callback(param); // callback(param);
return; // return;
} // }
if (!reentrantLock) { if (!reentrantLock) {
// this would guard the queue and disable interrupts // this would guard the queue and disable interrupts
lock(); lock();
} }
queue.insertTask(scheduling, nowUs + delayUs, callback, param); queue.insertTask(scheduling, timeUs, callback, param);
if (!reentrantLock) { if (!reentrantLock) {
doExecute(nowUs); doExecute(getTimeNowUs());
unlock(); 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) { void Executor::execute(uint64_t nowUs) {
lock(); lock();
doExecute(nowUs); 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); 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) { void initSignalExecutorImpl(void) {
globalTimerCallback = executorCallback; globalTimerCallback = executorCallback;
#if EFI_PROD_CODE #if EFI_PROD_CODE

View File

@ -15,6 +15,7 @@ class Executor {
public: public:
Executor(); Executor();
void schedule(const char *prefix, scheduling_s *scheduling, uint64_t nowUs, int delayUs, schfunc_t callback, void *param); 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); void execute(uint64_t nowUs);
private: private:
EventQueue queue; EventQueue queue;

View File

@ -114,34 +114,34 @@ static uint64_t togglePwmState(PwmConfig *state) {
scheduleMsg(&logger, "%s: nextSwitchTime %d", state->name, nextSwitchTime); scheduleMsg(&logger, "%s: nextSwitchTime %d", state->name, nextSwitchTime);
#endif #endif
// signed value is needed here // signed value is needed here
int64_t timeToSwitch = nextSwitchTimeUs - getTimeNowUs(); // int64_t timeToSwitch = nextSwitchTimeUs - getTimeNowUs();
if (timeToSwitch < 1) { // if (timeToSwitch < 1) {
/** // /**
* We are here if we are late for a state transition. // * 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 // * At 12000RPM=200Hz with a 60 toothed wheel we need to change state every
* 1000000 / 200 / 120 = ~41 uS. We are kind of OK. // * 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, // * 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. // * 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"); // //todo: introduce error and test this error handling warning(OBD_PCM_Processor_Fault, "PWM: negative switch time");
timeToSwitch = 10; // timeToSwitch = 10;
} // }
state->safe.phaseIndex++; state->safe.phaseIndex++;
if (state->safe.phaseIndex == state->phaseCount) { if (state->safe.phaseIndex == state->phaseCount) {
state->safe.phaseIndex = 0; // restart state->safe.phaseIndex = 0; // restart
state->safe.iteration++; state->safe.iteration++;
} }
return timeToSwitch; return nextSwitchTimeUs;
} }
/** /**
* Main PWM loop: toggle pin & schedule next invocation * Main PWM loop: toggle pin & schedule next invocation
*/ */
static void timerCallback(PwmConfig *state) { static void timerCallback(PwmConfig *state) {
time_t timeToSleepUs = togglePwmState(state); uint64_t switchTimeUs = togglePwmState(state);
scheduleTask("pwm", &state->scheduling, timeToSleepUs, (schfunc_t) timerCallback, state); scheduleTask2("pwm", &state->scheduling, switchTimeUs, (schfunc_t) timerCallback, state);
} }
/** /**

View File

@ -30,6 +30,7 @@ extern "C"
#endif /* __cplusplus */ #endif /* __cplusplus */
void scheduleTask(const char *prefix, scheduling_s *scheduling, int delayUs, schfunc_t callback, void *param); 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 #ifdef __cplusplus
} }