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

Files at this revision

API Documentation at this revision

Comitter:
va009039
Date:
Sun Jun 22 12:04:16 2014 +0000
Parent:
17:4e1205ce031f
Commit message:
add LPC11U68 interface.

Changed in this revision

Semihost.cpp Show annotated file Show diff for this revision Revisions of this file
Target2.cpp Show annotated file Show diff for this revision Revisions of this file
USBLocalFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main_KL46Z_KL25Z.cpp Show diff for this revision Revisions of this file
main_LPC11U35_SD.cpp Show diff for this revision Revisions of this file
main_LPC1549.cpp Show diff for this revision Revisions of this file
main_LPC1768_LPC4088.cpp Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
mydebug.h Show annotated file Show diff for this revision Revisions of this file
tests/mydebug.cpp Show annotated file Show diff for this revision Revisions of this file
tests/test_mytrace.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Semihost.cpp	Sat May 03 12:54:55 2014 +0000
+++ b/Semihost.cpp	Sun Jun 22 12:04:16 2014 +0000
@@ -1,4 +1,4 @@
-// Semihost.cpp 2014/5/3
+// Semihost.cpp 2014/6/22
 #include "Semihost.h"
 #include "mydebug.h"
 
@@ -15,6 +15,7 @@
 
 int Semihost::poll()
 {
+    TRACE();
     if (_target->getStatus() != TARGET_HALTED) {
         return 0;
     }
--- a/Target2.cpp	Sat May 03 12:54:55 2014 +0000
+++ b/Target2.cpp	Sun Jun 22 12:04:16 2014 +0000
@@ -1,4 +1,4 @@
-// Target2.cpp 2013/9/23
+// Target2.cpp 2014/6/22
 #include "Target2.h"
 #include "mydebug.h"
 
@@ -51,6 +51,7 @@
 
 bool Target2::setup()
 {
+    TRACE();
     _swd->Setup();
     JTAG2SWD();
     
@@ -149,24 +150,29 @@
 
 void Target2::SoftwareReset()
 {
+    TRACE();
     writeMemory(NVIC_AIRCR, 0x05fa0004);
 }
 
 uint32_t Target2::readMemory(uint32_t addr)
 {
+    TRACE1(addr);
     _setaddr(addr);
 
     uint32_t data;
     uint8_t ack = _swd->Transfer(AP_DRW_R, &data); // dummy read
+    TRACE1(ack);
     TEST_ASSERT(ack == SWD_OK);
 
     ack = _swd->Transfer(DP_RDBUFF, &data);
+    TRACE1(ack);
     TEST_ASSERT(ack == SWD_OK);
     return data;
 }
 
 void Target2::readMemory(uint32_t addr, uint32_t* data, int count)
 {
+    TRACE1(addr);
     if (count == 0) {
         return;
     }
@@ -202,6 +208,7 @@
 
 uint8_t Target2::readMemory8(uint32_t addr)
 {
+    TRACE1(addr);
     _setaddr8(addr);
 
     uint32_t data32;
@@ -225,22 +232,28 @@
 
 void Target2::_setaddr(uint32_t addr)
 {
+    TRACE1(addr);
     uint32_t ctl = CSW_VALUE|CSW_SIZE32;
     uint8_t ack = _swd->Transfer(AP_CSW, &ctl);
+    TRACE1(ack);
     TEST_ASSERT(ack == SWD_OK);
 
     ack = _swd->Transfer(DP_RDBUFF, NULL);
+    TRACE1(ack);
     TEST_ASSERT(ack == SWD_OK);
 
     ack = _swd->Transfer(AP_TAR, &addr);
+    TRACE1(ack);
     TEST_ASSERT(ack == SWD_OK);
 
     ack = _swd->Transfer(DP_RDBUFF, NULL);
+    TRACE1(ack);
     TEST_ASSERT(ack == SWD_OK);
 } 
 
 void Target2::_setaddr8(uint32_t addr)
 {
+    TRACE1(addr);
     uint32_t ctl = CSW_VALUE|CSW_SIZE8;
     uint8_t ack = _swd->Transfer(AP_CSW, &ctl);
     TEST_ASSERT(ack == SWD_OK);
@@ -264,11 +277,13 @@
 
 int Target2::getStatus()
 {
+    TRACE();
     return readMemory(DHCSR) & 6 ? TARGET_HALTED : TARGET_RUNNING;
 }
 
 bool Target2::wait_status(int status, int timeout_ms)
 {
+    TRACE1(status);
     Timer t;
     t.reset();
     t.start();
@@ -282,6 +297,7 @@
 
 bool Target2::prog_status()
 {
+    TRACE();
     writeMemory(DEMCR, 1);
     int status = getStatus();
     TEST_ASSERT(status == TARGET_HALTED);
@@ -305,6 +321,7 @@
 
 bool Target2::setBreakpoint0(uint32_t addr)
 {
+    TRACE1(addr);
     if ((addr&1) == 0 || addr >= 0x20000000) {
         return false;
     }
@@ -321,22 +338,26 @@
 
 void Target2::removeBreakpoint0(uint32_t addr)
 {
+    TRACE1(addr);
     writeMemory(FP_COMP0, 0); // breakpoint clear
     writeMemory(FP_CTRL, 2); // desable FPB
 }
 
 void Target2::halt()
 {
+    TRACE();
     writeMemory(DHCSR, 0xa05f0003);     
 }
 
 void Target2::resume()
 {
+    TRACE();
     writeMemory(DHCSR, 0xa05f0001);     
 }
 
 void Target2::step()
 {
+    TRACE();
     writeMemory(DHCSR, 0xa05f0005);     
 }
 
--- a/USBLocalFileSystem.lib	Sat May 03 12:54:55 2014 +0000
+++ b/USBLocalFileSystem.lib	Sun Jun 22 12:04:16 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/va009039/code/USBLocalFileSystem/#39eb4d5b97df
+http://mbed.org/users/va009039/code/USBLocalFileSystem/#528036abfb02
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Jun 22 12:04:16 2014 +0000
@@ -0,0 +1,116 @@
+// main.cpp 2014/6/22
+#if 1
+#include "Target2.h"
+#include "Flash.h"
+#include "USBLocalFileSystem.h"
+#include "Semihost.h"
+#include "mydebug.h"
+
+#if defined(TARGET_KL46Z)
+#define TARGET_SWDIO D12
+#define TARGET_SWCLK D10
+#define TARGET_nRESET D6
+#define SW_PIN SW1
+#define SW_MODE PullUp
+#define LED_OFF 1
+
+#elif defined(TARGET_LPC1549)
+#define TARGET_SWDIO D12
+#define TARGET_SWCLK D10
+#define TARGET_nRESET D6
+#define SW_PIN P1_9 // SW3
+#define SW_MODE PullUp
+#define LED_OFF 1
+
+#elif defined(TARGET_LPC11U68)
+#define TARGET_SWDIO D12
+#define TARGET_SWCLK D10
+#define TARGET_nRESET D6
+#define SW_PIN P0_1 // SW2
+#define SW_MODE PullUp
+#define LED_OFF 1
+
+#elif defined(TARGET_LPC1768)
+#define TARGET_SWDIO p21
+#define TARGET_SWCLK p22
+#define TARGET_nRESET p30
+#define SW_PIN p14 // joystick mbed application board
+#define LED_OFF 0
+
+#elif defined(TARGET_LPC4088)
+#define TARGET_SWDIO p25
+#define TARGET_SWCLK p26
+#define TARGET_nRESET p34
+#define SW_PIN p14 // joystick mbed application board
+#define LED_OFF 0
+
+#else
+#error "target error"
+#endif
+
+Serial pc(USBTX,USBRX);
+SWD swd(TARGET_SWDIO, TARGET_SWCLK, TARGET_nRESET);
+InterruptIn sw(SW_PIN); 
+DigitalOut led_flash(LED1);
+
+void callback_flash() {
+    led_flash = !led_flash;
+}
+
+__IO bool write_start = false;
+void swIRQ() {
+    wait_ms(100);
+    write_start = true;
+}
+
+int main() {
+    TRACE();
+    pc.baud(9600);
+    pc.printf("%s\n", __FILE__);
+    led_flash = LED_OFF;
+
+#ifdef SW_MODE
+    sw.mode(SW_MODE);
+#endif
+    sw.rise(swIRQ);
+        
+    USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB)
+
+    Target2* lpc = new Target2(&swd);
+    if (!lpc->setup()) {
+        pc.printf("*** SWD error.\n");
+    }    
+    Semihost semihost(lpc, &pc, usb_local);
+    semihost.mount("/local");
+    lpc->resume(); // C_DEBUGEN ON
+    while(1) {
+        if (write_start) {
+            usb_local->lock(true);
+            usb_local->remount();
+            char filename[64];
+            if (usb_local->find(filename, sizeof(filename), "*.BIN")) {
+                pc.printf("*** bin filename=[%s]\n", filename);
+                if (!lpc->setup()) {
+                    pc.printf("*** SWD error.\n");
+                } else {
+                    Flash flash(lpc, &pc);
+                    flash.attachEvent(callback_flash);
+                    if (flash.init()) {
+                        flash.write(filename);
+                        flash.verify(filename);
+                    }
+                }
+            } else {
+                pc.printf("*** binary image file not found.\n");
+            }
+            usb_local->lock(false);
+            lpc->SoftwareReset();    
+            lpc->HardwareReset();
+            led_flash = LED_OFF;
+            write_start = false;
+        }
+        semihost.poll();
+    }        
+}
+#endif
+
--- a/main_KL46Z_KL25Z.cpp	Sat May 03 12:54:55 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,86 +0,0 @@
-// main_KL46Z_KL25Z.cpp 2014/5/3
-#if defined(TARGET_KL46Z)||defined(TARGET_KL25Z)
-#include "Target2.h"
-#include "Flash.h"
-#include "USBLocalFileSystem.h"
-#include "Semihost.h"
-
-#define USE_SD 0
-#define SD_DI A1
-#define SD_DO A2
-#define SD_CK A3
-#define SD_CS A4
-
-#if defined(TARGET_KL25Z)
-#define SW1 D4
-#define SW3 D5
-#endif
-
-Serial pc(USBTX, USBRX);
-SWD swd(D12,D10,D6); // SWDIO,SWCLK,nReset
-InterruptIn sw1(SW1);
-InterruptIn sw3(SW3);
-DigitalOut led_disk(LED_RED);
-DigitalOut led_flash(LED_GREEN);
-#define LED_ON  0
-#define LED_OFF 1
-
-void callback_disk() {
-    led_disk = !led_disk;
-}
-
-void callback_flash() {
-    led_flash = !led_flash;
-}
-
-__IO bool write_start = false;
-void swIRQ() {
-    wait_ms(100);
-    write_start = true;
-}
-
-int main() {
-    pc.baud(9600);
-    pc.printf("%s\n", __FILE__);
-    led_disk = LED_OFF;
-    led_flash = LED_OFF;
-
-    sw1.rise(swIRQ);
-    sw3.rise(swIRQ);
-
-#if USE_SD        
-    USBLocalFileSystem* usb_local = new USBLocalFileSystem(SD_DI,SD_DO,SD_CK,SD_CS); // mosi,miso,clk,cs
-#else
-    USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB)
-#endif
-    usb_local->attachEvent(callback_disk);
-    Target2* lpc = new Target2(&swd);
-    lpc->setup();
-    Semihost semihost(lpc, &pc, usb_local);
-    semihost.mount("/local");
-    lpc->resume(); // C_DEBUGEN ON
-    while(1) {
-        if (write_start) {
-            usb_local->remount();
-            mystring filename;
-            if (LocalStorage::find_bin(filename)) {
-                pc.printf("*** bin filename=[%s]\n", filename.c_str());
-                lpc->setup();
-                Flash flash(lpc, &pc);
-                flash.attachEvent(callback_flash);
-                if (flash.init()) {
-                    flash.write(filename.c_str());
-                    flash.verify(filename.c_str());
-                }
-            } else {
-                pc.printf("*** binary image file not found.\n");
-            }
-            lpc->SoftwareReset();    
-            lpc->HardwareReset();
-            led_flash = LED_OFF;
-            write_start = false;
-        }
-        semihost.poll();
-    }        
-}
-#endif // TARGET_KL46Z,TARGET_KL25Z
--- a/main_LPC11U35_SD.cpp	Sat May 03 12:54:55 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,80 +0,0 @@
-// main_LPC11U35_SD.cpp 2014/5/3
-#if 1 && defined(TARGET_LPC11U35_401)
-#include "Target2.h"
-#include "Flash.h"
-#include "USBLocalFileSystem.h"
-#include "Semihost.h"
-
-#define USE_SD 1
-#define SD_DI P1_15
-#define SD_DO P0_14
-#define SD_CK P0_23
-#define SD_CS P0_17
-
-Serial pc(P0_19,P0_18); // tx,rx
-SWD swd(P0_8,P0_7,P0_2); // SWDIO,SWCLK,nReset
-InterruptIn sw_isp(P0_1);
-DigitalOut led_disk(P0_20);
-DigitalOut led_flash(P0_21);
-#define LED_ON  0
-#define LED_OFF 1
-
-void callback_disk() {
-    led_disk = !led_disk;
-}
-
-void callback_flash() {
-    led_flash = !led_flash;
-}
-
-__IO bool write_start = false;
-void swIRQ() {
-    wait_ms(500);
-    write_start = true;
-}
-
-int main() {
-    pc.baud(9600);
-    pc.printf("%s\n", __FILE__);
-    led_disk = LED_OFF;
-    led_flash = LED_OFF;
-
-    sw_isp.mode(PullUp);
-    sw_isp.fall(swIRQ);
-
-#if USE_SD
-    USBLocalFileSystem* usb_local = new USBLocalFileSystem(SD_DI,SD_DO,SD_CK,SD_CS); // mosi,miso,clk,cs
-#else
-    USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB)
-#endif        
-    usb_local->attachEvent(callback_disk);
-    Target2* lpc = new Target2(&swd);
-    lpc->setup();
-    Semihost semihost(lpc, &pc, usb_local);
-    semihost.mount("/local");
-    lpc->resume(); // C_DEBUGEN ON
-    while(1) {
-        if (write_start) {
-            usb_local->remount();
-            mystring filename;
-            if (LocalStorage::find_bin(filename)) {
-                pc.printf("*** bin filename=[%s]\n", filename.c_str());
-                lpc->setup();
-                Flash flash(lpc, &pc);
-                flash.attachEvent(callback_flash);
-                if (flash.init()) {
-                    flash.write(filename.c_str());
-                    flash.verify(filename.c_str());
-                }
-            } else {
-                pc.printf("*** binary image file not found.\n");
-            }
-            lpc->SoftwareReset();    
-            lpc->HardwareReset();
-            led_flash = LED_OFF;
-            write_start = false;
-        }
-        semihost.poll();
-    }        
-}
-#endif // TARGET_LPC11U35_401
--- a/main_LPC1549.cpp	Sat May 03 12:54:55 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,70 +0,0 @@
-// main_LPC1549.cpp 2014/5/3
-#if defined(TARGET_LPC1549)
-#include "Target2.h"
-#include "Flash.h"
-#include "USBLocalFileSystem.h"
-#include "Semihost.h"
-
-Serial pc(USBTX,USBRX);
-SWD swd(D12,D10,D6); // SWDIO,SWCLK,nReset
-InterruptIn sw3(P1_9);
-DigitalOut led_disk(LED1);
-DigitalOut led_flash(LED2);
-#define LED_ON  0
-#define LED_OFF 1
-
-void callback_disk() {
-    led_disk = !led_disk;
-}
-
-void callback_flash() {
-    led_flash = !led_flash;
-}
-
-__IO bool write_start = false;
-void swIRQ() {
-    wait_ms(100);
-    write_start = true;
-}
-
-int main() {
-    pc.baud(9600);
-    pc.printf("%s\n", __FILE__);
-    led_disk = LED_OFF;
-    led_flash = LED_OFF;
-
-    sw3.mode(PullUp);
-    sw3.rise(swIRQ);
-        
-    USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB)
-    usb_local->attachEvent(callback_disk);
-    Target2* lpc = new Target2(&swd);
-    lpc->setup();
-    Semihost semihost(lpc, &pc, usb_local);
-    semihost.mount("/local");
-    lpc->resume(); // C_DEBUGEN ON
-    while(1) {
-        if (write_start) {
-            usb_local->remount();
-            mystring filename;
-            if (LocalStorage::find_bin(filename)) {
-                pc.printf("*** bin filename=[%s]\n", filename.c_str());
-                lpc->setup();
-                Flash flash(lpc, &pc);
-                flash.attachEvent(callback_flash);
-                if (flash.init()) {
-                    flash.write(filename.c_str());
-                    flash.verify(filename.c_str());
-                }
-            } else {
-                pc.printf("*** binary image file not found.\n");
-            }
-            lpc->SoftwareReset();    
-            lpc->HardwareReset();
-            led_flash = LED_OFF;
-            write_start = false;
-        }
-        semihost.poll();
-    }        
-}
-#endif // TARGET_LPC1549
--- a/main_LPC1768_LPC4088.cpp	Sat May 03 12:54:55 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,82 +0,0 @@
-// main_LPC1768_LPC4088.cpp 2014/5/3
-#if defined(TARGET_LPC1768)||defined(TARGET_LPC4088)
-#include "Target2.h"
-#include "Flash.h"
-#include "USBLocalFileSystem.h"
-#include "Semihost.h"
-
-#if defined(TARGET_LPC1768)
-#define TARGET_SWDIO p21
-#define TARGET_SWCLK p22
-#define TARGET_nRESET p30
-#elif defined(TARGET_LPC4088)
-#define TARGET_SWDIO p25
-#define TARGET_SWCLK p26
-#define TARGET_nRESET p34
-#else
-#error "target error"
-#endif
-
-Serial pc(MBED_UARTUSB);
-SWD swd(TARGET_SWDIO, TARGET_SWCLK, TARGET_nRESET);
-InterruptIn sw_center(p14); // joystick mbed application board
-DigitalOut led_disk(LED1);
-DigitalOut led_flash(LED2);
-#define LED_ON  1
-#define LED_OFF 0
-
-void callback_disk() {
-    led_disk = !led_disk;
-}
-
-void callback_flash() {
-    led_flash = !led_flash;
-}
-
-__IO bool write_start = false;
-void swIRQ() {
-    wait_ms(100);
-    write_start = true;
-}
-
-int main() {
-    pc.baud(9600);
-    pc.printf("%s\n", __FILE__);
-    led_disk = LED_OFF;
-    led_flash = LED_OFF;
-
-    sw_center.rise(swIRQ);
-        
-    USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB)
-    usb_local->attachEvent(callback_disk);
-    Target2* lpc = new Target2(&swd);
-    lpc->setup();
-    Semihost semihost(lpc, &pc, usb_local);
-    semihost.mount("/local");
-    lpc->resume(); // C_DEBUGEN ON
-    while(1) {
-        if (write_start) {
-            usb_local->remount();
-            mystring filename;
-            if (LocalStorage::find_bin(filename)) {
-                pc.printf("*** bin filename=[%s]\n", filename.c_str());
-                lpc->setup();
-                Flash flash(lpc, &pc);
-                flash.attachEvent(callback_flash);
-                if (flash.init()) {
-                    flash.write(filename.c_str());
-                    flash.verify(filename.c_str());
-                }
-            } else {
-                pc.printf("*** binary image file not found.\n");
-            }
-            lpc->SoftwareReset();    
-            lpc->HardwareReset();
-            led_flash = LED_OFF;
-            write_start = false;
-        }
-        semihost.poll();
-    }        
-}
-#endif // TARGET_LPC1768, TARGET_LPC4088
-
--- a/mbed.bld	Sat May 03 12:54:55 2014 +0000
+++ b/mbed.bld	Sun Jun 22 12:04:16 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/024bf7f99721
\ No newline at end of file
--- a/mydebug.h	Sat May 03 12:54:55 2014 +0000
+++ b/mydebug.h	Sun Jun 22 12:04:16 2014 +0000
@@ -1,4 +1,4 @@
-// mydebug.h 2013/8/31
+// mydebug.h 2014/6/22
 #pragma once
 
 #define MY_DEBUG 1
@@ -9,13 +9,27 @@
 #define DBG(x, ...) debug("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
 #define DBG_HEX(A,B) do{debug("[%s:%d]\r\n",__PRETTY_FUNCTION__, __LINE__);debug_hex(A,B);}while(0);
 #else
-#define DBG(x, ...) while(0);
-#define DBG_HEX(A,B) while(0);
+#define DBG(x, ...) while(0)
+#define DBG_HEX(A,B) while(0)
+#endif
+
+#if 1
+extern void myassert(const char* func, int line, const char* expr); 
+#define TEST_ASSERT(A) while(!(A)){myassert(__PRETTY_FUNCTION__, __LINE__, #A);}
+#else
+#define TEST_ASSERT(A) while(0)
 #endif
 
 #if 1
-#include "mbed_debug.h"
-#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
+extern void mytrace(const char* func, int line);
+extern void mytrace1(const char* func, int line, uint32_t value);
+extern void mytrace_view();
+#define TRACE() mytrace(__PRETTY_FUNCTION__, __LINE__)
+#define TRACE1(A) mytrace1(__PRETTY_FUNCTION__, __LINE__, A)
+#define TRACE_VIEW() mytrace_view()
 #else
-#define TEST_ASSERT(A) while(0);
+#define TRACE() while(0)
+#define TRACE1(A) while(0)
+#define TRACE_VIEW() while(0)
 #endif
+ 
--- a/tests/mydebug.cpp	Sat May 03 12:54:55 2014 +0000
+++ b/tests/mydebug.cpp	Sun Jun 22 12:04:16 2014 +0000
@@ -1,7 +1,73 @@
-// mydebug.cpp 2013/4/22
+// mydebug.cpp 2014/6/22
 #include "mydebug.h"
 #ifdef MY_DEBUG
-#include <string.h>
+#include "mbed.h"
+
+struct trace_info {
+    uint32_t timestamp_ms;
+    const char* func;
+    int line;
+    uint32_t value;
+};
+    
+struct StTrace {
+    static const int TRACE_MAX = 32;
+    trace_info buf[TRACE_MAX];
+    int pos;
+    int count;
+    Timer t;
+    StTrace() {
+        clear();
+        t.reset();
+        t.start();
+    }
+    void clear() {
+        pos = 0;
+        count = 0;
+    }
+    void view() {
+        fprintf(stderr, "TRACE:\n");
+        int n = pos;
+        for(int i = 0; i < TRACE_MAX && i < count; i++) {
+            if (--n < 0) {
+                n = TRACE_MAX-1;
+            }
+            fprintf(stderr, "%d %s@%d %p\n", buf[n].timestamp_ms, buf[n].func, buf[n].line, buf[n].value);
+        }
+    }
+    void put(const char* func, int line, uint32_t value = 0) {
+        buf[pos].timestamp_ms = t.read_ms();
+        buf[pos].func = func;
+        buf[pos].line = line;
+        buf[pos].value = value;
+        if (++pos >= TRACE_MAX) {
+            pos = 0;
+        }
+        count++;
+    }    
+};
+
+StTrace trace;
+
+void mytrace(const char* func, int line) {
+    trace.put(func, line);
+}
+
+void mytrace1(const char* func, int line, uint32_t value) {
+    trace.put(func, line, value);
+}
+
+void mytrace_view() {
+    trace.view();
+}
+      
+void myassert(const char* func, int line, const char* expr) {
+    fprintf(stderr, "\n\n%s@%d %s ASSERT!!!\n\n", func, line, expr);
+    trace.view();
+    trace.clear();
+    exit(1);
+}
+
 void debug_hex(uint8_t* buf, int len)
 {
     for(int i = 0; i < len; i++) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_mytrace.cpp	Sun Jun 22 12:04:16 2014 +0000
@@ -0,0 +1,46 @@
+// test_mytrace.cpp 2014/6/22
+#if 0
+#include "mbed.h"
+#include "mydebug.h"
+#include "mytest.h"
+
+Serial pc(USBTX, USBRX);
+DigitalOut led1(LED1);
+
+TEST(mytrace,test0) {
+    TRACE();
+    wait_ms(10);
+    ASSERT_TRUE(1);
+}
+
+TEST(mytrace,test1) {
+    TRACE1(123);
+    wait_ms(20);
+    ASSERT_TRUE(1);
+}
+
+TEST(mytrace,test2) {
+    TRACE1(456);
+    wait_ms(30);
+    ASSERT_TRUE(1);
+}
+
+TEST(mytrace,test9) {
+    TRACE_VIEW();
+    ASSERT_TRUE(1);
+}
+
+int main() {
+    //pc.baud(921600);
+    pc.baud(9600);
+    DBG("%s", __FILE__);
+
+    RUN_ALL_TESTS();
+    while(1) {
+        led1 = led1;
+        wait_ms(200);
+    }
+}
+
+#endif
+