I think the OP was trying to solve some problem, but he/she was confused.
So I looked up the ST7789 datasheet and found that MADCTL (Memory Data Access Control) not only controls mirroring/rotation, but also RGB
and GBR
.
Then, I analyzed the source code of Adafruit and TFT_eSPI and wrote a test program.
My testbed
Test program
This program is a little hard to read, but it uses Adafruit and TFT_eSPI to control RGB
and GBR
using low-level functions with MADCTL.
#include <Arduino.h>
#include <SPI.h>
// Seeed Studio XIAO ESP32-S3
// https://github.com/espressif/arduino-esp32/blob/master/variants/XIAO_ESP32S3/pins_arduino.h
#define TFT_SCLK SCK
#define TFT_MISO MISO
#define TFT_MOSI MOSI
#define TFT_CS SS
#define TFT_RST D1
#define TFT_DC D0
// Defalult orientation: Portrait
#define TFT_WIDTH 240
#define TFT_HEIGHT 320
/*================================================================================
* STEP1: Enable or Disable for MADCTL functional test
*================================================================================*/
#define TEST_MADCTL false
/*================================================================================
* STEP2: Select GFX library
*================================================================================*/
#if 1
/*---------------------------------------------------
* Adafruit GFX Library
* https://github.com/adafruit/Adafruit-GFX-Library
* https://github.com/adafruit/Adafruit-ST7735-Library/
*---------------------------------------------------*/
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
#define SCREEN_ROTATION 0
#define INVERT_DSIALAY false
// https://github.com/adafruit/Adafruit-ST7735-Library/blob/master/Adafruit_ST7789.cpp#L40-L77
static const uint8_t PROGMEM
generic_st7789[] = { // Init commands for 7789 screens
9, // 9 commands in list:
ST77XX_SWRESET, ST_CMD_DELAY, // 1: Software reset, no args, w/delay
150, // ~150 ms delay
ST77XX_SLPOUT, ST_CMD_DELAY, // 2: Out of sleep mode, no args, w/delay
10, // 10 ms delay
ST77XX_COLMOD, 1 + ST_CMD_DELAY, // 3: Set color mode, 1 arg + delay:
0x55, // 16-bit color
10, // 10 ms delay
ST77XX_MADCTL, 1, // 4: Mem access ctrl (directions), 1 arg:
0x08, // Row/col addr, bottom-top refresh
ST77XX_CASET, 4, // 5: Column addr set, 4 args, no delay:
0x00,
0, // XSTART = 0
0,
240, // XEND = 240
ST77XX_RASET, 4, // 6: Row addr set, 4 args, no delay:
0x00,
0, // YSTART = 0
320 >> 8,
320 & 0xFF, // YEND = 320
ST77XX_INVON, ST_CMD_DELAY, // 7: hack
10,
ST77XX_NORON, ST_CMD_DELAY, // 8: Normal display on, no args, w/delay
10, // 10 ms delay
ST77XX_DISPON, ST_CMD_DELAY, // 9: Main screen turn on, no args, delay
10
}; // 10 ms delay
#else
/*---------------------------------------------------
* TFT_eSPI Library
* https://github.com/Bodmer/TFT_eSPI
*---------------------------------------------------*/
#include <TFT_eSPI.h>
#define SCREEN_ROTATION 0
#define INVERT_DSIALAY false
#define ST77XX_BLUE TFT_BLUE
#define ST77XX_WHITE TFT_WHITE
TFT_eSPI tft = TFT_eSPI();
#endif // Adafruit or TFT_eSPI
/*================================================================================
* STEP3: Run setup() and loop()
*================================================================================*/
void setup() {
Serial.begin(115200);
#if defined (_ADAFRUIT_GFX_H)
tft.init(TFT_WIDTH, TFT_HEIGHT);
tft.invertDisplay(INVERT_DSIALAY);
tft.setRotation(SCREEN_ROTATION);
#if TEST_MADCTL
// Adafruit_SPITFT Send Command handles complete sending of commands and data
// https://github.com/adafruit/Adafruit-GFX-Library/blob/master/Adafruit_SPITFT.cpp#L1973-L2002
tft.sendCommand(ST77XX_MADCTL, generic_st7789, 1);
#endif // TEST_MADCTL
#elif defined (_TFT_eSPIH_)
tft.init();
tft.invertDisplay(INVERT_DSIALAY);
tft.setRotation(SCREEN_ROTATION);
#if TEST_MADCTL
// Overwrite MADCTL directly after library setup
// https://github.com/Bodmer/TFT_eSPI/blob/master/TFT_eSPI.cpp#L975-L1005
// https://github.com/Bodmer/TFT_eSPI/blob/master/TFT_Drivers/ST7789_Defines.h#L69-L78
tft.writecommand(ST7789_MADCTL); // MADCTL command (0x36)
tft.writedata(TFT_MAD_RGB); // TFT_MAD_RGB: 0x00, TFT_MAD_BGR: 0x08
#endif // TEST_MADCTL
#endif // _ADAFRUIT_GFX_H or _TFT_eSPIH_
// Background and text
tft.fillScreen(ST77XX_BLUE);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2);
tft.setCursor(50, 100);
tft.print("Hello, World!");
}
void loop() {}
Result of Adafruit
#define TEST_MADCTL false
#define TEST_MADCTL true
When I run this program, it fails to rewrite the flash unless I start the bootloader. This may be due to the use of low-level functions.
@cpaul09 , I brought up TFT_eSPI because we talked about RGB
and BGR
, but like @horace did, why not go back to basics and start with something that works with Adafruit?
And please post your full scketch (without MADCTL
part). Something should work.