#include <Arduino.h>
extern "C" {
#include "soc/soc.h"
#include "soc/timer_group_reg.h"
}
// Read 64-bit counter of Timer0 in group `grp` (0 or 1)
static inline uint64_t read_t0_count(int grp) {
REG_WRITE(TIMG_T0UPDATE_REG(grp), 1); // latch counter
uint32_t lo = REG_READ(TIMG_T0LO_REG(grp));
uint32_t hi = REG_READ(TIMG_T0HI_REG(grp));
return (uint64_t(hi) << 32) | lo;
}
void setup() {
Serial.begin(115200);
// Configure TIMG0.T0: count up, prescaler = 80 (1 MHz), enable
// Set prescaler bits
SET_PERI_REG_BITS(TIMG_T0CONFIG_REG(0), TIMG_T0_DIVIDER, 80, TIMG_T0_DIVIDER_S);
// Count up + enable counter
REG_SET_BIT(TIMG_T0CONFIG_REG(0), TIMG_T0_INCREASE);
REG_SET_BIT(TIMG_T0CONFIG_REG(0), TIMG_T0_EN);
}
void loop() {
uint64_t ticks = read_t0_count(0); // Timer Group 0, Timer 0
Serial.printf("T0 ticks=%llu (~%lluus)\n", ticks, ticks);
delay(500);
}