![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
USB composite device example program, drag-and-drop flash writer.
Dependencies: SWD USBDevice mbed BaseDAP
Diff: USBMSD_LPC.cpp
- Revision:
- 0:2385683c867a
- Child:
- 1:ea8e179320d7
diff -r 000000000000 -r 2385683c867a USBMSD_LPC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBMSD_LPC.cpp Tue Sep 17 04:33:44 2013 +0000 @@ -0,0 +1,80 @@ +// USBMSD_LPC.cpp 2013/9/17 +#include "mbed.h" +#include "USBMSD_LPC.h" +#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(); +} + +/* virtual */ int USBMSD_LPC::disk_initialize() +{ + _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); +} + +/* virtual */ int USBMSD_LPC::disk_write(const uint8_t * data, uint64_t block) +{ + 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; + } + } + 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) { + _flash->init(); + _flash->eraseAll(); + } + _flash->write(addr, buf, size); +}