void StepperMotor::initialize(brain_pin_e stepPin, brain_pin_e directionPin, pin_output_mode_e directionPinMode, float reactionTime, int totalSteps, brain_pin_e enablePin, Logging *sharedLogger) { this->reactionTime = maxF(1, reactionTime); this->totalSteps = maxI(3, totalSteps); logger = sharedLogger; if (stepPin == GPIO_UNASSIGNED || directionPin == GPIO_UNASSIGNED) { return; } stepPort = getHwPort("step", stepPin); this->stepPin = getHwPin("step", stepPin); this->directionPinMode = directionPinMode; this->directionPin.initPin("stepper dir", directionPin, &this->directionPinMode); enablePort = getHwPort("enable", enablePin); this->enablePin = getHwPin("enable", enablePin); efiSetPadMode("stepper step", stepPin, PAL_MODE_OUTPUT_PUSHPULL); efiSetPadMode("stepper enable", enablePin, PAL_MODE_OUTPUT_PUSHPULL); palWritePad(this->enablePort, enablePin, true); // disable stepper // All pins must be 0 for correct hardware startup (e.g. stepper auto-disabling circuit etc.). palWritePad(this->stepPort, this->stepPin, false); this->directionPin.setValue(false); this->currentDirection = false; chThdCreateStatic(stThreadStack, sizeof(stThreadStack), NORMALPRIO, (tfunc_t) stThread, this); }
void start(bool useTwoWires, brain_pin_e pinEnable, brain_pin_e pinDir1, brain_pin_e pinDir2) { dcMotor.SetType(useTwoWires ? TwoPinDcMotor::ControlType::PwmDirectionPins : TwoPinDcMotor::ControlType::PwmEnablePin); m_pinEnable.initPin("ETB Enable", pinEnable); m_pinDir1.initPin("ETB Dir 1", pinDir1); m_pinDir2.initPin("ETB Dir 2", pinDir2); // Clamp to >100hz int freq = maxI(100, engineConfiguration->etbFreq); startSimplePwm(&m_pwmEnable, "ETB Enable", &engine->executor, &m_pinEnable, freq, 0, (pwm_gen_callback*)applyPinState); startSimplePwm(&m_pwmDir1, "ETB Dir 1", &engine->executor, &m_pinDir1, freq, 0, (pwm_gen_callback*)applyPinState); startSimplePwm(&m_pwmDir2, "ETB Dir 2", &engine->executor, &m_pinDir2, freq, 0, (pwm_gen_callback*)applyPinState); }
/** * @brief sets new idle valve duty cycle: checks the bounds and reports new value */ static int setNewValue(IdleValveState *idle, int currentRpm, int now, char * msg, int newValue) { newValue = maxI(newValue, MIN_IDLE); newValue = minI(newValue, MAX_IDLE); if (idle->value != newValue) { idleDebug(msg, currentRpm); idle->timeOfLastIdleChange = now; } idle->value = newValue; return newValue; }
/* * we need safe iteration here because 'callback' might change change 'current->next' * while re-inserting it into the queue from within the callback */ LL_FOREACH_SAFE(executionList, current, tmp) { efiAssert(CUSTOM_ERR_ASSERT, current->callback != NULL, "callback==null2", 0); uint32_t before = GET_TIMESTAMP(); current->isScheduled = false; uint32_t howFarOff = now - current->momentX; maxSchedulingPrecisionLoss = maxI(maxSchedulingPrecisionLoss, howFarOff); #if EFI_UNIT_TEST || defined(__DOXYGEN__) printf("QUEUE: execute current=%d param=%d\r\n", (long)current, (long)current->param); #endif current->callback(current->param); // even with overflow it's safe to subtract here lastEventCallbackDuration = GET_TIMESTAMP() - before; if (lastEventCallbackDuration > maxEventCallbackDuration) maxEventCallbackDuration = lastEventCallbackDuration; if (lastEventCallbackDuration > 2000) { longScheduling = current; // what is this line about? lastEventCallbackDuration++; } }