Black Pill with adafruit ST7789

Post here first, or if you can't find a relevant section!
Post Reply
heretop
Posts: 39
Joined: Sun Jun 20, 2021 2:09 pm

Black Pill with adafruit ST7789

Post by heretop »

I am attempting to use black pill (weact v3.1, newest stm32core installed through arduino IDE, https://github.com/WeActTC/MiniSTM32F4x1) with 2 inch ST7789 IPS display (https://www.waveshare.com/2inch-lcd-module.htm) using the Adafruit_ST7789 (https://github.com/adafruit/Adafruit-ST ... t_ST7789.h) under Adafruit_GFX. I have attached my code here. My code works fine with the arduino nano. However it does not work on black pill. Arduino IDE successfully uploaded the program to the black pill. But the display is always black (black light is on, but nothing shows). I tried to solve it on my own, like change the pin number and make sure the pin is correctly connected, look into the library but does not know what is wrong. May I know anyone know what wrong with this? Thanks a lot!

Code: Select all

#include <SPI.h>
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789

#define TFT_CS    PA4//8
#define TFT_DC    PB0//9
#define TFT_RST   PB1//10
#define TFT_MOSI    PA7//11
#define TFT_SCLK    PA5//13

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

void setup(void) {
  tft.init(240, 320);           // Init ST7789 320x240
  tft.fillScreen(ST77XX_BLACK);
  tft.setRotation(1);
  tft.setTextWrap(false);
  tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
  tft.setTextSize(5);
  tft.setCursor(0, 75);
  tft.println("CODE TEST");
  tft.setCursor(0, 125);
  tft.println("HELLO");

  pinMode(PC13, OUTPUT); // LED connect to pin PC13
}

void loop() {
  digitalWrite(PC13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for 100mS
  digitalWrite(PC13, LOW);    // turn the LED off by making the voltage LOW
  delay(100);               // wait for 100mS
}
My computer development environment is: Windows 10 19042.1052, arduino IDE 1.8.13, core 2.0.0, upload method DFU, adafruit st7735 and st7789 library 1.7.3, adafruit gfx library 1.10.10
arduinoMethod.png
arduinoMethod.png (10.52 KiB) Viewed 6416 times
by fpiSTM » Thu Aug 26, 2021 5:00 pm
I've found the issue, see: viewtopic.php?p=8212#p8212
Go to full post
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: Black Pill with adafruit ST7789

Post by dannyf »

My guess would be timing mostly. The stm32 is much faster than your avr and the code may require adjustment. Look up the display and see where the timing is and how the code implement such timing - likely by looking around. If so, you need to slow them down or worse yet make them optimization resistant.

If a hardware module is used (i2C or GPIO - to simulate pull up for example) you may need to adjust to the new targets too.
heretop
Posts: 39
Joined: Sun Jun 20, 2021 2:09 pm

Re: Black Pill with adafruit ST7789

Post by heretop »

dannyf wrote: Mon Jun 21, 2021 2:41 pm My guess would be timing mostly. The stm32 is much faster than your avr and the code may require adjustment. Look up the display and see where the timing is and how the code implement such timing - likely by looking around. If so, you need to slow them down or worse yet make them optimization resistant.

If a hardware module is used (i2C or GPIO - to simulate pull up for example) you may need to adjust to the new targets too.
Thanks for reply. I found that adafruit already include code to make their stm32 feather (Adafruit Feather STM32F405 Express) to work with their library. In their library, I found STM32_FEATHER shown at two places: "Adafruit_ST77xx.cpp" (https://github.com/adafruit/Adafruit-ST ... ST77xx.cpp)

Code: Select all

#ifndef ARDUINO_STM32_FEATHER
#include "pins_arduino.h"
#include "wiring_private.h"
#endif
under "Adafruit_SPITFT.h" (https://github.com/adafruit/Adafruit-GF ... t_SPITFT.h)

Code: Select all

#elif defined(ARDUINO_STM32_FEATHER) // WICED
typedef class HardwareSPI SPIClass;        ///< SPI is a bit odd on WICED
typedef uint32_t ADAGFX_PORT_t;            ///< PORT values are 32-bit
#elif defined(__arm__)
I think these stuff does not relate to the speed. I was think I can adapt these methods for my blackpill STM32F411.

However, I try to pretend my board as ARDUINO_STM32_FEATHER by add

Code: Select all

#define ARDUINO_STM32_FEATHER 1
, I got error:

Code: Select all

Documents\Arduino\libraries\Adafruit_GFX_Library/Adafruit_SPITFT.h:34:27: error: conflicting declaration 'typedef class HardwareSPI SPIClass'
   34 | typedef class HardwareSPI SPIClass;        ///< SPI is a bit odd on WICED
      |                           ^~~~~~~~
In file included from Documents\Arduino\libraries\Adafruit_GFX_Library/Adafruit_SPITFT.h:26,
                 from Documents\Arduino\libraries\Adafruit_ST7735_and_ST7789_Library/Adafruit_ST77xx.h:31,
                 from Documents\Arduino\libraries\Adafruit_ST7735_and_ST7789_Library/Adafruit_ST7789.h:4,
                 from stm32duino\sketch_file\midi_controller_adafruit\midi_controller_adafruit.ino:5:
AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.0.0\libraries\SPI\src/SPI.h:114:7: note: previous declaration as 'class SPIClass'
  114 | class SPIClass {
      |       ^~~~~~~~
exit status 1
Error compiling for board Generic STM32F4 series.
I comment out the the

Code: Select all

typedef class HardwareSPI SPIClass;        ///< SPI is a bit odd on WICED
. This time the code can compile and upload without error, but the display is still not working.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Black Pill with adafruit ST7789

Post by ag123 »

well, you do need SPI,
https://github.com/stm32duino/Arduino_C ... raries/SPI
the reference is here
https://github.com/stm32duino/wiki/wiki/API#spi
an example is found here
https://github.com/stm32duino/wiki/wiki/API#example-4

among those things to try is to get comms working across SPI, that would involve for instance reading registers off the LCD
it should at least get you some values u'd think they are sane than arbitrary noise

one of the 'tricky' part involves spi setup, selecting the appropriate spi interface, if in doubt try with SPI 1 first, normally that is the default SPI,
and initialization is 'easier'. it is probably taken cared of by the library.

i kind of 'ported' a ili9341 spi lcd lib over to stm32duino sometime back, couldn't remember various things in context as it has been a while
you could use it as an 'example'
https://github.com/ag88/Adafruit_ILI9341_SPI_stm32duino
in this example, the graphics test sketch as well as the library itself is in src directory

hope it helps
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: Black Pill with adafruit ST7789

Post by dannyf »

if you have a debugger (jtag or stlink) you can step through the code and inspect the registers to see what's happening.

short of that, use the uart of a more "static" debugger: for example printing out the values of the spi - related registers to see if they are taking on values as expected. You can also insert uart prints in the initialization loop to "slow" it down and see what's actually send to the lcd, or if there is anything back from the lcd.

the first is really to go through the initialization piece and make sure that the lcd is correctly initialized.

again, the datasheets (the lcd's and the host's) are your best friend here.
heretop
Posts: 39
Joined: Sun Jun 20, 2021 2:09 pm

Re: Black Pill with adafruit ST7789

Post by heretop »

dannyf wrote: Mon Jun 21, 2021 4:56 pm if you have a debugger (jtag or stlink) you can step through the code and inspect the registers to see what's happening.

short of that, use the uart of a more "static" debugger: for example printing out the values of the spi - related registers to see if they are taking on values as expected. You can also insert uart prints in the initialization loop to "slow" it down and see what's actually send to the lcd, or if there is anything back from the lcd.
Thanks for the suggestion. Currently, I am using the DFU mode. Every time I upload the program, the board will be out of DFU mode, and arduino IDE will lose its connection to the board. It is indeed very difficult that I dont have any reliable debug method. Maybe I really need to buy a stlink now.
ag123 wrote: Mon Jun 21, 2021 3:47 pm one of the 'tricky' part involves spi setup, selecting the appropriate spi interface, if in doubt try with SPI 1 first, normally that is the default SPI,
and initialization is 'easier'. it is probably taken cared of by the library.
Thanks for the reply and links. I reread their library especially the part about SPI, I found there is another board related part (but without ARDUINO_STM32_FEATHER):

Code: Select all

inline void Adafruit_SPITFT::SPI_BEGIN_TRANSACTION(void) {
  if (connection == TFT_HARD_SPI) {
#if defined(SPI_HAS_TRANSACTION)
    hwspi._spi->beginTransaction(hwspi.settings);
#else // No transactions, configure SPI manually...
#if defined(__AVR__) || defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32F1)
    hwspi._spi->setClockDivider(SPI_CLOCK_DIV2);
#elif defined(__arm__)
    hwspi._spi->setClockDivider(11);
#elif defined(ESP8266) || defined(ESP32)
    hwspi._spi->setFrequency(hwspi._freq);
#elif defined(RASPI) || defined(ARDUINO_ARCH_STM32F1)
    hwspi._spi->setClock(hwspi._freq);
#endif
    hwspi._spi->setBitOrder(MSBFIRST);
    hwspi._spi->setDataMode(hwspi._mode);
#endif // end !SPI_HAS_TRANSACTION
  }
}
I think this might be the part that my code not working.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Black Pill with adafruit ST7789

Post by ag123 »

if i remembered correctly the default SPI has the variable name SPI, which is normally SPI 1.
i kind of remembered that of Adafruit lcd libraries, during the lcd setup, i.e. when you create the variable for the library object, if SPI interface is not specified. e.g.

Code: Select all

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
it defaults to SPI (i.e. SPI1). the default SPI is normally declared once you

Code: Select all

#include <SPI.h>
.
so one thing is to just review the pins to see that it is connected at the correct SPI pins.
those defines such as SPI_HAS_TRANSACTION are useful and you can define them so that the lcd library would use it.

to change parameters or use a different SPI class it could be done in the

Code: Select all

tft_library.begin(SPIClass spi);
call. e.g. you would need to declare your own spi class (possibly with different parameters) and calling begin() with a different spi class object.
heretop
Posts: 39
Joined: Sun Jun 20, 2021 2:09 pm

Re: Black Pill with adafruit ST7789

Post by heretop »

ag123 wrote: Tue Jun 22, 2021 1:32 am call. e.g. you would need to declare your own spi class (possibly with different parameters) and calling begin() with a different spi class object.
I found there are three different function call methods for the Adafruit_ST7789. One of them take SPIClass as input. I tried all three methods with two different SPI and make sure the pins changing accordingly. All got compiled and uploaded, but no display.

Code: Select all

#define TFT_CS    PA4//8
#define TFT_DC    PB0//9
#define TFT_RST   PB1//10
#define TFT_MOSI    PB15//PA7//11
#define TFT_SCLK    PB13//PA5//13

//SPIClass SPI_1(PA7, PA6, PA5);
SPIClass SPI_2(PB15, PB14, PB13);

//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
Adafruit_ST7789 tft = Adafruit_ST7789(&SPI_2, TFT_CS, TFT_DC, TFT_RST);
//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Black Pill with adafruit ST7789

Post by fpiSTM »

Hi @heretop
I had an Adafruit shield with ST7735 so I was able to test the Adafruit library.
And it works fine with the graphic test example.
Did you try with the default sketch example from the library?
heretop
Posts: 39
Joined: Sun Jun 20, 2021 2:09 pm

Re: Black Pill with adafruit ST7789

Post by heretop »

fpiSTM wrote: Tue Jun 22, 2021 7:50 am Hi @heretop
I had an Adafruit shield with ST7735 so I was able to test the Adafruit library.
And it works fine with the graphic test example.
Did you try with the default sketch example from the library?
Yeah, I just tried the graphic test example one more time. My code is also based on their example. I am planning to get a NUCLEO-F411RE, so I will get a st link and another f411 board other than the black pill to test my code XD.
Here is the pin connection for my board and display. The cable color between display and board are matching.
IMG_4941.jpg
IMG_4941.jpg (86.91 KiB) Viewed 6302 times
Post Reply

Return to “General discussion”