amiro-blt / Host / Source / SerialBoot / xcpmaster.c @ 6886c3b6
History | View | Annotate | Download (31.187 KB)
1 | 69661903 | Thomas Schöpping | /************************************************************************************//** |
---|---|---|---|
2 | * \file xcpmaster.c
|
||
3 | * \brief XCP Master source file.
|
||
4 | * \ingroup SerialBoot
|
||
5 | * \internal
|
||
6 | *----------------------------------------------------------------------------------------
|
||
7 | * C O P Y R I G H T
|
||
8 | *----------------------------------------------------------------------------------------
|
||
9 | * Copyright (c) 2014 by Feaser http://www.feaser.com All rights reserved
|
||
10 | *
|
||
11 | *----------------------------------------------------------------------------------------
|
||
12 | * L I C E N S E
|
||
13 | *----------------------------------------------------------------------------------------
|
||
14 | * This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
||
15 | * modify it under the terms of the GNU General Public License as published by the Free
|
||
16 | * Software Foundation, either version 3 of the License, or (at your option) any later
|
||
17 | * version.
|
||
18 | *
|
||
19 | * OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||
20 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||
21 | * PURPOSE. See the GNU General Public License for more details.
|
||
22 | *
|
||
23 | * You should have received a copy of the GNU General Public License along with OpenBLT.
|
||
24 | * If not, see <http://www.gnu.org/licenses/>.
|
||
25 | *
|
||
26 | * A special exception to the GPL is included to allow you to distribute a combined work
|
||
27 | * that includes OpenBLT without being obliged to provide the source code for any
|
||
28 | * proprietary components. The exception text is included at the bottom of the license
|
||
29 | * file <license.html>.
|
||
30 | *
|
||
31 | * \endinternal
|
||
32 | ****************************************************************************************/
|
||
33 | |||
34 | /****************************************************************************************
|
||
35 | * Include files
|
||
36 | ****************************************************************************************/
|
||
37 | #include <assert.h> /* assertion module */ |
||
38 | #include <sb_types.h> /* C types */ |
||
39 | #include "xcpmaster.h" /* XCP master protocol module */ |
||
40 | |||
41 | |||
42 | /****************************************************************************************
|
||
43 | * Macro definitions
|
||
44 | ****************************************************************************************/
|
||
45 | /* XCP command codes as defined by the protocol currently supported by this module */
|
||
46 | #define XCP_MASTER_CMD_CONNECT (0xFF) |
||
47 | #define XCP_MASTER_CMD_DISCONNECT (0xFE) |
||
48 | #define XCP_MASTER_CMD_SET_MTA (0xF6) |
||
49 | #define XCP_MASTER_CMD_UPLOAD (0xF5) |
||
50 | #define XCP_MASTER_CMD_PROGRAM_START (0xD2) |
||
51 | #define XCP_MASTER_CMD_PROGRAM_CLEAR (0xD1) |
||
52 | #define XCP_MASTER_CMD_PROGRAM (0xD0) |
||
53 | #define XCP_MASTER_CMD_PROGRAM_RESET (0xCF) |
||
54 | #define XCP_MASTER_CMD_PROGRAM_MAX (0xC9) |
||
55 | |||
56 | /* XCP response packet IDs as defined by the protocol */
|
||
57 | #define XCP_MASTER_CMD_PID_RES (0xFF) /* positive response */ |
||
58 | |||
59 | /* timeout values */
|
||
60 | #define XCP_MASTER_CONNECT_TIMEOUT_MS (20) |
||
61 | #define XCP_MASTER_TIMEOUT_T1_MS (1000) /* standard command timeout */ |
||
62 | #define XCP_MASTER_TIMEOUT_T2_MS (2000) /* build checksum timeout */ |
||
63 | #define XCP_MASTER_TIMEOUT_T3_MS (2000) /* program start timeout */ |
||
64 | #define XCP_MASTER_TIMEOUT_T4_MS (10000) /* erase timeout */ |
||
65 | #define XCP_MASTER_TIMEOUT_T5_MS (1000) /* write and reset timeout */ |
||
66 | #define XCP_MASTER_TIMEOUT_T6_MS (1000) /* user specific connect connect timeout */ |
||
67 | #define XCP_MASTER_TIMEOUT_T7_MS (2000) /* wait timer timeout */ |
||
68 | |||
69 | /* XCP error codes */
|
||
70 | /** \brief Cmd processor synchronization error code. */
|
||
71 | #define XCP_ERR_CMD_SYNCH (0x00) |
||
72 | /** \brief Command was not executed error code. */
|
||
73 | #define XCP_ERR_CMD_BUSY (0x10) |
||
74 | /** \brief Unknown or unsupported command error code. */
|
||
75 | #define XCP_ERR_CMD_UNKNOWN (0x20) |
||
76 | /** \brief Parameter out of range error code. */
|
||
77 | #define XCP_ERR_OUT_OF_RANGE (0x22) |
||
78 | /** \brief Protected error code. Seed/key required. */
|
||
79 | #define XCP_ERR_ACCESS_LOCKED (0x25) |
||
80 | /** \brief Cal page not valid error code. */
|
||
81 | #define XCP_ERR_PAGE_NOT_VALID (0x26) |
||
82 | /** \brief Sequence error code. */
|
||
83 | #define XCP_ERR_SEQUENCE (0x29) |
||
84 | /** \brief Generic error code. */
|
||
85 | #define XCP_ERR_GENERIC (0x31) |
||
86 | |||
87 | /** \brief Number of retries to connect to the XCP slave. */
|
||
88 | #define XCP_MASTER_CONNECT_RETRIES (5) |
||
89 | |||
90 | |||
91 | /****************************************************************************************
|
||
92 | * Function prototypes
|
||
93 | ****************************************************************************************/
|
||
94 | static sb_uint8 XcpMasterSendCmdConnect(sb_uint8 flashingTargetID);
|
||
95 | static sb_uint8 XcpMasterSendCmdSetMta(sb_uint32 address);
|
||
96 | static sb_uint8 XcpMasterSendCmdUpload(sb_uint8 data[], sb_uint8 length);
|
||
97 | static sb_uint8 XcpMasterSendCmdProgramStart(void); |
||
98 | static sb_uint8 XcpMasterSendCmdDisconnect(void); |
||
99 | static sb_uint8 XcpMasterSendCmdProgramReset(void); |
||
100 | static sb_uint8 XcpMasterSendCmdProgram(sb_uint8 length, sb_uint8 data[]);
|
||
101 | static sb_uint8 XcpMasterSendCmdProgramMax(sb_uint8 data[]);
|
||
102 | static sb_uint8 XcpMasterSendCmdProgramClear(sb_uint32 length);
|
||
103 | static void XcpMasterSetOrderedLong(sb_uint32 value, sb_uint8 data[]); |
||
104 | static void XcpMasterPrintError(sb_uint8 error); |
||
105 | |||
106 | |||
107 | /****************************************************************************************
|
||
108 | * Local data declarations
|
||
109 | ****************************************************************************************/
|
||
110 | /** \brief Store the byte ordering of the XCP slave. */
|
||
111 | static sb_uint8 xcpSlaveIsIntel = SB_FALSE;
|
||
112 | |||
113 | /** \brief The max number of bytes in the command transmit object (master->slave). */
|
||
114 | static sb_uint8 xcpMaxCto;
|
||
115 | |||
116 | /** \brief The max number of bytes in the command transmit object (master->slave) during
|
||
117 | * a programming session.
|
||
118 | */
|
||
119 | static sb_uint8 xcpMaxProgCto = 0; |
||
120 | |||
121 | /** \brief The max number of bytes in the data transmit object (slave->master). */
|
||
122 | static sb_uint8 xcpMaxDto;
|
||
123 | |||
124 | /** \brief Internal data buffer for storing the data of the XCP response packet. */
|
||
125 | static tXcpTransportResponsePacket responsePacket;
|
||
126 | |||
127 | |||
128 | /************************************************************************************//** |
||
129 | ** \brief Initializes the XCP master protocol layer.
|
||
130 | ** \param device Serial communication device name. For example "COM4".
|
||
131 | ** \param baudrate Communication speed in bits/sec.
|
||
132 | ** \return SB_TRUE is successful, SB_FALSE otherwise.
|
||
133 | **
|
||
134 | ****************************************************************************************/
|
||
135 | sb_uint8 XcpMasterInit(sb_char *device, sb_uint32 baudrate, sb_uint8 comIsUart) |
||
136 | { |
||
137 | /* initialize the underlying transport layer that is used for the communication */
|
||
138 | return XcpTransportInit(device, baudrate, comIsUart);
|
||
139 | } /*** end of XcpMasterInit ***/
|
||
140 | |||
141 | |||
142 | /************************************************************************************//** |
||
143 | ** \brief Uninitializes the XCP master protocol layer.
|
||
144 | ** \return none.
|
||
145 | **
|
||
146 | ****************************************************************************************/
|
||
147 | void XcpMasterDeinit(void) |
||
148 | { |
||
149 | XcpTransportClose(); |
||
150 | } /*** end of XcpMasterDeinit ***/
|
||
151 | |||
152 | |||
153 | /************************************************************************************//** |
||
154 | ** \brief Connect to the XCP slave.
|
||
155 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
156 | **
|
||
157 | ****************************************************************************************/
|
||
158 | sb_uint8 XcpMasterConnect(sb_uint8 flashingTargetID) |
||
159 | { |
||
160 | sb_uint8 cnt; |
||
161 | sb_uint8 retries = 1;
|
||
162 | if (flashingTargetID <= 0) { |
||
163 | retries = XCP_MASTER_CONNECT_RETRIES; |
||
164 | } |
||
165 | |||
166 | /* try to connect with a finite amount of retries */
|
||
167 | for (cnt=0; cnt<retries; cnt++) |
||
168 | { |
||
169 | /* send the connect command */
|
||
170 | if (XcpMasterSendCmdConnect(flashingTargetID) == SB_TRUE)
|
||
171 | { |
||
172 | /* connected so no need to retry */
|
||
173 | return SB_TRUE;
|
||
174 | } |
||
175 | } |
||
176 | /* still here so could not connect */
|
||
177 | return SB_FALSE;
|
||
178 | } /*** end of XcpMasterConnect ***/
|
||
179 | |||
180 | |||
181 | /************************************************************************************//** |
||
182 | ** \brief Disconnect the slave.
|
||
183 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
184 | **
|
||
185 | ****************************************************************************************/
|
||
186 | sb_uint8 XcpMasterDisconnect(void)
|
||
187 | { |
||
188 | /* send reset command instead of the disconnect. this causes the user program on the
|
||
189 | * slave to automatically start again if present.
|
||
190 | */
|
||
191 | return XcpMasterSendCmdDisconnect();
|
||
192 | } /*** end of XcpMasterDisconnect ***/
|
||
193 | |||
194 | |||
195 | /************************************************************************************//** |
||
196 | ** \brief Resets the user program.
|
||
197 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
198 | |||
199 | **
|
||
200 | ****************************************************************************************/
|
||
201 | sb_uint8 XcpMasterProgramReset(void)
|
||
202 | { |
||
203 | /* send reset command instead of the disconnect. this causes the user program on the
|
||
204 | * slave to automatically start again if present.
|
||
205 | |||
206 | */
|
||
207 | return XcpMasterSendCmdProgramReset();
|
||
208 | } /*** end of XcpMasterDisconnect ***/
|
||
209 | |||
210 | /************************************************************************************//** |
||
211 | ** \brief Puts a connected slave in programming session.
|
||
212 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
213 | **
|
||
214 | ****************************************************************************************/
|
||
215 | sb_uint8 XcpMasterStartProgrammingSession(void)
|
||
216 | { |
||
217 | /* place the slave in programming mode */
|
||
218 | return XcpMasterSendCmdProgramStart();
|
||
219 | } /*** end of XcpMasterStartProgrammingSession ***/
|
||
220 | |||
221 | |||
222 | /************************************************************************************//** |
||
223 | ** \brief Stops the programming session by sending a program command with size 0 and
|
||
224 | ** then resetting the slave.
|
||
225 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
226 | **
|
||
227 | ****************************************************************************************/
|
||
228 | sb_uint8 XcpMasterStopProgrammingSession(void)
|
||
229 | { |
||
230 | /* stop programming by sending the program command with size 0 */
|
||
231 | if (XcpMasterSendCmdProgram(0, SB_NULL) == SB_FALSE) |
||
232 | { |
||
233 | return SB_FALSE;
|
||
234 | } |
||
235 | /* request a reset of the slave */
|
||
236 | // return XcpMasterSendCmdProgramReset();
|
||
237 | return SB_TRUE;
|
||
238 | } /*** end of XcpMasterStopProgrammingSession ***/
|
||
239 | |||
240 | |||
241 | /************************************************************************************//** |
||
242 | ** \brief Erases non volatile memory on the slave.
|
||
243 | ** \param addr Base memory address for the erase operation.
|
||
244 | ** \param len Number of bytes to erase.
|
||
245 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
246 | **
|
||
247 | ****************************************************************************************/
|
||
248 | sb_uint8 XcpMasterClearMemory(sb_uint32 addr, sb_uint32 len) |
||
249 | { |
||
250 | /* first set the MTA pointer */
|
||
251 | if (XcpMasterSendCmdSetMta(addr) == SB_FALSE)
|
||
252 | { |
||
253 | return SB_FALSE;
|
||
254 | } |
||
255 | /* now perform the erase operation */
|
||
256 | return XcpMasterSendCmdProgramClear(len);
|
||
257 | } /*** end of XcpMasterClearMemory ***/
|
||
258 | |||
259 | |||
260 | /************************************************************************************//** |
||
261 | ** \brief Reads data from the slave's memory.
|
||
262 | ** \param addr Base memory address for the read operation
|
||
263 | ** \param len Number of bytes to read.
|
||
264 | ** \param data Destination buffer for storing the read data bytes.
|
||
265 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
266 | **
|
||
267 | ****************************************************************************************/
|
||
268 | sb_uint8 XcpMasterReadData(sb_uint32 addr, sb_uint32 len, sb_uint8 data[]) |
||
269 | { |
||
270 | sb_uint8 currentReadCnt; |
||
271 | sb_uint32 bufferOffset = 0;
|
||
272 | |||
273 | /* first set the MTA pointer */
|
||
274 | if (XcpMasterSendCmdSetMta(addr) == SB_FALSE)
|
||
275 | { |
||
276 | return SB_FALSE;
|
||
277 | } |
||
278 | /* perform segmented upload of the data */
|
||
279 | while (len > 0) |
||
280 | { |
||
281 | /* set the current read length to make optimal use of the available packet data. */
|
||
282 | currentReadCnt = len % (xcpMaxDto - 1);
|
||
283 | if (currentReadCnt == 0) |
||
284 | { |
||
285 | currentReadCnt = (xcpMaxDto - 1);
|
||
286 | } |
||
287 | /* upload some data */
|
||
288 | if (XcpMasterSendCmdUpload(&data[bufferOffset], currentReadCnt) == SB_FALSE)
|
||
289 | { |
||
290 | return SB_FALSE;
|
||
291 | } |
||
292 | /* update loop variables */
|
||
293 | len -= currentReadCnt; |
||
294 | bufferOffset += currentReadCnt; |
||
295 | } |
||
296 | /* still here so all data successfully read from the slave */
|
||
297 | return SB_TRUE;
|
||
298 | } /*** end of XcpMasterReadData ***/
|
||
299 | |||
300 | |||
301 | /************************************************************************************//** |
||
302 | ** \brief Programs data to the slave's non volatile memory. Note that it must be
|
||
303 | ** erased first.
|
||
304 | ** \param addr Base memory address for the program operation
|
||
305 | ** \param len Number of bytes to program.
|
||
306 | ** \param data Source buffer with the to be programmed bytes.
|
||
307 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
308 | **
|
||
309 | ****************************************************************************************/
|
||
310 | sb_uint8 XcpMasterProgramData(sb_uint32 addr, sb_uint32 len, sb_uint8 data[]) |
||
311 | { |
||
312 | sb_uint8 currentWriteCnt; |
||
313 | sb_uint32 bufferOffset = 0;
|
||
314 | |||
315 | /* first set the MTA pointer */
|
||
316 | if (XcpMasterSendCmdSetMta(addr) == SB_FALSE)
|
||
317 | { |
||
318 | return SB_FALSE;
|
||
319 | } |
||
320 | /* perform segmented programming of the data */
|
||
321 | while (len > 0) |
||
322 | { |
||
323 | /* set the current read length to make optimal use of the available packet data. */
|
||
324 | currentWriteCnt = len % (xcpMaxProgCto - 1);
|
||
325 | if (currentWriteCnt == 0) |
||
326 | { |
||
327 | currentWriteCnt = (xcpMaxProgCto - 1);
|
||
328 | } |
||
329 | /* prepare the packed data for the program command */
|
||
330 | if (currentWriteCnt < (xcpMaxProgCto - 1)) |
||
331 | { |
||
332 | /* program data */
|
||
333 | if (XcpMasterSendCmdProgram(currentWriteCnt, &data[bufferOffset]) == SB_FALSE)
|
||
334 | { |
||
335 | return SB_FALSE;
|
||
336 | } |
||
337 | } |
||
338 | else
|
||
339 | { |
||
340 | /* program max data */
|
||
341 | if (XcpMasterSendCmdProgramMax(&data[bufferOffset]) == SB_FALSE)
|
||
342 | { |
||
343 | return SB_FALSE;
|
||
344 | } |
||
345 | } |
||
346 | /* update loop variables */
|
||
347 | len -= currentWriteCnt; |
||
348 | bufferOffset += currentWriteCnt; |
||
349 | } |
||
350 | /* still here so all data successfully programmed */
|
||
351 | return SB_TRUE;
|
||
352 | } /*** end of XcpMasterProgramData ***/
|
||
353 | |||
354 | |||
355 | /************************************************************************************//** |
||
356 | ** \brief Sends the XCP Connect command.
|
||
357 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
358 | **
|
||
359 | ****************************************************************************************/
|
||
360 | static sb_uint8 XcpMasterSendCmdConnect(sb_uint8 flashingTargetID)
|
||
361 | { |
||
362 | sb_uint8 packetData[2];
|
||
363 | tXcpTransportResponsePacket *responsePacketPtr; |
||
364 | |||
365 | /* prepare the command packet */
|
||
366 | packetData[0] = XCP_MASTER_CMD_CONNECT;
|
||
367 | packetData[1] = flashingTargetID; /* normal mode */ |
||
368 | |||
369 | /* send the packet */
|
||
370 | if (XcpTransportSendPacket(packetData, 2, XCP_MASTER_CONNECT_TIMEOUT_MS) == SB_FALSE) |
||
371 | { |
||
372 | /* cound not set packet or receive response within the specified timeout */
|
||
373 | return SB_FALSE;
|
||
374 | } |
||
375 | /* still here so a response was received */
|
||
376 | responsePacketPtr = XcpTransportReadResponsePacket(); |
||
377 | |||
378 | /* check if the reponse was valid */
|
||
379 | if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) ) |
||
380 | { |
||
381 | /* not a valid or positive response */
|
||
382 | return SB_FALSE;
|
||
383 | } |
||
384 | |||
385 | /* process response data */
|
||
386 | if ((responsePacketPtr->data[2] & 0x01) == 0) |
||
387 | { |
||
388 | /* store slave's byte ordering information */
|
||
389 | xcpSlaveIsIntel = SB_TRUE; |
||
390 | } |
||
391 | /* store max number of bytes the slave allows for master->slave packets. */
|
||
392 | xcpMaxCto = responsePacketPtr->data[3];
|
||
393 | xcpMaxProgCto = xcpMaxCto; |
||
394 | /* store max number of bytes the slave allows for slave->master packets. */
|
||
395 | if (xcpSlaveIsIntel == SB_TRUE)
|
||
396 | { |
||
397 | xcpMaxDto = responsePacketPtr->data[4] + (responsePacketPtr->data[5] << 8); |
||
398 | } |
||
399 | else
|
||
400 | { |
||
401 | xcpMaxDto = responsePacketPtr->data[5] + (responsePacketPtr->data[4] << 8); |
||
402 | } |
||
403 | |||
404 | /* double check size configuration of the master */
|
||
405 | assert(XCP_MASTER_TX_MAX_DATA >= xcpMaxCto); |
||
406 | assert(XCP_MASTER_RX_MAX_DATA >= xcpMaxDto); |
||
407 | |||
408 | /* still here so all went well */
|
||
409 | return SB_TRUE;
|
||
410 | } /*** end of XcpMasterSendCmdConnect ***/
|
||
411 | |||
412 | |||
413 | /************************************************************************************//** |
||
414 | ** \brief Sends the XCP Set MTA command.
|
||
415 | ** \param address New MTA address for the slave.
|
||
416 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
417 | **
|
||
418 | ****************************************************************************************/
|
||
419 | static sb_uint8 XcpMasterSendCmdSetMta(sb_uint32 address)
|
||
420 | { |
||
421 | sb_uint8 packetData[8];
|
||
422 | tXcpTransportResponsePacket *responsePacketPtr; |
||
423 | |||
424 | /* prepare the command packet */
|
||
425 | packetData[0] = XCP_MASTER_CMD_SET_MTA;
|
||
426 | packetData[1] = 0; /* reserved */ |
||
427 | packetData[2] = 0; /* reserved */ |
||
428 | packetData[3] = 0; /* address extension not supported */ |
||
429 | |||
430 | /* set the address taking into account byte ordering */
|
||
431 | XcpMasterSetOrderedLong(address, &packetData[4]);
|
||
432 | |||
433 | /* send the packet */
|
||
434 | if (XcpTransportSendPacket(packetData, 8, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T1_MS |
||
435 | { |
||
436 | /* cound not set packet or receive response within the specified timeout */
|
||
437 | printf("\nno response (set MTA)\n");
|
||
438 | return SB_FALSE;
|
||
439 | } |
||
440 | /* still here so a response was received */
|
||
441 | responsePacketPtr = XcpTransportReadResponsePacket(); |
||
442 | |||
443 | /* check if the reponse was valid */
|
||
444 | if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) ) |
||
445 | { |
||
446 | /* not a valid or positive response */
|
||
447 | if (responsePacketPtr->len == 0) { |
||
448 | printf("\nmessage length = 0");
|
||
449 | } else {
|
||
450 | XcpMasterPrintError(responsePacketPtr->data[1]);
|
||
451 | } |
||
452 | printf(" (set MTA)\n");
|
||
453 | return SB_FALSE;
|
||
454 | } |
||
455 | |||
456 | /* still here so all went well */
|
||
457 | return SB_TRUE;
|
||
458 | } /*** end of XcpMasterSendCmdSetMta ***/
|
||
459 | |||
460 | |||
461 | /************************************************************************************//** |
||
462 | ** \brief Sends the XCP UPLOAD command.
|
||
463 | ** \param data Destination data buffer.
|
||
464 | ** \param length Number of bytes to upload.
|
||
465 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
466 | **
|
||
467 | ****************************************************************************************/
|
||
468 | static sb_uint8 XcpMasterSendCmdUpload(sb_uint8 data[], sb_uint8 length)
|
||
469 | { |
||
470 | sb_uint8 packetData[2];
|
||
471 | tXcpTransportResponsePacket *responsePacketPtr; |
||
472 | sb_uint8 data_index; |
||
473 | |||
474 | /* cannot request more data then the max rx data - 1 */
|
||
475 | assert(length < XCP_MASTER_RX_MAX_DATA); |
||
476 | |||
477 | /* prepare the command packet */
|
||
478 | packetData[0] = XCP_MASTER_CMD_UPLOAD;
|
||
479 | packetData[1] = length;
|
||
480 | |||
481 | /* send the packet */
|
||
482 | if (XcpTransportSendPacket(packetData, 2, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T1_MS |
||
483 | { |
||
484 | /* cound not set packet or receive response within the specified timeout */
|
||
485 | printf("\nno response (upload)\n");
|
||
486 | return SB_FALSE;
|
||
487 | } |
||
488 | /* still here so a response was received */
|
||
489 | responsePacketPtr = XcpTransportReadResponsePacket(); |
||
490 | |||
491 | /* check if the reponse was valid */
|
||
492 | if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) ) |
||
493 | { |
||
494 | /* not a valid or positive response */
|
||
495 | if (responsePacketPtr->len == 0) { |
||
496 | printf("\nmessage length = 0");
|
||
497 | } else {
|
||
498 | XcpMasterPrintError(responsePacketPtr->data[1]);
|
||
499 | } |
||
500 | printf(" (upload)\n");
|
||
501 | return SB_FALSE;
|
||
502 | } |
||
503 | |||
504 | /* now store the uploaded data */
|
||
505 | for (data_index=0; data_index<length; data_index++) |
||
506 | { |
||
507 | data[data_index] = responsePacketPtr->data[data_index+1];
|
||
508 | } |
||
509 | |||
510 | /* still here so all went well */
|
||
511 | return SB_TRUE;
|
||
512 | } /*** end of XcpMasterSendCmdUpload ***/
|
||
513 | |||
514 | |||
515 | /************************************************************************************//** |
||
516 | ** \brief Sends the XCP PROGRAM START command.
|
||
517 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
518 | **
|
||
519 | ****************************************************************************************/
|
||
520 | static sb_uint8 XcpMasterSendCmdProgramStart(void) |
||
521 | { |
||
522 | sb_uint8 packetData[1];
|
||
523 | tXcpTransportResponsePacket *responsePacketPtr; |
||
524 | |||
525 | /* prepare the command packet */
|
||
526 | packetData[0] = XCP_MASTER_CMD_PROGRAM_START;
|
||
527 | |||
528 | /* send the packet */
|
||
529 | if (XcpTransportSendPacket(packetData, 1, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T3_MS |
||
530 | { |
||
531 | /* cound not set packet or receive response within the specified timeout */
|
||
532 | printf("\nno response (program start)\n");
|
||
533 | return SB_FALSE;
|
||
534 | } |
||
535 | /* still here so a response was received */
|
||
536 | responsePacketPtr = XcpTransportReadResponsePacket(); |
||
537 | |||
538 | /* check if the reponse was valid */
|
||
539 | if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) ) |
||
540 | { |
||
541 | /* not a valid or positive response */
|
||
542 | if (responsePacketPtr->len == 0) { |
||
543 | printf("\nmessage length = 0");
|
||
544 | } else {
|
||
545 | XcpMasterPrintError(responsePacketPtr->data[1]);
|
||
546 | } |
||
547 | printf(" (program start)\n");
|
||
548 | return SB_FALSE;
|
||
549 | } |
||
550 | |||
551 | /* store max number of bytes the slave allows for master->slave packets during the
|
||
552 | * programming session
|
||
553 | */
|
||
554 | xcpMaxProgCto = responsePacketPtr->data[3];
|
||
555 | |||
556 | /* still here so all went well */
|
||
557 | return SB_TRUE;
|
||
558 | } /*** end of XcpMasterSendCmdProgramStart ***/
|
||
559 | |||
560 | |||
561 | /************************************************************************************//** |
||
562 | ** \brief Sends the XCP PROGRAM DISCONNECT command.
|
||
563 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
564 | **
|
||
565 | ****************************************************************************************/
|
||
566 | static sb_uint8 XcpMasterSendCmdDisconnect(void) |
||
567 | { |
||
568 | sb_uint8 packetData[1];
|
||
569 | tXcpTransportResponsePacket *responsePacketPtr; |
||
570 | |||
571 | /* prepare the command packet */
|
||
572 | packetData[0] = XCP_MASTER_CMD_DISCONNECT;
|
||
573 | |||
574 | /* send the packet */
|
||
575 | if (XcpTransportSendPacket(packetData, 1, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) |
||
576 | { |
||
577 | /* cound not set packet or receive response within the specified timeout */
|
||
578 | return SB_FALSE;
|
||
579 | } |
||
580 | /* still here so a response was received */
|
||
581 | responsePacketPtr = XcpTransportReadResponsePacket(); |
||
582 | |||
583 | /* check if the reponse was valid */
|
||
584 | if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) ) |
||
585 | { |
||
586 | /* not a valid or positive response */
|
||
587 | return SB_FALSE;
|
||
588 | } |
||
589 | |||
590 | /* still here so all went well */
|
||
591 | return SB_TRUE;
|
||
592 | } /*** end of XcpMasterSendCmdProgramStart ***/
|
||
593 | |||
594 | |||
595 | /************************************************************************************//** |
||
596 | ** \brief Sends the XCP PROGRAM RESET command. Note that this command is a bit
|
||
597 | ** different as in it does not require a response.
|
||
598 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
599 | **
|
||
600 | ****************************************************************************************/
|
||
601 | static sb_uint8 XcpMasterSendCmdProgramReset(void) |
||
602 | { |
||
603 | sb_uint8 packetData[1];
|
||
604 | tXcpTransportResponsePacket *responsePacketPtr; |
||
605 | |||
606 | /* prepare the command packet */
|
||
607 | packetData[0] = XCP_MASTER_CMD_PROGRAM_RESET;
|
||
608 | |||
609 | /* send the packet, assume the sending itself is ok and check if a response was
|
||
610 | * received.
|
||
611 | */
|
||
612 | if (XcpTransportSendPacket(packetData, 1, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T5_MS |
||
613 | { |
||
614 | /* probably no response received within the specified timeout, but that is allowed
|
||
615 | * for the reset command.
|
||
616 | */
|
||
617 | // printf("no response");
|
||
618 | return SB_TRUE;
|
||
619 | } |
||
620 | |||
621 | /*
|
||
622 | * The following part is critical if the user program uses the same connection like the flashing program!
|
||
623 | * In this case the bootloader doesn't send any information after the reset command. According to this
|
||
624 | * the following code part can be skipped.
|
||
625 | */
|
||
626 | |||
627 | // /* still here so a response was received */
|
||
628 | // responsePacketPtr = XcpTransportReadResponsePacket();
|
||
629 | //
|
||
630 | // /* check if the reponse was valid */
|
||
631 | // printf("response not valid\nlength: %u\nresponse: %i", responsePacketPtr->len, *(responsePacketPtr->data));
|
||
632 | // if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) )
|
||
633 | // {
|
||
634 | // /* not a valid or positive response */
|
||
635 | // return SB_FALSE;
|
||
636 | // }
|
||
637 | |||
638 | |||
639 | /* still here so all went well */
|
||
640 | return SB_TRUE;
|
||
641 | } /*** end of XcpMasterSendCmdProgramReset ***/
|
||
642 | |||
643 | |||
644 | /************************************************************************************//** |
||
645 | ** \brief Sends the XCP PROGRAM command.
|
||
646 | ** \param length Number of bytes in the data array to program.
|
||
647 | ** \param data Array with data bytes to program.
|
||
648 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
649 | **
|
||
650 | ****************************************************************************************/
|
||
651 | static sb_uint8 XcpMasterSendCmdProgram(sb_uint8 length, sb_uint8 data[])
|
||
652 | { |
||
653 | sb_uint8 packetData[XCP_MASTER_TX_MAX_DATA]; |
||
654 | tXcpTransportResponsePacket *responsePacketPtr; |
||
655 | sb_uint8 cnt; |
||
656 | |||
657 | /* verify that this number of bytes actually first in this command */
|
||
658 | assert(length <= (xcpMaxProgCto-2) && (xcpMaxProgCto <= XCP_MASTER_TX_MAX_DATA));
|
||
659 | |||
660 | /* prepare the command packet */
|
||
661 | packetData[0] = XCP_MASTER_CMD_PROGRAM;
|
||
662 | packetData[1] = length;
|
||
663 | for (cnt=0; cnt<length; cnt++) |
||
664 | { |
||
665 | packetData[cnt+2] = data[cnt];
|
||
666 | } |
||
667 | |||
668 | /* send the packet */
|
||
669 | if (XcpTransportSendPacket(packetData, length+2, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T5_MS |
||
670 | { |
||
671 | /* cound not set packet or receive response within the specified timeout */
|
||
672 | printf("\nno response (program)\n");
|
||
673 | return SB_FALSE;
|
||
674 | } |
||
675 | /* still here so a response was received */
|
||
676 | responsePacketPtr = XcpTransportReadResponsePacket(); |
||
677 | |||
678 | /* check if the reponse was valid */
|
||
679 | if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) ) |
||
680 | { |
||
681 | /* not a valid or positive response */
|
||
682 | if (responsePacketPtr->len == 0) { |
||
683 | printf("\nmessage length = 0");
|
||
684 | } else {
|
||
685 | XcpMasterPrintError(responsePacketPtr->data[1]);
|
||
686 | } |
||
687 | printf(" (program)\n");
|
||
688 | return SB_FALSE;
|
||
689 | } |
||
690 | |||
691 | /* still here so all went well */
|
||
692 | return SB_TRUE;
|
||
693 | } /*** end of XcpMasterSendCmdProgram ***/
|
||
694 | |||
695 | |||
696 | /************************************************************************************//** |
||
697 | ** \brief Sends the XCP PROGRAM MAX command.
|
||
698 | ** \param data Array with data bytes to program.
|
||
699 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
700 | **
|
||
701 | ****************************************************************************************/
|
||
702 | static sb_uint8 XcpMasterSendCmdProgramMax(sb_uint8 data[])
|
||
703 | { |
||
704 | sb_uint8 packetData[XCP_MASTER_TX_MAX_DATA]; |
||
705 | tXcpTransportResponsePacket *responsePacketPtr; |
||
706 | sb_uint8 cnt; |
||
707 | |||
708 | /* verify that this number of bytes actually first in this command */
|
||
709 | assert(xcpMaxProgCto <= XCP_MASTER_TX_MAX_DATA); |
||
710 | |||
711 | /* prepare the command packet */
|
||
712 | packetData[0] = XCP_MASTER_CMD_PROGRAM_MAX;
|
||
713 | for (cnt=0; cnt<(xcpMaxProgCto-1); cnt++) |
||
714 | { |
||
715 | packetData[cnt+1] = data[cnt];
|
||
716 | } |
||
717 | |||
718 | /* send the packet */
|
||
719 | if (XcpTransportSendPacket(packetData, xcpMaxProgCto, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T5_MS |
||
720 | { |
||
721 | /* cound not set packet or receive response within the specified timeout */
|
||
722 | printf("\nno response (program max)\n");
|
||
723 | return SB_FALSE;
|
||
724 | } |
||
725 | /* still here so a response was received */
|
||
726 | responsePacketPtr = XcpTransportReadResponsePacket(); |
||
727 | |||
728 | /* check if the reponse was valid */
|
||
729 | if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) ) |
||
730 | { |
||
731 | /* not a valid or positive response */
|
||
732 | if (responsePacketPtr->len == 0) { |
||
733 | printf("\nmessage length = 0");
|
||
734 | } else {
|
||
735 | XcpMasterPrintError(responsePacketPtr->data[1]);
|
||
736 | } |
||
737 | printf(" (program max)\n");
|
||
738 | return SB_FALSE;
|
||
739 | } |
||
740 | |||
741 | /* still here so all went well */
|
||
742 | return SB_TRUE;
|
||
743 | } /*** end of XcpMasterSendCmdProgramMax ***/
|
||
744 | |||
745 | |||
746 | /************************************************************************************//** |
||
747 | ** \brief Sends the XCP PROGRAM CLEAR command.
|
||
748 | ** \return SB_TRUE is successfull, SB_FALSE otherwise.
|
||
749 | **
|
||
750 | ****************************************************************************************/
|
||
751 | static sb_uint8 XcpMasterSendCmdProgramClear(sb_uint32 length)
|
||
752 | { |
||
753 | sb_uint8 packetData[8];
|
||
754 | tXcpTransportResponsePacket *responsePacketPtr; |
||
755 | |||
756 | /* prepare the command packet */
|
||
757 | packetData[0] = XCP_MASTER_CMD_PROGRAM_CLEAR;
|
||
758 | packetData[1] = 0; /* use absolute mode */ |
||
759 | packetData[2] = 0; /* reserved */ |
||
760 | packetData[3] = 0; /* reserved */ |
||
761 | |||
762 | /* set the erase length taking into account byte ordering */
|
||
763 | XcpMasterSetOrderedLong(length, &packetData[4]);
|
||
764 | |||
765 | |||
766 | /* send the packet */
|
||
767 | if (XcpTransportSendPacket(packetData, 8, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) |
||
768 | { |
||
769 | /* cound not set packet or receive response within the specified timeout */
|
||
770 | printf("\nno response (program clear)\n");
|
||
771 | return SB_FALSE;
|
||
772 | } |
||
773 | /* still here so a response was received */
|
||
774 | responsePacketPtr = XcpTransportReadResponsePacket(); |
||
775 | |||
776 | /* check if the reponse was valid */
|
||
777 | if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) ) |
||
778 | { |
||
779 | /* not a valid or positive response */
|
||
780 | if (responsePacketPtr->len == 0) { |
||
781 | printf("\nmessage length = 0");
|
||
782 | } else {
|
||
783 | XcpMasterPrintError(responsePacketPtr->data[1]);
|
||
784 | } |
||
785 | printf(" (program clear)\n");
|
||
786 | return SB_FALSE;
|
||
787 | } |
||
788 | |||
789 | /* still here so all went well */
|
||
790 | return SB_TRUE;
|
||
791 | } /*** end of XcpMasterSendCmdProgramClear ***/
|
||
792 | |||
793 | |||
794 | /************************************************************************************//** |
||
795 | ** \brief Stores a 32-bit value into a byte buffer taking into account Intel
|
||
796 | ** or Motorola byte ordering.
|
||
797 | ** \param value The 32-bit value to store in the buffer.
|
||
798 | ** \param data Array to the buffer for storage.
|
||
799 | ** \return none.
|
||
800 | **
|
||
801 | ****************************************************************************************/
|
||
802 | static void XcpMasterSetOrderedLong(sb_uint32 value, sb_uint8 data[]) |
||
803 | { |
||
804 | if (xcpSlaveIsIntel == SB_TRUE)
|
||
805 | { |
||
806 | data[3] = (sb_uint8)(value >> 24); |
||
807 | data[2] = (sb_uint8)(value >> 16); |
||
808 | data[1] = (sb_uint8)(value >> 8); |
||
809 | data[0] = (sb_uint8)value;
|
||
810 | } |
||
811 | else
|
||
812 | { |
||
813 | data[0] = (sb_uint8)(value >> 24); |
||
814 | data[1] = (sb_uint8)(value >> 16); |
||
815 | data[2] = (sb_uint8)(value >> 8); |
||
816 | data[3] = (sb_uint8)value;
|
||
817 | } |
||
818 | } /*** end of XcpMasterSetOrderedLong ***/
|
||
819 | |||
820 | |||
821 | |||
822 | |||
823 | |||
824 | static void XcpMasterPrintError(sb_uint8 error) { |
||
825 | printf("\nError: ");
|
||
826 | switch (error) {
|
||
827 | case XCP_ERR_CMD_SYNCH:
|
||
828 | printf("command processor synchronization"); break; |
||
829 | case XCP_ERR_CMD_BUSY:
|
||
830 | printf("command was not executed"); break; |
||
831 | case XCP_ERR_CMD_UNKNOWN:
|
||
832 | printf("unknown or unsupported command"); break; |
||
833 | case XCP_ERR_OUT_OF_RANGE:
|
||
834 | printf("parameter out of range"); break; |
||
835 | case XCP_ERR_ACCESS_LOCKED:
|
||
836 | printf("protected - seed/key required"); break; |
||
837 | case XCP_ERR_PAGE_NOT_VALID:
|
||
838 | printf("cal page not valid"); break; |
||
839 | case XCP_ERR_SEQUENCE:
|
||
840 | printf("sequence"); break; |
||
841 | case XCP_ERR_GENERIC:
|
||
842 | printf("generic"); break; |
||
843 | default:
|
||
844 | printf("!UNKNOWN ERROR!");
|
||
845 | } |
||
846 | printf("\n");
|
||
847 | } |
||
848 | |||
849 | /*********************************** end of xcpmaster.c ********************************/
|