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

Dependencies:   mbed

Fork of I2Cboot_betav1_02 by Siva ram

Committer:
Piasiv1206
Date:
Mon May 25 13:36:12 2015 +0000
Revision:
8:771e674458bd
Parent:
7:00b148a7e11d
Child:
9:91fb9341d36c
Added reset command

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:f76c26307f9a 1 #include "mbed.h"
Piasiv1206 1:c39e5581f901 2 #include <stdbool.h>
Piasiv1206 1:c39e5581f901 3 #include <stdint.h>
Piasiv1206 5:0801179037cd 4 //#include <fcntl.h>
Piasiv1206 1:c39e5581f901 5 #include <stdlib.h>
Piasiv1206 1:c39e5581f901 6 #include <stdio.h>
Piasiv1206 5:0801179037cd 7 //#include <memory.h>
Piasiv1206 5:0801179037cd 8
Piasiv1206 5:0801179037cd 9
Piasiv1206 5:0801179037cd 10 /*
Piasiv1206 1:c39e5581f901 11
Piasiv1206 5:0801179037cd 12 Use pull up resistors
Piasiv1206 5:0801179037cd 13 Test Sample I2C with Tiva
Piasiv1206 5:0801179037cd 14 Learn about the I2C in freescale boards
Piasiv1206 5:0801179037cd 15 Check I2CReceiveData and I2CSendData compatibitily with Tiva
Piasiv1206 5:0801179037cd 16 Now Use ROM boot loader
Piasiv1206 5:0801179037cd 17 if it works get the flash code from CDMS people and try with that
Piasiv1206 5:0801179037cd 18
Piasiv1206 5:0801179037cd 19 */
Piasiv1206 1:c39e5581f901 20
Piasiv1206 1:c39e5581f901 21 // BootLoader
Piasiv1206 1:c39e5581f901 22
Piasiv1206 1:c39e5581f901 23 Serial pc(USBTX, USBRX); // tx, rx
mbed_official 0:f76c26307f9a 24
Piasiv1206 5:0801179037cd 25 I2C i2c(D15, D14);
mbed_official 0:f76c26307f9a 26
Piasiv1206 5:0801179037cd 27 const int addr = 0x42;
mbed_official 0:f76c26307f9a 28
Piasiv1206 1:c39e5581f901 29 #define COMMAND_PING 0x20
Piasiv1206 1:c39e5581f901 30 #define COMMAND_DOWNLOAD 0x21
Piasiv1206 1:c39e5581f901 31 #define COMMAND_RUN 0x22
Piasiv1206 1:c39e5581f901 32 #define COMMAND_GET_STATUS 0x23
Piasiv1206 1:c39e5581f901 33 #define COMMAND_SEND_DATA 0x24
Piasiv1206 1:c39e5581f901 34 #define COMMAND_RESET 0x25
Piasiv1206 1:c39e5581f901 35 #define COMMAND_RET_SUCCESS 0x40
Piasiv1206 1:c39e5581f901 36 #define COMMAND_RET_UNKNOWN_CMD 0x41
Piasiv1206 1:c39e5581f901 37 #define COMMAND_RET_INVALID_CMD 0x42
Piasiv1206 1:c39e5581f901 38 #define COMMAND_RET_INVALID_ADR 0x43
Piasiv1206 1:c39e5581f901 39 #define COMMAND_RET_FLASH_FAIL 0x44
Piasiv1206 1:c39e5581f901 40 #define COMMAND_RET_CRC_FAIL 0x45
Piasiv1206 1:c39e5581f901 41 #define COMMAND_ACK 0xcc
Piasiv1206 1:c39e5581f901 42 #define COMMAND_NAK 0x33
Piasiv1206 1:c39e5581f901 43
Piasiv1206 1:c39e5581f901 44 //*****************************************************************************
Piasiv1206 1:c39e5581f901 45 //
Piasiv1206 1:c39e5581f901 46 //! This variable will be set to the starting address of the binary will be
Piasiv1206 1:c39e5581f901 47 //! programmed into to device.
Piasiv1206 1:c39e5581f901 48 //
Piasiv1206 8:771e674458bd 49
Piasiv1206 1:c39e5581f901 50 static uint32_t g_ui32DownloadAddress;
Piasiv1206 1:c39e5581f901 51
Piasiv1206 8:771e674458bd 52
Piasiv1206 1:c39e5581f901 53 //
Piasiv1206 1:c39e5581f901 54 //! This variable will be set to the start execution address for the binary
Piasiv1206 1:c39e5581f901 55 //! that is downloaded.
Piasiv1206 1:c39e5581f901 56 //
Piasiv1206 8:771e674458bd 57
Piasiv1206 1:c39e5581f901 58 static uint32_t g_ui32StartAddress;
Piasiv1206 1:c39e5581f901 59
Piasiv1206 8:771e674458bd 60
Piasiv1206 1:c39e5581f901 61
Piasiv1206 1:c39e5581f901 62
Piasiv1206 1:c39e5581f901 63 uint32_t g_ui32DataSize;
Piasiv1206 1:c39e5581f901 64 uint8_t g_pui8Buffer[256];
Piasiv1206 1:c39e5581f901 65
Piasiv1206 5:0801179037cd 66 uint8_t CheckSum(uint8_t *pui8Data, uint8_t ui8Size);
Piasiv1206 1:c39e5581f901 67
Piasiv1206 1:c39e5581f901 68 //
Piasiv1206 1:c39e5581f901 69
Piasiv1206 1:c39e5581f901 70 int
Piasiv1206 1:c39e5581f901 71 I2CSendData(uint8_t const *pui8Data, uint8_t ui8Size)
Piasiv1206 1:c39e5581f901 72 {
Piasiv1206 1:c39e5581f901 73
Piasiv1206 5:0801179037cd 74 int i;
Piasiv1206 5:0801179037cd 75 char temp[ui8Size];
Piasiv1206 5:0801179037cd 76
Piasiv1206 5:0801179037cd 77
Piasiv1206 5:0801179037cd 78
Piasiv1206 5:0801179037cd 79 for(i=0;i<ui8Size;i++)
Piasiv1206 5:0801179037cd 80 {
Piasiv1206 5:0801179037cd 81
Piasiv1206 5:0801179037cd 82 temp[i] = pui8Data[i];
Piasiv1206 5:0801179037cd 83
Piasiv1206 5:0801179037cd 84 }
Piasiv1206 5:0801179037cd 85
Piasiv1206 5:0801179037cd 86
Piasiv1206 5:0801179037cd 87 if(i2c.write(addr, temp, ui8Size,0) == 0)
Piasiv1206 5:0801179037cd 88 {
Piasiv1206 5:0801179037cd 89 return (0);
Piasiv1206 5:0801179037cd 90 }
Piasiv1206 5:0801179037cd 91
Piasiv1206 7:00b148a7e11d 92
Piasiv1206 7:00b148a7e11d 93 pc.printf("Write Error\n");
Piasiv1206 5:0801179037cd 94 return (-1);
Piasiv1206 1:c39e5581f901 95
Piasiv1206 7:00b148a7e11d 96
Piasiv1206 1:c39e5581f901 97 }
Piasiv1206 1:c39e5581f901 98
Piasiv1206 1:c39e5581f901 99 int
Piasiv1206 1:c39e5581f901 100 I2CReceiveData(uint8_t *pui8Data, uint8_t ui8Size)
Piasiv1206 1:c39e5581f901 101 {
Piasiv1206 5:0801179037cd 102
Piasiv1206 5:0801179037cd 103 int i;
Piasiv1206 5:0801179037cd 104 char temp[ui8Size];
Piasiv1206 5:0801179037cd 105
Piasiv1206 5:0801179037cd 106
Piasiv1206 5:0801179037cd 107
Piasiv1206 5:0801179037cd 108 for(i=0;i<ui8Size;i++)
Piasiv1206 5:0801179037cd 109 {
Piasiv1206 5:0801179037cd 110
Piasiv1206 5:0801179037cd 111 temp[i] = pui8Data[i];
Piasiv1206 5:0801179037cd 112
Piasiv1206 5:0801179037cd 113 }
Piasiv1206 5:0801179037cd 114
Piasiv1206 5:0801179037cd 115
Piasiv1206 5:0801179037cd 116 if(i2c.read(addr, temp, ui8Size,0) == 0)
Piasiv1206 1:c39e5581f901 117 {
Piasiv1206 1:c39e5581f901 118 return (0);
Piasiv1206 1:c39e5581f901 119 }
Piasiv1206 1:c39e5581f901 120
Piasiv1206 7:00b148a7e11d 121 pc.printf("Recieve Error\n");
Piasiv1206 1:c39e5581f901 122 return (-1);
Piasiv1206 1:c39e5581f901 123
Piasiv1206 1:c39e5581f901 124 }
mbed_official 0:f76c26307f9a 125
Piasiv1206 1:c39e5581f901 126
Piasiv1206 1:c39e5581f901 127 //****************************************************************************
Piasiv1206 1:c39e5581f901 128 //
Piasiv1206 1:c39e5581f901 129 //! AckPacket() sends an Acknowledge a packet.
Piasiv1206 1:c39e5581f901 130 //!
Piasiv1206 1:c39e5581f901 131 //! This function acknowledges a packet has been received from the device.
Piasiv1206 1:c39e5581f901 132 //!
Piasiv1206 1:c39e5581f901 133 //! \return The function returns zero to indicated success while any non-zero
Piasiv1206 1:c39e5581f901 134 //! value indicates a failure.
Piasiv1206 1:c39e5581f901 135 //
Piasiv1206 1:c39e5581f901 136 //****************************************************************************
Piasiv1206 5:0801179037cd 137
Piasiv1206 5:0801179037cd 138
Piasiv1206 5:0801179037cd 139
Piasiv1206 5:0801179037cd 140
Piasiv1206 5:0801179037cd 141
Piasiv1206 1:c39e5581f901 142 int32_t
Piasiv1206 1:c39e5581f901 143 AckPacket(void)
Piasiv1206 1:c39e5581f901 144 {
Piasiv1206 1:c39e5581f901 145 uint8_t ui8Ack;
Piasiv1206 1:c39e5581f901 146
Piasiv1206 1:c39e5581f901 147 ui8Ack = COMMAND_ACK;
Piasiv1206 1:c39e5581f901 148 return(I2CSendData(&ui8Ack, 1));
Piasiv1206 1:c39e5581f901 149 }
Piasiv1206 1:c39e5581f901 150
Piasiv1206 8:771e674458bd 151
Piasiv1206 1:c39e5581f901 152 //
Piasiv1206 1:c39e5581f901 153 //! NakPacket() sends a No Acknowledge packet.
Piasiv1206 1:c39e5581f901 154 //!
Piasiv1206 1:c39e5581f901 155 //! This function sends a no acknowledge for a packet that has been
Piasiv1206 1:c39e5581f901 156 //! received unsuccessfully from the device.
Piasiv1206 1:c39e5581f901 157 //!
Piasiv1206 1:c39e5581f901 158 //! \return The function returns zero to indicated success while any non-zero
Piasiv1206 1:c39e5581f901 159 //! value indicates a failure.
Piasiv1206 1:c39e5581f901 160 //
Piasiv1206 8:771e674458bd 161
Piasiv1206 1:c39e5581f901 162 int32_t
Piasiv1206 1:c39e5581f901 163 NakPacket(void)
Piasiv1206 1:c39e5581f901 164 {
Piasiv1206 1:c39e5581f901 165 uint8_t ui8Nak;
Piasiv1206 1:c39e5581f901 166
Piasiv1206 1:c39e5581f901 167 ui8Nak = COMMAND_NAK;
Piasiv1206 1:c39e5581f901 168 return(I2CSendData(&ui8Nak, 1));
Piasiv1206 1:c39e5581f901 169 }
Piasiv1206 1:c39e5581f901 170
Piasiv1206 8:771e674458bd 171
Piasiv1206 1:c39e5581f901 172 //
Piasiv1206 1:c39e5581f901 173 //! GetPacket() receives a data packet.
Piasiv1206 1:c39e5581f901 174 //!
Piasiv1206 1:c39e5581f901 175 //! \param pui8Data is the location to store the data received from the device.
Piasiv1206 1:c39e5581f901 176 //! \param pui8Size is the number of bytes returned in the pui8Data buffer that
Piasiv1206 1:c39e5581f901 177 //! was provided.
Piasiv1206 1:c39e5581f901 178 //!
Piasiv1206 1:c39e5581f901 179 //! This function receives a packet of data from UART port.
Piasiv1206 1:c39e5581f901 180 //!
Piasiv1206 1:c39e5581f901 181 //! \returns The function returns zero to indicated success while any non-zero
Piasiv1206 1:c39e5581f901 182 //! value indicates a failure.
Piasiv1206 1:c39e5581f901 183 //
Piasiv1206 8:771e674458bd 184
Piasiv1206 1:c39e5581f901 185 int32_t
Piasiv1206 1:c39e5581f901 186 GetPacket(uint8_t *pui8Data, uint8_t *pui8Size)
Piasiv1206 1:c39e5581f901 187 {
Piasiv1206 1:c39e5581f901 188 uint8_t ui8CheckSum;
Piasiv1206 1:c39e5581f901 189 uint8_t ui8Size;
Piasiv1206 1:c39e5581f901 190
Piasiv1206 1:c39e5581f901 191 //
Piasiv1206 1:c39e5581f901 192 // Get the size and the checksum.
Piasiv1206 1:c39e5581f901 193 //
Piasiv1206 1:c39e5581f901 194 do
Piasiv1206 1:c39e5581f901 195 {
Piasiv1206 1:c39e5581f901 196 if(I2CReceiveData(&ui8Size, 1))
Piasiv1206 1:c39e5581f901 197 {
Piasiv1206 1:c39e5581f901 198 return(-1);
Piasiv1206 1:c39e5581f901 199 }
Piasiv1206 1:c39e5581f901 200 }
Piasiv1206 1:c39e5581f901 201 while(ui8Size == 0);
Piasiv1206 1:c39e5581f901 202
Piasiv1206 1:c39e5581f901 203 if(I2CReceiveData(&ui8CheckSum, 1))
Piasiv1206 1:c39e5581f901 204 {
Piasiv1206 1:c39e5581f901 205 return(-1);
Piasiv1206 1:c39e5581f901 206 }
Piasiv1206 1:c39e5581f901 207 *pui8Size = ui8Size - 2;
Piasiv1206 1:c39e5581f901 208
Piasiv1206 1:c39e5581f901 209 if(I2CReceiveData(pui8Data, *pui8Size))
Piasiv1206 1:c39e5581f901 210 {
Piasiv1206 1:c39e5581f901 211 *pui8Size = 0;
Piasiv1206 1:c39e5581f901 212 return(-1);
Piasiv1206 1:c39e5581f901 213 }
Piasiv1206 1:c39e5581f901 214
Piasiv1206 1:c39e5581f901 215 //
Piasiv1206 1:c39e5581f901 216 // Calculate the checksum from the data.
Piasiv1206 1:c39e5581f901 217 //
Piasiv1206 1:c39e5581f901 218 if(CheckSum(pui8Data, *pui8Size) != ui8CheckSum)
Piasiv1206 1:c39e5581f901 219 {
Piasiv1206 1:c39e5581f901 220 *pui8Size = 0;
Piasiv1206 1:c39e5581f901 221 return(NakPacket());
Piasiv1206 1:c39e5581f901 222 }
Piasiv1206 1:c39e5581f901 223
Piasiv1206 1:c39e5581f901 224 return(AckPacket());
Piasiv1206 1:c39e5581f901 225 }
Piasiv1206 1:c39e5581f901 226
Piasiv1206 8:771e674458bd 227
Piasiv1206 1:c39e5581f901 228 //
Piasiv1206 1:c39e5581f901 229 //! CheckSum() Calculates an 8 bit checksum
Piasiv1206 1:c39e5581f901 230 //!
Piasiv1206 1:c39e5581f901 231 //! \param pui8Data is a pointer to an array of 8 bit data of size ui8Size.
Piasiv1206 1:c39e5581f901 232 //! \param ui8Size is the size of the array that will run through the checksum
Piasiv1206 1:c39e5581f901 233 //! algorithm.
Piasiv1206 1:c39e5581f901 234 //!
Piasiv1206 1:c39e5581f901 235 //! This function simply calculates an 8 bit checksum on the data passed in.
Piasiv1206 1:c39e5581f901 236 //!
Piasiv1206 1:c39e5581f901 237 //! \return The function returns the calculated checksum.
Piasiv1206 1:c39e5581f901 238 //
Piasiv1206 8:771e674458bd 239
Piasiv1206 1:c39e5581f901 240 uint8_t
Piasiv1206 1:c39e5581f901 241 CheckSum(uint8_t *pui8Data, uint8_t ui8Size)
Piasiv1206 1:c39e5581f901 242 {
Piasiv1206 1:c39e5581f901 243 int32_t i;
Piasiv1206 1:c39e5581f901 244 uint8_t ui8CheckSum;
Piasiv1206 1:c39e5581f901 245
Piasiv1206 1:c39e5581f901 246 ui8CheckSum = 0;
Piasiv1206 1:c39e5581f901 247
Piasiv1206 1:c39e5581f901 248 for(i = 0; i < ui8Size; ++i)
Piasiv1206 1:c39e5581f901 249 {
Piasiv1206 1:c39e5581f901 250 ui8CheckSum += pui8Data[i];
Piasiv1206 1:c39e5581f901 251 }
Piasiv1206 1:c39e5581f901 252 return(ui8CheckSum);
Piasiv1206 1:c39e5581f901 253 }
Piasiv1206 1:c39e5581f901 254
Piasiv1206 8:771e674458bd 255
Piasiv1206 1:c39e5581f901 256 //
Piasiv1206 1:c39e5581f901 257 //! SendPacket() sends a data packet.
Piasiv1206 1:c39e5581f901 258 //!
Piasiv1206 1:c39e5581f901 259 //! \param pui8Data is the location of the data to be sent to the device.
Piasiv1206 1:c39e5581f901 260 //! \param ui8Size is the number of bytes to send from puData.
Piasiv1206 1:c39e5581f901 261 //! \param bAck is a boolean that is true if an ACK/NAK packet should be
Piasiv1206 1:c39e5581f901 262 //! received in response to this packet.
Piasiv1206 1:c39e5581f901 263 //!
Piasiv1206 1:c39e5581f901 264 //! This function sends a packet of data to the device.
Piasiv1206 1:c39e5581f901 265 //!
Piasiv1206 1:c39e5581f901 266 //! \returns The function returns zero to indicated success while any non-zero
Piasiv1206 1:c39e5581f901 267 //! value indicates a failure.
Piasiv1206 1:c39e5581f901 268 //
Piasiv1206 8:771e674458bd 269
Piasiv1206 1:c39e5581f901 270 int32_t
Piasiv1206 1:c39e5581f901 271 SendPacket(uint8_t *pui8Data, uint8_t ui8Size, bool bAck)
Piasiv1206 1:c39e5581f901 272 {
Piasiv1206 1:c39e5581f901 273 uint8_t ui8CheckSum;
Piasiv1206 1:c39e5581f901 274 uint8_t ui8Ack;
Piasiv1206 1:c39e5581f901 275
Piasiv1206 1:c39e5581f901 276 ui8CheckSum = CheckSum(pui8Data, ui8Size);
Piasiv1206 1:c39e5581f901 277
Piasiv1206 1:c39e5581f901 278 //
Piasiv1206 1:c39e5581f901 279 // Make sure that we add the bytes for the size and checksum to the total.
Piasiv1206 1:c39e5581f901 280 //
Piasiv1206 1:c39e5581f901 281 ui8Size += 2;
Piasiv1206 1:c39e5581f901 282
Piasiv1206 1:c39e5581f901 283 //
Piasiv1206 1:c39e5581f901 284 // Send the Size in bytes.
Piasiv1206 1:c39e5581f901 285 //
Piasiv1206 1:c39e5581f901 286 if(I2CSendData(&ui8Size, 1))
Piasiv1206 1:c39e5581f901 287 {
Piasiv1206 1:c39e5581f901 288 return(-1);
Piasiv1206 1:c39e5581f901 289 }
Piasiv1206 1:c39e5581f901 290
Piasiv1206 1:c39e5581f901 291 //
Piasiv1206 1:c39e5581f901 292 // Send the CheckSum
Piasiv1206 1:c39e5581f901 293 //
Piasiv1206 1:c39e5581f901 294 if(I2CSendData(&ui8CheckSum, 1))
Piasiv1206 1:c39e5581f901 295 {
Piasiv1206 1:c39e5581f901 296 return(-1);
Piasiv1206 1:c39e5581f901 297 }
Piasiv1206 1:c39e5581f901 298
Piasiv1206 1:c39e5581f901 299 //
Piasiv1206 1:c39e5581f901 300 // Now send the remaining bytes out.
Piasiv1206 1:c39e5581f901 301 //
Piasiv1206 1:c39e5581f901 302 ui8Size -= 2;
Piasiv1206 1:c39e5581f901 303
Piasiv1206 1:c39e5581f901 304 //
Piasiv1206 1:c39e5581f901 305 // Send the Data
Piasiv1206 1:c39e5581f901 306 //
Piasiv1206 1:c39e5581f901 307 if(I2CSendData(pui8Data, ui8Size))
Piasiv1206 1:c39e5581f901 308 {
Piasiv1206 1:c39e5581f901 309 return(-1);
Piasiv1206 1:c39e5581f901 310 }
Piasiv1206 1:c39e5581f901 311
Piasiv1206 1:c39e5581f901 312 //
Piasiv1206 1:c39e5581f901 313 // Return immediately if no ACK/NAK is expected.
Piasiv1206 1:c39e5581f901 314 //
Piasiv1206 1:c39e5581f901 315 if(!bAck)
Piasiv1206 1:c39e5581f901 316 {
Piasiv1206 1:c39e5581f901 317 return(0);
Piasiv1206 1:c39e5581f901 318 }
Piasiv1206 1:c39e5581f901 319
Piasiv1206 1:c39e5581f901 320 //
Piasiv1206 1:c39e5581f901 321 // Wait for the acknowledge from the device.
Piasiv1206 1:c39e5581f901 322 //
Piasiv1206 1:c39e5581f901 323 do
Piasiv1206 1:c39e5581f901 324 {
Piasiv1206 1:c39e5581f901 325 if(I2CReceiveData(&ui8Ack, 1))
Piasiv1206 1:c39e5581f901 326 {
Piasiv1206 1:c39e5581f901 327 return(-1);
Piasiv1206 1:c39e5581f901 328 }
Piasiv1206 1:c39e5581f901 329 }
Piasiv1206 1:c39e5581f901 330 while(ui8Ack == 0);
Piasiv1206 1:c39e5581f901 331
Piasiv1206 1:c39e5581f901 332 if(ui8Ack != COMMAND_ACK)
Piasiv1206 1:c39e5581f901 333 {
Piasiv1206 1:c39e5581f901 334 return(-1);
Piasiv1206 1:c39e5581f901 335 }
Piasiv1206 1:c39e5581f901 336 return(0);
Piasiv1206 1:c39e5581f901 337 }
Piasiv1206 1:c39e5581f901 338
Piasiv1206 1:c39e5581f901 339
Piasiv1206 1:c39e5581f901 340 int32_t
Piasiv1206 1:c39e5581f901 341 SendCommand(uint8_t *pui8Command, uint8_t ui8Size)
Piasiv1206 1:c39e5581f901 342 {
Piasiv1206 1:c39e5581f901 343 uint8_t ui8Status;
Piasiv1206 1:c39e5581f901 344
Piasiv1206 1:c39e5581f901 345 //
Piasiv1206 1:c39e5581f901 346 // Send the command itself.
Piasiv1206 1:c39e5581f901 347 //
Piasiv1206 1:c39e5581f901 348 if(SendPacket(pui8Command, ui8Size, 1) < 0)
Piasiv1206 1:c39e5581f901 349 {
Piasiv1206 1:c39e5581f901 350 return(-1);
Piasiv1206 1:c39e5581f901 351 }
Piasiv1206 1:c39e5581f901 352
Piasiv1206 1:c39e5581f901 353 //
Piasiv1206 1:c39e5581f901 354 // Send the get status command to tell the device to return status to
Piasiv1206 1:c39e5581f901 355 // the host.
Piasiv1206 1:c39e5581f901 356 //
Piasiv1206 1:c39e5581f901 357 ui8Status = COMMAND_GET_STATUS;
Piasiv1206 1:c39e5581f901 358 if(SendPacket(&ui8Status, 1, 1) < 0)
Piasiv1206 1:c39e5581f901 359 {
Piasiv1206 1:c39e5581f901 360 pc.printf("Failed to Get Status\n");
Piasiv1206 1:c39e5581f901 361 return(-1);
Piasiv1206 1:c39e5581f901 362 }
Piasiv1206 1:c39e5581f901 363
Piasiv1206 1:c39e5581f901 364 //
Piasiv1206 1:c39e5581f901 365 // Read back the status provided from the device.
Piasiv1206 1:c39e5581f901 366 //
Piasiv1206 1:c39e5581f901 367 ui8Size = sizeof(ui8Status);
Piasiv1206 1:c39e5581f901 368 if(GetPacket(&ui8Status, &ui8Size) < 0)
Piasiv1206 1:c39e5581f901 369 {
Piasiv1206 1:c39e5581f901 370 pc.printf("Failed to Get Packet\n");
Piasiv1206 1:c39e5581f901 371 return(-1);
Piasiv1206 1:c39e5581f901 372 }
Piasiv1206 1:c39e5581f901 373 if(ui8Status != COMMAND_RET_SUCCESS)
Piasiv1206 1:c39e5581f901 374 {
Piasiv1206 1:c39e5581f901 375 pc.printf("Failed to get download command Return Code: %04x\n",
Piasiv1206 1:c39e5581f901 376 ui8Status);
Piasiv1206 1:c39e5581f901 377 return(-1);
Piasiv1206 1:c39e5581f901 378 }
Piasiv1206 1:c39e5581f901 379 return(0);
Piasiv1206 1:c39e5581f901 380 }
Piasiv1206 1:c39e5581f901 381
mbed_official 0:f76c26307f9a 382
Piasiv1206 1:c39e5581f901 383
Piasiv1206 1:c39e5581f901 384 int main() {
Piasiv1206 1:c39e5581f901 385
Piasiv1206 1:c39e5581f901 386 g_ui32DownloadAddress = 0;
Piasiv1206 1:c39e5581f901 387 g_ui32StartAddress = 0xffffffff;
Piasiv1206 1:c39e5581f901 388 g_ui32DataSize = 8;
Piasiv1206 1:c39e5581f901 389 uint8_t ui8Command;
Piasiv1206 1:c39e5581f901 390
Piasiv1206 1:c39e5581f901 391
Piasiv1206 5:0801179037cd 392 uint32_t ui32TransferLength = 1424;
Piasiv1206 1:c39e5581f901 393 uint32_t ui32Offset;
Piasiv1206 5:0801179037cd 394 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 , 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 , 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 , 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 , 0 , 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 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 107 , 5 , 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 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 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 , 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 , 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 , 0 , 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};
Piasiv1206 5:0801179037cd 395
Piasiv1206 1:c39e5581f901 396
Piasiv1206 5:0801179037cd 397 if(g_ui32StartAddress != 0xffffffff)
Piasiv1206 5:0801179037cd 398 {
Piasiv1206 5:0801179037cd 399 //
Piasiv1206 5:0801179037cd 400 // Send the run command but just send the packet, there will likely
Piasiv1206 5:0801179037cd 401 // be no boot loader to answer after this command completes.
Piasiv1206 5:0801179037cd 402 //
Piasiv1206 5:0801179037cd 403 g_pui8Buffer[0] = COMMAND_RUN;
Piasiv1206 5:0801179037cd 404 g_pui8Buffer[1] = (uint8_t)(g_ui32StartAddress>>24);
Piasiv1206 5:0801179037cd 405 g_pui8Buffer[2] = (uint8_t)(g_ui32StartAddress>>16);
Piasiv1206 5:0801179037cd 406 g_pui8Buffer[3] = (uint8_t)(g_ui32StartAddress>>8);
Piasiv1206 5:0801179037cd 407 g_pui8Buffer[4] = (uint8_t)g_ui32StartAddress;
Piasiv1206 5:0801179037cd 408 if(SendPacket(g_pui8Buffer, 5, 0) < 0)
Piasiv1206 5:0801179037cd 409 {
Piasiv1206 6:4d4ffd9fe1a2 410 pc.printf("Failed to Send Run command\n");
Piasiv1206 5:0801179037cd 411 }
Piasiv1206 5:0801179037cd 412 else
Piasiv1206 5:0801179037cd 413 {
Piasiv1206 6:4d4ffd9fe1a2 414 pc.printf("Running from address %08x\n",g_ui32StartAddress);
Piasiv1206 5:0801179037cd 415 }
Piasiv1206 5:0801179037cd 416
Piasiv1206 5:0801179037cd 417 }
Piasiv1206 5:0801179037cd 418
Piasiv1206 5:0801179037cd 419 else
Piasiv1206 5:0801179037cd 420 {
Piasiv1206 5:0801179037cd 421
Piasiv1206 5:0801179037cd 422 g_pui8Buffer[0] = COMMAND_DOWNLOAD;
Piasiv1206 5:0801179037cd 423 g_pui8Buffer[1] = (uint8_t)(g_ui32DownloadAddress >> 24);
Piasiv1206 5:0801179037cd 424 g_pui8Buffer[2] = (uint8_t)(g_ui32DownloadAddress >> 16);
Piasiv1206 5:0801179037cd 425 g_pui8Buffer[3] = (uint8_t)(g_ui32DownloadAddress >> 8);
Piasiv1206 5:0801179037cd 426 g_pui8Buffer[4] = (uint8_t)g_ui32DownloadAddress;
Piasiv1206 5:0801179037cd 427 g_pui8Buffer[5] = (uint8_t)(ui32TransferLength>>24);
Piasiv1206 5:0801179037cd 428 g_pui8Buffer[6] = (uint8_t)(ui32TransferLength>>16);
Piasiv1206 5:0801179037cd 429 g_pui8Buffer[7] = (uint8_t)(ui32TransferLength>>8);
Piasiv1206 5:0801179037cd 430 g_pui8Buffer[8] = (uint8_t)ui32TransferLength;
Piasiv1206 5:0801179037cd 431 }
Piasiv1206 5:0801179037cd 432
Piasiv1206 1:c39e5581f901 433
Piasiv1206 1:c39e5581f901 434 ui8Command = COMMAND_PING;
Piasiv1206 1:c39e5581f901 435 if(SendCommand(&ui8Command, 1) < 0)
Piasiv1206 1:c39e5581f901 436 {
Piasiv1206 1:c39e5581f901 437 pc.printf("Ping fail");
Piasiv1206 1:c39e5581f901 438 return(-1);
Piasiv1206 1:c39e5581f901 439 }
Piasiv1206 1:c39e5581f901 440
Piasiv1206 1:c39e5581f901 441 if(SendCommand(g_pui8Buffer, 9) < 0)
Piasiv1206 1:c39e5581f901 442 {
Piasiv1206 6:4d4ffd9fe1a2 443 pc.printf("Failed to Send Download Command\n");
Piasiv1206 1:c39e5581f901 444 return(-1);
mbed_official 0:f76c26307f9a 445 }
Piasiv1206 1:c39e5581f901 446
Piasiv1206 1:c39e5581f901 447
Piasiv1206 1:c39e5581f901 448 ui32Offset = 0;
Piasiv1206 1:c39e5581f901 449
Piasiv1206 6:4d4ffd9fe1a2 450 pc.printf("Remaining Bytes: ");
Piasiv1206 1:c39e5581f901 451 do
Piasiv1206 1:c39e5581f901 452 {
Piasiv1206 1:c39e5581f901 453 uint8_t ui8BytesSent;
Piasiv1206 1:c39e5581f901 454
Piasiv1206 1:c39e5581f901 455 g_pui8Buffer[0] = COMMAND_SEND_DATA;
Piasiv1206 1:c39e5581f901 456
Piasiv1206 6:4d4ffd9fe1a2 457 pc.printf("%08ld", ui32TransferLength);
Piasiv1206 1:c39e5581f901 458
Piasiv1206 1:c39e5581f901 459 //
Piasiv1206 1:c39e5581f901 460 // Send out 8 bytes at a time to throttle download rate and avoid
Piasiv1206 1:c39e5581f901 461 // overruning the device since it is programming flash on the fly.
Piasiv1206 1:c39e5581f901 462 //
Piasiv1206 1:c39e5581f901 463 if(ui32TransferLength >= g_ui32DataSize)
Piasiv1206 1:c39e5581f901 464 {
Piasiv1206 1:c39e5581f901 465 memcpy(&g_pui8Buffer[1], &pui8FileBuffer[ui32Offset], g_ui32DataSize);
Piasiv1206 1:c39e5581f901 466
Piasiv1206 1:c39e5581f901 467 ui32Offset += g_ui32DataSize;
Piasiv1206 1:c39e5581f901 468 ui32TransferLength -= g_ui32DataSize;
Piasiv1206 1:c39e5581f901 469 ui8BytesSent = g_ui32DataSize + 1;
Piasiv1206 1:c39e5581f901 470 }
Piasiv1206 1:c39e5581f901 471 else
Piasiv1206 1:c39e5581f901 472 {
Piasiv1206 1:c39e5581f901 473 memcpy(&g_pui8Buffer[1], &pui8FileBuffer[ui32Offset], ui32TransferLength);
Piasiv1206 1:c39e5581f901 474 ui32Offset += ui32TransferLength;
Piasiv1206 1:c39e5581f901 475 ui8BytesSent = ui32TransferLength + 1;
Piasiv1206 1:c39e5581f901 476 ui32TransferLength = 0;
Piasiv1206 1:c39e5581f901 477 }
Piasiv1206 1:c39e5581f901 478 //
Piasiv1206 1:c39e5581f901 479 // Send the Send Data command to the device.
Piasiv1206 1:c39e5581f901 480 //
Piasiv1206 1:c39e5581f901 481 if(SendCommand(g_pui8Buffer, ui8BytesSent) < 0)
Piasiv1206 1:c39e5581f901 482 {
Piasiv1206 6:4d4ffd9fe1a2 483 pc.printf("Failed to Send Packet data\n");
Piasiv1206 1:c39e5581f901 484 break;
Piasiv1206 1:c39e5581f901 485 }
Piasiv1206 1:c39e5581f901 486
Piasiv1206 6:4d4ffd9fe1a2 487 pc.printf("\b\b\b\b\b\b\b\b");
Piasiv1206 1:c39e5581f901 488 } while (ui32TransferLength);
Piasiv1206 6:4d4ffd9fe1a2 489 pc.printf("00000000\n");
Piasiv1206 8:771e674458bd 490
Piasiv1206 8:771e674458bd 491 g_pui8Buffer[0] = COMMAND_RESET;
Piasiv1206 8:771e674458bd 492 SendPacket(g_pui8Buffer, 1, 0);
Piasiv1206 1:c39e5581f901 493
mbed_official 0:f76c26307f9a 494 }