Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"SPARKFUN ESP32 THING" ISSUE WITH ESPRESSIF VERSION 3.0.0 - CORE CLOCK INCORRECT FREQUENCY? #7

Closed
dfrodin opened this issue Jun 6, 2024 · 10 comments

Comments

@dfrodin
Copy link

dfrodin commented Jun 6, 2024

This applies to the "Sparkfun ESP32 Thing" not the Thing Plus.

With the latest 3.0.0 rev of the espressif ESP32 library with the Arduino IDE there is an issue with several of the clocks or perhaps just the core clock.

I initialize the serial port to 115k baud in setup(). With the latest espressif library change I have to reduce the baud rate on the serial monitor to 74880 to get meaningful output. The ratio of those two values is 115200/74880 = 1.55.

I noticed that pressing the reset button on the "ESP32 Thing" produces the normal output of ...
ets Jul 29 2019 12:21:46
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1288
load:0x40078000,len:13872
load:0x40080400,len:4
ho 8 tail 4 room 4
load:0x40080404,len:3048
entry 0x40080590

This output arrives at the correct 115200 baud rather than the runtime baudrate of 74880.

If I output a message every 60,000 msec using the Arduino millis() function you should get a message output every minute.
This has worked in the past but now outputs a message every 1 min 33 seconds. The ratio of these values is also 93/60 = 1.55.

I haven't been able to reproduce these problems on a XIAO_ESP32C3 board.

I'm guessing that there is a missing configuration of the ESP32 core clock divider/multiplier setting that didn't get adjusted with all of the other changes made by espressif in the 3.0.0 release.

I'm unaware of what version/variant of the ESP32 is on the "Sparkfun ESP32 THING".
Feel free to ask if you need details on any particulars I can provide.

@dfrodin
Copy link
Author

dfrodin commented Jun 10, 2024

This chunk of code was pulled out of the Arduino examples/eps32/serial/Serial_All_CPU_Freqs.ino produces different
results depending on which esp32 board library is used.

uint32_t Freq = getCpuFrequencyMhz();
Serial.print("CPU Freq = ");
Serial.print(Freq);
Serial.println(" MHz");
Freq = getXtalFrequencyMhz();
Serial.print("XTAL Freq = ");
Serial.print(Freq);
Serial.println(" MHz");
Freq = getApbFrequency();
Serial.print("APB Freq = ");
Serial.print(Freq);
Serial.println(" Hz");

For the 2.0.17 version it produces ...
CPU Freq = 240 MHz
XTAL Freq = 26 MHz
APB Freq = 80000000 Hz

For the 3.0.1 version it produces ...
CPU Freq = 240 MHz
XTAL Freq = 40 MHz
APB Freq = 80000000 Hz

I would guess that the XTAL frequency is a hardcoded value defined in the board code. And even though the 3.0.1 version reports the CPU Freq as 240MHz it might be 160MHz.

@PaulZC
Copy link

PaulZC commented Jun 11, 2024

Hi Dave (@dfrodin ),

Many thanks for digging into this.

We haven't seen the same issue on other ESP32 boards - but they usually use ESP32 modules with the crystal built-in. On the Thing, the XTAL is 26MHz and is 'external' to the ESP32-D0WDQ6-V3 processor:

image

I suspect v3.0.0 of the core isn't detecting the XTAL frequency correctly, or is defaulting to the wrong value.

This code is relevant:

https://github.com/espressif/esp-idf/blob/8760e6d2a7e19913bc40675dd71f374bcd51b0ae/components/esp_hw_support/port/esp32/rtc_clk.c#L547-L559

It might be possible to correct the XTAL frequency by calling rtc_clk_xtal_freq_update((rtc_xtal_freq_t)26); ?

I don't have a Thing here... Could you please set Core Debug Level to Verbose and give this a try:

#include "soc/rtc.h"

void setup() {
  delay(1000);

  rtc_clk_xtal_freq_update((rtc_xtal_freq_t)26);
  
  Serial.begin(115200);
  Serial.println("Hello world");
}

void loop() {
}

Best wishes,
Paul

@dfrodin
Copy link
Author

dfrodin commented Jun 11, 2024

Paul,
Thanks for your response.
I'm using Arduino IDE ver 1.8.19. The verbose options for it are for "compilation" and/or "upload".
Was the "set Core Debug Level to Verbose" request a setting in the ver 2.x IDE?

I added a call to getXtalFrequencyMhz() which shows the transition from 40MHz to 26Mhz. It had no impact on the
serial baud rate or the millis() timer.
If the rtc_xtal_freq gets updated, doesn't a call need to be made to do the calculations/configuration of the divider/multiplers to get to 240MHz from 26MHz rather than 40MHz?

@PaulZC
Copy link

PaulZC commented Jun 11, 2024

Hi Dave,

The Verbose option is under Tools \ Core Debug Level (in the "Boards" section).

So, no magic wand... That's a shame. I tried the code on a ESP32 Thing Plus C and it didn't change the baud rate either... I was hoping it might work on the Thing, but I'm not very surprised it isn't... I'll take another look at this tomorrow. We might need to ask Espressif for advice and/or a fix.

More later,
Paul

@PaulZC
Copy link

PaulZC commented Jun 11, 2024

Hi again Dave,

I think you're right about the divider (re)initialization. rtc_clk_cpu_freq_set_xtal(); might be what we're looking for. It certainly changes the baud rate on the Thing Plus C. Please give it a go on your Thing.

#include "soc/rtc.h"

void setup() {
  delay(1000);

  rtc_clk_xtal_freq_update((rtc_xtal_freq_t)26);
  rtc_clk_cpu_freq_set_xtal();
  
  Serial.begin(115200);
  Serial.println("Hello world");
}

void loop() {
}

Best,
Paul

@dfrodin
Copy link
Author

dfrodin commented Jun 11, 2024

Paul,
You nailed it so go ahead and take the rest of the day off.

this ...
rtc_clk_xtal_freq_update((rtc_xtal_freq_t)26);
rtc_clk_cpu_freq_set_xtal();

produces this result...
CPU Freq = 26 MHz
XTAL Freq = 26 MHz
APB Freq = 26000000 Hz

then...
setCpuFrequencyMhz(240);

produces...
CPU Freq = 240 MHz
XTAL Freq = 26 MHz
APB Freq = 80000000 Hz

with the 3 calls from above the baud rate is correct at 115200 and the millis() timer is dead on.

I guess espressif should be made aware that there's some sort of bug in their clock init code.
I'll add the xtal/clk init changes to my existing code and go on my merry way.
If I see any future updates from 3.0.1 version of the espressif lib I'll remove the xtal/clk tweak to
see if it was fixed in the library.

Thanks for finding this work around for me.
Dave Frodin

@PaulZC
Copy link

PaulZC commented Jun 12, 2024

Issue opened here: espressif/arduino-esp32#9837

@PaulZC
Copy link

PaulZC commented Jun 13, 2024

Resolved! Please see: espressif/arduino-esp32#9844

I'll leave this open until the fix is built into a arduino-esp32 Release Candidate

@dfrodin
Copy link
Author

dfrodin commented Jun 24, 2024

PaulZC,
I've retested with the 3.0.2 version of the ESP32 library and everything is behaving correctly. Thanks for your efforts.

@PaulZC
Copy link

PaulZC commented Jun 25, 2024

Thanks Dave. Closing...

@PaulZC PaulZC closed this as completed Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants