I2C boot loader with Blinky code Should test it with Freescale boards fingers crossed should work

Dependencies:   mbed

Fork of I2Cboot_betav1_01 by Siva ram

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <stdbool.h>
00003 #include <stdint.h>
00004 //#include <fcntl.h>
00005 #include <stdlib.h>
00006 #include <stdio.h>
00007 //#include <memory.h>
00008 
00009 
00010 /*
00011 
00012 Use pull up resistors
00013 Test Sample I2C with Tiva
00014 Learn about the I2C in freescale boards
00015 Check I2CReceiveData and I2CSendData compatibitily with Tiva
00016 Now Use ROM boot loader
00017 if it works get the flash code from CDMS people and try with that
00018 
00019 */
00020 
00021 // BootLoader
00022 
00023 Serial pc(USBTX, USBRX); // tx, rx
00024 
00025 I2C i2c(D15, D14);
00026 
00027 const int addr = 0x42;
00028 
00029 #define COMMAND_PING            0x20
00030 #define COMMAND_DOWNLOAD        0x21
00031 #define COMMAND_RUN             0x22
00032 #define COMMAND_GET_STATUS      0x23
00033 #define COMMAND_SEND_DATA       0x24
00034 #define COMMAND_RESET           0x25
00035 #define COMMAND_RET_SUCCESS     0x40
00036 #define COMMAND_RET_UNKNOWN_CMD 0x41
00037 #define COMMAND_RET_INVALID_CMD 0x42
00038 #define COMMAND_RET_INVALID_ADR 0x43
00039 #define COMMAND_RET_FLASH_FAIL  0x44
00040 #define COMMAND_RET_CRC_FAIL    0x45
00041 #define COMMAND_ACK             0xcc
00042 #define COMMAND_NAK             0x33
00043 
00044 //*****************************************************************************
00045 //
00046 //! This variable will be set to the starting address of the binary will be
00047 //! programmed into to device.
00048 //
00049 
00050 static uint32_t g_ui32DownloadAddress;
00051 
00052 
00053 //
00054 //! This variable will be set to the start execution address for the binary
00055 //! that is downloaded.
00056 //
00057 
00058 static uint32_t g_ui32StartAddress;
00059 
00060 
00061 
00062 
00063 uint32_t g_ui32DataSize;
00064 uint8_t g_pui8Buffer[256];
00065 
00066 uint8_t CheckSum(uint8_t *pui8Data, uint8_t ui8Size);
00067 
00068 //
00069 
00070 int 
00071 I2CSendData(uint8_t const *pui8Data, uint8_t ui8Size)
00072 {
00073    
00074     int i;
00075     char temp[ui8Size];
00076     
00077     
00078     
00079     for(i=0;i<ui8Size;i++)
00080     {
00081         
00082         temp[i] = pui8Data[i];
00083            
00084     }
00085     
00086     
00087     if(i2c.write(addr, temp, ui8Size,0) == 0)
00088     {
00089         return (0);
00090     }
00091     
00092     
00093     pc.printf("Write Error\n");
00094     return (-1);
00095     
00096     
00097 }
00098 
00099 int
00100 I2CReceiveData(uint8_t *pui8Data, uint8_t ui8Size)
00101 {
00102    
00103     int i;
00104     char temp[ui8Size];
00105     
00106     
00107     
00108     for(i=0;i<ui8Size;i++)
00109     {
00110         
00111         temp[i] = pui8Data[i];
00112            
00113     }
00114     
00115     
00116     if(i2c.read(addr, temp, ui8Size,0) == 0)
00117     {
00118         return (0);
00119     }
00120     
00121     pc.printf("Recieve Error\n");
00122     return (-1);
00123         
00124 }        
00125  
00126 
00127 //****************************************************************************
00128 //
00129 //! AckPacket() sends an Acknowledge a packet.
00130 //!
00131 //! This function acknowledges a packet has been received from the device.
00132 //!
00133 //! \return The function returns zero to indicated success while any non-zero
00134 //! value indicates a failure.
00135 //
00136 //****************************************************************************
00137 
00138 
00139 
00140 
00141 
00142 int32_t
00143 AckPacket(void)
00144 {
00145     uint8_t ui8Ack;
00146 
00147     ui8Ack = COMMAND_ACK;
00148     return(I2CSendData(&ui8Ack, 1));
00149 }
00150 
00151 
00152 //
00153 //! NakPacket() sends a No Acknowledge packet.
00154 //!
00155 //! This function sends a no acknowledge for a packet that has been
00156 //! received unsuccessfully from the device.
00157 //!
00158 //! \return The function returns zero to indicated success while any non-zero
00159 //! value indicates a failure.
00160 //
00161 
00162 int32_t
00163 NakPacket(void)
00164 {
00165     uint8_t ui8Nak;
00166 
00167     ui8Nak = COMMAND_NAK;
00168     return(I2CSendData(&ui8Nak, 1));
00169 }
00170 
00171 
00172 //
00173 //! GetPacket() receives a data packet.
00174 //!
00175 //! \param pui8Data is the location to store the data received from the device.
00176 //! \param pui8Size is the number of bytes returned in the pui8Data buffer that
00177 //! was provided.
00178 //!
00179 //! This function receives a packet of data from UART port.
00180 //!
00181 //! \returns The function returns zero to indicated success while any non-zero
00182 //! value indicates a failure.
00183 //
00184 
00185 int32_t
00186 GetPacket(uint8_t *pui8Data, uint8_t *pui8Size)
00187 {
00188     uint8_t ui8CheckSum;
00189     uint8_t ui8Size;
00190 
00191     //
00192     // Get the size and the checksum.
00193     //
00194     do
00195     {
00196         if(I2CReceiveData(&ui8Size, 1))
00197         {
00198             return(-1);
00199         }
00200     }
00201     while(ui8Size == 0);
00202 
00203     if(I2CReceiveData(&ui8CheckSum, 1))
00204     {
00205         return(-1);
00206     }
00207     *pui8Size = ui8Size - 2;
00208 
00209     if(I2CReceiveData(pui8Data, *pui8Size))
00210     {
00211         *pui8Size = 0;
00212         return(-1);
00213     }
00214 
00215     //
00216     // Calculate the checksum from the data.
00217     //
00218     if(CheckSum(pui8Data, *pui8Size) != ui8CheckSum)
00219     {
00220         *pui8Size = 0;
00221         return(NakPacket());
00222     }
00223 
00224     return(AckPacket());
00225 }
00226 
00227 
00228 //
00229 //! CheckSum() Calculates an 8 bit checksum
00230 //!
00231 //! \param pui8Data is a pointer to an array of 8 bit data of size ui8Size.
00232 //! \param ui8Size is the size of the array that will run through the checksum
00233 //!     algorithm.
00234 //!
00235 //! This function simply calculates an 8 bit checksum on the data passed in.
00236 //!
00237 //! \return The function returns the calculated checksum.
00238 //
00239 
00240 uint8_t
00241 CheckSum(uint8_t *pui8Data, uint8_t ui8Size)
00242 {
00243     int32_t i;
00244     uint8_t ui8CheckSum;
00245 
00246     ui8CheckSum = 0;
00247 
00248     for(i = 0; i < ui8Size; ++i)
00249     {
00250         ui8CheckSum += pui8Data[i];
00251     }
00252     return(ui8CheckSum);
00253 }
00254 
00255 
00256 //
00257 //! SendPacket() sends a data packet.
00258 //!
00259 //! \param pui8Data is the location of the data to be sent to the device.
00260 //! \param ui8Size is the number of bytes to send from puData.
00261 //! \param bAck is a boolean that is true if an ACK/NAK packet should be
00262 //! received in response to this packet.
00263 //!
00264 //! This function sends a packet of data to the device.
00265 //!
00266 //! \returns The function returns zero to indicated success while any non-zero
00267 //!     value indicates a failure.
00268 //
00269 
00270 int32_t
00271 SendPacket(uint8_t *pui8Data, uint8_t ui8Size, bool bAck)
00272 {
00273     uint8_t ui8CheckSum;
00274     uint8_t ui8Ack;
00275 
00276     ui8CheckSum = CheckSum(pui8Data, ui8Size);
00277 
00278     //
00279     // Make sure that we add the bytes for the size and checksum to the total.
00280     //
00281     ui8Size += 2;
00282 
00283     //
00284     // Send the Size in bytes.
00285     //
00286     if(I2CSendData(&ui8Size, 1))
00287     {
00288         return(-1);
00289     }
00290 
00291     //
00292     // Send the CheckSum
00293     //
00294     if(I2CSendData(&ui8CheckSum, 1))
00295     {
00296         return(-1);
00297     }
00298 
00299     //
00300     // Now send the remaining bytes out.
00301     //
00302     ui8Size -= 2;
00303 
00304     //
00305     // Send the Data
00306     //
00307     if(I2CSendData(pui8Data, ui8Size))
00308     {
00309         return(-1);
00310     }
00311 
00312     //
00313     // Return immediately if no ACK/NAK is expected.
00314     //
00315     if(!bAck)
00316     {
00317         return(0);
00318     }
00319 
00320     //
00321     // Wait for the acknowledge from the device.
00322     //
00323     do
00324     {
00325         if(I2CReceiveData(&ui8Ack, 1))
00326         {
00327             return(-1);
00328         }
00329     }
00330     while(ui8Ack == 0);
00331 
00332     if(ui8Ack != COMMAND_ACK)
00333     {
00334         return(-1);
00335     }
00336     return(0);
00337 }
00338 
00339 
00340 int32_t
00341 SendCommand(uint8_t *pui8Command, uint8_t ui8Size)
00342 {
00343     uint8_t ui8Status;
00344 
00345     //
00346     // Send the command itself.
00347     //
00348     if(SendPacket(pui8Command, ui8Size, 1) < 0)
00349     {
00350         return(-1);
00351     }
00352 
00353     //
00354     // Send the get status command to tell the device to return status to
00355     // the host.
00356     //
00357     ui8Status = COMMAND_GET_STATUS;
00358     if(SendPacket(&ui8Status, 1, 1) < 0)
00359     {
00360         pc.printf("Failed to Get Status\n");
00361         return(-1);
00362     }
00363 
00364     //
00365     // Read back the status provided from the device.
00366     //
00367     ui8Size = sizeof(ui8Status);
00368     if(GetPacket(&ui8Status, &ui8Size) < 0)
00369     {
00370         pc.printf("Failed to Get Packet\n");
00371         return(-1);
00372     }
00373     if(ui8Status != COMMAND_RET_SUCCESS)
00374     {
00375         pc.printf("Failed to get download command Return Code: %04x\n",
00376             ui8Status);
00377         return(-1);
00378     }
00379     return(0);
00380 }
00381 
00382  
00383     
00384 int main() {
00385     
00386     g_ui32DownloadAddress = 0;
00387     g_ui32StartAddress = 0xffffffff;    
00388     g_ui32DataSize = 8;
00389     uint8_t ui8Command;
00390     
00391     
00392     uint32_t ui32TransferLength = 1424;
00393     uint32_t ui32Offset;
00394     uint8_t pui8FileBuffer[] = {0 ,  1 ,  0 ,  32 ,  95 ,  5 ,  0 ,  0 ,  109 ,  5 ,  0 ,  0 ,  251 ,  3 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 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0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  107 ,  5 ,  0 ,  0 ,  0 ,  42 ,  74 ,  208 ,  95 ,  234 ,  0 ,  12 ,  139 ,  7 ,  28 ,  209 ,  131 ,  7 ,  34 ,  209 ,  16 ,  42 ,  8 ,  211 ,  112 ,  180 ,  16 ,  58 ,  120 ,  201 ,  120 ,  192 ,  16 ,  58 ,  251 ,  210 ,  112 ,  188 ,  16 ,  50 ,  56 ,  208 ,  4 ,  42 ,  44 ,  211 ,  8 ,  42 ,  5 ,  211 ,  12 ,  42 ,  36 ,  191 ,  8 ,  201 ,  8 ,  192 ,  8 ,  201 ,  8 ,  192 ,  8 ,  201 ,  8 ,  192 ,  146 ,  7 ,  42 ,  208 ,  146 ,  15 ,  34 ,  224 ,  11 ,  120 ,  3 ,  112 ,  73 ,  28 ,  64 ,  28 ,  82 ,  30 ,  34 ,  208 ,  139 ,  7 ,  247 ,  209 ,  195 ,  7 ,  20 ,  209 ,  131 ,  7 ,  216 ,  208 ,  18 ,  31 ,  18 ,  211 ,  8 ,  201 ,  3 ,  128 ,  27 ,  12 ,  67 ,  128 ,  0 ,  29 ,  18 ,  31 ,  248 ,  210 ,  10 ,  224 ,  8 ,  201 ,  3 ,  112 ,  27 ,  10 ,  67 ,  112 ,  27 ,  10 ,  131 ,  112 ,  27 ,  10 ,  195 ,  112 ,  0 ,  29 ,  18 ,  31 ,  244 ,  210 ,  18 ,  29 ,  5 ,  208 ,  11 ,  120 ,  3 ,  112 ,  73 ,  28 ,  64 ,  28 ,  82 ,  30 ,  249 ,  209 ,  96 ,  70 ,  112 ,  71 ,  240 ,  181 ,  30 ,  72 ,  1 ,  39 ,  248 ,  66 ,  1 ,  208 ,  0 ,  240 ,  195 ,  248 ,  30 ,  77 ,  30 ,  79 ,  31 ,  78 ,  189 ,  66 ,  6 ,  210 ,  3 ,  205 ,  4 ,  120 ,  164 ,  0 ,  52 ,  89 ,  64 ,  28 ,  160 ,  71 ,  246 ,  231 ,  22 ,  77 ,  22 ,  79 ,  189 ,  66 ,  2 ,  210 ,  16 ,  205 ,  160 ,  71 ,  250 ,  231 ,  240 ,  189 ,  3 ,  37 ,  27 ,  224 ,  65 ,  104 ,  8 ,  48 ,  10 ,  28 ,  42 ,  64 ,  11 ,  209 ,  26 ,  28 ,  42 ,  64 ,  171 ,  67 ,  5 ,  208 ,  4 ,  104 ,  4 ,  48 ,  12 ,  96 ,  4 ,  49 ,  4 ,  59 ,  249 ,  209 ,  19 ,  28 ,  5 ,  208 ,  4 ,  120 ,  1 ,  48 ,  12 ,  112 ,  1 ,  49 ,  1 ,  59 ,  249 ,  209 ,  2 ,  28 ,  42 ,  64 ,  1 ,  208 ,  168 ,  67 ,  4 ,  48 ,  3 ,  104 ,  0 ,  43 ,  224 ,  209 ,  112 ,  71 ,  192 ,  70 ,  255 ,  255 ,  255 ,  255 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  136 ,  5 ,  0 ,  0 ,  144 ,  5 ,  0 ,  0 ,  128 ,  5 ,  0 ,  0 ,  112 ,  181 ,  16 ,  248 ,  1 ,  75 ,  27 ,  224 ,  64 ,  28 ,  90 ,  177 ,  29 ,  10 ,  9 ,  209 ,  2 ,  37 ,  16 ,  248 ,  1 ,  107 ,  109 ,  30 ,  70 ,  234 ,  3 ,  35 ,  249 ,  209 ,  1 ,  224 ,  4 ,  43 ,  2 ,  211 ,  16 ,  248 ,  1 ,  91 ,  3 ,  224 ,  37 ,  70 ,  1 ,  224 ,  1 ,  248 ,  1 ,  91 ,  91 ,  30 ,  179 ,  241 ,  255 ,  63 ,  249 ,  209 ,  1 ,  224 ,  1 ,  248 ,  1 ,  59 ,  16 ,  248 ,  1 ,  59 ,  156 ,  66 ,  249 ,  209 ,  16 ,  248 ,  1 ,  59 ,  0 ,  43 ,  231 ,  209 ,  16 ,  248 ,  1 ,  91 ,  3 ,  120 ,  45 ,  2 ,  91 ,  25 ,  213 ,  209 ,  112 ,  189 ,  254 ,  231 ,  18 ,  72 ,  19 ,  75 ,  32 ,  33 ,  1 ,  96 ,  2 ,  104 ,  18 ,  73 ,  173 ,  241 ,  8 ,  13 ,  0 ,  146 ,  2 ,  32 ,  88 ,  96 ,  0 ,  34 ,  195 ,  248 ,  32 ,  1 ,  24 ,  104 ,  64 ,  240 ,  2 ,  0 ,  24 ,  96 ,  0 ,  146 ,  0 ,  152 ,  129 ,  66 ,  11 ,  216 ,  24 ,  104 ,  32 ,  240 ,  2 ,  0 ,  24 ,  96 ,  0 ,  146 ,  0 ,  152 ,  129 ,  66 ,  239 ,  217 ,  0 ,  152 ,  64 ,  28 ,  0 ,  144 ,  248 ,  231 ,  0 ,  152 ,  64 ,  28 ,  0 ,  144 ,  236 ,  231 ,  8 ,  225 ,  15 ,  64 ,  252 ,  83 ,  2 ,  64 ,  64 ,  13 ,  3 ,  0 ,  78 ,  246 ,  136 ,  81 ,  206 ,  242 ,  0 ,  1 ,  8 ,  104 ,  79 ,  240 ,  240 ,  3 ,  64 ,  234 ,  3 ,  64 ,  8 ,  96 ,  9 ,  72 ,  133 ,  70 ,  9 ,  72 ,  133 ,  68 ,  111 ,  70 ,  7 ,  32 ,  135 ,  67 ,  189 ,  70 ,  7 ,  72 ,  111 ,  70 ,  7 ,  96 ,  255 ,  247 ,  67 ,  255 ,  0 ,  240 ,  79 ,  248 ,  1 ,  32 ,  0 ,  240 ,  48 ,  248 ,  254 ,  231 ,  192 ,  70 ,  0 ,  0 ,  0 ,  32 ,  0 ,  1 ,  0 ,  0 ,  16 ,  1 ,  0 ,  32 ,  112 ,  181 ,  15 ,  77 ,  6 ,  70 ,  115 ,  136 ,  0 ,  36 ,  22 ,  224 ,  160 ,  0 ,  0 ,  235 ,  196 ,  0 ,  48 ,  24 ,  65 ,  104 ,  194 ,  104 ,  128 ,  104 ,  74 ,  185 ,  42 ,  104 ,  82 ,  177 ,  10 ,  120 ,  85 ,  248 ,  34 ,  48 ,  74 ,  28 ,  1 ,  70 ,  16 ,  70 ,  152 ,  71 ,  1 ,  224 ,  255 ,  247 ,  206 ,  254 ,  115 ,  136 ,  100 ,  28 ,  164 ,  178 ,  163 ,  66 ,  230 ,  220 ,  112 ,  189 ,  128 ,  5 ,  0 ,  0 ,  0 ,  191 ,  112 ,  71 ,  8 ,  181 ,  255 ,  247 ,  251 ,  255 ,  254 ,  231 ,  56 ,  181 ,  5 ,  70 ,  9 ,  72 ,  0 ,  104 ,  128 ,  71 ,  9 ,  76 ,  32 ,  29 ,  0 ,  104 ,  16 ,  177 ,  1 ,  70 ,  40 ,  70 ,  136 ,  71 ,  32 ,  104 ,  0 ,  177 ,  128 ,  71 ,  5 ,  72 ,  0 ,  104 ,  128 ,  71 ,  255 ,  247 ,  232 ,  255 ,  56 ,  189 ,  192 ,  70 ,  0 ,  1 ,  0 ,  32 ,  8 ,  1 ,  0 ,  32 ,  4 ,  1 ,  0 ,  32 ,  4 ,  73 ,  177 ,  241 ,  255 ,  63 ,  7 ,  191 ,  0 ,  33 ,  8 ,  28 ,  8 ,  104 ,  9 ,  29 ,  255 ,  247 ,  98 ,  191 ,  255 ,  255 ,  255 ,  255 ,  3 ,  73 ,  8 ,  96 ,  112 ,  71 ,  2 ,  73 ,  9 ,  31 ,  8 ,  96 ,  112 ,  71 ,  112 ,  71 ,  4 ,  1 ,  0 ,  32 ,  208 ,  248 ,  3 ,  32 ,  195 ,  29 ,  8 ,  70 ,  25 ,  70 ,  255 ,  247 ,  135 ,  190 ,  255 ,  247 ,  121 ,  191 ,  112 ,  71 ,  1 ,  34 ,  255 ,  247 ,  25 ,  191 ,  254 ,  231 ,  254 ,  231 ,  0 ,  0 ,  0 ,  1 ,  75 ,  5 ,  0 ,  0 ,  75 ,  5 ,  1 ,  14 ,  0 ,  1 ,  0 ,  0 ,  0 ,  0 ,  101 ,  5 ,  0 ,  0 ,  81 ,  5 ,  0 ,  0 ,  112 ,  5 ,  0 ,  0 ,  0 ,  1 ,  0 ,  32};
00395     
00396     
00397     if(g_ui32StartAddress != 0xffffffff)
00398     {
00399         //
00400         // Send the run command but just send the packet, there will likely
00401         // be no boot loader to answer after this command completes.
00402         //
00403         g_pui8Buffer[0] = COMMAND_RUN;
00404         g_pui8Buffer[1] = (uint8_t)(g_ui32StartAddress>>24);
00405         g_pui8Buffer[2] = (uint8_t)(g_ui32StartAddress>>16);
00406         g_pui8Buffer[3] = (uint8_t)(g_ui32StartAddress>>8);
00407         g_pui8Buffer[4] = (uint8_t)g_ui32StartAddress;
00408         if(SendPacket(g_pui8Buffer, 5, 0) < 0)
00409         {
00410             pc.printf("Failed to Send Run command\n");
00411         }
00412         else
00413         {
00414             pc.printf("Running from address %08x\n",g_ui32StartAddress);
00415         }
00416         
00417     }
00418     
00419     else
00420     {
00421     
00422         g_pui8Buffer[0] = COMMAND_DOWNLOAD;
00423         g_pui8Buffer[1] = (uint8_t)(g_ui32DownloadAddress >> 24);
00424         g_pui8Buffer[2] = (uint8_t)(g_ui32DownloadAddress >> 16);
00425         g_pui8Buffer[3] = (uint8_t)(g_ui32DownloadAddress >> 8);
00426         g_pui8Buffer[4] = (uint8_t)g_ui32DownloadAddress;
00427         g_pui8Buffer[5] = (uint8_t)(ui32TransferLength>>24);
00428         g_pui8Buffer[6] = (uint8_t)(ui32TransferLength>>16);
00429         g_pui8Buffer[7] = (uint8_t)(ui32TransferLength>>8);
00430         g_pui8Buffer[8] = (uint8_t)ui32TransferLength;
00431     }
00432     
00433     
00434     ui8Command = COMMAND_PING;
00435     if(SendCommand(&ui8Command, 1) < 0)
00436     {
00437         pc.printf("Ping fail");
00438         return(-1);
00439     }
00440     
00441     if(SendCommand(g_pui8Buffer, 9) < 0)
00442     {
00443         pc.printf("Failed to Send Download Command\n");
00444         return(-1);
00445     }
00446     
00447     
00448 ui32Offset = 0;
00449 
00450     pc.printf("Remaining Bytes: ");
00451     do
00452     {
00453         uint8_t ui8BytesSent;
00454 
00455         g_pui8Buffer[0] = COMMAND_SEND_DATA;
00456 
00457         pc.printf("%08ld", ui32TransferLength);
00458 
00459         //
00460         // Send out 8 bytes at a time to throttle download rate and avoid
00461         // overruning the device since it is programming flash on the fly.
00462         //
00463         if(ui32TransferLength >= g_ui32DataSize)
00464         {
00465             memcpy(&g_pui8Buffer[1], &pui8FileBuffer[ui32Offset], g_ui32DataSize);
00466 
00467             ui32Offset += g_ui32DataSize;
00468             ui32TransferLength -= g_ui32DataSize;
00469             ui8BytesSent = g_ui32DataSize + 1;
00470         }
00471         else
00472         {
00473             memcpy(&g_pui8Buffer[1], &pui8FileBuffer[ui32Offset], ui32TransferLength);
00474             ui32Offset += ui32TransferLength;
00475             ui8BytesSent = ui32TransferLength + 1;
00476             ui32TransferLength = 0;
00477         }
00478         //
00479         // Send the Send Data command to the device.
00480         //
00481         if(SendCommand(g_pui8Buffer, ui8BytesSent) < 0)
00482         {
00483             pc.printf("Failed to Send Packet data\n");
00484             break;
00485         }
00486 
00487         pc.printf("\b\b\b\b\b\b\b\b");
00488     } while (ui32TransferLength);
00489     pc.printf("00000000\n");
00490     
00491     g_pui8Buffer[0] = COMMAND_RESET;
00492     SendPacket(g_pui8Buffer, 1, 0);
00493 
00494 }