0

I am trying to understand how the clock speed in the esp32 works.

I am running arduino ide 1.8.12 with 1.04 board definition - on a pico kit 4 module .

The device datasheet says it has a 40mhz crystal.

getCpuFrequencyMhz() reports 240mhz

But the following code is generating 1sec interrupts with 80mhz as a base i.e., as if the clock speed is 80mhz.

If I explicitly set the clock speed to 80 mhz then getCpufrequency correctly reports it as 80, and the code still keeps generating 1sec interrupts (as it should at 80). If I set the clock to 40mhz then I finally see interrupts at 2 secs as would be expected. What is the deal with the 240 report for what is actually 80 and where is 80 coming from on a supposedly 40 mhz part?

I am sure that the explanation is somewhere in expressif's docs, but I have been unable to glean it. Can anyone quickly explain what is going on or point me to some resource that explains this particular circumstance?

---------------
Sample output 
getCpuFrequencyMhz() = 240
interrupts so far = 1 - : - mills() = 1018
interrupts so far = 2 - : - mills() = 2018
interrupts so far = 3 - : - mills() = 3018
interrupts so far = 4 - : - mills() = 4018
interrupts so far = 5 - : - mills() = 5018

----------------Sample code

#include "esp32-hal-cpu.h"

volatile int interruptCounter;
int totalInterruptCounter;
 
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
 
void IRAM_ATTR onTimer() {
  portENTER_CRITICAL_ISR(&timerMux);
  interruptCounter++;
  portEXIT_CRITICAL_ISR(&timerMux);
 
}
 
void setup() {
 
  Serial.begin(115200);

  Serial.printf("getCpuFrequencyMhz() = %d\n",getCpuFrequencyMhz()); //Get CPU clock
 
  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 1000000, true);
  timerAlarmEnable(timer);
 
}
 
void loop() {
 
  if (interruptCounter > 0) {
 
    portENTER_CRITICAL(&timerMux);
    interruptCounter--;
    portEXIT_CRITICAL(&timerMux);
 
    totalInterruptCounter++;
 
    Serial.printf("interrupts so far = %d - : - mills() = %d \n", totalInterruptCounter, millis());
 
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.