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