From 288b98d31c1574289cbf81f4c05f5201db0e3a69 Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Tue, 16 Jan 2024 16:22:56 +1100 Subject: [PATCH] Make percentage() safe for 32-bit values. Fixes pwLimit overflow regression --- speeduino/globals.cpp | 2 +- speeduino/maths.h | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/speeduino/globals.cpp b/speeduino/globals.cpp index a149bcdb..b3c41cda 100644 --- a/speeduino/globals.cpp +++ b/speeduino/globals.cpp @@ -111,7 +111,7 @@ bool initialisationComplete = false; ///< Tracks whether the setup() function ha byte fpPrimeTime = 0; ///< The time (in seconds, based on @ref statuses.secl) that the fuel pump started priming uint8_t softLimitTime = 0; //The time (in 0.1 seconds, based on seclx10) that the soft limiter started volatile uint16_t mainLoopCount; //Main loop counter (incremented at each main loop rev., used for maintaining currentStatus.loopsPerSecond) -unsigned long revolutionTime; //The time in uS that one revolution would take at current speed (The time tooth 1 was last seen, minus the time it was seen prior to that) +uint32_t revolutionTime; //The time in uS that one revolution would take at current speed (The time tooth 1 was last seen, minus the time it was seen prior to that) volatile unsigned long timer5_overflow_count = 0; //Increments every time counter 5 overflows. Used for the fast version of micros() volatile unsigned long ms_counter = 0; //A counter that increments once per ms uint16_t fixedCrankingOverride = 0; diff --git a/speeduino/maths.h b/speeduino/maths.h index 65ea7d53..93e3ec63 100644 --- a/speeduino/maths.h +++ b/speeduino/maths.h @@ -183,10 +183,12 @@ static inline uint32_t div360(uint32_t n) { * @param value The value to operate on * @return uint32_t */ -static inline uint16_t percentage(uint8_t percent, uint16_t value) { - return (uint16_t)div100((uint32_t)value * (uint32_t)percent); +static inline uint32_t percentage(uint8_t percent, uint32_t value) +{ + return (uint32_t)div100((uint32_t)value * (uint32_t)percent); } + /** * @brief Integer based half-percentage calculation. *