KSM edits to RA8875

Dependents:   Liz_Test_Code

Revision:
66:468a11f05580
Parent:
62:ba5d33438fda
Child:
67:9f834f0ff97d
--- a/RA8875.cpp	Sun Aug 17 15:38:51 2014 +0000
+++ b/RA8875.cpp	Sun Aug 31 14:37:37 2014 +0000
@@ -241,6 +241,7 @@
 {
     for (int i=0; i<METRICCOUNT; i++)
         metrics[i] = 0;
+    idlecounter = 0;
 }
 
 
@@ -252,13 +253,18 @@
         metrics[method] = elapsed;
 }
 
+void RA8875::CountIdleTime(uint32_t t)
+{
+    idlecounter += t;
+}
 
 void RA8875::ReportPerformance(Serial & pc)
 {
     pc.printf("\r\nPerformance Metrics\r\n");
     for (int i=0; i<METRICCOUNT; i++) {
         pc.printf("%10d uS %s\r\n", metrics[i], metricsName[i]);
-    }    
+    }
+    pc.printf("Idle Counter: %u\r\n", idlecounter);
 }
 #endif
 
@@ -355,12 +361,41 @@
     unsigned char data;
     
     select(true);
-    spiwrite(0xC0);
+    spiwrite(0xC0);         // These two bits are for the special "Status Read" [STSR]
     data = spiread();
     select(false);
     return data;
 }
 
+/// @todo add a timeout and return false, but how long
+/// to wait since some operations can be very long.
+bool RA8875::_WaitWhileBusy(uint8_t mask)
+{
+    int i = 20000/POLLWAITuSec; // 20 msec max
+
+    while (i-- && ReadStatus() & mask)
+        wait_us(POLLWAITuSec);
+    if (i)
+        return true;
+    else
+        return false;
+}
+
+/// @todo add a timeout and return false, but how long
+/// to wait since some operations can be very long.
+bool RA8875::_WaitWhileReg(uint8_t reg, uint8_t mask)
+{
+    int i = 20000/POLLWAITuSec; // 20 msec max
+
+    while (i-- && ReadCommand(reg) & mask)
+        wait_us(POLLWAITuSec);
+    if (i)
+        return true;
+    else
+        return false;
+}
+
+
 
 dim_t RA8875::fontwidth(void)
 {
@@ -599,8 +634,7 @@
             WriteCommand(0x02);                 // RA8875 Internal Fonts
             select(true);
             WriteData(c);
-            while (ReadStatus() & 0x80)
-                wait_us(POLLWAITuSec);          // Chk_Busy();
+            _WaitWhileBusy(0x80);
             select(false);
         }
     }
@@ -657,8 +691,7 @@
         while (*string != '\0') {
             WriteData(*string);
             ++string;
-            while (ReadStatus() & 0x80)
-                wait_us(POLLWAITuSec);            // Chk_Busy();
+            _WaitWhileBusy(0x80);
         }
         select(false);
         #endif
@@ -729,8 +762,7 @@
 {
     PERFORMANCE_RESET;
     WriteCommand(0x8E, (region == ACTIVEWINDOW) ? 0xC0 : 0x80);
-    while (ReadCommand(0x8E) & 0x80)
-        wait_us(POLLWAITuSec);
+    _WaitWhileReg(0x8E, 0x80);
     REGISTERPERFORMANCE(PRF_CLS);
     return noerror;
 }
@@ -854,8 +886,7 @@
         unsigned char drawCmd = 0x00;       // Line
         WriteCommand(0x90, drawCmd);
         WriteCommand(0x90, 0x80 + drawCmd); // Start drawing.
-        while (ReadCommand(0x90) & 0x80)    // await completion.
-            wait_us(POLLWAITuSec);
+        _WaitWhileReg(0x90, 0x80);
     }
     REGISTERPERFORMANCE(PRF_DRAWLINE);
     return noerror;
@@ -897,8 +928,7 @@
             drawCmd |= 0x20;
         WriteCommand(0x90, drawCmd);
         WriteCommand(0x90, 0x80 + drawCmd); // Start drawing.
-        while (ReadCommand(0x90) & 0x80)    // await completion.
-            wait_us(POLLWAITuSec);
+        _WaitWhileReg(0x90, 0x80);
     }
     REGISTERPERFORMANCE(PRF_DRAWRECTANGLE);
     return noerror;
@@ -950,9 +980,7 @@
             drawCmd |= 0x40;
         WriteCommand(0xA0, drawCmd);
         WriteCommand(0xA0, 0x80 + drawCmd); // Start drawing.
-        while (ReadCommand(0xA0) & 0x80) {   // await completion.
-            wait_us(POLLWAITuSec);
-        }
+        _WaitWhileReg(0xA0, 0x80);
     }
     REGISTERPERFORMANCE(PRF_DRAWROUNDEDRECTANGLE);
     return ret;
@@ -1001,8 +1029,7 @@
             drawCmd |= 0x20;
         WriteCommand(0x90, drawCmd);
         WriteCommand(0x90, 0x80 + drawCmd); // Start drawing.
-        while (ReadCommand(0x90) & 0x80)    // await completion.
-            wait_us(POLLWAITuSec);
+        _WaitWhileReg(0x90, 0x80);
     }
     REGISTERPERFORMANCE(PRF_DRAWTRIANGLE);
     return ret;
@@ -1042,8 +1069,7 @@
             drawCmd |= 0x20;
         WriteCommand(0x90, drawCmd);
         WriteCommand(0x90, 0x40 + drawCmd); // Start drawing.
-        while (ReadCommand(0x90) & 0x40)    // await completion.
-            wait_us(POLLWAITuSec);
+        _WaitWhileReg(0x90, 0x40);
     }
     REGISTERPERFORMANCE(PRF_DRAWCIRCLE);
     return ret;
@@ -1083,8 +1109,7 @@
             drawCmd |= 0x40;
         WriteCommand(0xA0, drawCmd);
         WriteCommand(0xA0, 0x80 + drawCmd); // Start drawing.
-        while (ReadCommand(0xA0) & 0x80)    // await completion.
-            wait_us(POLLWAITuSec);
+        _WaitWhileReg(0xA0, 0x80);
     }
     REGISTERPERFORMANCE(PRF_DRAWELLIPSE);
     return ret;
@@ -1093,6 +1118,8 @@
 
 RetCode_t RA8875::frequency(unsigned long Hz)
 {
+    spiwritefreq = Hz;
+    spireadfreq = Hz/2;
     spi.frequency(Hz);
     //       __   ___
     // Clock   ___A     Rising edge latched
@@ -1265,7 +1292,9 @@
     unsigned char retval;
     unsigned char data = 0;
     
+    spi.frequency(spireadfreq);
     retval = spi.write(data);
+    spi.frequency(spiwritefreq);
     return retval;
 }
 
@@ -1280,7 +1309,7 @@
 RetCode_t RA8875::init(int width, int height, int color_bpp)
 {
     Backlight_u8(0);
-    WriteCommand(0x88, 0x0a);                   // PLLC1 - Phase Lock Loop registers
+    WriteCommand(0x88, 0x0B);                   // PLLC1 - Phase Lock Loop registers
     wait_ms(1);
     WriteCommand(0x89, 0x02);
     wait_ms(1);