ILI9341 read registers in ESP-IDF

noiseman
Posts: 2
Joined: Thu Dec 06, 2018 12:33 pm

ILI9341 read registers in ESP-IDF

Postby noiseman » Sat Feb 15, 2020 7:22 am

Hi ALL,

In my app for some purposes I need not only to draw on TFT but also read some registers of ILI9341. There is an example in ESP-IDF which demonstartes of how to use ILI9341: https://github.com/espressif/esp-idf/tr ... spi_master In this example there is a function to Read Display Identification Information:
  1. uint32_t lcd_get_id(spi_device_handle_t spi)
  2. {
  3.     //get_id cmd
  4.     lcd_cmd(spi, 0x04);
  5.  
  6.     spi_transaction_t t;
  7.     memset(&t, 0, sizeof(t));
  8.     t.length=8*3;
  9.     t.flags = SPI_TRANS_USE_RXDATA;
  10.     t.user = (void*)1;
  11.  
  12.     esp_err_t ret = spi_device_polling_transmit(spi, &t);
  13.     assert( ret == ESP_OK );
  14.  
  15.     return *(uint32_t*)t.rx_data;
  16. }
but it always returns zeros and thats OK, but if I change port for example to 0x0A (Read Display Power Mode) it also always returns zeros.... :?

I tried to test my hardware with Arduino and it works excellent:
  1. #include "SPI.h"
  2. #include "Adafruit_GFX.h"
  3. #include "Adafruit_ILI9341.h"
  4.  
  5. #define TFT_CS   27
  6. #define TFT_DC   18
  7. #define TFT_MOSI 15
  8. #define TFT_CLK  14
  9. #define TFT_RST  25
  10. #define TFT_MISO 2
  11. #define TFT_LED   26  // GPIO not managed by library
  12.  
  13. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
  14.  
  15. void setup() {
  16.   Serial.begin(115200);
  17.   Serial.println("ILI9341 Test!");
  18.  
  19.   tft.begin();
  20.   pinMode(TFT_LED, OUTPUT);
  21.  
  22.   uint8_t x = tft.readcommand8(ILI9341_RDMODE, 0);
  23.   Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
  24.   x = tft.readcommand8(ILI9341_RDMADCTL, 0);
  25.   Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
  26.   x = tft.readcommand8(ILI9341_RDPIXFMT, 0);
  27.   Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
  28.   x = tft.readcommand8(ILI9341_RDIMGFMT, 0);
  29.   Serial.print("Image Format: 0x"); Serial.println(x, HEX);
  30.   x = tft.readcommand8(ILI9341_RDSELFDIAG, 0);
  31.   Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
  32.   x = tft.readcommand8(ILI9341_RDDID, 0);
  33.   Serial.print("Identification information: 0x"); Serial.println(x, HEX);
  34. }
  35.  
  36. void loop(void){}
Using this code I'm finally got registers:
  1. 10:02:17.198 -> load:0x3fff001c,len:1216
  2. 10:02:17.198 -> ho 0 tail 12 room 4
  3. 10:02:17.198 -> load:0x40078000,len:9720
  4. 10:02:17.198 -> ho 0 tail 12 room 4
  5. 10:02:17.198 -> load:0x40080400,len:6364
  6. 10:02:17.198 -> entry 0x400806b8
  7. 10:02:17.367 -> ILI9341 Test!
  8. 10:02:18.084 -> Display Power Mode: 0x94
  9. 10:02:18.084 -> MADCTL Mode: 0x48
  10. 10:02:18.084 -> Pixel Format: 0x5
  11. 10:02:18.084 -> Image Format: 0x80
  12. 10:02:18.084 -> Self Diagnostic: 0xC0
  13. 10:02:18.084 -> Identification information: 0x0
In Arduino libs reading registers realized the following way:
  1.  
  2. uint8_t Adafruit_ILI9341::readcommand8(uint8_t commandByte, uint8_t index) {
  3.   uint8_t data = 0x10 + index;
  4.   Adafruit_SPITFT::sendCommand(0xD9, &data, 1); // Set Index Register
  5.   return Adafruit_SPITFT::readcommand8(commandByte);
  6. }
  7.  
  8. void Adafruit_SPITFT::sendCommand(uint8_t commandByte, uint8_t *dataBytes,
  9.                                   uint8_t numDataBytes) {
  10.   SPI_BEGIN_TRANSACTION();
  11.   if (_cs >= 0)
  12.     SPI_CS_LOW();
  13.  
  14.   SPI_DC_LOW();          // Command mode
  15.   spiWrite(commandByte); // Send the command byte
  16.  
  17.   SPI_DC_HIGH();
  18.   for (int i = 0; i < numDataBytes; i++) {
  19.     spiWrite(*dataBytes); // Send the data bytes
  20.     dataBytes++;
  21.     if ((connection == TFT_PARALLEL) && tft8.wide) {
  22.       SPI_WRITE16(*(uint16_t *)dataBytes);
  23.       dataBytes += 2;
  24.     } else {
  25.       spiWrite(*dataBytes); // Send the data bytes
  26.       dataBytes++;
  27.     }
  28.   }
  29.  
  30.   if (_cs >= 0)
  31.     SPI_CS_HIGH();
  32.   SPI_END_TRANSACTION();
  33. }
  34.  
  35. uint8_t Adafruit_SPITFT::readcommand8(uint8_t commandByte, uint8_t index) {
  36.   uint8_t result;
  37.   startWrite();
  38.   SPI_DC_LOW(); // Command mode
  39.   spiWrite(commandByte);
  40.   SPI_DC_HIGH(); // Data mode
  41.   do {
  42.     result = spiRead();
  43.   } while (index--); // Discard bytes up to index'th
  44.   endWrite();
  45.   return result;
  46. }
So the question is: How to read ILI9341 registers using ESP-IDF SPI API?

Thanks in advance!

SinglWolf
Posts: 3
Joined: Sat Dec 14, 2019 2:30 pm

Re: ILI9341 read registers in ESP-IDF

Postby SinglWolf » Thu Jun 10, 2021 2:19 am

The situation is similar with the display on ILI9488. Please clarify this issue.

Who is online

Users browsing this forum: No registered users and 51 guests