semihost server example program

Dependencies:   SWD mbed USBLocalFileSystem BaseDAP USBDAP

/media/uploads/va009039/kl46z-lpc800-360x480.jpg

LPCXpresso
LPC11U68
LPCXpresso
LPC1549
FRDM-KL46ZEA LPC4088 QSB
app-board
LPC1768
app-board
LPC810LPC1114FN28
serverserverserverserverserverclientclient
SWDIOD12D12D12p25p21p4(P0_2)p12
SWCLKD10D10D10p26p22p3(P0_3)p3
nRESET
*option
D6D6D6p34p30p1(P0_5)p23
GNDGNDGNDGNDp1p1p7p22
3.3VP3V3P3V3P3V3p44p40p6p21
flash writeSW2(P0_1)SW3(P1_9)SW1p14
joystick
center
p14
joystick
center

client example:

Import programlpc810-semihost_helloworld

semihost client example program

Revision:
3:d7a7cde0bfb8
Parent:
2:32e9437348ad
Child:
4:5e4107edcbdb
--- a/Flash.cpp	Thu Sep 05 09:34:12 2013 +0000
+++ b/Flash.cpp	Sun Sep 08 14:13:15 2013 +0000
@@ -1,8 +1,13 @@
-// Flash.cpp 2013/9/5
+// Flash.cpp 2013/9/6
 #include "Flash.h"
 #include "Target2.h"
 #include "mydebug.h"
 
+#define SYSMEMREMAP  0x40048000
+#define MAINCLKSEL   0x40048070
+#define MAINCLKUEN   0x40048074
+#define SYSAHBCLKDIV 0x40048078
+
 Flash::Flash(Target2* target, Serial* usbpc) :_target(target),_pc(usbpc)
 {
     _setup();
@@ -14,6 +19,29 @@
     _target->wait_status(TARGET_HALTED);
 }
 
+bool Flash::init()
+{
+    _pc->printf("Initializing.");
+    if (_target->idcode == 0x0bb11477) { // LPC1114FN28
+        _target->writeMemory(MAINCLKSEL, 0);  // Select Internal RC Oscillator
+        _target->writeMemory(MAINCLKUEN, 1);  // Update Main Clock Source
+        _target->writeMemory(MAINCLKUEN, 0);  // Toggle Update Register
+        _target->writeMemory(MAINCLKUEN, 1);
+        uint32_t data = _target->readMemory(MAINCLKUEN); // Wait until updated
+        if (!(data & 1)) {
+            _pc->printf("\nerror MAINCLKUEN=%08x\n", data);
+            return false;
+        }
+        _target->writeMemory(SYSAHBCLKDIV, 1);// Set Main Clock divider to 1
+        _target->writeMemory(SYSMEMREMAP, 2); // User Flash Mode
+    } else {
+        _pc->printf("\nerror idcode=%08x\n", _target->idcode);
+        return false;
+    }
+    _pc->printf("passed.\n");
+    return true;
+}
+
 bool Flash::write(const char* filename)
 {
     FILE* fp = fopen(filename, "rb");
@@ -25,11 +53,7 @@
     uint8_t buf[256];
     bool passed = false;
     for(int addr = 0; addr < 0x8000; addr += sizeof(buf)) {
-        if (feof(fp)) {
-            passed = true;
-            break;
-        }
-        fread(buf, sizeof(buf), 1, fp);
+        int ret = fread(buf, 1, sizeof(buf), fp);
         if (!_patch(buf, sizeof(buf), addr)) {
             break;
         }
@@ -52,6 +76,10 @@
             }
         }
         _pc->printf(".");
+        if (ret < sizeof(buf)) {
+            passed = true;
+            break;
+        }
     }
     fclose(fp);
     if (passed) {