Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix esp8266 timer1 testing #2709

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Sming/Core/HardwareTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ class Timer1Api : public CallbackTimerApi<Timer1Api<clkdiv, mode>>
template <hw_timer_clkdiv_t clkdiv, HardwareTimerMode mode> uint8_t Timer1Api<clkdiv, mode>::state;
template <hw_timer_clkdiv_t clkdiv, HardwareTimerMode mode> uint32_t Timer1Api<clkdiv, mode>::interval;

template <hw_timer_clkdiv_t clkdiv = TIMER_CLKDIV_16, HardwareTimerMode mode = eHWT_NonMaskable>

/**
* @brief Hardware Timer class template with selectable divider and interrupt mode
*/
template <hw_timer_clkdiv_t clkdiv = TIMER_CLKDIV_16, HardwareTimerMode mode = eHWT_NonMaskable>
using HardwareTimer1 = CallbackTimer<Timer1Api<clkdiv, mode>>;

/**
Expand Down
36 changes: 28 additions & 8 deletions tests/HostTests/modules/Clocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ template <class Clock, typename TimeType> 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<NanoTime::Milliseconds>(value);
check<NanoTime::Microseconds>(value);
Expand All @@ -37,19 +41,30 @@ template <class Clock, typename TimeType> class ClockTestTemplate : public TestG

TEST_CASE("vs. system time")
{
constexpr uint32_t duration{2000000};
auto startTime = system_get_time();
// 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<NanoTime::Microseconds>() - 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();
// Handle both up and down counters
auto elapsedTicks = (endTicks >= startTicks) ? endTicks - startTicks : startTicks - endTicks;
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(), elapsedTicks);
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);
Expand Down Expand Up @@ -252,10 +267,15 @@ template <hw_timer_clkdiv_t clkdiv>
class Timer1ClockTestTemplate : public ClockTestTemplate<Timer1Clock<clkdiv>, uint32_t>
{
public:
static void IRAM_ATTR callback(void*)
{
}

void execute() override
{
// Configure the hardware to match selected clock divider
Timer1Api<clkdiv, eHWT_Maskable> timer;
timer.setCallback(callback, nullptr);
timer.arm(false);

ClockTestTemplate<Timer1Clock<clkdiv>, uint32_t>::execute();
Expand Down
Loading