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

ST7920 on 2nd ESP32 SPI BUS (HSPI) in HW mode #1331

Closed
hilsonp opened this issue Nov 10, 2020 · 5 comments
Closed

ST7920 on 2nd ESP32 SPI BUS (HSPI) in HW mode #1331

hilsonp opened this issue Nov 10, 2020 · 5 comments
Labels

Comments

@hilsonp
Copy link

hilsonp commented Nov 10, 2020

I'm using:

My goal is to use this screen on the second SPI bus of the ESP32, in HW mode.

The ESP32 DEVKIT V1 with 30 GPIO pins used are the following:
SPI MOSI MISO CLK CS
VSPI GPIO 23 GPIO 19 GPIO 18 GPIO 5
HSPI GPIO 13 GPIO 12 GPIO 14 GPIO 15

To experiment this, I used the GraphicsTest.ino

My setup() is as follow:
void setup(void) { u8g2.setBusClock(600000); u8g2.begin(); }

Here are my tests (Markdown as messed up the comments around clock/data/reset):
// VSPI SW SPI => OK but slow
//U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=/ 18, / data=/ 23, / CS=/ 5, / reset=/ U8X8_PIN_NONE); // ESP32
// VSPI HW SPI => OK
//U8G2_ST7920_128X64_F_HW_SPI u8g2(U8G2_R0, /
CS=/ 5, / reset=/ U8X8_PIN_NONE);
// HSPI SW SPI => OK but slow
//U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /
clock=/ 14, / data=/ 13, / CS=/ 15, / reset=/ U8X8_PIN_NONE); // ESP32
// HSPI HW SPI => NOK
U8G2_ST7920_128X64_F_2ND_HW_SPI u8g2(U8G2_R0, /
CS=/ 15, / reset=*/ U8X8_PIN_NONE);

As you can see the 3 first combinations are working.
This let me conclude that:

  • My screen is working fine once I:
    • Reduced the speed in the setup()
    • I connected the EXP1-10 pin of the screen board to 5V.
  • My connections are OK with the HSPI pins as this works in SW_SPI mode with those pins.

Therefore, I'm raising my hand to get some help.
Is there anything I should change in the library sources to have U8G2_ST7920_128X64_F_2ND_HW_SPI working with my ESP32 ?

Notes: I have been reading plenty of tickets/forum pages before submitting this.

@olikraus
Copy link
Owner

In general 2ND HW is not supported for ESP (at least this was the case some time back).
You could change the library code instead. Locate the following function in the mentioned file on your local harddisk:

extern "C" uint8_t u8x8_byte_arduino_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)

Then replace SPI by any other object refering to your second HW SPI.

@hilsonp
Copy link
Author

hilsonp commented Dec 5, 2020

Thank you for the hint. I will give it a try.

@hilsonp hilsonp closed this as completed Dec 5, 2020
@hilsonp
Copy link
Author

hilsonp commented Dec 14, 2020

So yesterday, I succeeded to make my ESP32 (ESP32 DEVKIT V1) drive my ST7920 (RepRap 12864 LCD) in HW SPI mode on the 2nd SPI BUS (HSPI on pins 12 13 14 & 15) following the advise of @cbpdk : #377 (comment)

The advantage of this method is that it only touches the u8g2 files and leaves the ESP32 libraries intact.

So to make it as clear as possible for the next one facing the problem, here is exactly what I did:

  1. In U8x8lib.h, add this before the #ifdef SPI_INTERFACES_COUNT:
/* Declare a second SPI in case of ESP32 */
#if defined(ARDUINO_ARCH_ESP32)
#define SPI_INTERFACES_COUNT 2
#endif
  1. In U8x8lib.cpp, add this after the #include <SPI.h>:
/* Declare a second SPI in case of ESP32 */
#include "U8x8lib.h"
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif 
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

/* Declare a second SPI in case of ESP32 */
#if defined(ARDUINO_ARCH_ESP32)
#if SPI_INTERFACES_COUNT > 1
SPIClass SPI1(HSPI);
#endif
#endif

With those 2 modifications, I could run the u8g2 GraphicsTest.ino a full night without any problem (note: the speed change in the setup() ):

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

/* HSPI: ST7920 LCD driver in HW SPI mode */
/* LCD EXT1-9 : GND */
/* LCD EXT1-10: 5V */
#define HSPI_MOSI_PIN 13 // To EXP1-3 = LCDE  = ST7920 5 RW(SID)
#define HSPI_MISO_PIN 12 // 
#define HSPI_SCK_PIN  14 // To EXP1-5 = LCD4  = ST7920 6 E(SCK) 
#define HSPI_CS_PIN   15 // To EXP1-4 = LCDRS = ST7920 4 RS(CS)

U8G2_ST7920_128X64_F_2ND_HW_SPI u8g2(U8G2_R0, /* CS=*/ HSPI_CS_PIN, /* reset=*/ U8X8_PIN_NONE); //needed before u8g2.begin() : u8g2.setBusClock(600000); 

void setup(void) {
  u8g2.setBusClock(600000); // Needed with the "RepRap 12864 LCD" (ST7920)
  u8g2.begin();
}

void loop(void) {
  // put your code here
  delay(100);
}

I hope this helps.

When I have time, I will re-wire my ESP32 to verify that with this "patch" the fist SPI port can still be used and will report it here (so maybe @olikraus can include this in the library).

@HauTranCong
Copy link

Hi hilsonp !
I used Arduino ide to compile it, but not working for me. Are u using esp-idf for implementation?

@hilsonp
Copy link
Author

hilsonp commented Mar 25, 2022

I finally used another hardware for this project and can't do any tests right now.

To reply to your question: Everything was made in Arduino IDE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants