ILI9341 read registers in ESP-IDF
Posted: 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:
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:
Using this code I'm finally got registers:
In Arduino libs reading registers realized the following way:
So the question is: How to read ILI9341 registers using ESP-IDF SPI API?
Thanks in advance!
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:
- uint32_t lcd_get_id(spi_device_handle_t spi)
- {
- //get_id cmd
- lcd_cmd(spi, 0x04);
- spi_transaction_t t;
- memset(&t, 0, sizeof(t));
- t.length=8*3;
- t.flags = SPI_TRANS_USE_RXDATA;
- t.user = (void*)1;
- esp_err_t ret = spi_device_polling_transmit(spi, &t);
- assert( ret == ESP_OK );
- return *(uint32_t*)t.rx_data;
- }
I tried to test my hardware with Arduino and it works excellent:
- #include "SPI.h"
- #include "Adafruit_GFX.h"
- #include "Adafruit_ILI9341.h"
- #define TFT_CS 27
- #define TFT_DC 18
- #define TFT_MOSI 15
- #define TFT_CLK 14
- #define TFT_RST 25
- #define TFT_MISO 2
- #define TFT_LED 26 // GPIO not managed by library
- Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
- void setup() {
- Serial.begin(115200);
- Serial.println("ILI9341 Test!");
- tft.begin();
- pinMode(TFT_LED, OUTPUT);
- uint8_t x = tft.readcommand8(ILI9341_RDMODE, 0);
- Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
- x = tft.readcommand8(ILI9341_RDMADCTL, 0);
- Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
- x = tft.readcommand8(ILI9341_RDPIXFMT, 0);
- Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
- x = tft.readcommand8(ILI9341_RDIMGFMT, 0);
- Serial.print("Image Format: 0x"); Serial.println(x, HEX);
- x = tft.readcommand8(ILI9341_RDSELFDIAG, 0);
- Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
- x = tft.readcommand8(ILI9341_RDDID, 0);
- Serial.print("Identification information: 0x"); Serial.println(x, HEX);
- }
- void loop(void){}
- 10:02:17.198 -> load:0x3fff001c,len:1216
- 10:02:17.198 -> ho 0 tail 12 room 4
- 10:02:17.198 -> load:0x40078000,len:9720
- 10:02:17.198 -> ho 0 tail 12 room 4
- 10:02:17.198 -> load:0x40080400,len:6364
- 10:02:17.198 -> entry 0x400806b8
- 10:02:17.367 -> ILI9341 Test!
- 10:02:18.084 -> Display Power Mode: 0x94
- 10:02:18.084 -> MADCTL Mode: 0x48
- 10:02:18.084 -> Pixel Format: 0x5
- 10:02:18.084 -> Image Format: 0x80
- 10:02:18.084 -> Self Diagnostic: 0xC0
- 10:02:18.084 -> Identification information: 0x0
- uint8_t Adafruit_ILI9341::readcommand8(uint8_t commandByte, uint8_t index) {
- uint8_t data = 0x10 + index;
- Adafruit_SPITFT::sendCommand(0xD9, &data, 1); // Set Index Register
- return Adafruit_SPITFT::readcommand8(commandByte);
- }
- void Adafruit_SPITFT::sendCommand(uint8_t commandByte, uint8_t *dataBytes,
- uint8_t numDataBytes) {
- SPI_BEGIN_TRANSACTION();
- if (_cs >= 0)
- SPI_CS_LOW();
- SPI_DC_LOW(); // Command mode
- spiWrite(commandByte); // Send the command byte
- SPI_DC_HIGH();
- for (int i = 0; i < numDataBytes; i++) {
- spiWrite(*dataBytes); // Send the data bytes
- dataBytes++;
- if ((connection == TFT_PARALLEL) && tft8.wide) {
- SPI_WRITE16(*(uint16_t *)dataBytes);
- dataBytes += 2;
- } else {
- spiWrite(*dataBytes); // Send the data bytes
- dataBytes++;
- }
- }
- if (_cs >= 0)
- SPI_CS_HIGH();
- SPI_END_TRANSACTION();
- }
- uint8_t Adafruit_SPITFT::readcommand8(uint8_t commandByte, uint8_t index) {
- uint8_t result;
- startWrite();
- SPI_DC_LOW(); // Command mode
- spiWrite(commandByte);
- SPI_DC_HIGH(); // Data mode
- do {
- result = spiRead();
- } while (index--); // Discard bytes up to index'th
- endWrite();
- return result;
- }
Thanks in advance!