Dependencies:   mbed

Committer:
daugihao
Date:
Thu Sep 29 15:09:50 2011 +0000
Revision:
2:9e3ee0c6536b
Parent:
1:0872c208795f
Child:
3:eb6d9211592d
Removed line end error

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daugihao 0:15536fa79743 1 #include "mbed.h"
daugihao 0:15536fa79743 2 #include "LPC.h"
daugihao 0:15536fa79743 3
daugihao 0:15536fa79743 4 Serial pc(USBTX, USBRX);
daugihao 0:15536fa79743 5 LocalFileSystem local("fs");
daugihao 0:15536fa79743 6
daugihao 1:0872c208795f 7 int SerialBuffered::ProgramFile() {
daugihao 0:15536fa79743 8 sum20 = 0; readAll = false; bytesfilled=45; lines=1; firstencode = true;
daugihao 0:15536fa79743 9 char str0[] = "0"; //Command success string
daugihao 1:0872c208795f 10 char strRAM[40];
daugihao 0:15536fa79743 11
daugihao 0:15536fa79743 12 //Following arrays contain addresses and the size of the addresses
daugihao 0:15536fa79743 13 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};
daugihao 0:15536fa79743 14 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};
daugihao 0:15536fa79743 15
daugihao 0:15536fa79743 16 pc.printf("**** LPC1xxx Flash Bootloader ****\n\n\r");
daugihao 0:15536fa79743 17 if(CheckTargetPresent()==0) {
daugihao 0:15536fa79743 18 pc.printf("ERROR: Communication failed with board. Ensure board reset and P2.10 pulled low before running mbed program!\r\n\n");
daugihao 0:15536fa79743 19 exit(0);
daugihao 0:15536fa79743 20 }
daugihao 0:15536fa79743 21
daugihao 1:0872c208795f 22 sprintf(strRAM, "W %d 1024", RAM);
daugihao 1:0872c208795f 23
daugihao 1:0872c208795f 24 DIR *d = opendir("/fs"); // Opens the root directory of the local file system
daugihao 1:0872c208795f 25 struct dirent *p;
daugihao 1:0872c208795f 26
daugihao 1:0872c208795f 27 fname[0] = 0;
daugihao 1:0872c208795f 28 while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system
daugihao 1:0872c208795f 29 if ( strcmp(p->d_name+strlen(p->d_name)-3,"LPC") == 0 )
daugihao 1:0872c208795f 30 {
daugihao 1:0872c208795f 31 strcpy(fname,"/fs/");
daugihao 1:0872c208795f 32 strcat(fname,p->d_name);
daugihao 1:0872c208795f 33 }
daugihao 1:0872c208795f 34 }
daugihao 1:0872c208795f 35
daugihao 0:15536fa79743 36 f = fopen(fname, "r"); //Opens the binary file for reading
daugihao 0:15536fa79743 37 if (f == NULL) {
daugihao 0:15536fa79743 38 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");
daugihao 0:15536fa79743 39 exit(0);
daugihao 0:15536fa79743 40 return 1;
daugihao 0:15536fa79743 41 }
daugihao 0:15536fa79743 42 else pc.printf("%s opened successfully\n\n\r", fname);
daugihao 0:15536fa79743 43
daugihao 0:15536fa79743 44 TargetSendStringAndCR(strRAM);
daugihao 0:15536fa79743 45 readBytes(buf, 3);
daugihao 0:15536fa79743 46 if ( strncmp(buf,str0, 1) != 0 )
daugihao 0:15536fa79743 47 return 0;
daugihao 0:15536fa79743 48
daugihao 0:15536fa79743 49 pc.printf("Writing to chip (may take a couple of minutes)");
daugihao 0:15536fa79743 50
daugihao 0:15536fa79743 51 FirstEncode();
daugihao 0:15536fa79743 52 TargetSendString(uuline);
daugihao 0:15536fa79743 53 fclose(f);
daugihao 0:15536fa79743 54 f = fopen(fname, "r");
daugihao 0:15536fa79743 55 fseek(f, 45, SEEK_SET); //Skip forward past the 45 bytes already encoded in FirstEncode()
daugihao 0:15536fa79743 56 remove("/fs/delete.bin"); //Remove temporary file
daugihao 0:15536fa79743 57
daugihao 0:15536fa79743 58 int summation=0;
daugihao 0:15536fa79743 59 for (int i=0; i<30; i++) { //Works out number of sectors required and compares it with number available
daugihao 0:15536fa79743 60 summation+=sector[i];
daugihao 0:15536fa79743 61 if (summation>=filesize) {
daugihao 0:15536fa79743 62 maxsector = i;
daugihao 0:15536fa79743 63 break;
daugihao 0:15536fa79743 64 }
daugihao 0:15536fa79743 65 }
daugihao 0:15536fa79743 66 if (lastSector<maxsector) {
daugihao 0:15536fa79743 67 pc.printf("ERROR: File size too large for available FLASH memory\n\r");
daugihao 0:15536fa79743 68 exit(0);
daugihao 0:15536fa79743 69 }
daugihao 0:15536fa79743 70
daugihao 0:15536fa79743 71 int totalloaded=0;
daugihao 0:15536fa79743 72 for (int b=0; b<=maxsector; b++) {
daugihao 0:15536fa79743 73 int data = sector[b];
daugihao 0:15536fa79743 74 while (data>0) {
daugihao 0:15536fa79743 75 SectorFill(startadd[b]+(sector[b]-data));
daugihao 0:15536fa79743 76 data-=1024;
daugihao 0:15536fa79743 77 totalloaded+=1024;
daugihao 0:15536fa79743 78 if (totalloaded>=filesize)
daugihao 0:15536fa79743 79 break;
daugihao 0:15536fa79743 80 }
daugihao 0:15536fa79743 81 }
daugihao 0:15536fa79743 82
daugihao 0:15536fa79743 83 fclose(f);
daugihao 0:15536fa79743 84 pc.printf("\n\n\rFLASH COMPLETED!\n\n\r");
daugihao 0:15536fa79743 85 return 0;
daugihao 0:15536fa79743 86 }
daugihao 0:15536fa79743 87
daugihao 0:15536fa79743 88 int SerialBuffered::SectorFill(int add) {
daugihao 0:15536fa79743 89 char sendStr[64];
daugihao 0:15536fa79743 90 char str0[] = "0";
daugihao 2:9e3ee0c6536b 91 char strOK[] = "OK";
daugihao 1:0872c208795f 92 char strRAM[40];
daugihao 1:0872c208795f 93 sprintf(strRAM, "W %d 1024", RAM);
daugihao 0:15536fa79743 94
daugihao 0:15536fa79743 95 if (!firstencode) {
daugihao 0:15536fa79743 96 TargetSendStringAndCR(strRAM);
daugihao 0:15536fa79743 97 readBytes(buf, 3);
daugihao 0:15536fa79743 98 }
daugihao 0:15536fa79743 99 firstencode = false;
daugihao 0:15536fa79743 100
daugihao 0:15536fa79743 101 while (bytesfilled<1024) {
daugihao 0:15536fa79743 102 if (bytesfilled>=990) { //If not enough room for a full UUEncoded line use EndUUEncode()
daugihao 0:15536fa79743 103 EndUUEncode();
daugihao 0:15536fa79743 104 TargetSendString(enduuline);
daugihao 1:0872c208795f 105 sprintf(sendStr, "%d", sum20); //Checksum
daugihao 0:15536fa79743 106 TargetSendStringAndCR(sendStr);
daugihao 1:0872c208795f 107 readBytes(buf, 4);
daugihao 2:9e3ee0c6536b 108 if (strncmp(buf,strOK,2)!=0)
daugihao 2:9e3ee0c6536b 109 pc.printf("ERROR: Line corrupted!\n\r");
daugihao 1:0872c208795f 110 sum20=0; lines=0;
daugihao 0:15536fa79743 111 pc.printf(".");
daugihao 0:15536fa79743 112 break;
daugihao 0:15536fa79743 113 }
daugihao 0:15536fa79743 114 UUEncode();
daugihao 0:15536fa79743 115 TargetSendString(uuline);
daugihao 0:15536fa79743 116 bytesfilled+=45;
daugihao 0:15536fa79743 117 lines++;
daugihao 0:15536fa79743 118 if (lines==20) {
daugihao 1:0872c208795f 119 sprintf(sendStr, "%d", sum20); //Checksum
daugihao 0:15536fa79743 120 TargetSendStringAndCR(sendStr);
daugihao 0:15536fa79743 121 readBytes(buf, 4);
daugihao 2:9e3ee0c6536b 122 if (strncmp(buf,strOK,2)!=0)
daugihao 2:9e3ee0c6536b 123 pc.printf("ERROR: Line corrupted!\n\r");
daugihao 0:15536fa79743 124 sum20=0; lines=0;
daugihao 0:15536fa79743 125 }
daugihao 0:15536fa79743 126 }
daugihao 0:15536fa79743 127
daugihao 0:15536fa79743 128 sprintf(sendStr, "P 0 %d", lastSector); //Fills the sendStr string with the prepare function specific to the chip as specified in the switch function
daugihao 0:15536fa79743 129 TargetSendStringAndCR(sendStr); //Prepares the Flash for writing to
daugihao 0:15536fa79743 130 readBytes(buf, 3);
daugihao 0:15536fa79743 131 if (strncmp(buf,str0,1)!=0)
daugihao 0:15536fa79743 132 return 0;
daugihao 0:15536fa79743 133
daugihao 1:0872c208795f 134 sprintf(sendStr, "C %d %d 1024", add, RAM); //Copies data from the RAM to FLASH
daugihao 0:15536fa79743 135 TargetSendStringAndCR(sendStr);
daugihao 0:15536fa79743 136 readBytes(buf, 3);
daugihao 0:15536fa79743 137 bytesfilled = 0; lines=0; sum20=0;
daugihao 0:15536fa79743 138 return 0;
daugihao 0:15536fa79743 139 }
daugihao 0:15536fa79743 140
daugihao 0:15536fa79743 141 int SerialBuffered::CheckTargetPresent() { //Synchronises the board with the mbed and prepares the RAM for writing to
daugihao 0:15536fa79743 142 char strQn[] = "?";
daugihao 0:15536fa79743 143 char str0[] = "0";
daugihao 0:15536fa79743 144 char strSpeed[] = "4000";
daugihao 0:15536fa79743 145 char strJ[] = "J";
daugihao 0:15536fa79743 146 char strEcho[] = "A 0";
daugihao 0:15536fa79743 147 char strSync[] = "Synchronized";
daugihao 0:15536fa79743 148 char strOk[] = "OK";
daugihao 0:15536fa79743 149 char strUnlock[] = "U 23130";
daugihao 0:15536fa79743 150 char sendStr[64];
daugihao 0:15536fa79743 151
daugihao 0:15536fa79743 152 TargetSendString(strQn); //Sends a question mark and looks for "Synchronized" (wish it was spelt with an 's') to be returned
daugihao 0:15536fa79743 153 readBytes(buf, 14);
daugihao 0:15536fa79743 154 if (strncmp(buf,strSync, 12) != 0 )
daugihao 0:15536fa79743 155 return 0;
daugihao 0:15536fa79743 156 TargetSendStringAndCR(strSync); //Sends "Synchronized" back and looks for "OK" to be returned
daugihao 0:15536fa79743 157 readBytes(buf, 17);
daugihao 0:15536fa79743 158 if (strcspn(buf,strOk) == 17 )
daugihao 0:15536fa79743 159 return 0;
daugihao 0:15536fa79743 160
daugihao 0:15536fa79743 161 TargetSendStringAndCR(strSpeed); //Sends "0" and waits for "OK" to be returned
daugihao 0:15536fa79743 162 readBytes(buf, 5+strlen(strSpeed));
daugihao 0:15536fa79743 163 if ( strcspn(buf,strOk) == 5+strlen(strSpeed) )
daugihao 0:15536fa79743 164 return 0;
daugihao 0:15536fa79743 165
daugihao 0:15536fa79743 166 TargetSendStringAndCR(strEcho);
daugihao 0:15536fa79743 167 readBytes(buf,7);
daugihao 0:15536fa79743 168 if (strcspn(buf,str0) == 7 )
daugihao 0:15536fa79743 169 return 0;
daugihao 0:15536fa79743 170
daugihao 0:15536fa79743 171 TargetSendStringAndCR(strJ); //Sends "J" (ID code request) and reads the returned int
daugihao 0:15536fa79743 172 readBytes(buf, 3);
daugihao 0:15536fa79743 173 if (strncmp(buf,str0,1) != 0 )
daugihao 0:15536fa79743 174 return 0;
daugihao 0:15536fa79743 175 readBytes(buf, 11);
daugihao 0:15536fa79743 176 for (int i=0; i<9; i++)
daugihao 0:15536fa79743 177 id = (id*10)+buf[i]-'0';
daugihao 0:15536fa79743 178
daugihao 1:0872c208795f 179 int finish = IDCheck(id);
daugihao 0:15536fa79743 180 pc.printf("Chip to bootload: %s\n\r", strChipType);
daugihao 1:0872c208795f 181 if (finish==2) exit(0);
daugihao 0:15536fa79743 182
daugihao 0:15536fa79743 183 TargetSendStringAndCR(strUnlock); //Unlocks the Flash for writing to
daugihao 0:15536fa79743 184 readBytes(buf, 3);
daugihao 0:15536fa79743 185 if ( strncmp(buf,str0,1) != 0 )
daugihao 0:15536fa79743 186 return 0;
daugihao 0:15536fa79743 187
daugihao 0:15536fa79743 188 sprintf(sendStr, "P 0 %d", lastSector); //Fills the sendStr string with the prepare function specific to the chip as specified in the switch function
daugihao 0:15536fa79743 189 TargetSendStringAndCR(sendStr); //Prepares the Flash for writing to
daugihao 0:15536fa79743 190 readBytes(buf, 3);
daugihao 0:15536fa79743 191 if ( strncmp(buf,str0,1) != 0 )
daugihao 0:15536fa79743 192 return 0;
daugihao 0:15536fa79743 193
daugihao 0:15536fa79743 194 sprintf(sendStr, "E 0 %d", lastSector);
daugihao 0:15536fa79743 195 TargetSendStringAndCR(sendStr); //Erases the Flash completely, ready to be loaded
daugihao 0:15536fa79743 196 readBytes(buf, 3);
daugihao 0:15536fa79743 197 if ( strncmp(buf,str0,1) != 0 )
daugihao 0:15536fa79743 198 return 0;
daugihao 0:15536fa79743 199
daugihao 0:15536fa79743 200 pc.printf("Ready to write to chip...\n\r");
daugihao 0:15536fa79743 201 return 1;
daugihao 0:15536fa79743 202 }