Bodmer / TFT_eSPI

Arduino and PlatformIO IDE compatible TFT library optimised for the Raspberry Pi Pico (RP2040), STM32, ESP8266 and ESP32 that supports different driver chips
Other
3.66k stars 1.05k forks source link

Support SPI1 on Raspberry Pi Pico #1460

Closed jas0995 closed 2 years ago

jas0995 commented 2 years ago

The Pico-ResTouch-LCD-3.5 is a display with a PICO socket on the back. A very neat compact display package.

However, I have not been able to get it working for me.

I have copied Setup60_RP2040_ILI9341 and made changes specific to this display and saved it as Setup61_RP2040_ILI9488.

Then in User_Setup_Select.h the only uncommented setup is number 61.

I have tried several example programs that are included mainly focusing on the Read_User_Setup example.

I have tried this with both platformio and the Arduino IDE and in both cases setup() runs some Serial print code that i inserted and nothing else happens. Regardless of which demo app I run.

I assume that the IDEs are telling the code what processor I am using, because I don't see anywhere I need to specify it.

The key part of Setup61 I changed is:

// For the Pico use these #define lines
#define TFT_MISO  12
#define TFT_MOSI  11
#define TFT_SCLK  10
#define TFT_CS     9  // Chip select control pin
#define TFT_DC     8  // Data Command control pin
#define TFT_RST   15  // Reset pin (could connect to Arduino RESET pin)

 #define TFT_BL   13            // LED back-light control pin
 #define TFT_BACKLIGHT_ON LOW  // Level to turn ON back-light (HIGH or LOW)

#define TOUCH_CS  16     // Chip select pin (T_CS) of touch screen

Am I missing something?

Thanks -John

jas0995 commented 2 years ago

While poking around with a o'scope, I noticed that the silk screen on the board did not match up with the pin numbers one the board docs.... Fixed that and now I have a new problem..

Now when I upload any program, I get four fast and four slow flashes of the led.

This seems to indicate the crashing of the mbed lib that the Arduino IDE uses.

Any ideas what may be causing this?

Thanks -John

Bodmer commented 2 years ago

The ones you have selected do not work. TFT_eSPI uses SPI port 0 (SPI0) in the hardware and this can only be allocated to certain pins. The example pins will work OK:

#define TFT_MISO  0
#define TFT_MOSI  3
#define TFT_SCLK  2
#define TFT_CS    4  // Chip select control pin
#define TFT_DC    5  // Data Command control pin
#define TFT_RST   6  // Reset pin (could connect to Arduino RESET pin)
//#define TFT_BL     // LED back-light

#define TOUCH_CS  7     // Chip select pin (T_CS) of touch screen

You need to check the Pico data sheet to find out which pins will work with SPI port 0. I will look too and see what the restrictions are. The official pinout for the Pico is here which indicates GPIO 16,17,18 and 19 are OK, I suspect there are other pin combinations that would be work but have not tested them.

Bodmer commented 2 years ago

I have tested with these pins on a RPi Pico, and my ILI9341 SPI display works fine:

// For the Pico use these #define lines
#define TFT_MISO  16
#define TFT_MOSI  19
#define TFT_SCLK  18
#define TFT_CS    17  // Chip select control pin
#define TFT_DC    20  // Data Command control pin
#define TFT_RST   21  // Reset pin (could connect to Arduino RESET pin)

#define TOUCH_CS  22     // Chip select pin (T_CS) of touch screen

TFT_CS/DC/RST/BL and Touch_CS can be allocated to other pins, only GPIO 16, 1, 19 are important as they are valid SPI0 pins.

Kabron287 commented 2 years ago

I made exactly the same setup as jas0995 did.

Execution cycled between static inline uint spi_get_index and static inline spi_hw_t spi_get_hw subs in spi.h file lines 118 and 123.

@jas0995, what silkscreen error did you find?

It also have to be stated that this display uses onboard SPI->Parallel converter. Maybe it is the reason.

Bodmer commented 2 years ago

@Kabron287 it appears you have a completely different problem to @jas0995

spi.h is not part of TFT_eSPI. It sounds like you have a custom SPI implementation so you will need to look more closely at that library.

Incidentally, TFT_eSPI does support 16 bit serial to parallel interfaces such as the MHS 4.0 Raspberry Pi TFT. One complication that may be relevant to you is that TFT commands are 8 bits on a normal SPI interface. So in TFT_eSPI, when RPI_DISPLAY_TYPE is defined the library will send 16 bit commands.

I am guessing the Raspberry Pi Pico bottom silk screen labels for GPIO have been misunderstood (e.g. GP14 and 15 positions) as they are deliberately shifted to miss the mounting holes, they are in the correct sequence. This is quite common on cramped boards and is not an error:

image

Bodmer commented 2 years ago

I had a closer look at the waveshare LCD, I can see that the LCD is pre-wired to the SPI1 port so unfortunately TFT_eSPI cannot be used with that display at the moment. I suspect that a simple change could be done to support the SPI1 port so I will ad this to my TODO list.

Kabron287 commented 2 years ago

Thanks for attention Bodmer!

only GPIO 16, 1, 19 are important as they are valid SPI0 pins.

But this board works with Waveshare examples and also with Picomite MMBasic.

It sounds like you have a custom SPI implementation so you will need to look more closely at that library.

Not at all. I use exactly your library.

And what is important also, that should be stated out I guess: your library does not work with https://github.com/earlephilhower/arduino-pico package even if I manually add ARDUINO_ARCH_RP2040 definition. Maybe due to aforementioned reasons.

Kabron287 commented 2 years ago

I confirm that ILI9341 works fine, but with rather strange output diagnostics. [/code] TFT_eSPI ver = 2.3.84 Processor = Transactions = Yes Interface = SPI Display driver = 9341 Display width = 240 Display height = 320

MOSI = GPIO -119 MISO = GPIO -116 SCK = GPIO -118 TFT_CS = GPIO -1 TFT_DC = GPIO -1 TFT_RST = GPIO -1

Font GLCD loaded Font 2 loaded Font 4 loaded Font 6 loaded Font 7 loaded Font 8 loaded Smooth font enabled

Display SPI frequency = 70.00 [/code]

Bodmer commented 2 years ago

Earle's RP2040 board package seems to work fine for me when SPI0 pins are used.

I found a simple temporary fix to use SPI1 instead, this ONLY works with Earle Philhower's board package, simply add a new #define as follows:

#define spi0 spi1
#define TFT_MISO  12
#define TFT_MOSI  11
#define TFT_SCLK  10
#define TFT_CS     9  // Chip select control pin
#define TFT_DC     8  // Data Command control pin
#define TFT_RST   15  // Reset pin (could connect to Arduino RESET pin)

 #define TFT_BL   13            // LED back-light control pin
 #define TFT_BACKLIGHT_ON LOW  // Level to turn ON back-light (HIGH or LOW)

#define TOUCH_CS  16     // Chip select pin (T_CS) of touch screen

This has been tested on an ILI9341.

Bodmer commented 2 years ago

I have updated the Read_User_Setup sketch to suit the RP2040, thanks for reporting that.

Bodmer commented 2 years ago

The Pico-ResTouch-LCD-3.5 appears to use the same shift register design as the RPi TFT boards. So it will need 16 bit commands instead of the normal 8 bit shifts. To invoke this add the following line to the beginning of your User_Setup file:

#define RPI_DISPLAY_TYPE // 20MHz maximum SPI

Not that the shift register design on typically fails to work above 20MHz, so a lower frequency may need to be invoked.

jas0995 commented 2 years ago

Wow! I go off to fix a 48,000 gallon water leak, and this thread takes off!! :)

@Bodmer, thanks for the work you do on this project!
It looks like one of my biggest point of confusion may have been about the pinout of the Pico and the Display board.

When I first looked at the display and the pico, this is what I found... image

and

image

At first I tried the GPxx numbers from the display, then when that did not work I switched to the pico pin numbers. That is why there is some confusion on the i/o pin numbers I was using. So with the Pico, I should be using the GPxx numbers.

I was planning on working over the code today to switch it from spi0 to spi1, but it looks like you beat me to it.

These are handy little boards available on Amazon, so it is good to hear that you have them on your TODO list to work on.

I will make changes to my Setup61_RP2040_ILI9488 file and let you know how it works.

Thanks for your work on this project! -John

jas0995 commented 2 years ago

@Bodmer OK, It is working now!!

Here is my new setup file: Setup61_RP2040_ILI9488.h

//                            USER DEFINED SETTINGS
//   Set driver type, fonts to be loaded, pins used and SPI control method etc
//
//   See the User_Setup_Select.h file if you wish to be able to define multiple
//   setups and then easily select which setup file is used by the compiler.
//
//   If this file is edited correctly then all the library example sketches should
//   run without the need to make any more changes for a particular hardware setup!
//   Note that some sketches are designed for a particular TFT pixel width/height

// ##################################################################################
//
// Section 1. Call up the right driver file and any options for it
//
// ##################################################################################

// Tell the library to use 8 bit parallel mode (otherwise SPI is assumed)
//#define TFT_PARALLEL_8_BIT

// Display type -  only define if RPi display
#define RPI_DISPLAY_TYPE // 20MHz maximum SPI

// Only define one driver, the other ones must be commented out
//#define ILI9341_DRIVER
//#define ST7735_DRIVER      // Define additional parameters below for this display
//#define ILI9163_DRIVER     // Define additional parameters below for this display
//#define S6D02A1_DRIVER
//#define RPI_ILI9486_DRIVER // 20MHz maximum SPI
//#define HX8357D_DRIVER
//#define ILI9481_DRIVER
//#define ILI9486_DRIVER
#define ILI9488_DRIVER     // WARNING: Do not connect ILI9488 display SDO to MISO if other devices share the SPI bus (TFT SDO does NOT tristate when CS is high)
//#define ST7789_DRIVER      // Full configuration option, define additional parameters below for this display
//#define ST7789_2_DRIVER    // Minimal configuration option, define additional parameters below for this display
//#define R61581_DRIVER
//#define RM68140_DRIVER
//#define ST7796_DRIVER
//#define SSD1963_480_DRIVER
//#define SSD1963_800_DRIVER
//#define SSD1963_800ALT_DRIVER
//#define ILI9225_DRIVER

// Some displays support SPI reads via the MISO pin, other displays have a single
// bi-directional SDA pin and the library will try to read this via the MOSI line.
// To use the SDA line for reading data from the TFT uncomment the following line:

// #define TFT_SDA_READ      // This option is for ESP32 ONLY, tested with ST7789 display only

// For ST7735, ST7789 and ILI9341 ONLY, define the colour order IF the blue and red are swapped on your display
// Try ONE option at a time to find the correct colour order for your display

//  #define TFT_RGB_ORDER TFT_RGB  // Colour order Red-Green-Blue
//  #define TFT_RGB_ORDER TFT_BGR  // Colour order Blue-Green-Red

// For ST7789, ST7735 and ILI9163 ONLY, define the pixel width and height in portrait orientation
// #define TFT_WIDTH  80
// #define TFT_WIDTH  128
// #define TFT_WIDTH  240 // ST7789 240 x 240 and 240 x 320
// #define TFT_HEIGHT 160
// #define TFT_HEIGHT 128
// #define TFT_HEIGHT 240 // ST7789 240 x 240
// #define TFT_HEIGHT 320 // ST7789 240 x 320

// For ST7735 ONLY, define the type of display, originally this was based on the
// colour of the tab on the screen protector film but this is not always true, so try
// out the different options below if the screen does not display graphics correctly,
// e.g. colours wrong, mirror images, or tray pixels at the edges.
// Comment out ALL BUT ONE of these options for a ST7735 display driver, save this
// this User_Setup file, then rebuild and upload the sketch to the board again:

// #define ST7735_INITB
// #define ST7735_GREENTAB
// #define ST7735_GREENTAB2
// #define ST7735_GREENTAB3
// #define ST7735_GREENTAB128    // For 128 x 128 display
// #define ST7735_GREENTAB160x80 // For 160 x 80 display (BGR, inverted, 26 offset)
// #define ST7735_REDTAB
// #define ST7735_BLACKTAB
// #define ST7735_REDTAB160x80   // For 160 x 80 display with 24 pixel offset

// If colours are inverted (white shows as black) then uncomment one of the next
// 2 lines try both options, one of the options should correct the inversion.

// #define TFT_INVERSION_ON
// #define TFT_INVERSION_OFF

// ##################################################################################
//
// Section 2. Define the pins that are used to interface with the display here
//
// ##################################################################################

// If a backlight control signal is available then define the TFT_BL pin in Section 2
// below. The backlight will be turned ON when tft.begin() is called, but the library
// needs to know if the LEDs are ON with the pin HIGH or LOW. If the LEDs are to be
// driven with a PWM signal or turned OFF/ON then this must be handled by the user
// sketch. e.g. with digitalWrite(TFT_BL, LOW);

// #define TFT_BL   32            // LED back-light control pin
// #define TFT_BACKLIGHT_ON HIGH  // Level to turn ON back-light (HIGH or LOW)

// We must use hardware SPI, a minimum of 3 GPIO pins is needed.
// Typical setup for the RP2040 is :
//
// Display SDO/MISO  to RP2040 pin D0 (or leave disconnected if not reading TFT)
// Display LED       to RP2040 pin 3V3 or 5V
// Display SCK       to RP2040 pin D2
// Display SDI/MOSI  to RP2040 pin D3
// Display DC (RS/AO)to RP2040 pin D18 (can use another pin if desired)
// Display RESET     to RP2040 pin D19 (can use another pin if desired)
// Display CS        to RP2040 pin D20 (can use another pin if desired, or GND, see below)
// Display GND       to RP2040 pin GND (0V)
// Display VCC       to RP2040 5V or 3.3V (5v if display has a 5V to 3.3V regulator fitted)
//
// The DC (Data Command) pin may be labelled AO or RS (Register Select)
//
// With some displays such as the ILI9341 the TFT CS pin can be connected to GND if no more
// SPI devices (e.g. an SD Card) are connected, in this case comment out the #define TFT_CS
// line below so it is NOT defined. Other displays such at the ST7735 require the TFT CS pin
// to be toggled during setup, so in these cases the TFT_CS line must be defined and connected.

// For the Pico use these #define lines
#define spi0 spi1
#define TFT_MISO  12
#define TFT_MOSI  11
#define TFT_SCLK  10
#define TFT_CS     9  // Chip select control pin
#define TFT_DC     8  // Data Command control pin
#define TFT_RST   15  // Reset pin (could connect to Arduino RESET pin)

 #define TFT_BL   13            // LED back-light control pin
 #define TFT_BACKLIGHT_ON HIGH  // Level to turn ON back-light (HIGH or LOW)

#define TOUCH_CS  16     // Chip select pin (T_CS) of touch screen

// ##################################################################################
//
// Section 3. Define the fonts that are to be used here
//
// ##################################################################################

// Comment out the #defines below with // to stop that font being loaded
// The ESP8366 and ESP32 have plenty of memory so commenting out fonts is not
// normally necessary. If all fonts are loaded the extra FLASH space required is
// about 17Kbytes. To save FLASH space only enable the fonts you need!

#define LOAD_GLCD   // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
#define LOAD_FONT2  // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
#define LOAD_FONT4  // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
#define LOAD_FONT6  // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm
#define LOAD_FONT7  // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:-.
#define LOAD_FONT8  // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.
//#define LOAD_FONT8N // Font 8. Alternative to Font 8 above, slightly narrower, so 3 digits fit a 160 pixel TFT
#define LOAD_GFXFF  // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts

// Comment out the #define below to stop the SPIFFS filing system and smooth font code being loaded
// this will save ~20kbytes of FLASH
#define SMOOTH_FONT

// ##################################################################################
//
// Section 4. Other options
//
// ##################################################################################

// Define the SPI clock frequency, this affects the graphics rendering speed. Too
// fast and the TFT driver will not keep up and display corruption appears.
// With an ILI9341 display 40MHz works OK, 80MHz sometimes fails
// With a ST7735 display more than 27MHz may not work (spurious pixels and lines)
// With an ILI9163 display 27 MHz works OK.

// #define SPI_FREQUENCY   1000000
// #define SPI_FREQUENCY   5000000
// #define SPI_FREQUENCY  10000000
// #define SPI_FREQUENCY  20000000
//#define SPI_FREQUENCY  32000000
 #define SPI_FREQUENCY    60000000

// Optional reduced SPI frequency for reading TFT
#define SPI_READ_FREQUENCY  20000000

// The XPT2046 requires a lower SPI clock rate of 2.5MHz so we define that here:
#define SPI_TOUCH_FREQUENCY  2500000

Here is the output of TFT_graphicstest_one_lib

17:20:45.394 -> TFT_eSPI library test!
17:20:45.792 -> Benchmark                Time (microseconds)
17:20:45.792 -> Screen fill              433939
17:20:46.225 -> Text                     45342
17:20:46.358 -> Lines                    352488
17:20:48.126 -> Horiz/Vert Lines         48322
17:20:48.260 -> Rectangles (outline)     25218
17:20:48.360 -> Rectangles (filled)      1051232
17:20:49.526 -> Circles (filled)         193651
17:20:49.826 -> Circles (outline)        173807
17:20:49.993 -> Triangles (outline)      77371
17:20:50.160 -> Triangles (filled)       382569
17:20:50.693 -> Rounded rects (outline)  69444
17:20:50.860 -> Rounded rects (filled)   1075347
17:20:52.026 -> Done!

If you would like me to run anymore tests, let me know THANKS!!! -John

Bodmer commented 2 years ago

I have had a look at the official Arduino board package and there seems to be no easy way to persuade it to use SPI port 1 instead of port 0. There appear to be some hooks in place that refers to SPI1 but the code is incomplete.

So the at the moment SPI1 can only be used with Earle Philhower's board package. I think that board package is supperior anyway as it supports dual core use easily, LittleFS and lots of board options. The Arduino package seems to be a WIP.

jas0995 commented 2 years ago

That works for me... I use platformio anyway. Unless I am testing something at least...

And Earle's board package can be used in platformio now.

-John

Kabron287 commented 2 years ago

Bingo! It works on Waveshare ! Colors were initially inverted.

It works on both packages: Earle Philhower's board package clock freq 24 Mhz and Arduino package clock freq 31 Mhz

Bodmer commented 2 years ago

I have updated my Arduino mbed package and now find the SPI1 mod works with that package too.

I will upload a fix later today.

Bodmer commented 2 years ago

I have aded the option to define the port is the setup file:

// For the RP2040 processor define the SPI port channel used (default 0 if undefined)
//#define TFT_SPI_PORT 1 // Set to 0 if SPI0 pins are used, or 1 if spi1 pins used
Kabron287 commented 2 years ago

Another strange thing, now concerning ILI9341. Could be reproduced with Read_User_Setup with light modification (attached). Compiled with Earle Philhower's board package as Generic RP2040 gives SPI clock 24 MHz. Compiled with Classic Arduino MBED package as Raspberry PI Pico gives 64 MHz. Read_User_Setup.zip

Kabron287 commented 2 years ago

Found the reason, I guess. It depends on speed settings in CPU speed menu: 133 MHz gives 24MHz SPI clock, while 125MHz gives 64 MHz. Strange of course.

zpm1066 commented 2 years ago

@Bodmer, The Pico Waveshare HATs work well on SPI1. Thank you.

Bodmer commented 2 years ago

@zpm1066 Thanks for letting me know.

@Kabron287 Unfortunately when the clock increases from125MHz the perpipheral clock gets attached to the USB clock at 48MHz, this means the SPI baud rate gets dropped from 62.5MHz to a maximum of 24MHz. It is possible to boost the peripheral clock by attaching it to a higher frequency source but this messes up the baud rate for UART serial links apparently.

I will soon be releasing a PIO SPI implementation that will allow the SPI frequency to track the overclock frequency, then the processor performance gains from overclocking can be used to greater effect.

Bodmer commented 2 years ago

THE SPI PIO imlementation is now in version 2.4.22 available direct from Github.

Bodmer commented 2 years ago

I have found that it is possble to over-clock the RP2040 processor with Earle's board package and then increase the available SPI frequency by changing the clock source to the UART and SPI. Add this code at the very start of setup() before the tft or Serial ports are initialised:

 uint32_t freq = clock_get_hz(clk_sys);

 // clk_peri does not have a divider, so in and out frequencies must be the same
 clock_configure(clk_peri,
                    0,
                    CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS,
                    freq,
                    freq);

If you have overclocked to 250MHz this will mean that the maximum SPI frrequency is 125MHz but most displays will not work at this speed.

archearth commented 2 months ago

I have gotten the display (Pico-ResTouch-LCD-3.5) to work with my pico W. However, I want to use the SD to load images/gif when a send a MQTT message to the pico. I was able to get to load images and gifs from LittleFS but when I try from the SD it renders in a glitchy slow way. I think the I dont have the right SPI and SD config for the SD and the display to work at the same time. Any ideas what pins to assign to the SD and how to configure it so the the TFT and SD both work at the same time? I am not very familiar with SPI and how all that works. Thanks!