William Reynolds
/
LPC_Bootloader
ISP.cpp@1:0872c208795f, 2011-09-29 (annotated)
- Committer:
- daugihao
- Date:
- Thu Sep 29 11:32:52 2011 +0000
- Revision:
- 1:0872c208795f
- Parent:
- 0:15536fa79743
- Child:
- 2:9e3ee0c6536b
Now no need to update main.cpp. Will load the first .lpc file it finds on the local filesystem.
Who changed what in which revision?
User | Revision | Line number | New 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 | 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 | 1:0872c208795f | 107 | sum20=0; lines=0; |
daugihao | 0:15536fa79743 | 108 | pc.printf("."); |
daugihao | 0:15536fa79743 | 109 | break; |
daugihao | 0:15536fa79743 | 110 | } |
daugihao | 0:15536fa79743 | 111 | UUEncode(); |
daugihao | 0:15536fa79743 | 112 | TargetSendString(uuline); |
daugihao | 0:15536fa79743 | 113 | bytesfilled+=45; |
daugihao | 0:15536fa79743 | 114 | lines++; |
daugihao | 0:15536fa79743 | 115 | if (lines==20) { |
daugihao | 1:0872c208795f | 116 | sprintf(sendStr, "%d", sum20); //Checksum |
daugihao | 0:15536fa79743 | 117 | TargetSendStringAndCR(sendStr); |
daugihao | 0:15536fa79743 | 118 | readBytes(buf, 4); |
daugihao | 0:15536fa79743 | 119 | sum20=0; lines=0; |
daugihao | 0:15536fa79743 | 120 | } |
daugihao | 0:15536fa79743 | 121 | } |
daugihao | 0:15536fa79743 | 122 | |
daugihao | 0:15536fa79743 | 123 | 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 | 124 | TargetSendStringAndCR(sendStr); //Prepares the Flash for writing to |
daugihao | 0:15536fa79743 | 125 | readBytes(buf, 3); |
daugihao | 0:15536fa79743 | 126 | if (strncmp(buf,str0,1)!=0) |
daugihao | 0:15536fa79743 | 127 | return 0; |
daugihao | 0:15536fa79743 | 128 | |
daugihao | 1:0872c208795f | 129 | sprintf(sendStr, "C %d %d 1024", add, RAM); //Copies data from the RAM to FLASH |
daugihao | 0:15536fa79743 | 130 | TargetSendStringAndCR(sendStr); |
daugihao | 0:15536fa79743 | 131 | readBytes(buf, 3); |
daugihao | 0:15536fa79743 | 132 | bytesfilled = 0; lines=0; sum20=0; |
daugihao | 0:15536fa79743 | 133 | return 0; |
daugihao | 0:15536fa79743 | 134 | } |
daugihao | 0:15536fa79743 | 135 | |
daugihao | 0:15536fa79743 | 136 | int SerialBuffered::CheckTargetPresent() { //Synchronises the board with the mbed and prepares the RAM for writing to |
daugihao | 0:15536fa79743 | 137 | char strQn[] = "?"; |
daugihao | 0:15536fa79743 | 138 | char str0[] = "0"; |
daugihao | 0:15536fa79743 | 139 | char strSpeed[] = "4000"; |
daugihao | 0:15536fa79743 | 140 | char strJ[] = "J"; |
daugihao | 0:15536fa79743 | 141 | char strEcho[] = "A 0"; |
daugihao | 0:15536fa79743 | 142 | char strSync[] = "Synchronized"; |
daugihao | 0:15536fa79743 | 143 | char strOk[] = "OK"; |
daugihao | 0:15536fa79743 | 144 | char strUnlock[] = "U 23130"; |
daugihao | 0:15536fa79743 | 145 | char sendStr[64]; |
daugihao | 0:15536fa79743 | 146 | |
daugihao | 0:15536fa79743 | 147 | TargetSendString(strQn); //Sends a question mark and looks for "Synchronized" (wish it was spelt with an 's') to be returned |
daugihao | 0:15536fa79743 | 148 | readBytes(buf, 14); |
daugihao | 0:15536fa79743 | 149 | if (strncmp(buf,strSync, 12) != 0 ) |
daugihao | 0:15536fa79743 | 150 | return 0; |
daugihao | 0:15536fa79743 | 151 | TargetSendStringAndCR(strSync); //Sends "Synchronized" back and looks for "OK" to be returned |
daugihao | 0:15536fa79743 | 152 | readBytes(buf, 17); |
daugihao | 0:15536fa79743 | 153 | if (strcspn(buf,strOk) == 17 ) |
daugihao | 0:15536fa79743 | 154 | return 0; |
daugihao | 0:15536fa79743 | 155 | |
daugihao | 0:15536fa79743 | 156 | TargetSendStringAndCR(strSpeed); //Sends "0" and waits for "OK" to be returned |
daugihao | 0:15536fa79743 | 157 | readBytes(buf, 5+strlen(strSpeed)); |
daugihao | 0:15536fa79743 | 158 | if ( strcspn(buf,strOk) == 5+strlen(strSpeed) ) |
daugihao | 0:15536fa79743 | 159 | return 0; |
daugihao | 0:15536fa79743 | 160 | |
daugihao | 0:15536fa79743 | 161 | TargetSendStringAndCR(strEcho); |
daugihao | 0:15536fa79743 | 162 | readBytes(buf,7); |
daugihao | 0:15536fa79743 | 163 | if (strcspn(buf,str0) == 7 ) |
daugihao | 0:15536fa79743 | 164 | return 0; |
daugihao | 0:15536fa79743 | 165 | |
daugihao | 0:15536fa79743 | 166 | TargetSendStringAndCR(strJ); //Sends "J" (ID code request) and reads the returned int |
daugihao | 0:15536fa79743 | 167 | readBytes(buf, 3); |
daugihao | 0:15536fa79743 | 168 | if (strncmp(buf,str0,1) != 0 ) |
daugihao | 0:15536fa79743 | 169 | return 0; |
daugihao | 0:15536fa79743 | 170 | readBytes(buf, 11); |
daugihao | 0:15536fa79743 | 171 | for (int i=0; i<9; i++) |
daugihao | 0:15536fa79743 | 172 | id = (id*10)+buf[i]-'0'; |
daugihao | 0:15536fa79743 | 173 | |
daugihao | 1:0872c208795f | 174 | int finish = IDCheck(id); |
daugihao | 0:15536fa79743 | 175 | pc.printf("Chip to bootload: %s\n\r", strChipType); |
daugihao | 1:0872c208795f | 176 | if (finish==2) exit(0); |
daugihao | 0:15536fa79743 | 177 | |
daugihao | 0:15536fa79743 | 178 | TargetSendStringAndCR(strUnlock); //Unlocks the Flash for writing to |
daugihao | 0:15536fa79743 | 179 | readBytes(buf, 3); |
daugihao | 0:15536fa79743 | 180 | if ( strncmp(buf,str0,1) != 0 ) |
daugihao | 0:15536fa79743 | 181 | return 0; |
daugihao | 0:15536fa79743 | 182 | |
daugihao | 0:15536fa79743 | 183 | 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 | 184 | TargetSendStringAndCR(sendStr); //Prepares the Flash for writing to |
daugihao | 0:15536fa79743 | 185 | readBytes(buf, 3); |
daugihao | 0:15536fa79743 | 186 | if ( strncmp(buf,str0,1) != 0 ) |
daugihao | 0:15536fa79743 | 187 | return 0; |
daugihao | 0:15536fa79743 | 188 | |
daugihao | 0:15536fa79743 | 189 | sprintf(sendStr, "E 0 %d", lastSector); |
daugihao | 0:15536fa79743 | 190 | TargetSendStringAndCR(sendStr); //Erases the Flash completely, ready to be loaded |
daugihao | 0:15536fa79743 | 191 | readBytes(buf, 3); |
daugihao | 0:15536fa79743 | 192 | if ( strncmp(buf,str0,1) != 0 ) |
daugihao | 0:15536fa79743 | 193 | return 0; |
daugihao | 0:15536fa79743 | 194 | |
daugihao | 0:15536fa79743 | 195 | pc.printf("Ready to write to chip...\n\r"); |
daugihao | 0:15536fa79743 | 196 | return 1; |
daugihao | 0:15536fa79743 | 197 | } |