PWM shadowing

This commit is contained in:
Matthew Kennedy 2023-11-01 19:02:27 -04:00 committed by rusefillc
parent 0a767edd3d
commit 78391f04ee
2 changed files with 29 additions and 32 deletions

View File

@ -26,8 +26,8 @@ SimplePwm::SimplePwm()
seq.phaseCount = 2;
}
SimplePwm::SimplePwm(const char *p_name) : SimplePwm() {
name = p_name;
SimplePwm::SimplePwm(const char *name) : SimplePwm() {
m_name = name;
}
PwmConfig::PwmConfig() {
@ -37,10 +37,7 @@ PwmConfig::PwmConfig() {
periodNt = NAN;
mode = PM_NORMAL;
memset(&outputPins, 0, sizeof(outputPins));
pwmCycleCallback = nullptr;
stateChangeCallback = nullptr;
executor = nullptr;
name = "[noname]";
m_name = "[noname]";
}
/**
@ -54,13 +51,13 @@ void SimplePwm::setSimplePwmDutyCycle(float dutyCycle) {
return;
}
if (cisnan(dutyCycle)) {
warning(ObdCode::CUSTOM_DUTY_INVALID, "%s spwd:dutyCycle %.2f", name, dutyCycle);
warning(ObdCode::CUSTOM_DUTY_INVALID, "%s spwd:dutyCycle %.2f", m_name, dutyCycle);
return;
} else if (dutyCycle < 0) {
warning(ObdCode::CUSTOM_DUTY_TOO_LOW, "%s dutyCycle too low %.2f", name, dutyCycle);
warning(ObdCode::CUSTOM_DUTY_TOO_LOW, "%s dutyCycle too low %.2f", m_name, dutyCycle);
dutyCycle = 0;
} else if (dutyCycle > 1) {
warning(ObdCode::CUSTOM_PWM_DUTY_TOO_HIGH, "%s duty too high %.2f", name, dutyCycle);
warning(ObdCode::CUSTOM_PWM_DUTY_TOO_HIGH, "%s duty too high %.2f", m_name, dutyCycle);
dutyCycle = 1;
}
@ -75,16 +72,16 @@ void SimplePwm::setSimplePwmDutyCycle(float dutyCycle) {
if (dutyCycle < ZERO_PWM_THRESHOLD) {
mode = PM_ZERO;
if (stateChangeCallback) {
if (m_stateChangeCallback) {
// Manually fire falling edge
stateChangeCallback(0, this);
m_stateChangeCallback(0, this);
}
} else if (dutyCycle > FULL_PWM_THRESHOLD) {
mode = PM_FULL;
if (stateChangeCallback) {
if (m_stateChangeCallback) {
// Manually fire rising edge
stateChangeCallback(1, this);
m_stateChangeCallback(1, this);
}
} else {
mode = PM_NORMAL;
@ -141,8 +138,8 @@ void PwmConfig::handleCycleStart() {
return;
}
if (pwmCycleCallback) {
pwmCycleCallback(this);
if (m_pwmCycleCallback) {
m_pwmCycleCallback(this);
}
// Compute the maximum number of iterations without overflowing a uint32_t worth of timestamp
@ -187,8 +184,8 @@ efitick_t PwmConfig::togglePwmState() {
if (cisnan(periodNt)) {
// NaN period means PWM is paused, we also set the pin low
if (stateChangeCallback) {
stateChangeCallback(0, this);
if (m_stateChangeCallback) {
m_stateChangeCallback(0, this);
}
return getTimeNowNt() + MS2NT(NAN_FREQUENCY_SLEEP_PERIOD_MS);
@ -217,14 +214,14 @@ efitick_t PwmConfig::togglePwmState() {
{
ScopePerf perf(PE::PwmConfigStateChangeCallback);
if (stateChangeCallback) {
stateChangeCallback(cbStateIndex, this);
if (m_stateChangeCallback) {
m_stateChangeCallback(cbStateIndex, this);
}
}
efitick_t nextSwitchTimeNt = getNextSwitchTimeNt(this);
#if DEBUG_PWM
efiPrintf("%s: nextSwitchTime %d", state->name, nextSwitchTime);
efiPrintf("%s: nextSwitchTime %d", state->m_name, nextSwitchTime);
#endif /* DEBUG_PWM */
// If we're very far behind schedule, restart the cycle fresh to avoid scheduling a huge pile of events all at once
@ -264,12 +261,12 @@ static void timerCallback(PwmConfig *state) {
// we are here when PWM gets stopped
return;
}
if (state->executor == nullptr) {
firmwareError(ObdCode::CUSTOM_NULL_EXECUTOR, "exec on %s", state->name);
if (state->m_executor == nullptr) {
firmwareError(ObdCode::CUSTOM_NULL_EXECUTOR, "exec on %s", state->m_name);
return;
}
state->executor->scheduleByTimestampNt(state->name, &state->scheduling, switchTimeNt, { timerCallback, state });
state->m_executor->scheduleByTimestampNt(state->m_name, &state->scheduling, switchTimeNt, { timerCallback, state });
state->dbgNestingLevel--;
}
@ -288,12 +285,10 @@ void copyPwmParameters(PwmConfig *state, MultiChannelStateSequence const * seq)
* this method also starts the timer cycle
* See also startSimplePwm
*/
void PwmConfig::weComplexInit(ExecutorInterface *p_executor,
void PwmConfig::weComplexInit(ExecutorInterface *executor,
MultiChannelStateSequence const * seq,
pwm_cycle_callback *p_pwmCycleCallback, pwm_gen_callback *p_stateChangeCallback) {
executor = p_executor;
pwmCycleCallback = p_pwmCycleCallback;
stateChangeCallback = p_stateChangeCallback;
pwm_cycle_callback *pwmCycleCallback, pwm_gen_callback *stateChangeCallback) {
m_executor = executor;
isStopRequested = false;
// NaN is 'not initialized' but zero is not expected
@ -302,6 +297,8 @@ void PwmConfig::weComplexInit(ExecutorInterface *p_executor,
criticalAssertVoid(seq->phaseCount <= PWM_PHASE_MAX_COUNT, "too many phases in PWM");
criticalAssertVoid(seq->waveCount > 0, "waveCount should be positive");
m_pwmCycleCallback = pwmCycleCallback;
m_stateChangeCallback = stateChangeCallback;
copyPwmParameters(this, seq);

View File

@ -57,7 +57,7 @@ public:
pwm_cycle_callback *pwmCycleCallback,
pwm_gen_callback *callback);
ExecutorInterface *executor;
ExecutorInterface *m_executor = nullptr;
/**
* We need to handle zero duty cycle and 100% duty cycle in a special way
@ -71,7 +71,7 @@ public:
void setFrequency(float frequency);
void handleCycleStart();
const char *name;
const char *m_name;
// todo: 'outputPins' should be extracted away from here since technically one can want PWM scheduler without actual pin output
OutputPin *outputPins[PWM_PHASE_MAX_WAVE_PER_PWM];
@ -89,12 +89,12 @@ public:
/**
* this callback is invoked before each wave generation cycle
*/
pwm_cycle_callback *pwmCycleCallback;
pwm_cycle_callback *m_pwmCycleCallback = nullptr;
/**
* this main callback is invoked when it's time to switch level on any of the output channels
*/
pwm_gen_callback *stateChangeCallback = nullptr;
pwm_gen_callback *m_stateChangeCallback = nullptr;
private:
/**
* float value of PWM period