From 6acca898d7eb9d2e5855d6596cf314decf362e29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Betto?= <niccolo.betto@skywarder.eu> Date: Wed, 3 Apr 2024 21:08:44 +0200 Subject: [PATCH] [TimestampTimer] Migrate to `miosix::getTime()` and deprecate it --- CMakeLists.txt | 2 +- src/shared/drivers/timer/TimestampTimer.cpp | 47 ++-------- src/shared/drivers/timer/TimestampTimer.h | 86 ++++--------------- src/shared/utils/TimeUtils.h | 9 ++ .../algorithms/NAS/test-nas-with-triad.cpp | 1 - ...st-timestamptimer.cpp => test-gptimer.cpp} | 2 +- .../drivers/timer/test-timestamptimer.cpp | 4 - src/tests/sensors/test-l3gd20-fifo.cpp | 15 ++-- src/tests/sensors/test-l3gd20.cpp | 6 +- 9 files changed, 43 insertions(+), 129 deletions(-) rename src/tests/catch/{test-timestamptimer.cpp => test-gptimer.cpp} (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc3e9d964..bdc4f924b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -128,7 +128,7 @@ add_executable(catch-tests-boardcore # src/tests/catch/test-buttonhandler.cpp src/tests/catch/test-circularbuffer.cpp src/tests/catch/test-eventbroker.cpp - src/tests/catch/test-timestamptimer.cpp + src/tests/catch/test-gptimer.cpp src/tests/catch/test-kalman.cpp src/tests/catch/test-packetqueue.cpp src/tests/catch/test-sensormanager-catch.cpp diff --git a/src/shared/drivers/timer/TimestampTimer.cpp b/src/shared/drivers/timer/TimestampTimer.cpp index 993949a8d..2bd519efd 100644 --- a/src/shared/drivers/timer/TimestampTimer.cpp +++ b/src/shared/drivers/timer/TimestampTimer.cpp @@ -1,5 +1,5 @@ /* Copyright (c) 2020-2021 Skyward Experimental Rocketry - * Authors: Luca Conterio, Davide Mor, Alberto Nidasio + * Authors: Luca Conterio, Davide Mor, Alberto Nidasio, Niccolò Betto * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,51 +22,16 @@ #include "TimestampTimer.h" -#include <diagnostic/PrintLogger.h> +#include <miosix.h> +#include <utils/TimeUtils.h> namespace Boardcore { - -#ifndef COMPILE_FOR_HOST - namespace TimestampTimer { - -GP32bitTimer timestampTimer = initTimestampTimer(); - -} // namespace TimestampTimer - -void TimestampTimer::resetTimestamp() { timestampTimer.setCounter(0); } - -// TODO: Keep support for STM32F103 -GP32bitTimer TimestampTimer::initTimestampTimer() +unsigned long long getTimestamp() { - GP32bitTimer timer = GP32bitTimer{TIM2}; - { - miosix::FastInterruptDisableLock dLock; - // Enable TIM2 peripheral clock - RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; - } - - timer.reset(); - - timer.setFrequency(TIMER_FREQUENCY); - - // Generate an update event to apply the new prescaler value - timer.generateUpdate(); - - timestampTimer.enable(); - - PrintLogger logger = Logging::getLogger("timestamptimer"); - LOG_INFO(logger, "Initialized timestamp timer"); - - return timer; + return static_cast<unsigned long long>(nsToUs(miosix::getTime())); } - -#else - -void TimestampTimer::resetTimestamp() {} - -#endif - +} // namespace TimestampTimer } // namespace Boardcore diff --git a/src/shared/drivers/timer/TimestampTimer.h b/src/shared/drivers/timer/TimestampTimer.h index f2fdbee84..eef81b209 100644 --- a/src/shared/drivers/timer/TimestampTimer.h +++ b/src/shared/drivers/timer/TimestampTimer.h @@ -1,5 +1,5 @@ /* Copyright (c) 2020-2021 Skyward Experimental Rocketry - * Authors: Luca Conterio, Davide Mor, Alberto Nidasio + * Authors: Luca Conterio, Davide Mor, Alberto Nidasio, Niccolò Betto * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,84 +22,32 @@ #pragma once -#include <Singleton.h> -#include <kernel/kernel.h> - -#ifndef COMPILE_FOR_HOST -#include "GeneralPurposeTimer.h" -#include "TimerUtils.h" -#endif - namespace Boardcore { /** - * @brief Utility for precise timestamp values. - * - * TimestanTimer works with 32bit timers, TIM2 or TIM5. - * - * TIM2 is used, if we'll need to use TIM5 or both works need to be done to - * implement timer selection. - * - * The preferred configuration is to use a timer frequency of 250KHz. This way - * the timer has a resolution of 4us, a reload timer of 4.7 hours and for tick - * to time conversion require only a shift operation. - * - * For timer resolution and duration refer to : - * https://docs.google.com/spreadsheets/d/1FiNDVU7Rg98yZzz1dZ4GDAq3-nEg994ziezCawJ-OK4/edit?usp=sharing + * @brief Utility for microsecond timestamp values. + * + * @deprecated + * This namespace provided access to sub-millisecond timestamp values for + * precise timing requirements before Miosix 2.7, since at that time Miosix only + * had millisecond-accurate tick values. It has now been deprecated in favor of + * `miosix::getTime()` which has nanosecond resolution. The class is kept + * for compatibility reasons and should be removed in the future. + * + * The old timer used a 32bit value and the TIM2 or TIM5 hardware peripherals, + * at a frequency of 250KHz. This way the timer had a resolution of 4us, it + * would overflow after 4.7 hours and only required a 2-bit shift left + * operation to convert from counter tick to microseconds. */ namespace TimestampTimer { - /** - * @brief Preferred timer clock frequency. - */ -static constexpr uint32_t TIMER_FREQUENCY = 250000; - -/** - * @brief Resets the timestamp timer to 0. - */ -void resetTimestamp(); - -/** - * @brief Compute the current timer value in microseconds. + * @brief Returns the current timer value in microseconds. + * @deprecated Use miosix::getTime() instead and update the code to nanoseconds. * * @return Current timestamp in microseconds. */ -uint64_t getTimestamp(); - -#ifndef COMPILE_FOR_HOST - -/** - * @brief Initialize the timer. - * - * Enables the timer clock, resets the timer registers and sets che correct - * timer configuration. - */ -GP32bitTimer initTimestampTimer(); - -/** - * @brief TimestampTimer defaults to TIM2. - */ -extern GP32bitTimer timestampTimer; - -#endif - +unsigned long long getTimestamp(); }; // namespace TimestampTimer - -inline uint64_t TimestampTimer::getTimestamp() -{ -#ifdef COMPILE_FOR_HOST - return time(NULL); -#else - // With a timer frequency of 250KHz, the conversion from timer ticks to - // microseconds only take a 2 byte shift (x4) - return static_cast<uint64_t>(timestampTimer.readCounter()) << 2; - - // If the timer frequency is not a multiple of 2 you must compute the value - // this way: - // return TimerUtils::toIntMicroSeconds(timestampTimer.getTimer()); -#endif -} - } // namespace Boardcore diff --git a/src/shared/utils/TimeUtils.h b/src/shared/utils/TimeUtils.h index f2009cd8d..e50469a40 100644 --- a/src/shared/utils/TimeUtils.h +++ b/src/shared/utils/TimeUtils.h @@ -26,10 +26,19 @@ namespace Boardcore { namespace Constants { +constexpr long long NS_IN_US = 1000ll; // Nanoseconds in 1 us constexpr long long NS_IN_MS = 1000000ll; // Nanoseconds in 1 ms constexpr long long NS_IN_S = 1000000000ll; // Nanoseconds in 1 s } // namespace Constants +/** + * @brief Convert nanoseconds to microseconds. + * + * @param ns Nanoseconds. + * @return Microseconds. + */ +constexpr long long nsToUs(long long ns) { return ns / Constants::NS_IN_US; } + /** * @brief Convert nanoseconds to milliseconds. * diff --git a/src/tests/algorithms/NAS/test-nas-with-triad.cpp b/src/tests/algorithms/NAS/test-nas-with-triad.cpp index fb5c1c20f..372a4137b 100644 --- a/src/tests/algorithms/NAS/test-nas-with-triad.cpp +++ b/src/tests/algorithms/NAS/test-nas-with-triad.cpp @@ -59,7 +59,6 @@ int main() nas = new NAS(getEKConfig()); // Logger::getInstance().start(); - TimestampTimer::resetTimestamp(); sensorManager->start(); while (true) diff --git a/src/tests/catch/test-timestamptimer.cpp b/src/tests/catch/test-gptimer.cpp similarity index 98% rename from src/tests/catch/test-timestamptimer.cpp rename to src/tests/catch/test-gptimer.cpp index 5c385ae81..e057c9049 100644 --- a/src/tests/catch/test-timestamptimer.cpp +++ b/src/tests/catch/test-gptimer.cpp @@ -27,7 +27,7 @@ #include "catch-tests-entry.cpp" #endif -#include <drivers/timer/TimestampTimer.h> +#include <drivers/timer/GeneralPurposeTimer.h> #include <miosix.h> #include <catch2/catch.hpp> diff --git a/src/tests/drivers/timer/test-timestamptimer.cpp b/src/tests/drivers/timer/test-timestamptimer.cpp index 3580a59f0..de3ed4696 100644 --- a/src/tests/drivers/timer/test-timestamptimer.cpp +++ b/src/tests/drivers/timer/test-timestamptimer.cpp @@ -55,10 +55,6 @@ int main() Kernel::Thread::sleepUntil(prevTick + 1000); } - printf("Now resetting the TimestampTimer\n"); - - TimestampTimer::resetTimestamp(); - while (true) { long long prevTick = Kernel::getOldTick(); diff --git a/src/tests/sensors/test-l3gd20-fifo.cpp b/src/tests/sensors/test-l3gd20-fifo.cpp index ffd75c040..2800a87e7 100644 --- a/src/tests/sensors/test-l3gd20-fifo.cpp +++ b/src/tests/sensors/test-l3gd20-fifo.cpp @@ -186,15 +186,12 @@ int main() for (int i = 0; i < level; i++) { // data[dataCounter++] = fifo[i]; - data[dataCounter++] = { - fifoNum, - fifo[i], - level, - TimerUtils::toIntMicroSeconds( - TimestampTimer::timestampTimer.getTimer(), watermarkDelta), - CpuMeter::getCpuStats().mean, - TimerUtils::toIntMicroSeconds( - TimestampTimer::timestampTimer.getTimer(), update)}; + data[dataCounter++] = {fifoNum, + fifo[i], + level, + watermarkDelta, + CpuMeter::getCpuStats().mean, + update}; // Stop if we have enough data if (dataCounter >= NUM_SAMPLES) diff --git a/src/tests/sensors/test-l3gd20.cpp b/src/tests/sensors/test-l3gd20.cpp index 8d5b57384..8e9dad81f 100644 --- a/src/tests/sensors/test-l3gd20.cpp +++ b/src/tests/sensors/test-l3gd20.cpp @@ -31,6 +31,7 @@ #include <diagnostic/CpuMeter/CpuMeter.h> #include <drivers/interrupt/external_interrupts.h> #include <drivers/spi/SPIDriver.h> +#include <drivers/timer/TimerUtils.h> #include <drivers/timer/TimestampTimer.h> #include <sensors/L3GD20/L3GD20.h> #include <utils/KernelTime.h> @@ -162,10 +163,9 @@ int main() { // clang-format off printf("%d,%llu,%llu,%llu,%f,%f,%f,%.2f\n", - 0, + i, data[i].timestamp, - TimerUtils::toIntMicroSeconds( - TimestampTimer::timestampTimer.getTimer(), data[i].sampleDelta), + data[i].sampleDelta, (data[i].timestamp - data[i - 1].timestamp), data[i].data.angularSpeedX, data[i].data.angularSpeedY, -- GitLab