From 7c973309b8e7c3b16d58b4de92cf2820c2223f9e Mon Sep 17 00:00:00 2001
From: Luca Erbetta <luca.erbetta@skywarder.eu>
Date: Sat, 7 Mar 2020 16:50:23 +0100
Subject: [PATCH] [HWTimer] Add conversion to int microseconds

---
 src/shared/drivers/HardwareTimer.h | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/shared/drivers/HardwareTimer.h b/src/shared/drivers/HardwareTimer.h
index cb82f77b6..4da7f7b2e 100644
--- a/src/shared/drivers/HardwareTimer.h
+++ b/src/shared/drivers/HardwareTimer.h
@@ -137,6 +137,8 @@ public:
      */
     float toMicroSeconds(Type ticks);
 
+    uint64_t toIntMicroSeconds(Type ticks);
+
     /**
      * @brief Converts from ticks to milliseconds, using the current prescaler
      * setting
@@ -185,9 +187,9 @@ public:
 
 private:
     TIM_TypeDef* tim;
-    unsigned prescaler_freq;
+    unsigned int prescaler_freq;
 
-    bool ticking = false;
+    bool ticking       = false;
     uint16_t prescaler = 0;
     Type auto_reload =
         static_cast<Type>(-1);  // Max value of Type (Type is unsigned)
@@ -253,6 +255,13 @@ float HardwareTimer<Type>::toMicroSeconds(Type ticks)
     return (1.0f * ticks * 1000000 * (1 + prescaler)) / prescaler_freq;
 }
 
+template <typename Type>
+uint64_t HardwareTimer<Type>::toIntMicroSeconds(Type ticks)
+{
+    return ((uint64_t)ticks * 1000000 * (uint64_t)(1 + prescaler)) /
+           (uint64_t)prescaler_freq;
+}
+
 template <typename Type>
 float HardwareTimer<Type>::toMilliSeconds(Type ticks)
 {
-- 
GitLab