Page 1 of 1

Timer register manipulation (TIMGn_T0CONFIG_REG)

Posted: Sun Dec 05, 2021 12:56 am
by Leonardo00
Hello,
I'm new to the ESP32, I was used to work with AVR or ST mcu, and I get quite disorientated by the esp32 Technical Reference Manual.

I would like to use one of the four general-purpose to precise count time between two events. I want to directly write TIMGn_T0CONFIG_REG (n = 0) register to configure the timer and read TIMGn_T0LO_REG and TIMGn_T0HI_REG (n = 0) registers when a pin interrupt occurs. I wrote this example code but it doesn't compile and I couldn't find any reference that explain how to properly use REG_WRITE and REG_READ macros.

I hope that someone could help me to figure out what is wrong with this code :)

Code: Select all

#define TIMG0_T0CONFIG_REG 0x3FF5F000
#define TIMG0_T0LO_REG
#define TIMG0_T0HI_REG

#define TIMG0_T0_DIVIDER 13
#define TIMG0_T0_INCREASE 30
#define TIMG0_T0_EN 31
#define T0_clk 0

uint32_t time_H = 0;
uint32_t time_L = 0;

void setup() {
  Serial.begin(115200);
  // Timer setup: counting up, no prescaler (80MHz) and start.
  REG_WRITE(TIMG0_T0CONFIG_REG, (T0_clk << TIMG0_T0_DIVIDER) | (1 << TIMG0_T0_INCREASE) | (1 << TIMG0_T0_EN));
}

void loop() {
  time_L = REG_READ(TIMG0_T0LO_REG);
  time_H = REG_READ(TIMG0_T0HI_REG);
  Serial.println((uint64_t(start_time_H << 32) + start_time_L));
  delay(1000);
}