semihost server example program
Dependencies: SWD mbed USBLocalFileSystem BaseDAP USBDAP
LPCXpresso LPC11U68 | LPCXpresso LPC1549 | FRDM-KL46Z | EA LPC4088 QSB app-board | LPC1768 app-board | LPC810 | LPC1114FN28 | |
---|---|---|---|---|---|---|---|
server | server | server | server | server | client | client | |
SWDIO | D12 | D12 | D12 | p25 | p21 | p4(P0_2) | p12 |
SWCLK | D10 | D10 | D10 | p26 | p22 | p3(P0_3) | p3 |
nRESET *option | D6 | D6 | D6 | p34 | p30 | p1(P0_5) | p23 |
GND | GND | GND | GND | p1 | p1 | p7 | p22 |
3.3V | P3V3 | P3V3 | P3V3 | p44 | p40 | p6 | p21 |
flash write | SW2(P0_1) | SW3(P1_9) | SW1 | p14 joystick center | p14 joystick center |
client example:
Import programlpc810-semihost_helloworld
semihost client example program
Revision 9:7e71c20c96e4, committed 2014-02-20
- Comitter:
- va009039
- Date:
- Thu Feb 20 09:43:03 2014 +0000
- Parent:
- 8:d7f5d80531e4
- Child:
- 10:12be7605a925
- Commit message:
- update ramdisk
Changed in this revision
--- a/Flash.cpp Tue Feb 18 09:32:40 2014 +0000 +++ b/Flash.cpp Thu Feb 20 09:43:03 2014 +0000 @@ -156,6 +156,7 @@ const int ram = 0x10000200; const int ram_size = 256; for(int i = 0; i < size; i += ram_size) { + update(); if (!_write_to_ram(ram, ram_size, data+i)) { _pc->printf("faild. write to ram %08x\n", ram); return false;
--- a/Flash.h Tue Feb 18 09:32:40 2014 +0000 +++ b/Flash.h Thu Feb 20 09:43:03 2014 +0000 @@ -40,6 +40,12 @@ IAP_STATUS remoteIAP(IAP_CMD cmd, uint32_t p0=0, uint32_t p1=0, uint32_t p2=0, uint32_t p3=0); uint32_t part_id; + void attachEvent(void (*ptr)()) { + if (ptr != NULL) { + onUpdate = ptr; + } + } + private: void _setup(); bool remoteREG(uint32_t addr, uint32_t value); @@ -50,11 +56,18 @@ bool sector_head(int addr); bool _write_to_ram(int addr, int size, const uint8_t* buf); bool _patch(uint8_t* buf, int size, int addr); + void (*onUpdate)(); + void update() { + if (onUpdate) { + (*onUpdate)(); + } + } struct { int iap_cclk; int sector_size; } cfg; + protected: Target2* _target; Serial* _pc;
--- a/Semihost.cpp Tue Feb 18 09:32:40 2014 +0000 +++ b/Semihost.cpp Thu Feb 20 09:43:03 2014 +0000 @@ -1,10 +1,11 @@ -// Semihost.cpp 2013/9/10 +// Semihost.cpp 2014/2/20 #include "Semihost.h" #include "mydebug.h" -Semihost::Semihost(Target2* target, Serial* usbpc) : _target(target),_pc(usbpc) +Semihost::Semihost(Target2* target, Serial* usbpc, USBStorage2* usb) : _target(target),_pc(usbpc),_usb(usb) { _dirpath = "/local"; + _writec_buf = -1; } void Semihost::mount(const char* dirpath) @@ -89,8 +90,21 @@ int Semihost::sys_writec(uint32_t arg) // 0x03 { - uint8_t c = rd<uint8_t>(arg); - return _pc->putc(c); + int c = rd<uint8_t>(arg); + _usb->putc(c); + return c; +} + +int Semihost::readable() +{ + return (_writec_buf == (-1)) ? 0 : 1; +} + +int Semihost::getc() +{ + int c = _writec_buf; + _writec_buf = -1; + return c; } int Semihost::sys_write0(uint32_t arg) // 0x04 @@ -139,7 +153,7 @@ int Semihost::sys_readc(uint32_t arg) // 0x07 { - return _pc->getc() & 0xff; + return _usb->getc() & 0xff; } int Semihost::sys_istty(uint32_t arg) // 0x09
--- a/Semihost.h Tue Feb 18 09:32:40 2014 +0000 +++ b/Semihost.h Thu Feb 20 09:43:03 2014 +0000 @@ -1,19 +1,23 @@ -// Semihost.h 2013/9/14 +// Semihost.h 2014/2/20 #pragma once #include "Target2.h" +#include "Storage.h" #include "mydebug.h" /** Semihosting */ class Semihost { public: - Semihost(Target2* target, Serial* usbpc); + Semihost(Target2* target, Serial* usbpc, USBStorage2* usb); void mount(const char* dirpath); int poll(); enum Reason { SYS_NONE = 0, SYS_EXIT = 0x18, }; + int readable(); + int getc(); + private: int exec(uint32_t reason, uint32_t arg); @@ -64,5 +68,7 @@ protected: Target2* _target; Serial* _pc; + USBStorage2* _usb; char* _dirpath; + int _writec_buf; };
--- a/USBMSD2/USB_CDC.h Tue Feb 18 09:32:40 2014 +0000 +++ b/USBMSD2/USB_CDC.h Thu Feb 20 09:43:03 2014 +0000 @@ -25,7 +25,7 @@ #define CDC_EPINT_IN EP1IN #define CDC_EPBULK_IN EP5IN #define CDC_EPBULK_OUT EP5OUT -#elif defined(TARGET_LPC1347) +#elif defined(TARGET_LPC1347)||defined(TARGET_LPC11U24) #define CDC_EPINT_IN EP1IN #define CDC_EPBULK_IN EP3IN #define CDC_EPBULK_OUT EP3OUT
--- a/main.cpp Tue Feb 18 09:32:40 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -// main.cpp 2013/9/23 -#if defined(TARGET_LPC1768) -#include "Target2.h" -#include "Flash.h" -#include "Semihost.h" - -LocalFileSystem local("local"); -Serial pc(USBTX, USBRX); -SWD swd(p21,p22,p17); // SWDIO(dp12),SWCLK(dp3),nReset(dp23) -Serial target_uart(p9,p10); // RXD(dp15),TXD(dp16) - -void Terminal(Target2* target) -{ - target->setup(); - Semihost semihost(target, &pc); - semihost.mount("/local"); - - pc.printf("*** dumb-terminal ***\n\n"); - while(1) { - if (target_uart.readable()) { - pc.putc(target_uart.getc()); - } - if(pc.readable()) { - target_uart.putc(pc.getc()); - } - if (semihost.poll() == Semihost::SYS_EXIT) { - break; - } - } - exit(0); -} - -int main() { - pc.baud(921600); - pc.printf("\ndumb-terminal with semihosting\n"); - - const char* filename = "/local/1114FN28.LPC"; - pc.printf("%s\n", filename); - - Target2* LPC1114 = new Target2(&swd); - LPC1114->setup(); - Flash flash(LPC1114, &pc); - flash.init(); - if (flash.eraseAll()) { - flash.write(filename); - flash.verify(filename); - } - target_uart.baud(9600); - LPC1114->SoftwareReset(); - Terminal(LPC1114); -} -#endif
--- a/main_KL46Z.cpp Tue Feb 18 09:32:40 2014 +0000 +++ b/main_KL46Z.cpp Thu Feb 20 09:43:03 2014 +0000 @@ -1,46 +1,52 @@ -// main_kl46z.cpp 2014/2/18 +// main_KL46Z.cpp 2014/2/20 #if defined(TARGET_KL46Z) #include "Target2.h" #include "Flash.h" -#include "Semihost.h" #include "RamDisk.h" #include "Storage.h" +#include "Semihost.h" Serial pc(USBTX, USBRX); -SWD swd(PTE2,PTE3,PTE6); // SWDIO(dp12),SWCLK(dp3),nReset(dp23) -Serial target_uart(PTE22,PTE23); // RXD(dp15),TXD(dp16) +SWD swd(PTE2,PTE3,PTE6); // SWDIO,SWCLK,nReset InterruptIn sw1(SW1); InterruptIn sw3(SW3); -DigitalOut led_sw(PTD5); -DigitalOut led_tx(PTE29); +DigitalOut led_disk(PTD5); +DigitalOut led_flash(PTE29); #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() { - led_sw = LED_ON; wait_ms(100); - led_sw = LED_OFF; write_start = true; } int main() { pc.baud(9600); pc.printf("%s\n", __FILE__); - led_sw = LED_OFF; - led_tx = LED_OFF; + led_disk = LED_OFF; + led_flash = LED_OFF; sw1.rise(swIRQ); sw3.rise(swIRQ); RamDisk* ramdisk = new RamDisk; + ramdisk->attachEvent(callback_disk); USBStorage2* usb = new USBStorage2(ramdisk); LocalStorage* local = NULL; Target2* lpc = new Target2(&swd); - pc.printf("*** dumb-terminal ***\n\n"); lpc->setup(); - Semihost semihost(lpc, &pc); + Semihost semihost(lpc, &pc, usb); semihost.mount("/local"); + lpc->resume(); // C_DEBUGEN ON while(1) { if (write_start) { if (local) { @@ -52,6 +58,7 @@ 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()); @@ -61,14 +68,9 @@ } lpc->SoftwareReset(); lpc->HardwareReset(); + led_flash = LED_OFF; write_start = false; } - if (target_uart.readable()) { - pc.putc(target_uart.getc()); - } - if(pc.readable()) { - target_uart.putc(pc.getc()); - } semihost.poll(); } }
--- a/mbed.bld Tue Feb 18 09:32:40 2014 +0000 +++ b/mbed.bld Thu Feb 20 09:43:03 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/869cf507173a \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/ed8466a608b4 \ No newline at end of file
--- a/src/RamDisk.cpp Tue Feb 18 09:32:40 2014 +0000 +++ b/src/RamDisk.cpp Thu Feb 20 09:43:03 2014 +0000 @@ -6,15 +6,10 @@ #define RAMDISK_DBG(...) #endif -RamDisk::RamDisk() +RamDisk::RamDisk(int sectors):_sectors(sectors) { + onUpdate = NULL; format(); - _sectors = 128; // 128*512 64Kbytes -} - -/* virtual */ int RamDisk::storage_initialize() -{ - return 0; } /* virtual */ uint32_t RamDisk::storage_sectors() @@ -27,28 +22,20 @@ return _sectors * 512; } -/* virtual */ int RamDisk::storage_status() -{ - return 0; -} - /* virtual */ int RamDisk::storage_read(uint8_t * data, uint32_t block) { RAMDISK_DBG("R block=%d", block); + update(); memset(data, 0x00, 512); if (_sector_image.count(block) > 0) { uint8_t* buf = _sector_image[block]; int size = buf[0]|(buf[1]<<8); + size &= 0x7fff; // remove ROM area bit memcpy(data, buf + 2, size); } return 0; } -bool is_romdata(uint8_t* data) -{ - return data < (uint8_t*)0x1ffff000; -} - static int block_size(const uint8_t* data) { for(int i = 512-1; i >= 0; i--) { @@ -61,10 +48,11 @@ /* virtual */ int RamDisk::storage_write(const uint8_t * data, uint32_t block) { + update(); int size = block_size(data); if (_sector_image.count(block) > 0) { uint8_t* buf = _sector_image[block]; - if (!is_romdata(buf)) { + if (!(buf[1] & 0x80)) { // ROM area free(buf); } buf = (uint8_t*)malloc(size + 2); @@ -104,7 +92,8 @@ int block = _sector_image.getKey(i); uint8_t* buf = _sector_image[block]; int size = buf[0]|(buf[1]<<8); - printf("0x%02x,0x%02x,0x%02x, // block=%d, size=%d\n", + size &= 0x7fff; + printf("0x%02x,0x%02x,0x%02x|0x80, // block=%d, size=%d, ROM area\n", block, buf[0], buf[1], block, size); if (mode == 1) { for(int j = 0; j < size; j++) {
--- a/src/RamDisk.h Tue Feb 18 09:32:40 2014 +0000 +++ b/src/RamDisk.h Thu Feb 20 09:43:03 2014 +0000 @@ -4,13 +4,17 @@ class RamDisk : public StorageInterface { public: - RamDisk(); + RamDisk(int sectors = 128); // 128*512 64Kbytes virtual int storage_read(uint8_t* data, uint32_t block); virtual int storage_write(const uint8_t* data, uint32_t block); - virtual int storage_initialize(); virtual uint32_t storage_sectors(); virtual uint32_t storage_size(); - virtual int storage_status(); + + void attachEvent(void (*ptr)()) { + if (ptr != NULL) { + onUpdate = ptr; + } + } void dump(int mode); @@ -18,4 +22,10 @@ void format(); int _sectors; mymap<int,uint8_t*>_sector_image; + void (*onUpdate)(); + void update() { + if (onUpdate) { + (*onUpdate)(); + } + } };
--- a/src/RamDiskFormat.cpp Tue Feb 18 09:32:40 2014 +0000 +++ b/src/RamDiskFormat.cpp Thu Feb 20 09:43:03 2014 +0000 @@ -2,7 +2,7 @@ #include "RamDisk.h" static const uint8_t block_image[] = { // 5 blocks -0x00,0x00,0x02, // block=0, size=512 +0x00,0x00,0x82, // block=0, size=512 0xeb,0x3c,0x90,0x4d,0x53,0x44,0x4f,0x53,0x35,0x2e,0x30,0x00,0x02,0x01,0x06,0x00, 0x02,0x00,0x02,0x80,0x00,0xf8,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x29,0xca,0xf4,0x10,0x94,0x4e,0x4f,0x20,0x4e,0x41, @@ -35,11 +35,11 @@ 0x6b,0x20,0x65,0x72,0x72,0x6f,0x72,0xff,0x0d,0x0a,0x50,0x72,0x65,0x73,0x73,0x20, 0x61,0x6e,0x79,0x20,0x6b,0x65,0x79,0x20,0x74,0x6f,0x20,0x72,0x65,0x73,0x74,0x61, 0x72,0x74,0x0d,0x0a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xac,0xcb,0xd8,0x55,0xaa, -0x06,0x05,0x00, // block=6, size=5 +0x06,0x05,0x80, // block=6, size=5 0xf8,0xff,0xff,0xff,0x0f, -0x07,0x05,0x00, // block=7, size=5 +0x07,0x05,0x80, // block=7, size=5 0xf8,0xff,0xff,0xff,0x0f, -0x08,0xfa,0x00, // block=8, size=250 +0x08,0xfa,0x80, // block=8, size=250 0x4c,0x50,0x43,0x38,0x31,0x49,0x53,0x50,0x20,0x20,0x20,0x08,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x87,0x4d,0x44,0x00,0x00,0x00,0x00,0x00,0x00, 0x41,0x2e,0x00,0x54,0x00,0x72,0x00,0x61,0x00,0x73,0x00,0x0f,0x00,0x25,0x68,0x00, @@ -56,7 +56,7 @@ 0x61,0x00,0x74,0x00,0x61,0x00,0x5f,0x00,0x6e,0x00,0x00,0x00,0x65,0x00,0x76,0x00, 0x4d,0x45,0x54,0x41,0x44,0x41,0x7e,0x31,0x20,0x20,0x20,0x20,0x00,0x38,0x1d,0x87, 0x4d,0x44,0x4d,0x44,0x00,0x00,0x85,0x75,0x8e,0x41, -0x28,0x5a,0x00, // block=40, size=90 +0x28,0x5a,0x80, // block=40, size=90 0x2e,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x10,0x00,0x68,0x1c,0x87, 0x4d,0x44,0x4d,0x44,0x00,0x00,0x1d,0x87,0x4d,0x44,0x02,0x00,0x00,0x00,0x00,0x00, 0x2e,0x2e,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x10,0x00,0x68,0x1c,0x87, @@ -74,6 +74,7 @@ break; } int size = p[0]|(p[1]<<8); + size &= 0x7fff; _sector_image[block] = (uint8_t*)p; p += size + 2; }
--- a/src/Storage.h Tue Feb 18 09:32:40 2014 +0000 +++ b/src/Storage.h Thu Feb 20 09:43:03 2014 +0000 @@ -34,10 +34,14 @@ virtual int disk_write(const uint8_t * data, uint64_t block) { return _storage->storage_write(data, block); } - virtual int disk_initialize() { return _storage->storage_initialize(); } - virtual uint64_t disk_sectors() { return _storage->storage_sectors(); } - virtual uint64_t disk_size() { return _storage->storage_size(); } - virtual int disk_status() { return _storage->storage_status(); } + virtual int disk_initialize() { return 0; } + virtual uint64_t disk_sectors() { + return _storage->storage_sectors(); + } + virtual int disk_status() { return 0; } + virtual uint64_t disk_size() { + return _storage->storage_size(); + } private: StorageInterface* _storage; @@ -54,11 +58,16 @@ virtual int disk_write(const uint8_t * data, uint64_t block) { return _storage->storage_write(data, block); } - virtual int disk_initialize() { return _storage->storage_initialize(); } - virtual uint64_t disk_sectors() { return _storage->storage_sectors(); } - virtual uint64_t disk_size() { return _storage->storage_size(); } - virtual int disk_status() { return _storage->storage_status(); } + virtual int disk_initialize() { return 0; } + virtual uint64_t disk_sectors() { + return _storage->storage_sectors(); + } + virtual int disk_status() { return 0; } + virtual uint64_t disk_size() { + return _storage->storage_size(); + } private: StorageInterface* _storage; }; +
--- a/src/StorageInterface.h Tue Feb 18 09:32:40 2014 +0000 +++ b/src/StorageInterface.h Thu Feb 20 09:43:03 2014 +0000 @@ -4,8 +4,6 @@ public: virtual int storage_read(uint8_t* data, uint32_t block) = 0; virtual int storage_write(const uint8_t* data, uint32_t block) = 0; - virtual int storage_initialize() = 0; virtual uint32_t storage_sectors() = 0; virtual uint32_t storage_size() = 0; - virtual int storage_status() = 0; };