U8g2 spi st7920 not working in esp idf but ok with Arduino

Gustav
Posts: 7
Joined: Tue Aug 18, 2020 7:57 am

U8g2 spi st7920 not working in esp idf but ok with Arduino

Postby Gustav » Tue Aug 18, 2020 8:25 am

Hello everyone!

I'm starting to learn esp idf, and I'm trying to use a GLCD 128x64 with ST7920 over SPI.

It works fine with the Arduino code, so the wiring it's ok (well... the rst pin is kind of a problem, but I can see things on the display so I'll check it later).

But when I use u8g2 and the relative hal from nkolban, the project compiles fine but I can see nothing in the display. The hardware is exactly the same, I just switch beetween the two environments.

here's the relative code in esp idf:

Code: Select all

     
void app_main(void)
{
    u8g2_esp32_hal_t u8g2_esp32_hw = U8G2_ESP32_HAL_DEFAULT;

    u8g2_esp32_hw.clk = 14;
    u8g2_esp32_hw.cs = 15;
    u8g2_esp32_hw.mosi = 13;
    u8g2_esp32_hw.reset = 16;
    u8g2_esp32_hal_init(u8g2_esp32_hw);

    u8g2_t u8g2;
    
    u8g2_Setup_st7920_s_128x64_f(
  &u8g2, U8G2_R0,
  u8g2_esp32_spi_byte_cb,
  u8g2_esp32_gpio_and_delay_cb);
  
  u8g2_InitDisplay(&u8g2);
  
  u8g2_SetPowerSave(&u8g2, 0);
  
  u8g2_ClearBuffer(&u8g2);

  u8g2_SetFont(&u8g2, u8g2_font_timR14_tf);
  u8g2_DrawStr(&u8g2, 2,17,"Hello World!");
  
  u8g2_SendBuffer(&u8g2);


}
what am I missing? I tried to change the font pixel color, transparency, but I still can't ser anything.
Any idea on why this isn't working?

thank you in advance!

Gustav
Posts: 7
Joined: Tue Aug 18, 2020 7:57 am

Re: U8g2 spi st7920 not working in esp idf but ok with Arduino

Postby Gustav » Wed Aug 19, 2020 7:57 am

I checked the SPI signals on the oscilloscope and I found out that the Arduino code keeps RS/CS high, while esp-idf code keeps switching the RS/CS line up and down (like a normal SPI should do), so I moved the RS/CS wire to a fixed 3v3 and then, lo and behold, the display started working!

According to the ST7920 datasheet the RS/CS pin follows this rule for serial mode -> 1: chip enable 0: chip disable, keeping CS always enabled even when you are not communicating with the display is bad. It could pick up noise on clk and data lines and write nonsense on the display, I should find a way to fix this issue.

Just for the record, my display works on 5V, since esp32 works on 3v3, the rst pin must be driven with a level shifer, otherwise it will not work properly, all the data lines work fine with 3V3.

weisert
Posts: 1
Joined: Sun Dec 12, 2021 7:43 am

Re: U8g2 spi st7920 not working in esp idf but ok with Arduino

Postby weisert » Sun Dec 12, 2021 7:47 am

I just ran into this same issue about a year later, and discovered (with help from this post) that the default direction of the SPI CS line is active *low*.

When using the u8g2_esp32_hal, the following change is required to switch the direction of the SPI line:

[Codebox]
spi_device_interface_config_t dev_config;
...
dev_config.flags = SPI_DEVICE_POSITIVE_CS;

ESP_ERROR_CHECK(spi_bus_add_device(...));
[/Codebox]

On the u8x8 there looks like a collection of device information, with a field indicating the direction of the CS line for that device. If I have time, I'll see about fashioning a PR to the HAL repo [the new fork?] to address this, or suffice it to leave this comment here for future Googlers.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 92 guests