Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ISP.cpp Source File

ISP.cpp

00001 #include "mbed.h"
00002 #include "LPC.h"
00003 
00004 Serial pc(USBTX, USBRX);
00005 LocalFileSystem local("fs");
00006 
00007 int SerialBuffered::ProgramFile() {
00008     sum20 = 0; readAll = false; bytesfilled=45; lines=1; firstencode = true;
00009     char str0[] = "0";  //Command success string
00010     char strRAM[40];
00011     
00012     //Following arrays contain addresses and the size of the addresses 
00013     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};
00014     int startadd[30] = {0x0, 0x1000, 0x2000, 0x3000, 0x4000, 0x5000, 0x6000, 0x7000, 0x8000, 0x9000, 0xA000, 0xB000, 0xC000, 0xD000, 0xE000, 0xF000, 0x10000, 0x18000, 0x20000, 0x28000, 0x30000, 0x38000, 0x40000, 0x48000, 0x50000, 0x58000, 0x60000, 0x68000, 0x70000, 0x78000};
00015     
00016     pc.printf("\n\r**** LPCxxxx Flash Bootloader ****\n\r");
00017     if(CheckTargetPresent()==0) {
00018         pc.printf("ERROR: Communication failed with board. Ensure board reset and P2.10 pulled low before running mbed program! If the setup is correct, try lowering the baud rate, set in main.cpp.\r\n\n");  
00019         exit(0);
00020     }
00021     
00022     DIR *d = opendir("/fs"); //Opens the root directory of the local file system
00023     struct dirent *p;
00024     
00025     fname[0] = 0;
00026     while((p = readdir(d)) != NULL) { //Print the names of the files in the local file system
00027         if ( strcmp(p->d_name+strlen(p->d_name)-3,"LPC") == 0 )
00028         {
00029             strcpy(fname,"/fs/");
00030             strcat(fname,p->d_name);
00031         }
00032     }
00033     
00034     f = fopen(fname, "r");  //Opens the binary file for reading
00035     if (f == NULL) {
00036         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");
00037         exit(0);
00038         return 1;
00039     } 
00040     else pc.printf("%s opened successfully\n\n\r", fname);
00041     
00042     sprintf(strRAM, "W %d 1024", RAM);
00043     TargetSendStringAndCR(strRAM);
00044     readBytes(buf, 3);
00045     if ( strncmp(buf,str0, 1) != 0 )
00046         return 0;
00047     
00048     pc.printf("Writing to chip (may take a couple of minutes)");
00049     
00050     FirstEncode();
00051     TargetSendString(uuline);
00052     fclose(f);
00053     f = fopen(fname, "r");
00054     fseek(f, 45, SEEK_SET);  //Skip forward past the 45 bytes already encoded in FirstEncode()
00055     remove("/fs/delete.bin");  //Remove temporary file
00056     
00057     int summation=0;
00058     for (int i=0; i<30; i++) {  //Works out number of sectors required and compares it with number available
00059         summation+=sector[i];
00060         if (summation>=filesize) {
00061             maxsector = i;
00062             break;
00063         }
00064     }
00065     if (lastSector<maxsector) {
00066             pc.printf("ERROR: File size too large for available FLASH memory\n\r");
00067             exit(0);
00068     }
00069     
00070     int totalloaded=0;
00071     for (int b=0; b<=maxsector; b++) {
00072         int data = sector[b];
00073         while (data>0) {
00074         SectorFill(startadd[b]+(sector[b]-data));
00075         data-=1024;
00076         totalloaded+=1024;
00077         if (totalloaded>=filesize)
00078             break;
00079         }
00080     }
00081         
00082     fclose(f);
00083     pc.printf("\n\n\rFLASH COMPLETED!\n\n\r");
00084     return 0;
00085 }
00086 
00087 int SerialBuffered::SectorFill(int add) {
00088     char sendStr[64];
00089     char str0[] = "0";
00090     char strOK[] = "OK";
00091     char strRAM[40];
00092     sprintf(strRAM, "W %d 1024", RAM);
00093         
00094     if (!firstencode) {
00095         TargetSendStringAndCR(strRAM);
00096         readBytes(buf, 3);
00097     }
00098     firstencode = false;
00099     
00100     while (bytesfilled<1024) {
00101         if (bytesfilled>=990) {        //If not enough room for a full UUEncoded line use EndUUEncode()
00102             EndUUEncode();
00103             TargetSendString(enduuline);
00104             sprintf(sendStr, "%d", sum20);      //Checksum
00105             TargetSendStringAndCR(sendStr);
00106             readBytes(buf, 4);
00107             if (strncmp(buf,strOK,2)!=0)
00108                 pc.printf("ERROR: Line corrupted!\n\r");
00109             sum20=0; lines=0;
00110             pc.printf(".");
00111             break;
00112         }
00113         UUEncode();
00114         TargetSendString(uuline);
00115         bytesfilled+=45;
00116         lines++;
00117         if (lines==20) {
00118             sprintf(sendStr, "%d", sum20);      //Checksum
00119             TargetSendStringAndCR(sendStr);
00120             readBytes(buf, 4);
00121             if (strncmp(buf,strOK,2)!=0)
00122                 pc.printf("ERROR: Line corrupted!\n\r");
00123             sum20=0; lines=0;
00124         }
00125     }
00126     
00127     sprintf(sendStr, "P 0 %d", lastSector);     //Fills the sendStr string with the prepare function specific to the chip as specified in the switch function
00128     TargetSendStringAndCR(sendStr);             //Prepares the Flash for writing to
00129     readBytes(buf, 3);
00130     if (strncmp(buf,str0,1)!=0)
00131         return 0;
00132     
00133     sprintf(sendStr, "C %d %d 1024", add, RAM);  //Copies data from the RAM to FLASH
00134     TargetSendStringAndCR(sendStr);
00135     readBytes(buf, 3);
00136     bytesfilled = 0; lines=0; sum20=0;
00137     return 0;
00138 }
00139 
00140 int SerialBuffered::CheckTargetPresent() {  //Synchronises the board with the mbed and prepares the RAM for writing to
00141     char strQn[] = "?";
00142     char str0[] = "0";
00143     char strJ[] = "J";
00144     char strEcho[] = "A 0";
00145     char strSync[] = "Synchronized";
00146     char strOk[] = "OK";
00147     char strUnlock[] = "U 23130";
00148     char sendStr[64];
00149     
00150     TargetSendString(strQn);                    //Sends a question mark and looks for "Synchronized" (wish it was spelt with an 's') to be returned
00151     readBytes(buf, 14);
00152     if (strncmp(buf,strSync, 12) != 0 )
00153         return 0;
00154     TargetSendStringAndCR(strSync);             //Sends "Synchronized" back and looks for "OK" to be returned
00155     readBytes(buf, 17);
00156     if (strcspn(buf,strOk) == 17 )
00157         return 0;
00158         
00159     TargetSendStringAndCR(speed);            //Sends "0" and waits for "OK" to be returned
00160     readBytes(buf, 5+strlen(speed));
00161     if ( strcspn(buf,strOk) == 5+strlen(speed) )
00162         return 0;
00163     
00164     TargetSendStringAndCR(strEcho);
00165     readBytes(buf,7);
00166     if (strcspn(buf,str0) == 7 )
00167         return 0;
00168 
00169     TargetSendStringAndCR(strJ);                //Sends "J" (ID code request) and reads the returned int
00170     readBytes(buf, 3);
00171     if (strncmp(buf,str0,1) != 0 )
00172         return 0;
00173     
00174     readBytes(buf,1);    
00175     while(buf[0]!=0x0D) {
00176         id = (id*10)+buf[0]-'0';
00177         readBytes(buf,1);
00178     }
00179     readBytes(buf,1);
00180     
00181     int finish = IDCheck(id);
00182     pc.printf("Chip to bootload: %s\n\r", strChipType);
00183     if (finish==2) exit(0);
00184         
00185     TargetSendStringAndCR(strUnlock);           //Unlocks the Flash for writing to
00186     readBytes(buf, 3);
00187     if ( strncmp(buf,str0,1) != 0 )
00188         return 0;
00189     
00190     sprintf(sendStr, "P 0 %d", lastSector);     //Fills the sendStr string with the prepare function specific to the chip as specified in the switch function
00191     TargetSendStringAndCR(sendStr);             //Prepares the Flash for writing to
00192     readBytes(buf, 3);
00193     if ( strncmp(buf,str0,1) != 0 )
00194         return 0;
00195     
00196     sprintf(sendStr, "E 0 %d", lastSector);
00197     TargetSendStringAndCR(sendStr);             //Erases the Flash completely, ready to be loaded
00198     readBytes(buf, 3);
00199     if ( strncmp(buf,str0,1) != 0 )
00200         return 0;
00201 
00202     pc.printf("Ready to write to chip\n\r");
00203     return 1;
00204 }