mbed support for LPC4088 Display Module

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

Dependents:   emptyProgram

Fork of DMSupport by Embedded Artists

Revision:
17:4ea2509445ac
Parent:
13:2c60e28503f8
Child:
20:9df19da50290
--- a/DMBoard.cpp	Fri Jan 02 14:17:03 2015 +0000
+++ b/DMBoard.cpp	Wed Jan 07 14:02:39 2015 +0100
@@ -16,7 +16,7 @@
 
 #include "mbed.h"
 #include "DMBoard.h"
-#include "InternalEEPROM.h"
+#include "BiosEEPROM.h"
 #include "bios.h"
 #include "crc.h"
 
@@ -59,6 +59,17 @@
 static DevNull null("null");
 #endif
 
+/*
+ * Make sure that we reserve at least this amount of RAM for future
+ * expansion of the BIOS. This prevents the user from squeezing out
+ * the last drop of available RAM in his application.
+ */
+#define BIOS_RESERVED_CHUNK  0x1000
+#define BIOS_MAX_SIZE        0x100000
+#ifndef MAX
+  #define MAX(__a, __b) (((__a)>(__b))?(__a):(__b))
+#endif
+
 /******************************************************************************
  * Local variables
  *****************************************************************************/
@@ -94,108 +105,71 @@
   }
 }
 
-DMBoard::BoardError DMBoard::readConfiguration()
-{
-  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");
-  for (int i = 0; i < 63; i++) {
-    for (int j = 0; j < 4; j++) {
-      _logger.printf("0x%04x  %02x %02x %02x %02x %02x %02x %02x %02x  %02x %02x %02x %02x %02x %02x %02x %02x\n",
-        i,
-        p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],
-        p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15]);
-      p += 16;
-    }
-    _logger.printf("\n");
-  }
-  
-  // find first non-zero page and write to that
-  for (int page = 0; page < mem.numPages(); page++) {
-    bool zeroPage = true;
-    for (int i = 0; i < mem.pageSize(); i++) {
-      if (buff[i + page*mem.pageSize()] != 0) {
-        zeroPage = false;
-        break;
-      }
-    }
-    if (zeroPage) {
-      _logger.printf("Will fill page 0x%04x (%d) with 0x00..0x3f\n", page, page);
-      p = buff;
-      for (int i = 0; i < mem.pageSize(); i++) {
-        *p++ = i;
-      }
-      mem.write(page, 0, buff, mem.pageSize());
-      memset(buff, 0, mem.memorySize());
-      mem.read(0, 0, buff, mem.memorySize());
-      p = buff;
-      _logger.printf("\n-------\nAFTER:\n-------\n");
-      for (int i = 0; i < 63; i++) {
-        for (int j = 0; j < 4; j++) {
-          _logger.printf("0x%04x  %02x %02x %02x %02x %02x %02x %02x %02x  %02x %02x %02x %02x %02x %02x %02x %02x\n",
-            i,
-            p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],
-            p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15]);
-          p += 16;
-        }
-        _logger.printf("\n");
-      }
-      break;
-    }
-  }  
-#endif
-  return Ok;
-}
-
 #if defined(DM_BOARD_USE_DISPLAY)
 DMBoard::BoardError DMBoard::readDisplayConfiguration(uint8_t** data, uint32_t* size)
 {
-  BoardError err = readConfiguration();
+  BoardError err = Ok;
+  BiosEEPROM eeprom;
+  file_header_t fh;
+
+  if (_conf != NULL) {
+    *data = _conf;
+    *size = _confSize;
+    return Ok;
+  }      
   do {
-    if (err != Ok) {
+    if (!eeprom.read(0, (char*)&fh, sizeof(file_header_t))) {
+      err = BiosStorageError;
       break;
     }
     
-    file_header_t* fh = (file_header_t*)_conf;
-    if (fh->magic != BIOS_MAGIC) {
+    if (fh.magic != BIOS_MAGIC) {
       err = BiosInvalidError;
       break;
     }
     
-    if (fh->version != BIOS_VER) {
+    if (fh.version != BIOS_VER) {
       err = BiosVersionError;
       break;
     }
     
-    if ((fh->headerSize + fh->size) > _confSize) {
+    if ((fh.headerSize + fh.size) > BIOS_MAX_SIZE) {
       err = BiosInvalidError;
       break;
     }
     
-    uint32_t crc = crc_Buffer((uint32_t*)(&_conf[fh->headerSize]), fh->size/4);
-    if (crc != fh->crc) {
+    _confSize = fh.headerSize + fh.size;
+    _conf = (uint8_t*)malloc(MAX(_confSize,BIOS_RESERVED_CHUNK));
+    if (_conf == NULL) {
+      _confSize = 0;
+      err = MemoryError;
+      break;
+    }
+    
+    if (!eeprom.read(0, (char*)_conf, _confSize)) {
+      err = BiosStorageError;
+      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;
+    *size = _confSize;
     err = Ok;
   } while (false);
+  
+  if (err != Ok) {
+    if (_conf != NULL) {
+      free(_conf);
+      _conf = NULL;
+      _confSize = 0;
+    }
+  }
 
   return err;
 }
@@ -245,8 +219,6 @@
       }
 #endif
       
-      readConfiguration();
-      
 #if defined(DM_BOARD_USE_DISPLAY)
       if (BiosDisplayAndTouch::instance().initDisplay() != Display::DisplayError_Ok) {
         err = DisplayError;