Dependencies:   mbed

Committer:
daugihao
Date:
Fri Sep 30 14:55:11 2011 +0000
Revision:
7:d0f98d61ec4c
Parent:
6:fab999f0e93b
Some slight alterations and comment addition

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