acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

TFT_Read_Reg.ino (4439B)

      1 /*
      2   Test the library readcommand8 member function
      3 
      4   This sketch reports via the Serial Monitor window
      5 
      6   #########################################################################
      7   ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
      8   ######       TO SELECT THE FONTS AND PINS YOU USE, SEE ABOVE       ######
      9   #########################################################################
     10 
     11   Created by Bodmer 14/1/17
     12 */
     13 
     14 //====================================================================================
     15 //                                  Libraries
     16 //====================================================================================
     17 
     18 #include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip
     19 #include <SPI.h>
     20 
     21 TFT_eSPI tft = TFT_eSPI();  // Invoke library
     22 
     23 //====================================================================================
     24 //                                    Setup
     25 //====================================================================================
     26 
     27 void setup(void) {
     28   Serial.begin(115200);
     29 
     30   tft.init();
     31   tft.setRotation(2);
     32 }
     33 
     34 //====================================================================================
     35 //                                    Loop
     36 //====================================================================================
     37 
     38 void loop() {
     39 
     40   tft.fillScreen(TFT_BLUE);
     41   tft.setCursor(0, 0, 2);
     42   // Set the font colour to be white with a black background
     43   tft.setTextColor(TFT_WHITE, TFT_BLACK);
     44   // We can now plot text on screen using the "print" class
     45   tft.println("Hello World!");
     46 
     47   delay(2000);
     48 
     49   // OK, now it has been shown that the display is working reset it to defaults
     50   // This will make the screen go "white" but we can still read registers
     51 
     52   digitalWrite(TFT_RST, LOW);
     53   delay(10);
     54   digitalWrite(TFT_RST, HIGH);
     55   delay(10);
     56 
     57   printSubset(); // Print a useful subset of the readable registers
     58 
     59   readTest(); // Test 8, 16 and 32 bit reads and index on the ID register
     60 
     61   //printRange32(0x00, 0xFF); // Print a range of registers (32 bits, index = 0)
     62 
     63   delay(2000);
     64   while (1) yield();
     65 }
     66 
     67 //====================================================================================
     68 //                                  Supporting functions
     69 //====================================================================================
     70 
     71 void readTest(void)
     72 {
     73   Serial.println(); Serial.println("Test 8, 16 and 32 bit reads and the index...");
     74   // Test 8, 16 and 32 bit reads and index
     75   // Note at index 0 the register values are typically undefined (Bxxxxxxxx)
     76   Serial.println(tft.readcommand8(ILI9341_RDID4, 2), HEX);
     77   Serial.println(tft.readcommand16(ILI9341_RDID4, 2), HEX);
     78   Serial.println(tft.readcommand32(ILI9341_RDID4, 0), HEX);
     79 }
     80 
     81 //====================================================================================
     82 
     83 void printRange32(uint8_t readStart, uint8_t readEnd)
     84 {
     85   Serial.print("Registers from "); Serial.print(readStart, HEX);
     86   Serial.print(" to "); Serial.println(readEnd, HEX);
     87 
     88   for (uint8_t cmd_reg = readStart; cmd_reg < readEnd; cmd_reg++) {
     89     readRegister(cmd_reg, 4, 0);
     90   }
     91 }
     92 
     93 //====================================================================================
     94 
     95 void printSubset(void)
     96 {
     97   Serial.println();  Serial.println();
     98   readRegister(ILI9341_RDDID, 3, 1);
     99   readRegister(ILI9341_RDDST, 4, 1);
    100   readRegister(ILI9341_RDMODE, 1, 1);
    101   readRegister(ILI9341_RDMADCTL, 1, 1);
    102   readRegister(ILI9341_RDPIXFMT, 1, 1);
    103   readRegister(ILI9341_RDSELFDIAG, 1, 1);
    104   readRegister(ILI9341_RAMRD, 3, 1);
    105 
    106   readRegister(ILI9341_RDID1, 1, 1);
    107   readRegister(ILI9341_RDID2, 1, 1);
    108   readRegister(ILI9341_RDID3, 1, 1);
    109   readRegister(ILI9341_RDIDX, 1, 1); // ?
    110   readRegister(ILI9341_RDID4, 3, 1);  // ID
    111 }
    112 
    113 //====================================================================================
    114 
    115 uint32_t readRegister(uint8_t reg, int16_t bytes, uint8_t index)
    116 {
    117   uint32_t  data = 0;
    118 
    119   while (bytes > 0) {
    120     bytes--;
    121     data = (data << 8) | tft.readcommand8(reg, index);
    122     index++;
    123   }
    124 
    125   Serial.print("Register 0x");
    126   if (reg < 0x10) Serial.print("0");
    127   Serial.print(reg , HEX);
    128 
    129   Serial.print(": 0x");
    130 
    131   // Add leading zeros as needed
    132   uint32_t mask = 0x1 << 28;
    133   while (data < mask && mask > 0x1) {
    134     Serial.print("0");
    135     mask = mask >> 4;
    136   }
    137 
    138   Serial.println(data, HEX);
    139 
    140   return data;
    141 }
    142 
    143 //====================================================================================