ST7789 DMA configuration: Yellow displayed instead of red

Ygles09
Posts: 1
Joined: Mon Jun 10, 2024 1:01 pm

ST7789 DMA configuration: Yellow displayed instead of red

Postby Ygles09 » Mon Jun 10, 2024 1:15 pm

Hello,
I have a ESP32-S3-R8N16 board with a TFT screen 170x320. When I use Adafruit_GFX library everything works fine but I want to use DMA on the screen to have more reactivity and I try to display many thing but come back to the beginning and now I just want to display red screen but I have this:
IMG_20240610_151102_6.jpg
IMG_20240610_151102_6.jpg (212.38 KiB) Viewed 461 times


I don't understand where is my error, can you help me please ? You can find my code here:


  1. #include <SPI.h>
  2. #include "driver/spi_master.h"
  3.  
  4. #define TFT_CS 10
  5. #define TFT_DC 11
  6. #define TFT_MOSI 13
  7. #define TFT_SCLK 12
  8. #define TFT_RST 1
  9. #define TFT_BL 14
  10.  
  11. #define WIDTH 170
  12. #define HEIGHT 320
  13.  
  14. spi_device_handle_t spi;
  15.  
  16. void setup() {
  17.   Serial.begin(115200);
  18.  
  19.   // Initialiser les broches
  20.   pinMode(TFT_CS, OUTPUT);
  21.   pinMode(TFT_DC, OUTPUT);
  22.   pinMode(TFT_RST, OUTPUT);
  23.   pinMode(TFT_BL, OUTPUT);
  24.   digitalWrite(TFT_CS, HIGH);
  25.   digitalWrite(TFT_BL, HIGH);
  26.  
  27.   // Configuration du SPI
  28.   spi_bus_config_t buscfg = {
  29.     .mosi_io_num = TFT_MOSI,
  30.     .miso_io_num = -1,
  31.     .sclk_io_num = TFT_SCLK,
  32.     .quadwp_io_num = -1,
  33.     .quadhd_io_num = -1,
  34.     .max_transfer_sz = WIDTH * HEIGHT * 2
  35.   };
  36.  
  37.   spi_device_interface_config_t devcfg = {
  38.     .mode = 0,
  39.     .clock_speed_hz = 20 * 1000 * 1000,
  40.     .spics_io_num = TFT_CS,
  41.     .queue_size = 7,
  42.     .pre_cb = NULL,
  43.     .post_cb = NULL,
  44.   };
  45.  
  46.   esp_err_t ret = spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO);
  47.   if (ret != ESP_OK) {
  48.     Serial.printf("Failed to initialize bus: %s\n", esp_err_to_name(ret));
  49.     while (1)
  50.       ;
  51.   }
  52.  
  53.   ret = spi_bus_add_device(SPI3_HOST, &devcfg, &spi);
  54.   if (ret != ESP_OK) {
  55.     Serial.printf("Failed to add device: %s\n", esp_err_to_name(ret));
  56.     while (1)
  57.       ;
  58.   }
  59.  
  60.   // Reset
  61.   digitalWrite(TFT_RST, LOW);
  62.   delay(100);
  63.   digitalWrite(TFT_RST, HIGH);
  64.   delay(100);
  65.  
  66.   // Init
  67.   sendCommand(0x01);      // Software reset
  68.   delay(150);
  69.   sendCommand(0x11);      // Sleep out
  70.   delay(500);
  71.   sendCommand(0x36);      // Memory data access control
  72.   sendData(0x00);
  73.   sendCommand(0x3A);      // Interface pixel format
  74.   sendData(0x55);         // 16-bit/pixel
  75.   sendCommand(0x29);      // Display on
  76.   sendCommand(0x13);      // Normal display
  77. }
  78.  
  79. void sendCommand(uint8_t cmd) {
  80.   digitalWrite(TFT_DC, LOW);
  81.   digitalWrite(TFT_CS, LOW);
  82.   spi_transaction_t t;
  83.   memset(&t, 0, sizeof(t));
  84.   t.length = 8;
  85.   t.tx_buffer = &cmd;
  86.   spi_device_transmit(spi, &t);
  87.   digitalWrite(TFT_CS, HIGH);
  88. }
  89.  
  90. void sendData(uint8_t data) {
  91.   digitalWrite(TFT_DC, HIGH);
  92.   digitalWrite(TFT_CS, LOW);
  93.   spi_transaction_t t;
  94.   memset(&t, 0, sizeof(t));
  95.   t.length = 8;
  96.   t.tx_buffer = &data;
  97.   spi_device_transmit(spi, &t);
  98.   digitalWrite(TFT_CS, HIGH);
  99. }
  100.  
  101. void loop() {
  102.   sendCommand(0x2A);  // Column address set
  103.   sendData(0x00);
  104.   sendData(0x23);     // Offset because there is 35px before and after ((240 - 170) / 2)
  105.   sendData(0x00);
  106.   sendData(0xCC);
  107.  
  108.   sendCommand(0x2B);  // Row address set
  109.   sendData(0x00);
  110.   sendData(0x00);
  111.   sendData((HEIGHT - 1) >> 8);
  112.   sendData((HEIGHT - 1) & 0xFF);
  113.  
  114.   sendCommand(0x2C);  // Memory write
  115.  
  116.   digitalWrite(TFT_CS, LOW);
  117.   delay(100);
  118.   digitalWrite(TFT_DC, HIGH);
  119.   delay(100);
  120.  
  121.   uint16_t lineBuffer[WIDTH];
  122.   for (int y = 0; y < HEIGHT; y++) {
  123.     for (int x = 0; x < WIDTH; x++) {
  124.       // RED in RGB565
  125.       lineBuffer[x] = 0xF800;
  126.     }
  127.     sendLine(lineBuffer);
  128.   }
  129.  
  130.   delay(500);
  131. }
  132.  
  133. void sendLine(uint16_t* lineBuffer) {
  134.   digitalWrite(TFT_DC, HIGH);
  135.   digitalWrite(TFT_CS, LOW);
  136.  
  137.   spi_transaction_t t;
  138.   memset(&t, 0, sizeof(t));
  139.   t.length = WIDTH * 16;
  140.   t.tx_buffer = lineBuffer;
  141.  
  142.   spi_device_transmit(spi, &t);
  143.  
  144.   digitalWrite(TFT_CS, HIGH);
  145. }



Thanks a lot

Ygles ;)

Who is online

Users browsing this forum: No registered users and 73 guests