ESP32-P4 SPI clock

falkenmeister
Posts: 4
Joined: Mon Jan 12, 2026 3:56 pm

ESP32-P4 SPI clock

Postby falkenmeister » Thu Jan 15, 2026 1:23 pm

Hello,

I am using the Waveshare ESP32-P4-ETH module and programming it via the Arduino IDE. Uploading and debug output are working fine via the USB connection.

However, when setting up the SPI and performing a transfer at 5 MHz clock speed, the actual SPI clock signal is only clocking at 2.222 MHz when measured with an oscilloscope. The factor seems to closely match the difference of the 40 MHz XTAL clock and the 90 MHz assumed ABP clock, that the sketch (see below) is outputting.

Could this be an issue with the Arduino port or do I have something misconfigured on my side? The "test pin" that is toggled below takes about 19.5ms for the delay loop in the sketch.

Interestingly, the UART output *does* have a correct timing for the 115200 baud setting.

Used Sketch:

Code: Select all

#include <SPI.h>

#define SCK 18
#define MOSI 19
#define MISO 54

void setup() {
  Serial.begin(115200);
  pinMode(17, OUTPUT); // test pin
  SPI.begin(SCK, MISO, MOSI);
}

void loop() {
  // put your main code here, to run repeatedly:

  Serial.println("Tick..");
  Serial.println(getApbFrequency()); // Outputs 90 MHz
  Serial.println(getXtalFrequencyMhz()); // Outputs 40 MHz

  digitalWrite(17, HIGH);
  for(volatile uint32_t i = 0; i < 1000000; i++); // Takes 19.5ms
  digitalWrite(17, LOW);

  SPI.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE0));
  SPI.write(0xAA);
  SPI.endTransaction();
  delay(1000);
}

falkenmeister
Posts: 4
Joined: Mon Jan 12, 2026 3:56 pm

Re: ESP32-P4 SPI clock

Postby falkenmeister » Thu Jan 15, 2026 3:14 pm

After a bit of further digging, it seems like the "SPI clock divider" is correctly set up for a 90 MHz clock:

Serial.printf("Current divider: %d", SPI.getClockDivider());

gives a raw value of 2101249 = 0x201001. This would ( I believe) correspond to

CLKCNT_N = 1
CLKCNT_H = 0
CLKCNT_L = 1
CLKDIV_PRE = 8

So a total divider of (1 + 1) * (8 + 1) = 18. Given an assumed clock of 90 MHz this would yield an SPI clock of 5 MHz, but it doesn't. So, can I assume that my MCU is currently only running at 40 MHz instead of the 90 MHz it should be running at? And how could I fix this?

lbernstone
Posts: 1131
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32-P4 SPI clock

Postby lbernstone » Fri Jan 16, 2026 12:15 am

Arduino is just using IDF functions to calculate the divider, and set the clock. I don't have a scope handy to test, but check to make sure the frequency actually changes if you double the number in your spi.begin. You will probably need to open a ticket in the repo (which will in turn need to be handled upstream).

falkenmeister
Posts: 4
Joined: Mon Jan 12, 2026 3:56 pm

Re: ESP32-P4 SPI clock

Postby falkenmeister » Fri Jan 16, 2026 7:49 am

Thank you for the reply, lbernstone. I confirmed and yes, the SPI clock does change when changing the SPISettings in the sketch. The factors of expected clock frequency and measured one is always the same (40 / 90, same as the two clocks).

I'll get in contact with the guys from the Arduino repository then, thank you.

lbernstone
Posts: 1131
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32-P4 SPI clock

Postby lbernstone » Fri Jan 16, 2026 5:05 pm

That's an encouraging sign- maybe it is just a math mistake in the clock divider functions.

Who is online

Users browsing this forum: Semrush [Bot] and 5 guests