From 2aef3364bca13ad7674c8b9a18589a0b181a8143 Mon Sep 17 00:00:00 2001
From: Andrew Tridgell <andrew@tridgell.net>
Date: Sat, 2 Oct 2021 14:55:20 +1000
Subject: [PATCH] HAL_ChibiOS: optimisation for AP_HAL::micros() on systems
 with 32 bit timers

most ChibiOS boards have a 32 bit timer. This optimisation reduces the
cost of micros() from 0.3us to 0.06us, which is significant in
interrupt handlers and for accurate timing. It takes advantage of the
timer being 32 bit with 1MHz clock
---
 libraries/AP_HAL_ChibiOS/system.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libraries/AP_HAL_ChibiOS/system.cpp b/libraries/AP_HAL_ChibiOS/system.cpp
index 2bd587fed7..cd23f8bb20 100644
--- a/libraries/AP_HAL_ChibiOS/system.cpp
+++ b/libraries/AP_HAL_ChibiOS/system.cpp
@@ -259,7 +259,12 @@ void panic(const char *errormsg, ...)
 
 uint32_t micros()
 {
+#if CH_CFG_ST_RESOLUTION == 32 && CH_CFG_ST_FREQUENCY==1000000U
+    // special case optimisation for 32 bit timers
+    return st_lld_get_counter();
+#else
     return hrt_micros32();
+#endif
 }
 
 uint32_t millis()