]> git.earman.xyz Git - sensor-watch.git/commitdiff
handle changing CPU speed
authorJoey Castillo <jose.castillo@gmail.com>
Sun, 29 Aug 2021 19:50:46 +0000 (15:50 -0400)
committerJoey Castillo <jose.castillo@gmail.com>
Sun, 29 Aug 2021 21:11:31 +0000 (17:11 -0400)
watch-library/hpl/core/hpl_core_m0plus_base.c
watch-library/watch/watch.c
watch-library/watch/watch.h
watch-library/watch/watch_uart.c

index 1d32300a2e6d0d4e7b368b3c76ecb12e21a22040..4a79ac3ed8b4e97f99d638c39236eb1059249245 100644 (file)
@@ -41,6 +41,7 @@
 #endif
 #include <utils_assert.h>
 #include <peripheral_clk_config.h>
+#include "watch.h"
 
 #ifndef CONF_CPU_FREQUENCY
 #define CONF_CPU_FREQUENCY 1000000
@@ -167,7 +168,9 @@ static inline uint32_t _get_cycles_for_us_internal(const uint16_t us, const uint
  */
 uint32_t _get_cycles_for_us(const uint16_t us)
 {
-       return _get_cycles_for_us_internal(us, CONF_CPU_FREQUENCY, CPU_FREQ_POWER);
+    uint32_t freq = watch_get_cpu_speed();
+    if (freq > 10000000) return _get_cycles_for_us_internal(us, freq, 8);
+    else return _get_cycles_for_us_internal(us, freq, 7);
 }
 
 /**
@@ -196,5 +199,7 @@ static inline uint32_t _get_cycles_for_ms_internal(const uint16_t ms, const uint
  */
 uint32_t _get_cycles_for_ms(const uint16_t ms)
 {
-       return _get_cycles_for_ms_internal(ms, CONF_CPU_FREQUENCY, CPU_FREQ_POWER);
+    uint32_t freq = watch_get_cpu_speed();
+    if (freq > 10000000) return _get_cycles_for_ms_internal(ms, freq, 8);
+    else return _get_cycles_for_ms_internal(ms, freq, 7);
 }
index 059c2a937fcfe31b4229ea0a4ba152babb53f88c..e3a6c4d541937b3ba167a030bd2831bc5ae89f8d 100644 (file)
 #include "watch_uart.c"
 #include "watch_deepsleep.c"
 #include "watch_private.c"
+
+uint32_t watch_get_cpu_speed() {
+    uint8_t fsel = hri_oscctrl_get_OSC16MCTRL_FSEL_bf(OSCCTRL, OSCCTRL_OSC16MCTRL_MASK);
+    switch (fsel) {
+        case OSCCTRL_OSC16MCTRL_FSEL_4_Val:
+            return 4000000;
+            break;
+        case OSCCTRL_OSC16MCTRL_FSEL_8_Val:
+            return 8000000;
+            break;
+        case OSCCTRL_OSC16MCTRL_FSEL_12_Val:
+            return 12000000;
+            break;
+        case OSCCTRL_OSC16MCTRL_FSEL_16_Val:
+            return 16000000;
+            break;
+    }
+    return 0;
+}
index 8c207ddb645ba6d06830b15d8b97c7e0585c07dc..0b27c5153dadb9d5bbcd61b10b065f39ca7c4d16 100644 (file)
@@ -65,4 +65,7 @@
 
 #include "watch_private.h"
 
+/// @brief gets the current CPU speed
+uint32_t watch_get_cpu_speed();
+
 #endif /* WATCH_H_ */
\ No newline at end of file
index afebff1b7744901a216e02822659d8c6d229a02e..1ab888bdb474105a818e6ad50c63de3222cbdfc6 100644 (file)
 #include "peripheral_clk_config.h"
 
 void watch_enable_debug_uart(uint32_t baud) {
-    uint8_t fsel = hri_oscctrl_get_OSC16MCTRL_FSEL_bf(OSCCTRL, OSCCTRL_OSC16MCTRL_MASK);
-    uint32_t freq = 0;
-    switch (fsel) {
-        case OSCCTRL_OSC16MCTRL_FSEL_4_Val:
-            freq = 4000000;
-            break;
-        case OSCCTRL_OSC16MCTRL_FSEL_8_Val:
-            freq = 8000000;
-            break;
-        case OSCCTRL_OSC16MCTRL_FSEL_12_Val:
-            freq = 12000000;
-            break;
-        case OSCCTRL_OSC16MCTRL_FSEL_16_Val:
-            freq = 16000000;
-            break;
-        default:
-            return;
-    }
+    uint32_t freq = watch_get_cpu_speed();
     uint64_t br = (uint64_t)65536 * ((freq * 4) - 16 * baud) / (freq * 4);
 
     gpio_set_pin_direction(D1, GPIO_DIRECTION_IN);