Dependencies:   mbed

Revision:
1:0872c208795f
Parent:
0:15536fa79743
Child:
2:9e3ee0c6536b
--- a/ISP.cpp	Tue Sep 27 15:44:55 2011 +0000
+++ b/ISP.cpp	Thu Sep 29 11:32:52 2011 +0000
@@ -4,10 +4,10 @@
 Serial pc(USBTX, USBRX);
 LocalFileSystem local("fs");
 
-int SerialBuffered::ProgramFile(char *fname) {
+int SerialBuffered::ProgramFile() {
     sum20 = 0; readAll = false; bytesfilled=45; lines=1; firstencode = true;
     char str0[] = "0";  //Command success string
-    char strRAM[] = "W 268435968 1024";  //Command to state that the following 1024 bytes should be written to RAM
+    char strRAM[40];
     
     //Following arrays contain addresses and the size of the addresses 
     int sector[30] = {4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 4096, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 32768};
@@ -19,6 +19,20 @@
         exit(0);
     }
     
+    sprintf(strRAM, "W %d 1024", RAM);
+    
+    DIR *d = opendir("/fs");               // Opens the root directory of the local file system
+    struct dirent *p;
+    
+    fname[0] = 0;
+    while((p = readdir(d)) != NULL) {         // Print the names of the files in the local file system
+        if ( strcmp(p->d_name+strlen(p->d_name)-3,"LPC") == 0 )
+        {
+            strcpy(fname,"/fs/");
+            strcat(fname,p->d_name);
+        }
+    }
+    
     f = fopen(fname, "r");  //Opens the binary file for reading
     if (f == NULL) {
         pc.printf("ERROR: File not found. Ensure file exists in the mbed's local filesystem and is spelt correctly in the main.cpp\n\n\r");
@@ -72,9 +86,10 @@
 }
 
 int SerialBuffered::SectorFill(int add) {
-    char strRAM[] = "W 268435968 1024";
     char sendStr[64];
     char str0[] = "0";
+    char strRAM[40];
+    sprintf(strRAM, "W %d 1024", RAM);
         
     if (!firstencode) {
         TargetSendStringAndCR(strRAM);
@@ -86,9 +101,10 @@
         if (bytesfilled>=990) {        //If not enough room for a full UUEncoded line use EndUUEncode()
             EndUUEncode();
             TargetSendString(enduuline);
-            sprintf(sendStr, "%d\0", sum20);
+            sprintf(sendStr, "%d", sum20);      //Checksum
             TargetSendStringAndCR(sendStr);
-            readBytes(buf, 10);
+            readBytes(buf, 4);
+            sum20=0; lines=0;
             pc.printf(".");
             break;
         }
@@ -97,7 +113,7 @@
         bytesfilled+=45;
         lines++;
         if (lines==20) {
-            sprintf(sendStr, "%d\0", sum20);      //Checksum
+            sprintf(sendStr, "%d", sum20);      //Checksum
             TargetSendStringAndCR(sendStr);
             readBytes(buf, 4);
             sum20=0; lines=0;
@@ -110,7 +126,7 @@
     if (strncmp(buf,str0,1)!=0)
         return 0;
     
-    sprintf(sendStr, "C %d 268435968 1024", add);  //Copies data from the RAM to FLASH
+    sprintf(sendStr, "C %d %d 1024", add, RAM);  //Copies data from the RAM to FLASH
     TargetSendStringAndCR(sendStr);
     readBytes(buf, 3);
     bytesfilled = 0; lines=0; sum20=0;
@@ -155,8 +171,9 @@
     for (int i=0; i<9; i++)
         id = (id*10)+buf[i]-'0';
     
-    IDCheck(id);
+    int finish = IDCheck(id);
     pc.printf("Chip to bootload: %s\n\r", strChipType);
+    if (finish==2) exit(0);
         
     TargetSendStringAndCR(strUnlock);           //Unlocks the Flash for writing to
     readBytes(buf, 3);