The Big Refactoring of 2019: scheduler should not be global #655

This commit is contained in:
rusefi 2019-01-09 08:50:51 -05:00
parent 84bcf68849
commit 9f63ea4130
13 changed files with 74 additions and 28 deletions

View File

@ -41,7 +41,7 @@ static void toggleTestAndScheduleNext() {
testPin.toggle(); testPin.toggle();
periodIndex = (periodIndex + 1) % TEST_LEN; periodIndex = (periodIndex + 1) % TEST_LEN;
testTime += test557[periodIndex]; testTime += test557[periodIndex];
scheduleByTimestamp(&scheduling, testTime, (schfunc_t) &toggleTestAndScheduleNext, NULL); engine->executor.scheduleByTimestamp(&scheduling, testTime, (schfunc_t) &toggleTestAndScheduleNext, NULL);
} }

View File

@ -115,7 +115,9 @@ static void turnAuxPidOn(int index) {
return; return;
} }
startSimplePwmExt(&auxPidPwm[index], "Aux PID", engineConfiguration->auxPidPins[index], startSimplePwmExt(&auxPidPwm[index], "Aux PID",
&engine->executor,
engineConfiguration->auxPidPins[index],
&auxPidPin[0], &auxPidPin[0],
engineConfiguration->auxPidFrequency[index], 0.1, applyPinState); engineConfiguration->auxPidFrequency[index], 0.1, applyPinState);
} }

View File

@ -24,7 +24,7 @@ EXTERN_ENGINE
static Logging *logger; static Logging *logger;
static SimplePwm alternatorControl; static SimplePwm alternatorControl("alt");
static pid_s *altPidS = &persistentState.persistentConfiguration.engineConfiguration.alternatorControl; static pid_s *altPidS = &persistentState.persistentConfiguration.engineConfiguration.alternatorControl;
static Pid altPid(altPidS); static Pid altPid(altPidS);
@ -162,7 +162,10 @@ void initAlternatorCtrl(Logging *sharedLogger) {
enginePins.alternatorPin.initPin("on/off alternator", boardConfiguration->alternatorControlPin); enginePins.alternatorPin.initPin("on/off alternator", boardConfiguration->alternatorControlPin);
} else { } else {
startSimplePwmExt(&alternatorControl, "Alternator control", boardConfiguration->alternatorControlPin, startSimplePwmExt(&alternatorControl,
"Alternator control",
&engine->executor,
boardConfiguration->alternatorControlPin,
&enginePins.alternatorPin, &enginePins.alternatorPin,
engineConfiguration->alternatorPwmFrequency, 0.1, applyAlternatorPinState); engineConfiguration->alternatorPwmFrequency, 0.1, applyAlternatorPinState);
} }

View File

@ -659,7 +659,9 @@ void initFsioImpl(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
if (frequency == 0) { if (frequency == 0) {
enginePins.fsioOutputs[i].initPin(getGpioPinName(i), boardConfiguration->fsioOutputPins[i], &DEFAULT_OUTPUT); enginePins.fsioOutputs[i].initPin(getGpioPinName(i), boardConfiguration->fsioOutputPins[i], &DEFAULT_OUTPUT);
} else { } else {
startSimplePwmExt(&fsioPwm[i], "FSIOpwm", brainPin, &enginePins.fsioOutputs[i], frequency, 0.5f, applyPinState); startSimplePwmExt(&fsioPwm[i], "FSIOpwm",
&engine->executor,
brainPin, &enginePins.fsioOutputs[i], frequency, 0.5f, applyPinState);
} }
} }
} }

View File

@ -82,10 +82,10 @@ static THD_WORKING_AREA(etbTreadStack, UTILITY_THREAD_STACK_SIZE);
/** /**
* @brief Pulse-Width Modulation state * @brief Pulse-Width Modulation state
*/ */
static SimplePwm etbPwmUp CCM_OPTIONAL; static SimplePwm etbPwmUp("etbUp") CCM_OPTIONAL;
static float valueOverride = NAN; static float valueOverride = NAN;
/* /*
static SimplePwm etbPwmDown CCM_OPTIONAL; static SimplePwm etbPwmDown("etbDown") CCM_OPTIONAL;
static OutputPin outputDirectionOpen CCM_OPTIONAL; static OutputPin outputDirectionOpen CCM_OPTIONAL;
*/ */
static OutputPin outputDirectionClose CCM_OPTIONAL; static OutputPin outputDirectionClose CCM_OPTIONAL;
@ -282,6 +282,7 @@ void startETBPins(void) {
// this line used for PWM // this line used for PWM
startSimplePwmExt(&etbPwmUp, "etb1", startSimplePwmExt(&etbPwmUp, "etb1",
&engine->executor,
boardConfiguration->etbControlPin1, boardConfiguration->etbControlPin1,
&enginePins.etbOutput1, &enginePins.etbOutput1,
freq, freq,

View File

@ -764,7 +764,7 @@ void initEngineContoller(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX)
#endif /* EFI_PROD_CODE */ #endif /* EFI_PROD_CODE */
} }
static char UNUSED_RAM_SIZE[10500]; static char UNUSED_RAM_SIZE[10400];
static char UNUSED_CCM_SIZE[7100] CCM_OPTIONAL; static char UNUSED_CCM_SIZE[7100] CCM_OPTIONAL;
@ -781,5 +781,5 @@ int getRusEfiVersion(void) {
if (initBootloader() != 0) if (initBootloader() != 0)
return 123; return 123;
#endif /* EFI_BOOTLOADER_INCLUDE_CODE */ #endif /* EFI_BOOTLOADER_INCLUDE_CODE */
return 20190105; return 20190109;
} }

View File

@ -57,7 +57,7 @@ static Pid idlePid(&engineConfiguration->idleRpmPid);
#endif /* EFI_IDLE_INCREMENTAL_PID_CIC */ #endif /* EFI_IDLE_INCREMENTAL_PID_CIC */
// todo: extract interface for idle valve hardware, with solenoid and stepper implementations? // todo: extract interface for idle valve hardware, with solenoid and stepper implementations?
static SimplePwm idleSolenoid; static SimplePwm idleSolenoid("idle");
static StepperMotor iacMotor; static StepperMotor iacMotor;
@ -465,7 +465,9 @@ static void initIdleHardware() {
/** /**
* Start PWM for idleValvePin * Start PWM for idleValvePin
*/ */
startSimplePwmExt(&idleSolenoid, "Idle Valve", boardConfiguration->idle.solenoidPin, &enginePins.idleSolenoidPin, startSimplePwmExt(&idleSolenoid, "Idle Valve",
&engine->executor,
boardConfiguration->idle.solenoidPin, &enginePins.idleSolenoidPin,
boardConfiguration->idle.solenoidFrequency, boardConfiguration->manIdlePosition / 100, boardConfiguration->idle.solenoidFrequency, boardConfiguration->manIdlePosition / 100,
applyIdleSolenoidPinState); applyIdleSolenoidPinState);
idlePositionSensitivityThreshold = 0.0f; idlePositionSensitivityThreshold = 0.0f;

View File

@ -10,6 +10,7 @@
#include "global.h" #include "global.h"
#include "pwm_generator_logic.h" #include "pwm_generator_logic.h"
#include "error_handling.h"
/** /**
* We need to limit the number of iterations in order to avoid precision loss while calculating * We need to limit the number of iterations in order to avoid precision loss while calculating
@ -29,6 +30,10 @@ SimplePwm::SimplePwm() {
init(_switchTimes, sr); init(_switchTimes, sr);
} }
SimplePwm::SimplePwm(const char *name) : SimplePwm() {
this->name = name;
}
void PwmConfig::baseConstructor() { void PwmConfig::baseConstructor() {
memset((void*)&scheduling, 0, sizeof(scheduling)); memset((void*)&scheduling, 0, sizeof(scheduling));
memset((void*)&safe, 0, sizeof(safe)); memset((void*)&safe, 0, sizeof(safe));
@ -39,6 +44,8 @@ void PwmConfig::baseConstructor() {
phaseCount = 0; phaseCount = 0;
pwmCycleCallback = NULL; pwmCycleCallback = NULL;
stateChangeCallback = NULL; stateChangeCallback = NULL;
executor = NULL;
name = "[noname]";
} }
PwmConfig::PwmConfig() { PwmConfig::PwmConfig() {
@ -216,6 +223,11 @@ static void timerCallback(PwmConfig *state) {
efiAssertVoid(CUSTOM_ERR_6581, state->dbgNestingLevel < 25, "PWM nesting issue"); efiAssertVoid(CUSTOM_ERR_6581, state->dbgNestingLevel < 25, "PWM nesting issue");
efitimeus_t switchTimeUs = state->togglePwmState(); efitimeus_t switchTimeUs = state->togglePwmState();
// if (state->executor == NULL) {
// firmwareError(CUSTOM_ERR_6695, "exec on %s", state->name);
// return;
// }
scheduleByTimestamp(&state->scheduling, switchTimeUs, (schfunc_t) timerCallback, state); scheduleByTimestamp(&state->scheduling, switchTimeUs, (schfunc_t) timerCallback, state);
state->dbgNestingLevel--; state->dbgNestingLevel--;
} }
@ -246,8 +258,9 @@ void copyPwmParameters(PwmConfig *state, int phaseCount, float *switchTimes, int
* this method also starts the timer cycle * this method also starts the timer cycle
* See also startSimplePwm * See also startSimplePwm
*/ */
void PwmConfig::weComplexInit(const char *msg, int phaseCount, float *switchTimes, int waveCount, void PwmConfig::weComplexInit(const char *msg, ExecutorInterface *executor, int phaseCount, float *switchTimes, int waveCount,
pin_state_t **pinStates, pwm_cycle_callback *pwmCycleCallback, pwm_gen_callback *stateChangeCallback) { pin_state_t **pinStates, pwm_cycle_callback *pwmCycleCallback, pwm_gen_callback *stateChangeCallback) {
this->executor = executor;
efiAssertVoid(CUSTOM_ERR_6582, periodNt != 0, "period is not initialized"); efiAssertVoid(CUSTOM_ERR_6582, periodNt != 0, "period is not initialized");
if (phaseCount == 0) { if (phaseCount == 0) {
@ -275,7 +288,8 @@ void PwmConfig::weComplexInit(const char *msg, int phaseCount, float *switchTime
timerCallback(this); timerCallback(this);
} }
void startSimplePwm(SimplePwm *state, const char *msg, OutputPin *output, float frequency, float dutyCycle, pwm_gen_callback *stateChangeCallback) { void startSimplePwm(SimplePwm *state, const char *msg, ExecutorInterface *executor,
OutputPin *output, float frequency, float dutyCycle, pwm_gen_callback *stateChangeCallback) {
efiAssertVoid(CUSTOM_ERR_6692, state != NULL, "state"); efiAssertVoid(CUSTOM_ERR_6692, state != NULL, "state");
efiAssertVoid(CUSTOM_ERR_6665, dutyCycle >= 0 && dutyCycle <= 1, "dutyCycle"); efiAssertVoid(CUSTOM_ERR_6665, dutyCycle >= 0 && dutyCycle <= 1, "dutyCycle");
if (frequency < 1) { if (frequency < 1) {
@ -292,15 +306,17 @@ void startSimplePwm(SimplePwm *state, const char *msg, OutputPin *output, float
state->outputPins[0] = output; state->outputPins[0] = output;
state->setFrequency(frequency); state->setFrequency(frequency);
state->weComplexInit(msg, 2, switchTimes, 1, pinStates, NULL, stateChangeCallback); state->weComplexInit(msg, executor, 2, switchTimes, 1, pinStates, NULL, stateChangeCallback);
} }
void startSimplePwmExt(SimplePwm *state, const char *msg, brain_pin_e brainPin, OutputPin *output, float frequency, void startSimplePwmExt(SimplePwm *state, const char *msg,
ExecutorInterface *executor,
brain_pin_e brainPin, OutputPin *output, float frequency,
float dutyCycle, pwm_gen_callback *stateChangeCallback) { float dutyCycle, pwm_gen_callback *stateChangeCallback) {
output->initPin(msg, brainPin); output->initPin(msg, brainPin);
startSimplePwm(state, msg, output, frequency, dutyCycle, stateChangeCallback); startSimplePwm(state, msg, executor, output, frequency, dutyCycle, stateChangeCallback);
} }
/** /**

View File

@ -54,10 +54,13 @@ public:
void init(float *switchTimes, SingleWave *waves); void init(float *switchTimes, SingleWave *waves);
void weComplexInit(const char *msg, void weComplexInit(const char *msg,
ExecutorInterface *executor,
int phaseCount, float *swithcTimes, int waveCount, pin_state_t **pinStates, int phaseCount, float *swithcTimes, int waveCount, pin_state_t **pinStates,
pwm_cycle_callback *pwmCycleCallback, pwm_cycle_callback *pwmCycleCallback,
pwm_gen_callback *callback); pwm_gen_callback *callback);
ExecutorInterface *executor;
/** /**
* We need to handle zero duty cycle and 100% duty cycle in a special way * We need to handle zero duty cycle and 100% duty cycle in a special way
*/ */
@ -69,7 +72,7 @@ public:
void setFrequency(float frequency); void setFrequency(float frequency);
void handleCycleStart(); void handleCycleStart();
const char *name;
OutputPin *outputPins[PWM_PHASE_MAX_WAVE_PER_PWM]; OutputPin *outputPins[PWM_PHASE_MAX_WAVE_PER_PWM];
MultiWave multiWave; MultiWave multiWave;
@ -106,6 +109,7 @@ private:
class SimplePwm : public PwmConfig { class SimplePwm : public PwmConfig {
public: public:
SimplePwm(); SimplePwm();
SimplePwm(const char *name);
void setSimplePwmDutyCycle(float dutyCycle); void setSimplePwmDutyCycle(float dutyCycle);
pin_state_t pinStates[2]; pin_state_t pinStates[2];
SingleWave sr[1]; SingleWave sr[1];
@ -119,7 +123,9 @@ private:
* *
* This method should be called after scheduling layer is started by initSignalExecutor() * This method should be called after scheduling layer is started by initSignalExecutor()
*/ */
void startSimplePwm(SimplePwm *state, const char *msg, OutputPin *output, void startSimplePwm(SimplePwm *state, const char *msg,
ExecutorInterface *executor,
OutputPin *output,
float dutyCycle, float frequency, pwm_gen_callback *stateChangeCallback); float dutyCycle, float frequency, pwm_gen_callback *stateChangeCallback);
/** /**
@ -127,7 +133,10 @@ void startSimplePwm(SimplePwm *state, const char *msg, OutputPin *output,
* *
* This method should be called after scheduling layer is started by initSignalExecutor() * This method should be called after scheduling layer is started by initSignalExecutor()
*/ */
void startSimplePwmExt(SimplePwm *state, const char *msg, brain_pin_e brainPin, OutputPin *output, void startSimplePwmExt(SimplePwm *state,
const char *msg,
ExecutorInterface *executor,
brain_pin_e brainPin, OutputPin *output,
float frequency, float dutyCycle, pwm_gen_callback *stateChangeCallback); float frequency, float dutyCycle, pwm_gen_callback *stateChangeCallback);
void copyPwmParameters(PwmConfig *state, int phaseCount, float *switchTimes, void copyPwmParameters(PwmConfig *state, int phaseCount, float *switchTimes,

View File

@ -160,7 +160,7 @@ static void resumeStimulator() {
stopEmulationAtIndex = DO_NOT_STOP; stopEmulationAtIndex = DO_NOT_STOP;
} }
void initTriggerEmulatorLogic(Logging *sharedLogger) { void initTriggerEmulatorLogic(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX) {
logger = sharedLogger; logger = sharedLogger;
TriggerShape *s = &engine->triggerCentral.triggerShape; TriggerShape *s = &engine->triggerCentral.triggerShape;
@ -169,7 +169,9 @@ void initTriggerEmulatorLogic(Logging *sharedLogger) {
s->wave.channels[0].pinStates, s->wave.channels[0].pinStates,
s->wave.channels[1].pinStates, s->wave.channels[1].pinStates,
s->wave.channels[2].pinStates }; s->wave.channels[2].pinStates };
triggerSignal.weComplexInit("position sensor", s->getSize(), s->wave.switchTimes, PWM_PHASE_MAX_WAVE_PER_PWM, triggerSignal.weComplexInit("position sensor",
&engine->executor,
s->getSize(), s->wave.switchTimes, PWM_PHASE_MAX_WAVE_PER_PWM,
pinStates, updateTriggerShapeIfNeeded, emulatorApplyPinState); pinStates, updateTriggerShapeIfNeeded, emulatorApplyPinState);
addConsoleActionI("rpm", setTriggerEmulatorRPM); addConsoleActionI("rpm", setTriggerEmulatorRPM);

View File

@ -21,6 +21,6 @@ public:
void handleEmulatorCallback(PwmConfig *state, int stateIndex); void handleEmulatorCallback(PwmConfig *state, int stateIndex);
}; };
void initTriggerEmulatorLogic(Logging *sharedLogger); void initTriggerEmulatorLogic(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX);
#endif /* TRIGGER_EMULATOR_ALGO_H_ */ #endif /* TRIGGER_EMULATOR_ALGO_H_ */

View File

@ -36,7 +36,7 @@ struct CJ125_state {
efitick_t prevNt; efitick_t prevNt;
}; };
static SimplePwm wboHeaterControl; static SimplePwm wboHeaterControl("wbo");
static OutputPin wboHeaterPin; static OutputPin wboHeaterPin;
static OutputPin cj125Cs; static OutputPin cj125Cs;
static Logging *logger; static Logging *logger;
@ -398,7 +398,9 @@ static void cjStartHeaterControl(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
if (boardConfiguration->wboHeaterPin != GPIO_UNASSIGNED) { if (boardConfiguration->wboHeaterPin != GPIO_UNASSIGNED) {
scheduleMsg(logger, "cj125: Starting heater control"); scheduleMsg(logger, "cj125: Starting heater control");
// todo: use custom pin state method, turn pin off while not running // todo: use custom pin state method, turn pin off while not running
startSimplePwmExt(&wboHeaterControl, "wboHeaterPin", boardConfiguration->wboHeaterPin, startSimplePwmExt(&wboHeaterControl, "wboHeaterPin",
&engine->executor,
boardConfiguration->wboHeaterPin,
&wboHeaterPin, CJ125_HEATER_PWM_FREQ, 0.0f, applyPinState); &wboHeaterPin, CJ125_HEATER_PWM_FREQ, 0.0f, applyPinState);
cjSetIdleHeater(PASS_ENGINE_PARAMETER_SIGNATURE); cjSetIdleHeater(PASS_ENGINE_PARAMETER_SIGNATURE);
} }

View File

@ -45,11 +45,13 @@ static void test100dutyCycle() {
print("*************************************** test100dutyCycle\r\n"); print("*************************************** test100dutyCycle\r\n");
expectedTimeOfNextEvent = timeNowUs = 0; expectedTimeOfNextEvent = timeNowUs = 0;
SimplePwm pwm; TestExecutor executor;
SimplePwm pwm("test PWM1");
OutputPin pin; OutputPin pin;
schedulingQueue.clear(); schedulingQueue.clear();
startSimplePwm(&pwm, "unit_test", startSimplePwm(&pwm, "unit_test",
&executor,
&pin, &pin,
1000 /* frequency */, 1000 /* frequency */,
1.0 /* duty cycle */, 1.0 /* duty cycle */,
@ -71,11 +73,13 @@ static void testSwitchToNanPeriod() {
print("*************************************** testSwitchToNanPeriod\r\n"); print("*************************************** testSwitchToNanPeriod\r\n");
expectedTimeOfNextEvent = timeNowUs = 0; expectedTimeOfNextEvent = timeNowUs = 0;
SimplePwm pwm; TestExecutor executor;
SimplePwm pwm("test PWM1");
OutputPin pin; OutputPin pin;
schedulingQueue.clear(); schedulingQueue.clear();
startSimplePwm(&pwm, "unit_test", startSimplePwm(&pwm, "unit_test",
&executor,
&pin, &pin,
1000 /* frequency */, 1000 /* frequency */,
0.60 /* duty cycle */, 0.60 /* duty cycle */,
@ -108,11 +112,14 @@ void testPwmGenerator() {
print("*************************************** testPwmGenerator\r\n"); print("*************************************** testPwmGenerator\r\n");
expectedTimeOfNextEvent = timeNowUs = 0; expectedTimeOfNextEvent = timeNowUs = 0;
SimplePwm pwm; TestExecutor executor;
SimplePwm pwm("test PWM3");
OutputPin pin; OutputPin pin;
schedulingQueue.clear(); schedulingQueue.clear();
startSimplePwm(&pwm, "unit_test", startSimplePwm(&pwm,
"unit_test",
&executor,
&pin, &pin,
1000 /* frequency */, 1000 /* frequency */,
0.80 /* duty cycle */, 0.80 /* duty cycle */,