On-Chip Peripherals — Hardware Timers, Watchdog
Two built-in peripherals that need no external components: the hardware counter for deterministic periodic alarms, and the watchdog for recovering a hung program. Both are thin classes — the configuration is a construction fact, and every method lowers to one Zephyr driver call.
Periodic software work belongs to a Thread — a kernel thread that sleeps between rounds. Use the hardware counter when you need deterministic, sub-millisecond periodic execution that does not depend on any thread.
Hardware Timer — Counter
Counter drives one of the chip’s free counter/timer instances through Zephyr’s counter driver. The alarm frequency is the construction fact; start() applies it as the top value and arms the alarm.
import { Counter } from '@typecad/hal';
const tick = new Counter(0, { hz: 1000 }); // instance 0, 1 kHz alarms
tick.onAlarm(() => { ticks++; }); // fires once per wrap (interrupt context)
tick.start();
// …
tick.stop();The instance index selects one of the chip’s declared free counters — instance 0 is the first (for example RTC1 on the nRF52840). Using an index the chip does not have is a build error that names the available counters.
⚙️ Advanced details — what start() emits
start() realizes the frequency as a top value of counter_freq / hz (counter_set_top_value), arms the alarm callback, and calls counter_start. stop() is counter_stop. The handler runs in interrupt context — the same ISR-safety rules as GPIO interrupts apply.
Watchdog — Watchdog
A watchdog resets the chip if your program stops feeding it. The timeout is the construction fact, in milliseconds.
import { Watchdog } from '@typecad/hal';
const wdt = new Watchdog(8000); // 8 s window
wdt.enable(); // arm — full CPU reset on expiry
// somewhere in the main loop:
wdt.feed(); // keep it aliveFeed at least once per window. disable() calls wdt_disable; not every driver supports disabling at runtime, so treat it as best-effort.
What is not here
Capacitive-touch GPIOs and the on-die temperature sensor were removed with the Arduino target — Zephyr has no stable driver surface for either on the supported chips. Touch display controllers (FT6336U and friends) are a separate feature under Display Configuration; temperature sensing is covered by the sensor catalog (a SENSOR. part with a temperature channel).
On This Page