Subdirectory provided by Embedded Artists

Dependencies:   DM_FATFileSystem DM_HttpServer DM_USBHost EthernetInterface USBDevice mbed-rpc mbed-rtos mbed-src

Dependents:   lpc4088_displaymodule_hello_world_Sept_2018

Fork of DMSupport by Embedded Artists

Revision:
10:1ac4b213f0f7
Parent:
9:a33326afd686
Child:
13:2c60e28503f8
--- a/DMBoard.cpp	Thu Dec 11 18:23:07 2014 +0000
+++ b/DMBoard.cpp	Fri Dec 19 09:03:25 2014 +0100
@@ -17,9 +17,11 @@
 #include "mbed.h"
 #include "DMBoard.h"
 #include "InternalEEPROM.h"
+#include "bios.h"
+#include "crc.h"
 
-#if defined(DM_BOARD_USE_TOUCH)
-  #include "AR1021.h"
+#if defined(DM_BOARD_USE_DISPLAY)
+  #include "BiosDisplayAndTouch.h"
 #endif
 
 #if defined(DM_BOARD_ENABLE_MEASSURING_PINS)
@@ -35,6 +37,10 @@
   #error The hardware supports either USB Device or USB Host - not both at the same time
 #endif
 
+#if defined(DM_BOARD_USE_TOUCH) && !defined(DM_BOARD_USE_DISPLAY)
+  #error Cannot have touch controller without a display!
+#endif
+
 /******************************************************************************
  * Defines and typedefs
  *****************************************************************************/
@@ -63,15 +69,13 @@
 
 DMBoard::DMBoard() : 
     _initialized(false),
+    _conf(NULL), _confSize(0),
 #if defined(DM_BOARD_USE_MCI_FS)
     _mcifs("mci", P4_16),
 #endif
 #if defined(DM_BOARD_USE_QSPI_FS)
     _qspifs("qspi"),
 #endif
-#if defined(DM_BOARD_USE_TOUCH)
-    _touch(NULL),
-#endif
     _buzzer(P1_5),
     _button(p23),
     _led1(LED1),
@@ -83,22 +87,28 @@
 
 DMBoard::~DMBoard()
 {
-#if defined(DM_BOARD_USE_TOUCH)
-  if (_touch != NULL) {
-    delete _touch;
-    _touch = NULL;
+  if (_conf != NULL) {
+    free(_conf);
+    _conf = NULL;
+    _confSize = 0;
   }
-#endif
 }
 
 DMBoard::BoardError DMBoard::readConfiguration()
 {
-  InternalEEPROM mem;
-  mem.init();
-  
-  uint8_t* buff = (uint8_t*)malloc(mem.memorySize());
-  mem.read(0, 0, buff, mem.memorySize());
-  mem.powerDown();
+  if (_conf == NULL) {
+    InternalEEPROM mem;
+    mem.init();
+    
+    _confSize = mem.memorySize();
+    _conf = (uint8_t*)malloc(_confSize);
+    if (_conf == NULL) {
+      _confSize = 0;
+      return MemoryError;
+    }
+    mem.read(0, 0, _conf, _confSize);
+    mem.powerDown();
+  }
 #if 0  
   uint8_t* p = buff;
   _logger.printf("\n-------\nBEFORE:\n-------\n");
@@ -153,56 +163,41 @@
 #if defined(DM_BOARD_USE_DISPLAY)
 DMBoard::BoardError DMBoard::readDisplayConfiguration(uint8_t** data, uint32_t* size)
 {
-  uint32_t allocsize = 17*sizeof(uint32_t);
-  uint32_t* stuff = (uint32_t*)malloc(allocsize);
-  if (stuff == NULL) {
-    return MemoryError;
-  }
-#if defined(DM_BOARD_USE_4_3_DISPLAY_TMP)
-  // Temporary setup, for a 4.3" display
+  BoardError err = readConfiguration();
+  do {
+    if (err != Ok) {
+      break;
+    }
+    
+    file_header_t* fh = (file_header_t*)_conf;
+    if (fh->magic != BIOS_MAGIC) {
+      err = BiosInvalidError;
+      break;
+    }
+    
+    if (fh->version != BIOS_VER) {
+      err = BiosVersionError;
+      break;
+    }
+    
+    if ((fh->headerSize + fh->size) > _confSize) {
+      err = BiosInvalidError;
+      break;
+    }
+    
+    uint32_t crc = crc_Buffer((uint32_t*)(&_conf[fh->headerSize]), fh->size/4);
+    if (crc != fh->crc) {
+      err = BiosInvalidError;
+      break;
+    }
+    
+    // Bios header has been verified and seems ok
+    *data = _conf;
+    *size = fh->headerSize + fh->size;
+    err = Ok;
+  } while (false);
 
-  stuff[ 0] = 40,    //horizontalBackPorch == HSYNC back porch
-  stuff[ 1] = 5,     //horizontalFrontPorch == HSYNC front porch
-  stuff[ 2] = 2,     //hsync  ==  HSYNC width
-  stuff[ 3] = 480,   //width == Horizontal active area
-  stuff[ 4] = 8,     //verticalBackPorch == VSYNC back porch
-  stuff[ 5] = 8,     //verticalFrontPorch == VSYNC front porch
-  stuff[ 6] = 2,     //vsync == VSYNC width
-  stuff[ 7] = 272,   //height == Vertical display area
-  stuff[ 8] = false,
-  stuff[ 9] = false,
-  stuff[10] = true,
-  stuff[11] = true,
-  stuff[12] = 1,
-  stuff[13] = Display::Resolution_16bit_rgb565;
-  stuff[14] = 9000000, //clock or 0 for external
-  stuff[15] = 0,       //LcdController::Tft,
-  stuff[16] = false;
-#else
-  // Temporary setup, for a 5" rogin display
-
-  stuff[ 0] = 46,    //horizontalBackPorch == HSYNC back porch
-  stuff[ 1] = 20,    //horizontalFrontPorch == HSYNC front porch
-  stuff[ 2] = 2,     //hsync  ==  HSYNC width
-  stuff[ 3] = 800,   //width == Horizontal active area
-  stuff[ 4] = 23,    //verticalBackPorch == VSYNC back porch
-  stuff[ 5] = 10,    //verticalFrontPorch == VSYNC front porch
-  stuff[ 6] = 3,     //vsync == VSYNC width
-  stuff[ 7] = 480,   //height == Vertical display area
-  stuff[ 8] = false,
-  stuff[ 9] = false,
-  stuff[10] = true,
-  stuff[11] = true,
-  stuff[12] = 1,
-  stuff[13] = Display::Resolution_16bit_rgb565;
-  stuff[14] = 30000000, //clock or 0 for external
-  stuff[15] = 0,        //LcdController::Tft,
-  stuff[16] = false;
-#endif  
-  *data = (uint8_t*)&(stuff[0]);
-  *size = allocsize;
-
-  return Ok;
+  return err;
 }
 #endif
 
@@ -227,7 +222,7 @@
       _led2 = 1;
       _led3 = 0;
       _led4 = 0;
-
+        
       // Make sure that the logger is always initialized even if
       // other initialization tasks fail
       _logger.init();
@@ -253,30 +248,18 @@
       readConfiguration();
       
 #if defined(DM_BOARD_USE_DISPLAY)
-      if (Display::instance().init() != Display::Ok) {
+      if (BiosDisplayAndTouch::instance().initDisplay() != Display::DisplayError_Ok) {
         err = DisplayError;
         break;
       }
 #endif
       
 #if defined(DM_BOARD_USE_TOUCH)
-      //if (... configuration says AR1021...) {
-        _touch = new AR1021(P2_27, P2_26, P2_22, P2_23, P2_25);
-        int att;
-        for (att = 1; att <= 3; att++) {
-            if (_touch->init(Display::instance().width(), Display::instance().height())) {
-              break;
-            }
-            _logger.printf("Failed to init touch, trying again...\n");
-            wait_ms(500);
-        }
-        if (att > 3) {
-          err = TouchError;
-          break;
-        }
-      //}
+      if (BiosDisplayAndTouch::instance().initTouchController() != TouchPanel::TouchError_Ok) {
+        err = TouchError;
+        break;
+      }
 #endif
-
       
       _initialized = true;
     } while(0);
@@ -320,3 +303,16 @@
 { 
   return _button.read() == 0;
 }
+
+TouchPanel* DMBoard::touchPanel()
+{
+  if (BiosDisplayAndTouch::instance().isTouchSupported()) {
+    return &BiosDisplayAndTouch::instance();
+  }
+  return NULL;
+}
+
+Display* DMBoard::display()
+{
+  return &BiosDisplayAndTouch::instance();
+}