ESP32 S3 Improving hardware timer accuracy with external 32.768 kHz crystal

misterd
Posts: 2
Joined: Tue Jul 09, 2024 9:12 pm

ESP32 S3 Improving hardware timer accuracy with external 32.768 kHz crystal

Postby misterd » Tue Jul 09, 2024 9:14 pm

I use hardware timers

Code: Select all

  bitTimer = timerBegin(0, 2, true);
  timerAttachInterrupt(bitTimer, &timerESP, true);
  timerAlarmWrite(bitTimer, halfBit, true);
  timerAlarmEnable(bitTimer);
My board uses a 40 MHz 10ppm crystal, I want to improve the accuracy of the timer. From the documentation I understood that the 40 MHz crystal does not affect the timers. But I saw that you can use the ability to connect an external 32 kHz crystal. But I can't find information about what exactly affects the accuracy of the hardware timer. Please tell me. Is it true that using an external 32 kHz crystal with an error of 0.2 ppm will increase the accuracy of the timers?

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 S3 Improving hardware timer accuracy with external 32.768 kHz crystal

Postby ESP_Sprite » Wed Jul 10, 2024 7:31 am

The timers by default do rely on the 40MHz crystal as a timing base, potentially via the PLL. You could potentially add a 32KHz crystal as well, and I think the hardware can switch to using that as a source, but I don't think Arduino would support that. Can I ask what you need such precision for?

misterd
Posts: 2
Joined: Tue Jul 09, 2024 9:12 pm

Re: ESP32 S3 Improving hardware timer accuracy with external 32.768 kHz crystal

Postby misterd » Wed Jul 10, 2024 11:04 am

I count cycles to output timecode (SMPTE 12M).
Other manufacturers have crystals of exactly 0.2ppm. This allows for drift of no more than 1 frame per day.

With the current crystal of 10ppm, the drift is too large. Therefore, I need to count as accurately as possible. Since I use timers, I am looking for a way to increase the accuracy. That is why I asked if, for example, replacing the crystal from 40MHz 10ppm to 40MHz 0.2ppm would help me.

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 S3 Improving hardware timer accuracy with external 32.768 kHz crystal

Postby ESP_Sprite » Thu Jul 11, 2024 3:50 am

Gotcha, makes sense. You can go both ways - changing the 40MHz crystal to a more precise one should get you to where you need to be and this is probably the easiest way. Using a 32KHz crystal is possible but it requires a bit of a hack - seems the GPTimer does *not* directly support running off it, but you could work around this by e.g. outputting the 32KHz signal on a GPIO, then looping the signal back into the pulse counter peripheral and using that as a timer.

Who is online

Users browsing this forum: No registered users and 30 guests