Skip to content

Commit 27a769a

Browse files
committed
Update samples to use improved timer API and add timers module to HostTests
1 parent f84d6ac commit 27a769a

File tree

8 files changed

+472
-25
lines changed

8 files changed

+472
-25
lines changed

docs/source/information/timers.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ range 10 - 10000 microseconds (or whichever time unit you've selected). It doesn
146146
code to the application. If the code compiles, then you can be confident that the timer
147147
will function as expected and you don't need to check return values.
148148

149+
You can see these checks in action in the :sample:`LiveDebug` sample, which uses the HardwareTimer
150+
with a /16 prescaler. If you change *BLINK_INTERVAL_MS* to 2000 then the code will not compile.
151+
152+
149153
Clocks
150154
------
151155

samples/HttpServer_WebSockets/app/application.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ void wsMessageReceived(WebsocketConnection& socket, const String& message)
5555

5656
// Don't shutdown immediately, wait a bit to allow messages to propagate
5757
auto timer = new SimpleTimer;
58-
timer->setCallback(
58+
timer->initializeMs<1000>(
5959
[](void* timer) {
6060
delete static_cast<SimpleTimer*>(timer);
6161
server.shutdown();
6262
},
6363
timer);
64-
timer->startMs(1000);
64+
timer->startOnce();
6565
return;
6666
}
6767

samples/LiveDebug/app/application.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@ const unsigned MAX_COMMAND_LENGTH = 64;
1717
/*
1818
* This example uses the hardware timer for best timing accuracy. There is only one of these on the ESP8266,
1919
* so it may not be available if another module requires it.
20-
* Most timing applications can use a SimpleTimer, which is good for intervals of up to about 268 seconds.
21-
* For longer intervals, use a Timer.
20+
*
21+
* Most software timing applications can use a `SimpleTimer`, which is good for intervals of up to about
22+
* 429 seconds, or around 2 hours if you compile with USE_US_TIMER=0.
23+
*
24+
* For longer intervals and delegate callback support use a `Timer`.
2225
*/
2326
#define TIMER_TYPE TIMERTYPE_HARDWARE
2427

28+
/*
29+
* We use the timer to blink the LED at this rate
30+
*/
31+
#define BLINK_INTERVAL_MS 1000
32+
2533
/*
2634
* HardwareTimer defaults to non-maskable mode, so the timer callback cannot be interrupted even by the
2735
* debugger. To use break/watchpoints we must set the timer to use maskable mode.
2836
*/
2937
#define HWTIMER_TYPE eHWT_Maskable
3038

3139
#if TIMER_TYPE == TIMERTYPE_HARDWARE
32-
HardwareTimer procTimer(HWTIMER_TYPE);
40+
HardwareTimer1<TIMER_CLKDIV_16, HWTIMER_TYPE> procTimer;
3341
// Hardware timer callbacks must always be in IRAM
3442
#define CALLBACK_ATTR IRAM_ATTR
3543
#elif TIMER_TYPE == TIMERTYPE_SIMPLE
@@ -660,6 +668,16 @@ extern "C" void gdb_on_attach(bool attached)
660668
}
661669
}
662670

671+
static void printTimerDetails()
672+
{
673+
Serial.print(procTimer);
674+
Serial.print(", maxTicks = ");
675+
Serial.print(procTimer.maxTicks());
676+
Serial.print(", maxTime = ");
677+
Serial.print(procTimer.micros().ticksToTime(procTimer.maxTicks()).value());
678+
Serial.println();
679+
}
680+
663681
void GDB_IRAM_ATTR init()
664682
{
665683
Serial.begin(SERIAL_BAUD_RATE);
@@ -677,10 +695,7 @@ void GDB_IRAM_ATTR init()
677695
}
678696

679697
pinMode(LED_PIN, OUTPUT);
680-
#if TIMER_TYPE == TIMERTYPE_SIMPLE
681-
procTimer.setCallback(SimpleTimerCallback(blink));
682-
procTimer.startMs(1000, true);
683-
#else
684-
procTimer.initializeMs(1000, blink).start();
685-
#endif
698+
procTimer.initializeMs<BLINK_INTERVAL_MS>(blink).start();
699+
700+
printTimerDetails();
686701
}

samples/LiveDebug/component.mk

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ ENABLE_GDB_CONSOLE ?= 1
77
ifeq ($(ENABLE_GDB_CONSOLE), 1)
88
USER_CFLAGS := -DGDBSTUB_ENABLE_SYSCALL
99

10-
all:
10+
App-build: gdb-console-warning
11+
12+
.PHONY: gdb-console-warning
13+
gdb-console-warning:
1114
$(warning WARNING! Enabling the GDB console may interfere with visual debuggers, like eclipse)
1215
$(warning If required, please build with `make ENABLE_GDB_CONSOLE=0`)
1316
endif
1417

1518

1619
# Emulate UART 0
1720
ENABLE_HOST_UARTID := 0
21+
22+
# Sample doesn't use network so don't bother initialising it
23+
# Also add a short delay so telnet client has time to start before we start printing stuff
24+
HOST_NETWORK_OPTIONS = --nonet --pause=1

samples/Wifi_Sniffer/app/application.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ const unsigned scanTimeoutMs = 2000; ///< End scan on channel if no new devices
1111
WifiSniffer sniffer;
1212
SimpleTimer timer;
1313

14-
static void restartTimer()
15-
{
16-
timer.startMs(scanTimeoutMs, false);
17-
}
18-
1914
static void printBeacon(const BeaconInfo& beacon)
2015
{
2116
if(beacon.err != 0) {
@@ -68,7 +63,7 @@ static void onBeacon(const BeaconInfo& beacon)
6863
if(knownAPs.indexOf(beacon.bssid) < 0) {
6964
knownAPs.add(beacon);
7065
printBeacon(beacon);
71-
restartTimer();
66+
timer.restart();
7267
}
7368
}
7469

@@ -77,7 +72,7 @@ static void onClient(const ClientInfo& client)
7772
if(knownClients.indexOf(client.station) < 0) {
7873
knownClients.add(client);
7974
printClient(client);
80-
restartTimer();
75+
timer.restart();
8176
}
8277
}
8378

@@ -88,8 +83,8 @@ static void scanChannel(void* param)
8883
// Scan the next channel
8984
debugf("Set channel: %u", channel);
9085
sniffer.setChannel(channel);
91-
timer.setCallback(scanChannel, reinterpret_cast<void*>(channel + 1));
92-
restartTimer();
86+
timer.initializeMs<scanTimeoutMs>(scanChannel, reinterpret_cast<void*>(channel + 1));
87+
timer.startOnce();
9388
} else {
9489
// Stop sniffing and display final scan results
9590
sniffer.end();

tests/HostTests/app/application.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <malloc_count.h>
1515

1616
Vector<TestGroupFactory> groupFactories;
17-
static Timer taskTimer;
17+
static SimpleTimer taskTimer;
1818
static unsigned taskIndex;
1919

2020
#define XX(t) extern void REGISTER_TEST(t);
@@ -36,7 +36,7 @@ static void runNextGroup()
3636
System.restart();
3737
#else
3838
taskIndex = 0;
39-
taskTimer.setIntervalMs(RESTART_DELAY);
39+
taskTimer.setIntervalMs<RESTART_DELAY>();
4040
taskTimer.startOnce();
4141
#endif
4242
} else {
@@ -68,7 +68,7 @@ void TestGroup::complete()
6868
m_printf(_F("\r\n** Test Group '%s' OK **\r\n"), name.c_str());
6969
}
7070
delete this;
71-
taskTimer.setIntervalMs(TEST_GROUP_INTERVAL);
71+
taskTimer.setIntervalMs<TEST_GROUP_INTERVAL>();
7272
taskTimer.startOnce();
7373
}
7474

tests/HostTests/app/modules.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
XX(json6) \
66
XX(files) \
77
XX(rational) \
8-
XX(clocks)
8+
XX(clocks) \
9+
XX(timers)

0 commit comments

Comments
 (0)