Fix #1873 - compute dynamic PWM iterationLimit (#1874)

* fix

* remove define

* add an assert

* typo

* don't do a 64bit divide

* oh that's a float
This commit is contained in:
Matthew Kennedy 2020-10-11 17:25:38 -07:00 committed by GitHub
parent fc5f47a653
commit 018db0df94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 8 deletions

View File

@ -17,12 +17,6 @@
#include "mpu_util.h" #include "mpu_util.h"
#endif #endif
/**
* We need to limit the number of iterations in order to avoid precision loss while calculating
* next toggle time
*/
#define ITERATION_LIMIT 100
// 1% duty cycle // 1% duty cycle
#define ZERO_PWM_THRESHOLD 0.01 #define ZERO_PWM_THRESHOLD 0.01
@ -121,7 +115,7 @@ static efitick_t getNextSwitchTimeNt(PwmConfig *state) {
/** /**
* Once 'iteration' gets relatively high, we might lose calculation precision here. * Once 'iteration' gets relatively high, we might lose calculation precision here.
* This is addressed by ITERATION_LIMIT * This is addressed by iterationLimit below, using any many cycles as possible without overflowing timeToSwitchNt
*/ */
uint32_t timeToSwitchNt = (uint32_t)((iteration + switchTime) * periodNt); uint32_t timeToSwitchNt = (uint32_t)((iteration + switchTime) * periodNt);
@ -157,8 +151,12 @@ void PwmConfig::handleCycleStart() {
if (pwmCycleCallback != NULL) { if (pwmCycleCallback != NULL) {
pwmCycleCallback(this); pwmCycleCallback(this);
} }
// Compute the maximum number of iterations without overflowing a uint32_t worth of timestamp
uint32_t iterationLimit = (0xFFFFFFFF / periodNt) - 2;
efiAssertVoid(CUSTOM_ERR_6580, periodNt != 0, "period not initialized"); efiAssertVoid(CUSTOM_ERR_6580, periodNt != 0, "period not initialized");
if (safe.periodNt != periodNt || safe.iteration == ITERATION_LIMIT) { efiAssertVoid(CUSTOM_ERR_6580, iterationLimit > 0, "iterationLimit invalid");
if (safe.periodNt != periodNt || safe.iteration == iterationLimit) {
/** /**
* period length has changed - we need to reset internal state * period length has changed - we need to reset internal state
*/ */