diff --git a/Sming/Core/HardwareTimer.h b/Sming/Core/HardwareTimer.h index bf3677e0a2..92c9327fbc 100644 --- a/Sming/Core/HardwareTimer.h +++ b/Sming/Core/HardwareTimer.h @@ -141,11 +141,10 @@ class Timer1Api : public CallbackTimerApi> template uint8_t Timer1Api::state; template uint32_t Timer1Api::interval; -template - /** * @brief Hardware Timer class template with selectable divider and interrupt mode */ +template using HardwareTimer1 = CallbackTimer>; /** diff --git a/tests/HostTests/modules/Clocks.cpp b/tests/HostTests/modules/Clocks.cpp index 99163d6433..0c3cb62f08 100644 --- a/tests/HostTests/modules/Clocks.cpp +++ b/tests/HostTests/modules/Clocks.cpp @@ -31,7 +31,11 @@ template class ClockTestTemplate : public TestG printLimits(); - for(unsigned i = 0; i < 2000; ++i) { + unsigned loopCount{2000}; +#ifdef ARCH_HOST + loopCount = 50; +#endif + while(loopCount--) { auto value = os_random(); check(value); check(value); @@ -42,19 +46,39 @@ template class ClockTestTemplate : public TestG TEST_CASE("vs. system time") { - constexpr uint32_t duration{2000000}; + // Determine whether this is an up or down-counter auto startTicks = Clock::ticks(); + os_delay_us(100); + auto endTicks = Clock::ticks(); + bool isDownCounter = (endTicks < startTicks); + debug_w("%s is %s counter", Clock::typeName(), isDownCounter ? "DOWN" : "UP"); + + // Run for a second or two and check timer ticks correspond approximately with system clock + constexpr uint64_t maxDuration = Clock::maxTicks().template as() - 5000ULL; + constexpr uint32_t duration = std::min(2000000ULL, maxDuration); auto startTime = system_get_time(); + startTicks = Clock::ticks(); uint32_t time; - while((time = system_get_time()) < startTime + duration) { + while((time = system_get_time()) - startTime < duration) { // } - auto endTicks = Clock::ticks(); + endTicks = Clock::ticks(); + if(isDownCounter) { + std::swap(startTicks, endTicks); + } + uint32_t elapsedTicks = (endTicks - startTicks) % (Clock::maxTicks() + 1); debug_w("System time elapsed: %u", time - startTime); - debug_w("%s ticks: %u", Clock::typeName(), endTicks - startTicks); - debug_w("Ratio: x %f", float(endTicks - startTicks) / (time - startTime)); - debug_w("Apparent time: %u", uint32_t(Micros::ticksToTime(endTicks - startTicks))); + debug_w("Ticks: %u (%u - %u)", elapsedTicks, startTicks, endTicks); + debug_w("Ratio: x %f", float(elapsedTicks) / (time - startTime)); + uint32_t us = Micros::ticksToTime(elapsedTicks); + debug_w("Apparent time: %u", us); +#ifndef ARCH_HOST + // Up-timers may report 0 if inactive + if(endTicks != 0 || startTicks != 0) { + REQUIRE(abs(int(us - duration)) < 500); // Allow some latitude + } +#endif } }