Dependencies:   mbed

Committer:
daugihao
Date:
Tue Sep 27 15:44:55 2011 +0000
Revision:
0:15536fa79743
Child:
1:0872c208795f

        

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