Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
daugihao
Date:
Tue Sep 27 15:44:55 2011 +0000
Child:
1:0872c208795f
Commit message:

Changed in this revision

ID.cpp Show annotated file Show diff for this revision Revisions of this file
ISP.cpp Show annotated file Show diff for this revision Revisions of this file
LPC.h Show annotated file Show diff for this revision Revisions of this file
SerialBuffered.cpp Show annotated file Show diff for this revision Revisions of this file
UUEncode.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ID.cpp	Tue Sep 27 15:44:55 2011 +0000
@@ -0,0 +1,147 @@
+#include "mbed.h"
+#include "LPC.h"
+//Function to document the relevant information on LPC chips to load onto
+int SerialBuffered::IDCheck(int idcode) {
+    switch (idcode) {
+        //Cortex M3 - LPC17xx
+        case 638664503:
+            strcpy(strChipType, "LPC1769 - Cortex M3");
+            lastSector = 29;
+            break;
+        case 637615927:
+            strcpy(strChipType, "LPC1768 - Cortex M3");
+            lastSector = 29;
+            break;
+        case 637610039:
+            strcpy(strChipType, "LPC1767 - Cortex M3");
+            lastSector = 29;
+            break;
+        case 637615923:
+            strcpy(strChipType, "LPC1766 - Cortex M3");
+            lastSector = 21;
+            break;
+        case 637613875:
+            strcpy(strChipType, "LPC1765 - Cortex M3");
+            lastSector = 21;
+            break;
+        case 637606178:
+            strcpy(strChipType, "LPC1764 - Cortex M3");
+            lastSector = 17;
+            break;
+        case 621885239:
+            strcpy(strChipType, "LPC1759 - Cortex M3");
+            lastSector = 29;
+            break;
+        case 620838711:
+            strcpy(strChipType, "LPC1758 - Cortex M3");
+            lastSector = 29;
+            break;
+        case 620828451:
+            strcpy(strChipType, "LPC1756 - Cortex M3");
+            lastSector = 21;
+            break;
+        case 620828450:
+            strcpy(strChipType, "LPC1754 - Cortex M3");
+            lastSector = 17;
+            break;
+        case 620761377:
+            strcpy(strChipType, "LPC1752 - Cortex M3");
+            lastSector = 15;
+            break;
+        case 620761368:
+            strcpy(strChipType, "LPC1751 - Cortex M3");
+            lastSector = 7;
+            break;
+        case 620761131:
+            strcpy(strChipType, "LPC1102 - Cortex M0");
+            lastSector = 7;
+            break;
+        //Cortex M0 - LPC111x 
+        case 0x041E502B:
+            strcpy(strChipType, "LPC1111FHN33/101 - Cortex M0");
+            lastSector = 1;
+            break;
+        case 0x2516D02B:
+            strcpy(strChipType, "LPC1111FHN33/102 - Cortex M0");
+            lastSector = 1;
+            break;
+        case 0x0416502B:
+            strcpy(strChipType, "LPC1111FHN33/201 - Cortex M0");
+            lastSector = 1;
+            break;
+        case 0x2516902B:
+            strcpy(strChipType, "LPC1111FHN33/202 - Cortex M0");
+            lastSector = 1;
+            break;
+        case 0x042D502B:
+            strcpy(strChipType, "LPC1112FHN33/101 - Cortex M0");
+            lastSector = 3;
+            break;
+        case 0x2524D02B:
+            strcpy(strChipType, "LPC1112FHN33/102 - Cortex M0");
+            lastSector = 3;
+            break;
+        case 0x0425502B:
+            strcpy(strChipType, "LPC1112FHN33/201 - Cortex M0");
+            lastSector = 3;
+            break;
+        case 0x2524902B:
+            strcpy(strChipType, "LPC1112FHN33/202 - Cortex M0");
+            lastSector = 3;
+            break;
+        case 0x0434502B:
+            strcpy(strChipType, "LPC1113FHN33/201 - Cortex M0");
+            lastSector = 5;
+            break;
+        case 0x2532902B:
+            strcpy(strChipType, "LPC1113FHN33/202 - Cortex M0");
+            lastSector = 5;
+            break;
+        case 0x0434102B:
+            strcpy(strChipType, "LPC1113FHN33/301 OR LPC1113FBD48/301 - Cortex M0");
+            lastSector = 5;
+            break;
+        case 0x2532102B:
+            strcpy(strChipType, "LPC1113FHN33/302 OR LPC1113FBD48/302 - Cortex M0");
+            lastSector = 5;
+            break;
+        case 0x0444502B:
+            strcpy(strChipType, "LPC1114FHN33/201 - Cortex M0");
+            lastSector = 7;
+            break;
+        case 0x2540902B:
+            strcpy(strChipType, "LPC1114FHN33/202 - Cortex M0");
+            lastSector = 7;
+            break;
+        case 0x0444102B:
+            strcpy(strChipType, "LPC1114FHN33/301 OR LPC1114FBD48/301 OR LPC1114FA44/301 - Cortex M0");
+            lastSector = 7;
+            break;
+        case 0x2540102B:
+            strcpy(strChipType, "LPC1114FHN33/302 OR LPC1114FBD48/302 OR LPC1114FA44/302 - Cortex M0");
+            lastSector = 7;
+            break;
+       //Cortex M0 - LPC11Cxx
+        case 0x1421102B:
+            strcpy(strChipType, "LPC11C12FBD48/301 - Cortex M0");
+            lastSector = 3;
+            break;
+        case 0x1440102B:
+            strcpy(strChipType, "LPC11C14FBD48/301 - Cortex M0");
+            lastSector = 7;
+            break;
+        case 0x1431102B:
+            strcpy(strChipType, "LPC11C22FBD48/301 - Cortex M0");
+            lastSector = 3;
+            break;
+        case 0x1430102B:
+            strcpy(strChipType, "LPC11C24FBD48/301 - Cortex M0");
+            lastSector = 7;
+            break;
+            
+        default:
+            strcpy(strChipType, "\n\rUnrecognised chip! Please add into the \"CheckTargetPresent\" function the chip's relevant information.\r\n\n");
+            exit(0);
+    }
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ISP.cpp	Tue Sep 27 15:44:55 2011 +0000
@@ -0,0 +1,180 @@
+#include "mbed.h"
+#include "LPC.h"
+
+Serial pc(USBTX, USBRX);
+LocalFileSystem local("fs");
+
+int SerialBuffered::ProgramFile(char *fname) {
+    sum20 = 0; readAll = false; bytesfilled=45; lines=1; firstencode = true;
+    char str0[] = "0";  //Command success string
+    char strRAM[] = "W 268435968 1024";  //Command to state that the following 1024 bytes should be written to RAM
+    
+    //Following arrays contain addresses and the size of the addresses 
+    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};
+    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};
+    
+    pc.printf("**** LPC1xxx Flash Bootloader ****\n\n\r");
+    if(CheckTargetPresent()==0) {
+        pc.printf("ERROR: Communication failed with board. Ensure board reset and P2.10 pulled low before running mbed program!\r\n\n");  
+        exit(0);
+    }
+    
+    f = fopen(fname, "r");  //Opens the binary file for reading
+    if (f == NULL) {
+        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");
+        exit(0);
+        return 1;
+    } 
+    else pc.printf("%s opened successfully\n\n\r", fname);
+    
+    TargetSendStringAndCR(strRAM);
+    readBytes(buf, 3);
+    if ( strncmp(buf,str0, 1) != 0 )
+        return 0;
+    
+    pc.printf("Writing to chip (may take a couple of minutes)");
+    
+    FirstEncode();
+    TargetSendString(uuline);
+    fclose(f);
+    f = fopen(fname, "r");
+    fseek(f, 45, SEEK_SET);  //Skip forward past the 45 bytes already encoded in FirstEncode()
+    remove("/fs/delete.bin");  //Remove temporary file
+    
+    int summation=0;
+    for (int i=0; i<30; i++) {  //Works out number of sectors required and compares it with number available
+        summation+=sector[i];
+        if (summation>=filesize) {
+            maxsector = i;
+            break;
+        }
+    }
+    if (lastSector<maxsector) {
+            pc.printf("ERROR: File size too large for available FLASH memory\n\r");
+            exit(0);
+    }
+    
+    int totalloaded=0;
+    for (int b=0; b<=maxsector; b++) {
+        int data = sector[b];
+        while (data>0) {
+        SectorFill(startadd[b]+(sector[b]-data));
+        data-=1024;
+        totalloaded+=1024;
+        if (totalloaded>=filesize)
+            break;
+        }
+    }
+        
+    fclose(f);
+    pc.printf("\n\n\rFLASH COMPLETED!\n\n\r");
+    return 0;
+}
+
+int SerialBuffered::SectorFill(int add) {
+    char strRAM[] = "W 268435968 1024";
+    char sendStr[64];
+    char str0[] = "0";
+        
+    if (!firstencode) {
+        TargetSendStringAndCR(strRAM);
+        readBytes(buf, 3);
+    }
+    firstencode = false;
+    
+    while (bytesfilled<1024) {
+        if (bytesfilled>=990) {        //If not enough room for a full UUEncoded line use EndUUEncode()
+            EndUUEncode();
+            TargetSendString(enduuline);
+            sprintf(sendStr, "%d\0", sum20);
+            TargetSendStringAndCR(sendStr);
+            readBytes(buf, 10);
+            pc.printf(".");
+            break;
+        }
+        UUEncode();
+        TargetSendString(uuline);
+        bytesfilled+=45;
+        lines++;
+        if (lines==20) {
+            sprintf(sendStr, "%d\0", sum20);      //Checksum
+            TargetSendStringAndCR(sendStr);
+            readBytes(buf, 4);
+            sum20=0; lines=0;
+        }
+    }
+    
+    sprintf(sendStr, "P 0 %d", lastSector);     //Fills the sendStr string with the prepare function specific to the chip as specified in the switch function
+    TargetSendStringAndCR(sendStr);             //Prepares the Flash for writing to
+    readBytes(buf, 3);
+    if (strncmp(buf,str0,1)!=0)
+        return 0;
+    
+    sprintf(sendStr, "C %d 268435968 1024", add);  //Copies data from the RAM to FLASH
+    TargetSendStringAndCR(sendStr);
+    readBytes(buf, 3);
+    bytesfilled = 0; lines=0; sum20=0;
+    return 0;
+}
+
+int SerialBuffered::CheckTargetPresent() {  //Synchronises the board with the mbed and prepares the RAM for writing to
+    char strQn[] = "?";
+    char str0[] = "0";
+    char strSpeed[] = "4000";
+    char strJ[] = "J";
+    char strEcho[] = "A 0";
+    char strSync[] = "Synchronized";
+    char strOk[] = "OK";
+    char strUnlock[] = "U 23130";
+    char sendStr[64];
+    
+    TargetSendString(strQn);                    //Sends a question mark and looks for "Synchronized" (wish it was spelt with an 's') to be returned
+    readBytes(buf, 14);
+    if (strncmp(buf,strSync, 12) != 0 )
+        return 0;
+    TargetSendStringAndCR(strSync);             //Sends "Synchronized" back and looks for "OK" to be returned
+    readBytes(buf, 17);
+    if (strcspn(buf,strOk) == 17 )
+        return 0;
+        
+    TargetSendStringAndCR(strSpeed);            //Sends "0" and waits for "OK" to be returned
+    readBytes(buf, 5+strlen(strSpeed));
+    if ( strcspn(buf,strOk) == 5+strlen(strSpeed) )
+        return 0;
+    
+    TargetSendStringAndCR(strEcho);
+    readBytes(buf,7);
+    if (strcspn(buf,str0) == 7 )
+        return 0;
+
+    TargetSendStringAndCR(strJ);                //Sends "J" (ID code request) and reads the returned int
+    readBytes(buf, 3);
+    if (strncmp(buf,str0,1) != 0 )
+        return 0;
+    readBytes(buf, 11);
+    for (int i=0; i<9; i++)
+        id = (id*10)+buf[i]-'0';
+    
+    IDCheck(id);
+    pc.printf("Chip to bootload: %s\n\r", strChipType);
+        
+    TargetSendStringAndCR(strUnlock);           //Unlocks the Flash for writing to
+    readBytes(buf, 3);
+    if ( strncmp(buf,str0,1) != 0 )
+        return 0;
+    
+    sprintf(sendStr, "P 0 %d", lastSector);     //Fills the sendStr string with the prepare function specific to the chip as specified in the switch function
+    TargetSendStringAndCR(sendStr);             //Prepares the Flash for writing to
+    readBytes(buf, 3);
+    if ( strncmp(buf,str0,1) != 0 )
+        return 0;
+    
+    sprintf(sendStr, "E 0 %d", lastSector);
+    TargetSendStringAndCR(sendStr);             //Erases the Flash completely, ready to be loaded
+    readBytes(buf, 3);
+    if ( strncmp(buf,str0,1) != 0 )
+        return 0;
+
+    pc.printf("Ready to write to chip...\n\r");
+    return 1;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LPC.h	Tue Sep 27 15:44:55 2011 +0000
@@ -0,0 +1,60 @@
+#ifndef MBED_LPC_H
+#define MBED_LPC_H
+#include "mbed.h"
+
+class SerialBuffered {
+public:
+    SerialBuffered();
+    SerialBuffered( size_t bufferSize, PinName tx, PinName rx );
+    virtual ~SerialBuffered();
+    int getc();                                             // will block till the next character turns up, or return -1 if there is a timeout
+    int readable();                                         // returns 1 if there is a character available to read, 0 otherwise
+    void setTimeout( float seconds );                       // maximum time in seconds that getc() should block while waiting for a character. Pass -1 to disable the timeout
+    size_t readBytes(char *bytes, size_t requested );       //Read requested bytes into a buffer, return number actually read, which may be less than requested if there has been a timeout
+    
+    void InitUART(int baud);                                //Sets the baud rate to a user-defined value
+    void TargetSendString(char string[]);                   //Sends a string to the board
+    void TargetSendStringAndCR(char string[]);              //Sends a string to the board followed by a carriage return character
+    int CheckTargetPresent(void);                           //Starts the handshaking and then verifies the type of chip from a returned ID code
+    int ProgramFile(char *fname);                           //Programs the UUEncoded data onto the RAM and the copies into the flash
+    int UUEncode(void);                                     //Encodes the file into UU-encoded format
+    int Encode3(void);                                      //Sub-function to UUEncode which encodes the 3 raw bytes into 4 UU-encoded bytes
+    int FirstEncode(void);                                  //Encodes the first UUEncoded line which needs the first checksum added to it as the 8th DWORD
+    int EndUUEncode(void);                                  //Function to encode the last 124 bytes of the 1024 byte blocks and output to enduuline[]
+    int SectorFill(int add);                                //Function to load the data onto the board, keeping track of position in FLASH memory
+    int IDCheck(int idcode);                                //Function that looks up the relevant information about the chip from a returned ID code
+
+private:
+    FILE *f;                                                //File pointer (binary file for writing to the chip and a temporary file with the checksum-added DWORD)
+    int lastSector;                                         //Variable defnining the number of sectors which exist in Flash memory on the specific LPC17xx chip
+    char strChipType[80];                                   //Char array containing the chip type
+    char fname[64];                                         //Char array containing the file name specified in the main.cpp
+    char buf[1024];                                         //Char array containing the currently returned string from the LPC17xx board
+    int n1, n2, n3, n4;                                     //UU-Encoded 4 bytes corresponding to the converted 3 raw bytes
+    int ch1, ch2, ch3;                                      //3 raw bytes for conversion into the 4 bytes above --^
+    int sum20, checksum;                                    //Checksum sent after a certain number of UU-Encoded lines
+    int lastByte;                                           //When the end of the file to be encoded has been reached this value defines the last byte in the last line to be sent to the board
+    bool readAll;                                           //Global variable determining if the file has been encoded and sent fully
+    char uuline[63];                                        //Contains the current UU-encoded line to be sent to the board next
+    char enduuline[51];                                     //Output array from EndUUEncode()
+    char sum[45], sum1[28];                                 //The char arrays assist in the conversion to big-endian for the FirstEncode() function
+    unsigned int id;                                        //ID code for the attached chip
+    int bytesfilled;                                        //Int to keep track of the position in the 1024 byte block (sets up triggering of switching to EndUUEncode()
+    int lines;                                              //Keeps track of the number of lines sent before a checksum (checksum should be sent after every 20 UUline block where possible
+    bool firstencode;                                       //True if no encoding of the file has been done
+    int maxsector;                                          //Number of sectors required in order to accommodate the program, based on filesize
+    long int filesize;                                      //Filesize is the...file...size (number of bytes in the file to load to the chip)
+    
+    //Serial Buffered
+    void handleInterrupt();
+    
+    uint8_t *m_buff;                                        //Points at a circular buffer, containing data from m_contentStart, for m_contentSize bytes, wrapping when you get to the end
+    uint16_t  m_contentStart;                               //Index of first bytes of content
+    uint16_t  m_contentEnd;                                 //Index of bytes after last byte of content
+    uint16_t m_buffSize;
+    float m_timeout;                                        //Can be user-defined
+    Timer m_timer;                                          //Timeout timer
+    
+    Serial target;
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialBuffered.cpp	Tue Sep 27 15:44:55 2011 +0000
@@ -0,0 +1,88 @@
+#include "mbed.h"
+#include "LPC.h"
+
+Serial pc2(USBTX, USBRX);
+
+SerialBuffered::SerialBuffered( size_t bufferSize, PinName tx, PinName rx ) : target(  tx,  rx ) {
+    m_buffSize = 0;
+    m_contentStart = 0;
+    m_contentEnd = 0;
+    m_timeout = 1.0;
+    target.attach( this, &SerialBuffered::handleInterrupt );
+    m_buff = (uint8_t *) malloc( bufferSize );
+    if ( m_buff == NULL ) {
+        pc2.printf("SerialBuffered - failed to alloc buffer size %d\r\n", (int) bufferSize );
+    } else {
+        m_buffSize = bufferSize;
+    }
+}
+
+SerialBuffered::~SerialBuffered() {
+    if ( m_buff )
+        free( m_buff );
+}
+
+void SerialBuffered::InitUART(int baud) {  //Sets the baud rates
+    pc2.baud(baud);
+    target.baud(baud);
+}
+
+void SerialBuffered::TargetSendString(char * str) {  //Sends string to the board for console communication
+    for (int i=0; i<strlen(str); i++)
+        target.putc(str[i]);
+}
+
+void SerialBuffered::TargetSendStringAndCR(char * str) {  //Sends string to the board for console communication followed by the carriage return character
+    for (int i=0; i<strlen(str); i++)
+        target.putc(str[i]);
+    target.putc(0x0D);
+}
+
+void SerialBuffered::setTimeout( float seconds ) {
+    m_timeout = seconds;
+}
+
+size_t SerialBuffered::readBytes(char *bytes, size_t requested) {
+    int i = 0;
+    for (int a=0; a<m_buffSize; a++)
+        bytes[a] = 0;
+    for (; i<requested;) {
+        int c = getc();
+        if (c<0)
+            break;
+        bytes[i] = c;
+        i++;
+    }
+    return i;
+}
+
+int SerialBuffered::getc() {
+    m_timer.reset();
+    m_timer.start();
+    while (m_contentStart == m_contentEnd) {
+        wait_ms(1);
+        if (m_timeout>0 && m_timer.read()>m_timeout)
+            return EOF;
+    }
+    m_timer.stop();
+    uint8_t result = m_buff[m_contentStart++];
+    m_contentStart =  m_contentStart % m_buffSize;
+    return result;
+}
+
+int SerialBuffered::readable() {
+    return m_contentStart != m_contentEnd ;
+}
+
+void SerialBuffered::handleInterrupt() {
+    while (target.readable()) {
+        if (m_contentStart == (m_contentEnd +1) % m_buffSize) {
+            pc2.printf("SerialBuffered - buffer overrun, data lost!\r\n" );
+            exit(0);
+            //target.getc();
+        } else {
+            m_buff[m_contentEnd++] = target.getc();
+            m_contentEnd = m_contentEnd % m_buffSize;
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UUEncode.cpp	Tue Sep 27 15:44:55 2011 +0000
@@ -0,0 +1,135 @@
+#include "mbed.h"
+#include "LPC.h"
+
+//ENCODING FUNCTIONS BELOW
+int SerialBuffered::UUEncode() {       //Encodes the next 45 bytes in the file into a UUEncoded string with checksum and LF/CR characters ready to be sent to the board
+    int loop=0;  int i;  checksum=0;
+    for (int a=0; a<=63; a++) uuline[a]=0;
+    for (i=0; i<15; i++) {
+        if ((ch1=fgetc(f)) == EOF)  //Padded with 0x00 making life easier when the end of the file is reached
+            ch1=0x00;
+        if ((ch2=fgetc(f)) == EOF)
+            ch2=0x00;
+        if ((ch3=fgetc(f)) == EOF)
+            ch3=0x00;
+        checksum +=3;
+        Encode3();
+        uuline[1+(loop*4)]=n1;
+        uuline[2+(loop*4)]=n2;
+        uuline[3+(loop*4)]=n3;
+        uuline[4+(loop*4)]=n4;
+        loop++;
+    }
+    lastByte = 62;
+    uuline[0] = checksum+32;
+    uuline[61] = 0x0D;
+    uuline[62] = 0x0A;
+    return 0;
+}
+
+int SerialBuffered:: Encode3() {   //Does the actual raw encoding. Laid out in a less compact way to explicitly show the process going on
+    n1 = 0, n2 = 0, n3 = 0, n4 = 0;
+    sum20 += ch1+ch2+ch3;
+    if ((ch1-128)>=0)   {ch1-=128;  n1+=32;}
+    if ((ch1-64)>=0)    {ch1-=64;   n1+=16;}
+    if ((ch1-32)>=0)    {ch1-=32;   n1+=8;}
+    if ((ch1-16)>=0)    {ch1-=16;   n1+=4;}
+    if ((ch1-8)>=0)     {ch1-=8;    n1+=2;}
+    if ((ch1-4)>=0)     {ch1-=4;    n1+=1;}
+
+    if ((ch1-2)>=0)     {ch1-=2;    n2+=32;}
+    if ((ch1-1)>=0)     {ch1-=1;    n2+=16;}
+    if ((ch2-128)>=0)   {ch2-=128;  n2+=8;}
+    if ((ch2-64)>=0)    {ch2-=64;   n2+=4;}
+    if ((ch2-32)>=0)    {ch2-=32;   n2+=2;}
+    if ((ch2-16)>=0)    {ch2-=16;   n2+=1;}
+
+    if ((ch2-8)>=0)     {ch2-=8;    n3+=32;}
+    if ((ch2-4)>=0)     {ch2-=4;    n3+=16;}
+    if ((ch2-2)>=0)     {ch2-=2;    n3+=8;}
+    if ((ch2-1)>=0)     {ch2-=1;    n3+=4;}
+    if ((ch3-128)>=0)   {ch3-=128;  n3+=2;}
+    if ((ch3-64)>=0)    {ch3-=64;   n3+=1;}
+
+    if ((ch3-32)>=0)    {ch3-=32;   n4+=32;}
+    if ((ch3-16)>=0)    {ch3-=16;   n4+=16;}
+    if ((ch3-8)>=0)     {ch3-=8;    n4+=8;}
+    if ((ch3-4)>=0)     {ch3-=4;    n4+=4;}
+    if ((ch3-2)>=0)     {ch3-=2;    n4+=2;}
+    if ((ch3-1)>=0)     {ch3-=1;    n4+=1;}
+
+    if (n1 == 0x00) n1=0x60;
+    else n1+=0x20;
+    if (n2 == 0x00) n2=0x60;
+    else n2+=0x20;
+    if (n3 == 0x00) n3=0x60;
+    else n3+=0x20;
+    if (n4 == 0x00) n4=0x60;
+    else n4+=0x20;
+    return 0;
+}
+
+int SerialBuffered::FirstEncode() {  //Function to add two's complement of the first 7 DWORDs into the space occupied by the 8th DWORD (DWORDs 1-8 should add to zero)
+    long int precheck = 0;
+    int a;
+    for (a=0; a<9; a++) {
+        ch1 = fgetc(f);  ch2 = fgetc(f);     ch3 = fgetc(f);
+        sum[a*3]=ch1;    sum[(a*3)+1]=ch2;   sum[(a*3)+2]=ch3;
+    }
+    ch1 = fgetc(f);  fgetc(f);  fgetc(f);  fgetc(f);  fgetc(f);  //Ignores the 4 bytes which are to be overwritten
+    sum[27] = ch1;
+    
+    for (a=0; a<7; a++) {
+        sum1[a*4] = sum[a*4+3];
+        sum1[a*4+1] = sum[a*4+2];
+        sum1[a*4+2] = sum[a*4+1];
+        sum1[a*4+3] = sum[a*4];
+        precheck += (sum1[a*4]*0x1000000) + (sum1[a*4+1]*0x10000) + (sum1[a*4+2]*0x100) + sum1[a*4+3];
+    }
+    precheck = ~precheck+1;  //Takes the two's complement of the checksum
+    sum[28] = precheck & 0xFF;
+    sum[29] = (precheck >> 8) & 0xFF;
+    sum[30] = (precheck >>16) & 0xFF;
+    sum[31] = (precheck >>24) & 0xFF;
+    sum[32] = fgetc(f);
+    for (int a=33; a<46; a++) sum[a] = fgetc(f);
+    fseek(f, 0, SEEK_END);
+    filesize = ftell(f);
+    fclose(f);
+    f=fopen("/fs/delete.bin", "w");   //Opens a temporary file for writing to
+    fwrite (sum, 1, sizeof(sum), f);  //Writes the checksum-added and encoded bytes
+    fclose(f);
+    f=fopen("/fs/delete.bin", "r");   //Opens the original binary file again for reading
+    UUEncode();  
+    return 0;
+}
+
+int SerialBuffered::EndUUEncode() {  //Encodes the last 124 bytes of a 1024 byte block
+    int loop=0;  int i;  checksum=0;
+    for (int a=0; a<=50; a++) enduuline[a]=0;
+    for (i=1; i<=12; i++) {
+        if ((ch1=fgetc(f)) == EOF)
+            ch1=0x00;
+        if ((ch2=fgetc(f)) == EOF)
+            ch2=0x00;
+        if ((ch3=fgetc(f)) == EOF)
+            ch3=0x00;
+        checksum +=3;
+        if (loop==11) {
+            checksum-=2;
+            fseek (f, -2, SEEK_CUR);
+            ch2=0x00; ch3=0x00;
+        }
+        Encode3();
+        enduuline[1+(loop*4)]=n1;
+        enduuline[2+(loop*4)]=n2;
+        enduuline[3+(loop*4)]=n3;
+        enduuline[4+(loop*4)]=n4;
+        loop++;
+    }
+    lastByte = 51;
+    enduuline[0] = checksum+32;
+    enduuline[49] = 0x0D;
+    enduuline[50] = 0x0A;
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Sep 27 15:44:55 2011 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+#include "LPC.h"
+
+SerialBuffered lpc(1024, p9, p10);
+
+int main() {
+    //RESET CHIP TO LOAD TO BEFORE RUNNING PROGRAM!!!
+    lpc.InitUART(230400);                   //Specified limit of 230400 baud
+    lpc.ProgramFile("/fs/t.bin");           //Write your program name in this box with the prefix /fs/ (sometimes unstable with special characters and long names)
+}
+
+//To add new chips to load to, add a case to the switch function in ID.cpp, using the ID code and last sector number information
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Sep 27 15:44:55 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912