Skip to content

Unable to Change SPI SCLK in SPI.cpp when using different IDEs (Component vs. PlatformIO) #708

Closed
@neilpanchal

Description

@neilpanchal

Hardware:

Board: ESP32 DevKit-C
Core Installation/update date: Latest
IDE name: Multiple (ESP-IDF Arduino Component vs. Platform IO)
Flash Frequency: 40Mhz
Upload Speed: 115200

Summary:

I've come across an interesting roadblock. I am not able to change the SPI SLCK frequency in the SPI.cpp library when using arduino-esp32 as a ESP-IDF component. When I use PlatformIO with identical settings, I am able to change the SPI SLKC frequency. It has nothing to do with the U8g2 library as I have tried to hardcode the SPI.cpp frequency values without success.

Description

Arduino-esp32 dictates 4 ways of using the Arduino API with ESP-IDF in the instructions/readme.

Method 1 - Using Arduino API as ESP-IDF Component [This doesn't work]

One of those ways is to use Arduino API as an ESP-IDF component as described here. I managed to add Arduino API to ESP-IDF using this method. Next, I've added a component.mk file to @olikraus's U8g2_Arduino fork.

Component.mk has the following options to add source directors and include files:

COMPONENT_SRCDIRS:=src src/clib
COMPONENT_ADD_INCLUDEDIRS:=src src/clib

Now, U8g2 is a component of ESP-IDF.

Everything works except There is one problem. Usually, I am able to edit the SPI frequency in the u8x2_d_ssd1306_128x64_noname.c,

/* sck_clock_hz = */ 10000000UL,

however, no matter what frequency I set and upload to ESP32, I always get 8MHz as the clock freq. I've validated it using a scope.

1

I've hardcoded the SPI freq to 10 MHz in the arduino-esp32 SPI.c file and it still doesn't work:

void SPIClass::beginTransaction(SPISettings settings)
{
    //check if last freq changed
    uint32_t cdiv = spiGetClockDiv(_spi);
    if(_freq != settings._clock || _div != cdiv) {
        // _freq = settings._clock;
        _freq = 10000000UL; // Temporarily hardcoding it for debug purpose. No matter what freq I set here, the default is always 8 MHz.
        _div = spiFrequencyToClockDiv(_freq);
    }
    spiTransaction(_spi, _div, settings._dataMode, settings._bitOrder);
    _inTransaction = true;
}

So I tried to use arduino-esp32 using PlatformIO. This problem does not happen when I use Platform IO and downloading latest U8g2 library + arduino-esp32 platform.

Method 2 - Using PlatformIO [This works]

So, I tried the same test as above and I changed the u8x8_d_ssd1306_128x64_noname.c LINE 235 to use 10 MHz as the clock frequency.

/* sck_clock_hz = */ 10000000UL,

This time, using Platform IO toolchain, it works! I checked it with a scope and I am getting 10 MHz as the clock frequency.

3

Scope screenshot says 9.62 MHz but it is 10 MHz when I change the horizontal scale.

I am pretty sure I am missing something obvious here.

Any ideas on what must be going on?

Sketch:

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>

U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI display1(U8G2_R0, /* CS = */ 16, /* DC = */  17, /* RS = */  4);

void setup(void) {
    display1.begin();
    display1.setFont(SPINZERO); // Font file not shown here
	display1.setFontPosCenter();
}

void loop(void) {

	static unsigned long thisMicros = 0;
	static unsigned long lastMicros = 0;

	for (int i = 0; i < 60; i++) {
		display1.clearBuffer();
		display1.setCursor(64,20);
		display1.print("FPS TEST!");

		// tpf = Time Per Frame
		unsigned long tpf = thisMicros - lastMicros;
		display1.setCursor(64,32);
		display1.print(tpf);
		display1.setCursor(88,32);
		display1.print("yS/F");

		// fps = Frame Per Second
		unsigned long fps;
		if (tpf ==0) {
			fps = 0;
		} else {
			fps = 1000000/tpf;
		}
		display1.setCursor(64,44);
		display1.print(fps);
		display1.setCursor(88,44);
		display1.print("FPS");
		
		display1.sendBuffer();

		lastMicros = thisMicros;
		thisMicros = micros();
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions