semihost server example program

Dependencies:   SWD mbed USBLocalFileSystem BaseDAP USBDAP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // main.cpp 2014/6/22
00002 #if 1
00003 #include "Target2.h"
00004 #include "Flash.h"
00005 #include "USBLocalFileSystem.h"
00006 #include "Semihost.h"
00007 #include "mydebug.h"
00008 
00009 #if defined(TARGET_KL46Z)
00010 #define TARGET_SWDIO D12
00011 #define TARGET_SWCLK D10
00012 #define TARGET_nRESET D6
00013 #define SW_PIN SW1
00014 #define SW_MODE PullUp
00015 #define LED_OFF 1
00016 
00017 #elif defined(TARGET_LPC1549)
00018 #define TARGET_SWDIO D12
00019 #define TARGET_SWCLK D10
00020 #define TARGET_nRESET D6
00021 #define SW_PIN P1_9 // SW3
00022 #define SW_MODE PullUp
00023 #define LED_OFF 1
00024 
00025 #elif defined(TARGET_LPC11U68)
00026 #define TARGET_SWDIO D12
00027 #define TARGET_SWCLK D10
00028 #define TARGET_nRESET D6
00029 #define SW_PIN P0_1 // SW2
00030 #define SW_MODE PullUp
00031 #define LED_OFF 1
00032 
00033 #elif defined(TARGET_LPC1768)
00034 #define TARGET_SWDIO p21
00035 #define TARGET_SWCLK p22
00036 #define TARGET_nRESET p30
00037 #define SW_PIN p14 // joystick mbed application board
00038 #define LED_OFF 0
00039 
00040 #elif defined(TARGET_LPC4088)
00041 #define TARGET_SWDIO p25
00042 #define TARGET_SWCLK p26
00043 #define TARGET_nRESET p34
00044 #define SW_PIN p14 // joystick mbed application board
00045 #define LED_OFF 0
00046 
00047 #else
00048 #error "target error"
00049 #endif
00050 
00051 Serial pc(USBTX,USBRX);
00052 SWD swd(TARGET_SWDIO, TARGET_SWCLK, TARGET_nRESET);
00053 InterruptIn sw(SW_PIN); 
00054 DigitalOut led_flash(LED1);
00055 
00056 void callback_flash() {
00057     led_flash = !led_flash;
00058 }
00059 
00060 __IO bool write_start = false;
00061 void swIRQ() {
00062     wait_ms(100);
00063     write_start = true;
00064 }
00065 
00066 int main() {
00067     TRACE();
00068     pc.baud(9600);
00069     pc.printf("%s\n", __FILE__);
00070     led_flash = LED_OFF;
00071 
00072 #ifdef SW_MODE
00073     sw.mode(SW_MODE);
00074 #endif
00075     sw.rise(swIRQ);
00076         
00077     USBLocalFileSystem* usb_local = new USBLocalFileSystem(); // RamDisk(64KB)
00078 
00079     Target2* lpc = new Target2(&swd);
00080     if (!lpc->setup()) {
00081         pc.printf("*** SWD error.\n");
00082     }    
00083     Semihost semihost(lpc, &pc, usb_local);
00084     semihost.mount("/local");
00085     lpc->resume(); // C_DEBUGEN ON
00086     while(1) {
00087         if (write_start) {
00088             usb_local->lock(true);
00089             usb_local->remount();
00090             char filename[64];
00091             if (usb_local->find(filename, sizeof(filename), "*.BIN")) {
00092                 pc.printf("*** bin filename=[%s]\n", filename);
00093                 if (!lpc->setup()) {
00094                     pc.printf("*** SWD error.\n");
00095                 } else {
00096                     Flash flash(lpc, &pc);
00097                     flash.attachEvent(callback_flash);
00098                     if (flash.init()) {
00099                         flash.write(filename);
00100                         flash.verify(filename);
00101                     }
00102                 }
00103             } else {
00104                 pc.printf("*** binary image file not found.\n");
00105             }
00106             usb_local->lock(false);
00107             lpc->SoftwareReset();    
00108             lpc->HardwareReset();
00109             led_flash = LED_OFF;
00110             write_start = false;
00111         }
00112         semihost.poll();
00113     }        
00114 }
00115 #endif
00116