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:
Sat May 30 20:05:44 2015 +0000
Revision:
9:91fb9341d36c
Parent:
8:771e674458bd
Receive command changed

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 9:91fb9341d36c 103 int i,stat;
Piasiv1206 5:0801179037cd 104 char temp[ui8Size];
Piasiv1206 5:0801179037cd 105
Piasiv1206 9:91fb9341d36c 106 stat = i2c.read(addr, temp, ui8Size,0);
Piasiv1206 5:0801179037cd 107 for(i=0;i<ui8Size;i++)
Piasiv1206 5:0801179037cd 108 {
Piasiv1206 5:0801179037cd 109
Piasiv1206 5:0801179037cd 110 temp[i] = pui8Data[i];
Piasiv1206 5:0801179037cd 111
Piasiv1206 5:0801179037cd 112 }
Piasiv1206 5:0801179037cd 113
Piasiv1206 9:91fb9341d36c 114 if(stat == 1)
Piasiv1206 1:c39e5581f901 115 {
Piasiv1206 1:c39e5581f901 116
Piasiv1206 7:00b148a7e11d 117 pc.printf("Recieve Error\n");
Piasiv1206 1:c39e5581f901 118 return (-1);
Piasiv1206 9:91fb9341d36c 119 }
Piasiv1206 9:91fb9341d36c 120
Piasiv1206 9:91fb9341d36c 121 return 0;
Piasiv1206 1:c39e5581f901 122 }
mbed_official 0:f76c26307f9a 123
Piasiv1206 1:c39e5581f901 124
Piasiv1206 1:c39e5581f901 125 //****************************************************************************
Piasiv1206 1:c39e5581f901 126 //
Piasiv1206 1:c39e5581f901 127 //! AckPacket() sends an Acknowledge a packet.
Piasiv1206 1:c39e5581f901 128 //!
Piasiv1206 1:c39e5581f901 129 //! This function acknowledges a packet has been received from the device.
Piasiv1206 1:c39e5581f901 130 //!
Piasiv1206 1:c39e5581f901 131 //! \return The function returns zero to indicated success while any non-zero
Piasiv1206 1:c39e5581f901 132 //! value indicates a failure.
Piasiv1206 1:c39e5581f901 133 //
Piasiv1206 1:c39e5581f901 134 //****************************************************************************
Piasiv1206 5:0801179037cd 135
Piasiv1206 5:0801179037cd 136
Piasiv1206 5:0801179037cd 137
Piasiv1206 5:0801179037cd 138
Piasiv1206 5:0801179037cd 139
Piasiv1206 1:c39e5581f901 140 int32_t
Piasiv1206 1:c39e5581f901 141 AckPacket(void)
Piasiv1206 1:c39e5581f901 142 {
Piasiv1206 1:c39e5581f901 143 uint8_t ui8Ack;
Piasiv1206 1:c39e5581f901 144
Piasiv1206 1:c39e5581f901 145 ui8Ack = COMMAND_ACK;
Piasiv1206 1:c39e5581f901 146 return(I2CSendData(&ui8Ack, 1));
Piasiv1206 1:c39e5581f901 147 }
Piasiv1206 1:c39e5581f901 148
Piasiv1206 8:771e674458bd 149
Piasiv1206 1:c39e5581f901 150 //
Piasiv1206 1:c39e5581f901 151 //! NakPacket() sends a No Acknowledge packet.
Piasiv1206 1:c39e5581f901 152 //!
Piasiv1206 1:c39e5581f901 153 //! This function sends a no acknowledge for a packet that has been
Piasiv1206 1:c39e5581f901 154 //! received unsuccessfully from the device.
Piasiv1206 1:c39e5581f901 155 //!
Piasiv1206 1:c39e5581f901 156 //! \return The function returns zero to indicated success while any non-zero
Piasiv1206 1:c39e5581f901 157 //! value indicates a failure.
Piasiv1206 1:c39e5581f901 158 //
Piasiv1206 8:771e674458bd 159
Piasiv1206 1:c39e5581f901 160 int32_t
Piasiv1206 1:c39e5581f901 161 NakPacket(void)
Piasiv1206 1:c39e5581f901 162 {
Piasiv1206 1:c39e5581f901 163 uint8_t ui8Nak;
Piasiv1206 1:c39e5581f901 164
Piasiv1206 1:c39e5581f901 165 ui8Nak = COMMAND_NAK;
Piasiv1206 1:c39e5581f901 166 return(I2CSendData(&ui8Nak, 1));
Piasiv1206 1:c39e5581f901 167 }
Piasiv1206 1:c39e5581f901 168
Piasiv1206 8:771e674458bd 169
Piasiv1206 1:c39e5581f901 170 //
Piasiv1206 1:c39e5581f901 171 //! GetPacket() receives a data packet.
Piasiv1206 1:c39e5581f901 172 //!
Piasiv1206 1:c39e5581f901 173 //! \param pui8Data is the location to store the data received from the device.
Piasiv1206 1:c39e5581f901 174 //! \param pui8Size is the number of bytes returned in the pui8Data buffer that
Piasiv1206 1:c39e5581f901 175 //! was provided.
Piasiv1206 1:c39e5581f901 176 //!
Piasiv1206 1:c39e5581f901 177 //! This function receives a packet of data from UART port.
Piasiv1206 1:c39e5581f901 178 //!
Piasiv1206 1:c39e5581f901 179 //! \returns The function returns zero to indicated success while any non-zero
Piasiv1206 1:c39e5581f901 180 //! value indicates a failure.
Piasiv1206 1:c39e5581f901 181 //
Piasiv1206 8:771e674458bd 182
Piasiv1206 1:c39e5581f901 183 int32_t
Piasiv1206 1:c39e5581f901 184 GetPacket(uint8_t *pui8Data, uint8_t *pui8Size)
Piasiv1206 1:c39e5581f901 185 {
Piasiv1206 1:c39e5581f901 186 uint8_t ui8CheckSum;
Piasiv1206 1:c39e5581f901 187 uint8_t ui8Size;
Piasiv1206 1:c39e5581f901 188
Piasiv1206 1:c39e5581f901 189 //
Piasiv1206 1:c39e5581f901 190 // Get the size and the checksum.
Piasiv1206 1:c39e5581f901 191 //
Piasiv1206 1:c39e5581f901 192 do
Piasiv1206 1:c39e5581f901 193 {
Piasiv1206 1:c39e5581f901 194 if(I2CReceiveData(&ui8Size, 1))
Piasiv1206 1:c39e5581f901 195 {
Piasiv1206 1:c39e5581f901 196 return(-1);
Piasiv1206 1:c39e5581f901 197 }
Piasiv1206 1:c39e5581f901 198 }
Piasiv1206 1:c39e5581f901 199 while(ui8Size == 0);
Piasiv1206 1:c39e5581f901 200
Piasiv1206 1:c39e5581f901 201 if(I2CReceiveData(&ui8CheckSum, 1))
Piasiv1206 1:c39e5581f901 202 {
Piasiv1206 1:c39e5581f901 203 return(-1);
Piasiv1206 1:c39e5581f901 204 }
Piasiv1206 1:c39e5581f901 205 *pui8Size = ui8Size - 2;
Piasiv1206 1:c39e5581f901 206
Piasiv1206 1:c39e5581f901 207 if(I2CReceiveData(pui8Data, *pui8Size))
Piasiv1206 1:c39e5581f901 208 {
Piasiv1206 1:c39e5581f901 209 *pui8Size = 0;
Piasiv1206 1:c39e5581f901 210 return(-1);
Piasiv1206 1:c39e5581f901 211 }
Piasiv1206 1:c39e5581f901 212
Piasiv1206 1:c39e5581f901 213 //
Piasiv1206 1:c39e5581f901 214 // Calculate the checksum from the data.
Piasiv1206 1:c39e5581f901 215 //
Piasiv1206 1:c39e5581f901 216 if(CheckSum(pui8Data, *pui8Size) != ui8CheckSum)
Piasiv1206 1:c39e5581f901 217 {
Piasiv1206 1:c39e5581f901 218 *pui8Size = 0;
Piasiv1206 1:c39e5581f901 219 return(NakPacket());
Piasiv1206 1:c39e5581f901 220 }
Piasiv1206 1:c39e5581f901 221
Piasiv1206 1:c39e5581f901 222 return(AckPacket());
Piasiv1206 1:c39e5581f901 223 }
Piasiv1206 1:c39e5581f901 224
Piasiv1206 8:771e674458bd 225
Piasiv1206 1:c39e5581f901 226 //
Piasiv1206 1:c39e5581f901 227 //! CheckSum() Calculates an 8 bit checksum
Piasiv1206 1:c39e5581f901 228 //!
Piasiv1206 1:c39e5581f901 229 //! \param pui8Data is a pointer to an array of 8 bit data of size ui8Size.
Piasiv1206 1:c39e5581f901 230 //! \param ui8Size is the size of the array that will run through the checksum
Piasiv1206 1:c39e5581f901 231 //! algorithm.
Piasiv1206 1:c39e5581f901 232 //!
Piasiv1206 1:c39e5581f901 233 //! This function simply calculates an 8 bit checksum on the data passed in.
Piasiv1206 1:c39e5581f901 234 //!
Piasiv1206 1:c39e5581f901 235 //! \return The function returns the calculated checksum.
Piasiv1206 1:c39e5581f901 236 //
Piasiv1206 8:771e674458bd 237
Piasiv1206 1:c39e5581f901 238 uint8_t
Piasiv1206 1:c39e5581f901 239 CheckSum(uint8_t *pui8Data, uint8_t ui8Size)
Piasiv1206 1:c39e5581f901 240 {
Piasiv1206 1:c39e5581f901 241 int32_t i;
Piasiv1206 1:c39e5581f901 242 uint8_t ui8CheckSum;
Piasiv1206 1:c39e5581f901 243
Piasiv1206 1:c39e5581f901 244 ui8CheckSum = 0;
Piasiv1206 1:c39e5581f901 245
Piasiv1206 1:c39e5581f901 246 for(i = 0; i < ui8Size; ++i)
Piasiv1206 1:c39e5581f901 247 {
Piasiv1206 1:c39e5581f901 248 ui8CheckSum += pui8Data[i];
Piasiv1206 1:c39e5581f901 249 }
Piasiv1206 1:c39e5581f901 250 return(ui8CheckSum);
Piasiv1206 1:c39e5581f901 251 }
Piasiv1206 1:c39e5581f901 252
Piasiv1206 8:771e674458bd 253
Piasiv1206 1:c39e5581f901 254 //
Piasiv1206 1:c39e5581f901 255 //! SendPacket() sends a data packet.
Piasiv1206 1:c39e5581f901 256 //!
Piasiv1206 1:c39e5581f901 257 //! \param pui8Data is the location of the data to be sent to the device.
Piasiv1206 1:c39e5581f901 258 //! \param ui8Size is the number of bytes to send from puData.
Piasiv1206 1:c39e5581f901 259 //! \param bAck is a boolean that is true if an ACK/NAK packet should be
Piasiv1206 1:c39e5581f901 260 //! received in response to this packet.
Piasiv1206 1:c39e5581f901 261 //!
Piasiv1206 1:c39e5581f901 262 //! This function sends a packet of data to the device.
Piasiv1206 1:c39e5581f901 263 //!
Piasiv1206 1:c39e5581f901 264 //! \returns The function returns zero to indicated success while any non-zero
Piasiv1206 1:c39e5581f901 265 //! value indicates a failure.
Piasiv1206 1:c39e5581f901 266 //
Piasiv1206 8:771e674458bd 267
Piasiv1206 1:c39e5581f901 268 int32_t
Piasiv1206 1:c39e5581f901 269 SendPacket(uint8_t *pui8Data, uint8_t ui8Size, bool bAck)
Piasiv1206 1:c39e5581f901 270 {
Piasiv1206 1:c39e5581f901 271 uint8_t ui8CheckSum;
Piasiv1206 1:c39e5581f901 272 uint8_t ui8Ack;
Piasiv1206 1:c39e5581f901 273
Piasiv1206 1:c39e5581f901 274 ui8CheckSum = CheckSum(pui8Data, ui8Size);
Piasiv1206 1:c39e5581f901 275
Piasiv1206 1:c39e5581f901 276 //
Piasiv1206 1:c39e5581f901 277 // Make sure that we add the bytes for the size and checksum to the total.
Piasiv1206 1:c39e5581f901 278 //
Piasiv1206 1:c39e5581f901 279 ui8Size += 2;
Piasiv1206 1:c39e5581f901 280
Piasiv1206 1:c39e5581f901 281 //
Piasiv1206 1:c39e5581f901 282 // Send the Size in bytes.
Piasiv1206 1:c39e5581f901 283 //
Piasiv1206 1:c39e5581f901 284 if(I2CSendData(&ui8Size, 1))
Piasiv1206 1:c39e5581f901 285 {
Piasiv1206 1:c39e5581f901 286 return(-1);
Piasiv1206 1:c39e5581f901 287 }
Piasiv1206 1:c39e5581f901 288
Piasiv1206 1:c39e5581f901 289 //
Piasiv1206 1:c39e5581f901 290 // Send the CheckSum
Piasiv1206 1:c39e5581f901 291 //
Piasiv1206 1:c39e5581f901 292 if(I2CSendData(&ui8CheckSum, 1))
Piasiv1206 1:c39e5581f901 293 {
Piasiv1206 1:c39e5581f901 294 return(-1);
Piasiv1206 1:c39e5581f901 295 }
Piasiv1206 1:c39e5581f901 296
Piasiv1206 1:c39e5581f901 297 //
Piasiv1206 1:c39e5581f901 298 // Now send the remaining bytes out.
Piasiv1206 1:c39e5581f901 299 //
Piasiv1206 1:c39e5581f901 300 ui8Size -= 2;
Piasiv1206 1:c39e5581f901 301
Piasiv1206 1:c39e5581f901 302 //
Piasiv1206 1:c39e5581f901 303 // Send the Data
Piasiv1206 1:c39e5581f901 304 //
Piasiv1206 1:c39e5581f901 305 if(I2CSendData(pui8Data, ui8Size))
Piasiv1206 1:c39e5581f901 306 {
Piasiv1206 1:c39e5581f901 307 return(-1);
Piasiv1206 1:c39e5581f901 308 }
Piasiv1206 1:c39e5581f901 309
Piasiv1206 1:c39e5581f901 310 //
Piasiv1206 1:c39e5581f901 311 // Return immediately if no ACK/NAK is expected.
Piasiv1206 1:c39e5581f901 312 //
Piasiv1206 1:c39e5581f901 313 if(!bAck)
Piasiv1206 1:c39e5581f901 314 {
Piasiv1206 1:c39e5581f901 315 return(0);
Piasiv1206 1:c39e5581f901 316 }
Piasiv1206 1:c39e5581f901 317
Piasiv1206 1:c39e5581f901 318 //
Piasiv1206 1:c39e5581f901 319 // Wait for the acknowledge from the device.
Piasiv1206 1:c39e5581f901 320 //
Piasiv1206 1:c39e5581f901 321 do
Piasiv1206 1:c39e5581f901 322 {
Piasiv1206 1:c39e5581f901 323 if(I2CReceiveData(&ui8Ack, 1))
Piasiv1206 1:c39e5581f901 324 {
Piasiv1206 1:c39e5581f901 325 return(-1);
Piasiv1206 1:c39e5581f901 326 }
Piasiv1206 1:c39e5581f901 327 }
Piasiv1206 1:c39e5581f901 328 while(ui8Ack == 0);
Piasiv1206 1:c39e5581f901 329
Piasiv1206 1:c39e5581f901 330 if(ui8Ack != COMMAND_ACK)
Piasiv1206 1:c39e5581f901 331 {
Piasiv1206 1:c39e5581f901 332 return(-1);
Piasiv1206 1:c39e5581f901 333 }
Piasiv1206 1:c39e5581f901 334 return(0);
Piasiv1206 1:c39e5581f901 335 }
Piasiv1206 1:c39e5581f901 336
Piasiv1206 1:c39e5581f901 337
Piasiv1206 1:c39e5581f901 338 int32_t
Piasiv1206 1:c39e5581f901 339 SendCommand(uint8_t *pui8Command, uint8_t ui8Size)
Piasiv1206 1:c39e5581f901 340 {
Piasiv1206 1:c39e5581f901 341 uint8_t ui8Status;
Piasiv1206 1:c39e5581f901 342
Piasiv1206 1:c39e5581f901 343 //
Piasiv1206 1:c39e5581f901 344 // Send the command itself.
Piasiv1206 1:c39e5581f901 345 //
Piasiv1206 1:c39e5581f901 346 if(SendPacket(pui8Command, ui8Size, 1) < 0)
Piasiv1206 1:c39e5581f901 347 {
Piasiv1206 1:c39e5581f901 348 return(-1);
Piasiv1206 1:c39e5581f901 349 }
Piasiv1206 1:c39e5581f901 350
Piasiv1206 1:c39e5581f901 351 //
Piasiv1206 1:c39e5581f901 352 // Send the get status command to tell the device to return status to
Piasiv1206 1:c39e5581f901 353 // the host.
Piasiv1206 1:c39e5581f901 354 //
Piasiv1206 1:c39e5581f901 355 ui8Status = COMMAND_GET_STATUS;
Piasiv1206 1:c39e5581f901 356 if(SendPacket(&ui8Status, 1, 1) < 0)
Piasiv1206 1:c39e5581f901 357 {
Piasiv1206 1:c39e5581f901 358 pc.printf("Failed to Get Status\n");
Piasiv1206 1:c39e5581f901 359 return(-1);
Piasiv1206 1:c39e5581f901 360 }
Piasiv1206 1:c39e5581f901 361
Piasiv1206 1:c39e5581f901 362 //
Piasiv1206 1:c39e5581f901 363 // Read back the status provided from the device.
Piasiv1206 1:c39e5581f901 364 //
Piasiv1206 1:c39e5581f901 365 ui8Size = sizeof(ui8Status);
Piasiv1206 1:c39e5581f901 366 if(GetPacket(&ui8Status, &ui8Size) < 0)
Piasiv1206 1:c39e5581f901 367 {
Piasiv1206 1:c39e5581f901 368 pc.printf("Failed to Get Packet\n");
Piasiv1206 1:c39e5581f901 369 return(-1);
Piasiv1206 1:c39e5581f901 370 }
Piasiv1206 1:c39e5581f901 371 if(ui8Status != COMMAND_RET_SUCCESS)
Piasiv1206 1:c39e5581f901 372 {
Piasiv1206 1:c39e5581f901 373 pc.printf("Failed to get download command Return Code: %04x\n",
Piasiv1206 1:c39e5581f901 374 ui8Status);
Piasiv1206 1:c39e5581f901 375 return(-1);
Piasiv1206 1:c39e5581f901 376 }
Piasiv1206 1:c39e5581f901 377 return(0);
Piasiv1206 1:c39e5581f901 378 }
Piasiv1206 1:c39e5581f901 379
mbed_official 0:f76c26307f9a 380
Piasiv1206 1:c39e5581f901 381
Piasiv1206 1:c39e5581f901 382 int main() {
Piasiv1206 1:c39e5581f901 383
Piasiv1206 1:c39e5581f901 384 g_ui32DownloadAddress = 0;
Piasiv1206 1:c39e5581f901 385 g_ui32StartAddress = 0xffffffff;
Piasiv1206 1:c39e5581f901 386 g_ui32DataSize = 8;
Piasiv1206 1:c39e5581f901 387 uint8_t ui8Command;
Piasiv1206 1:c39e5581f901 388
Piasiv1206 1:c39e5581f901 389
Piasiv1206 5:0801179037cd 390 uint32_t ui32TransferLength = 1424;
Piasiv1206 1:c39e5581f901 391 uint32_t ui32Offset;
Piasiv1206 5:0801179037cd 392 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 393
Piasiv1206 1:c39e5581f901 394
Piasiv1206 5:0801179037cd 395 if(g_ui32StartAddress != 0xffffffff)
Piasiv1206 5:0801179037cd 396 {
Piasiv1206 5:0801179037cd 397 //
Piasiv1206 5:0801179037cd 398 // Send the run command but just send the packet, there will likely
Piasiv1206 5:0801179037cd 399 // be no boot loader to answer after this command completes.
Piasiv1206 5:0801179037cd 400 //
Piasiv1206 5:0801179037cd 401 g_pui8Buffer[0] = COMMAND_RUN;
Piasiv1206 5:0801179037cd 402 g_pui8Buffer[1] = (uint8_t)(g_ui32StartAddress>>24);
Piasiv1206 5:0801179037cd 403 g_pui8Buffer[2] = (uint8_t)(g_ui32StartAddress>>16);
Piasiv1206 5:0801179037cd 404 g_pui8Buffer[3] = (uint8_t)(g_ui32StartAddress>>8);
Piasiv1206 5:0801179037cd 405 g_pui8Buffer[4] = (uint8_t)g_ui32StartAddress;
Piasiv1206 5:0801179037cd 406 if(SendPacket(g_pui8Buffer, 5, 0) < 0)
Piasiv1206 5:0801179037cd 407 {
Piasiv1206 6:4d4ffd9fe1a2 408 pc.printf("Failed to Send Run command\n");
Piasiv1206 5:0801179037cd 409 }
Piasiv1206 5:0801179037cd 410 else
Piasiv1206 5:0801179037cd 411 {
Piasiv1206 6:4d4ffd9fe1a2 412 pc.printf("Running from address %08x\n",g_ui32StartAddress);
Piasiv1206 5:0801179037cd 413 }
Piasiv1206 5:0801179037cd 414
Piasiv1206 5:0801179037cd 415 }
Piasiv1206 5:0801179037cd 416
Piasiv1206 5:0801179037cd 417 else
Piasiv1206 5:0801179037cd 418 {
Piasiv1206 5:0801179037cd 419
Piasiv1206 5:0801179037cd 420 g_pui8Buffer[0] = COMMAND_DOWNLOAD;
Piasiv1206 5:0801179037cd 421 g_pui8Buffer[1] = (uint8_t)(g_ui32DownloadAddress >> 24);
Piasiv1206 5:0801179037cd 422 g_pui8Buffer[2] = (uint8_t)(g_ui32DownloadAddress >> 16);
Piasiv1206 5:0801179037cd 423 g_pui8Buffer[3] = (uint8_t)(g_ui32DownloadAddress >> 8);
Piasiv1206 5:0801179037cd 424 g_pui8Buffer[4] = (uint8_t)g_ui32DownloadAddress;
Piasiv1206 5:0801179037cd 425 g_pui8Buffer[5] = (uint8_t)(ui32TransferLength>>24);
Piasiv1206 5:0801179037cd 426 g_pui8Buffer[6] = (uint8_t)(ui32TransferLength>>16);
Piasiv1206 5:0801179037cd 427 g_pui8Buffer[7] = (uint8_t)(ui32TransferLength>>8);
Piasiv1206 5:0801179037cd 428 g_pui8Buffer[8] = (uint8_t)ui32TransferLength;
Piasiv1206 5:0801179037cd 429 }
Piasiv1206 5:0801179037cd 430
Piasiv1206 1:c39e5581f901 431
Piasiv1206 1:c39e5581f901 432 ui8Command = COMMAND_PING;
Piasiv1206 1:c39e5581f901 433 if(SendCommand(&ui8Command, 1) < 0)
Piasiv1206 1:c39e5581f901 434 {
Piasiv1206 1:c39e5581f901 435 pc.printf("Ping fail");
Piasiv1206 1:c39e5581f901 436 return(-1);
Piasiv1206 1:c39e5581f901 437 }
Piasiv1206 1:c39e5581f901 438
Piasiv1206 1:c39e5581f901 439 if(SendCommand(g_pui8Buffer, 9) < 0)
Piasiv1206 1:c39e5581f901 440 {
Piasiv1206 6:4d4ffd9fe1a2 441 pc.printf("Failed to Send Download Command\n");
Piasiv1206 1:c39e5581f901 442 return(-1);
mbed_official 0:f76c26307f9a 443 }
Piasiv1206 1:c39e5581f901 444
Piasiv1206 1:c39e5581f901 445
Piasiv1206 1:c39e5581f901 446 ui32Offset = 0;
Piasiv1206 1:c39e5581f901 447
Piasiv1206 6:4d4ffd9fe1a2 448 pc.printf("Remaining Bytes: ");
Piasiv1206 1:c39e5581f901 449 do
Piasiv1206 1:c39e5581f901 450 {
Piasiv1206 1:c39e5581f901 451 uint8_t ui8BytesSent;
Piasiv1206 1:c39e5581f901 452
Piasiv1206 1:c39e5581f901 453 g_pui8Buffer[0] = COMMAND_SEND_DATA;
Piasiv1206 1:c39e5581f901 454
Piasiv1206 6:4d4ffd9fe1a2 455 pc.printf("%08ld", ui32TransferLength);
Piasiv1206 1:c39e5581f901 456
Piasiv1206 1:c39e5581f901 457 //
Piasiv1206 1:c39e5581f901 458 // Send out 8 bytes at a time to throttle download rate and avoid
Piasiv1206 1:c39e5581f901 459 // overruning the device since it is programming flash on the fly.
Piasiv1206 1:c39e5581f901 460 //
Piasiv1206 1:c39e5581f901 461 if(ui32TransferLength >= g_ui32DataSize)
Piasiv1206 1:c39e5581f901 462 {
Piasiv1206 1:c39e5581f901 463 memcpy(&g_pui8Buffer[1], &pui8FileBuffer[ui32Offset], g_ui32DataSize);
Piasiv1206 1:c39e5581f901 464
Piasiv1206 1:c39e5581f901 465 ui32Offset += g_ui32DataSize;
Piasiv1206 1:c39e5581f901 466 ui32TransferLength -= g_ui32DataSize;
Piasiv1206 1:c39e5581f901 467 ui8BytesSent = g_ui32DataSize + 1;
Piasiv1206 1:c39e5581f901 468 }
Piasiv1206 1:c39e5581f901 469 else
Piasiv1206 1:c39e5581f901 470 {
Piasiv1206 1:c39e5581f901 471 memcpy(&g_pui8Buffer[1], &pui8FileBuffer[ui32Offset], ui32TransferLength);
Piasiv1206 1:c39e5581f901 472 ui32Offset += ui32TransferLength;
Piasiv1206 1:c39e5581f901 473 ui8BytesSent = ui32TransferLength + 1;
Piasiv1206 1:c39e5581f901 474 ui32TransferLength = 0;
Piasiv1206 1:c39e5581f901 475 }
Piasiv1206 1:c39e5581f901 476 //
Piasiv1206 1:c39e5581f901 477 // Send the Send Data command to the device.
Piasiv1206 1:c39e5581f901 478 //
Piasiv1206 1:c39e5581f901 479 if(SendCommand(g_pui8Buffer, ui8BytesSent) < 0)
Piasiv1206 1:c39e5581f901 480 {
Piasiv1206 6:4d4ffd9fe1a2 481 pc.printf("Failed to Send Packet data\n");
Piasiv1206 1:c39e5581f901 482 break;
Piasiv1206 1:c39e5581f901 483 }
Piasiv1206 1:c39e5581f901 484
Piasiv1206 6:4d4ffd9fe1a2 485 pc.printf("\b\b\b\b\b\b\b\b");
Piasiv1206 1:c39e5581f901 486 } while (ui32TransferLength);
Piasiv1206 6:4d4ffd9fe1a2 487 pc.printf("00000000\n");
Piasiv1206 8:771e674458bd 488
Piasiv1206 8:771e674458bd 489 g_pui8Buffer[0] = COMMAND_RESET;
Piasiv1206 8:771e674458bd 490 SendPacket(g_pui8Buffer, 1, 0);
Piasiv1206 1:c39e5581f901 491
mbed_official 0:f76c26307f9a 492 }