USB composite device example program, drag-and-drop flash writer.

Dependencies:   SWD USBDevice mbed BaseDAP

Revision:
1:ea8e179320d7
Parent:
0:2385683c867a
--- a/USBMSD_LPC.cpp	Tue Sep 17 04:33:44 2013 +0000
+++ b/USBMSD_LPC.cpp	Sat Sep 28 03:21:14 2013 +0000
@@ -1,80 +1,45 @@
-// USBMSD_LPC.cpp 2013/9/17
+// USBMSD_LPC.cpp 2013/9/28
 #include "mbed.h"
 #include "USBMSD_LPC.h"
+#define MY_DEBUG
 #include "mydebug.h"
 
-USBMSD_LPC::USBMSD_LPC(PinName swdio, PinName swclk, PinName reset, Serial* usbpc)
-    : _target(swdio, swclk, reset), _pc(usbpc)
-{
-    _target.setup();
-    _flash = new Flash(&_target, _pc);
-    _status = 0x01;
-    _prog_mode = false;  
-    connect();
-}
+#ifdef TARGET_LPC1768
+#define LED_ON  1
+#define LED_OFF 0
+#endif
+#ifdef TARGET_KL25Z
+#define LED_ON  0
+#define LED_OFF 1
+#endif
+DigitalOut led3(LED3);
 
-/* virtual */ int USBMSD_LPC::disk_initialize()
+USBMSD_LPC::USBMSD_LPC(SWD* swd, Serial* usbpc) : _pc(usbpc)
 {
-    _status = 0x00;
-    return 0;
-}
-
-/* virtual */ int USBMSD_LPC::disk_status()
-{
-    return _status;
-}
-
-/* virtual */ int USBMSD_LPC::disk_read(uint8_t * data, uint64_t block)
-{
-    DBG("block=%d", (int)block);
-    return _romdisk.read(data, block);
+    _target = new Target2(swd);
+    _flash = NULL;
+    led3 = LED_OFF;
 }
 
-/* virtual */ int USBMSD_LPC::disk_write(const uint8_t * data, uint64_t block)
+/* virtual */ void USBMSD_LPC::Drop(const uint8_t* data, int len, int offset, int total)
 {
-    DBG("block=%d", (int)block);
-    if (_prog_mode == false) {
-        if (_romdisk.is_data(block)) {
-            _addr = 0;
-            flashBinary(_addr, data, 512);
-            _prev_block = block;
-            _addr += 512;
-            _prog_mode = true;
-            _pc->printf("Writing.");
-        }
-    } else {
-        if (!_romdisk.is_data(block)) {
-            _pc->printf("passed.\n");
-            _target.SoftwareReset();
-            _prog_mode = false;
-        } else if ((_prev_block+1) != block) {
-            _prog_mode = false;
-        } else {
-            flashBinary(_addr, data, 512);
-            _pc->printf(".");
-            _prev_block = block;
-            _addr += 512;
-        }
+    if (_flash == NULL) {
+        _target->setup();
+        _flash = new Flash(_target, _pc);
     }    
-    return _romdisk.write(data, block);
-}
-
-/* virtual */ uint64_t USBMSD_LPC::disk_sectors()
-{
-    return _romdisk.sectors();
-}
-
-/* virtual */ uint64_t USBMSD_LPC::disk_size()
-{
-    return _romdisk.sectors() * 512;
-}
-
-void USBMSD_LPC::flashBinary(int addr, const uint8_t* buf, int size)
-{
-    DBG("addr=%d(%d)", addr, size);
-    if (addr == 0) {
+    DBG("%d/%d", offset, total);
+    if (offset == 0) {
         _flash->init();
         _flash->eraseAll();
+        led3 = LED_ON;
     }
-    _flash->write(addr, buf, size);
+    _flash->write(offset, data, len);
+    led3 = !led3;
+    if (offset+len >= total) {
+        led3 = LED_OFF;
+        delete _flash;
+        _flash = NULL;
+        _target->SoftwareReset();
+        _target->setup();
+    }
 }