Statistics
| Branch: | Tag: | Revision:

amiro-blt / Host / Source / SerialBoot / xcpmaster.c @ f7d2c786

History | View | Annotate | Download (31.607 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 d54d2f07 Thomas Schöpping
* 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 69661903 Thomas Schöpping
* proprietary components. The exception text is included at the bottom of the license
29
* file <license.html>.
30 d54d2f07 Thomas Schöpping
*
31 69661903 Thomas Schöpping
* \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 d54d2f07 Thomas Schöpping
static sb_uint8 XcpMasterSendCmdConnect(sb_uint32 flashingTargetID);
95 69661903 Thomas Schöpping
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 d54d2f07 Thomas Schöpping
sb_uint8 XcpMasterConnect(sb_uint32 flashingTargetID)
159 69661903 Thomas Schöpping
{
160
  sb_uint8 cnt;
161
  sb_uint8 retries = 1;
162
  if (flashingTargetID <= 0) {
163
    retries = XCP_MASTER_CONNECT_RETRIES;
164
  }
165 d54d2f07 Thomas Schöpping
166 69661903 Thomas Schöpping
  /* 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 d54d2f07 Thomas Schöpping
static sb_uint8 XcpMasterSendCmdConnect(sb_uint32 flashingTargetID)
361 69661903 Thomas Schöpping
{
362 d54d2f07 Thomas Schöpping
  sb_uint8 packetData[1+4];
363 69661903 Thomas Schöpping
  tXcpTransportResponsePacket *responsePacketPtr;
364 d54d2f07 Thomas Schöpping
365 69661903 Thomas Schöpping
  /* prepare the command packet */
366
  packetData[0] = XCP_MASTER_CMD_CONNECT;
367 d54d2f07 Thomas Schöpping
  packetData[1] = (sb_uint8)(0xFF & flashingTargetID >> 0);
368
  packetData[2] = (sb_uint8)(0xFF & flashingTargetID >> 8);
369
  packetData[3] = (sb_uint8)(0xFF & flashingTargetID >> 16);
370
  packetData[4] = (sb_uint8)(0xFF & flashingTargetID >> 24);
371
372
  /* send the packet
373
   * NOTE: For legacy support the following code depends on the MSB if the flashingTargetID.
374
   *       If the MSB (packData[4]) is zero (i.e. 0x00??????) the system switches to legacy mode.
375
   *       In legacy mode, the flashingTargetID was only one byte - the LSB (packingData[1]).
376
   */
377
  if (XcpTransportSendPacket(packetData, 1 + ((packetData[4] != 0) ? 4 : 1), XCP_MASTER_CONNECT_TIMEOUT_MS) == SB_FALSE)
378 69661903 Thomas Schöpping
  {
379
    /* cound not set packet or receive response within the specified timeout */
380
    return SB_FALSE;
381
  }
382
  /* still here so a response was received */
383
  responsePacketPtr = XcpTransportReadResponsePacket();
384 d54d2f07 Thomas Schöpping
385 69661903 Thomas Schöpping
  /* check if the reponse was valid */
386
  if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) )
387
  {
388
    /* not a valid or positive response */
389
    return SB_FALSE;
390
  }
391 d54d2f07 Thomas Schöpping
392 69661903 Thomas Schöpping
  /* process response data */
393
  if ((responsePacketPtr->data[2] & 0x01) == 0)
394
  {
395
    /* store slave's byte ordering information */
396
    xcpSlaveIsIntel = SB_TRUE;
397
  }
398
  /* store max number of bytes the slave allows for master->slave packets. */
399
  xcpMaxCto = responsePacketPtr->data[3];
400
  xcpMaxProgCto = xcpMaxCto;
401
  /* store max number of bytes the slave allows for slave->master packets. */
402
  if (xcpSlaveIsIntel == SB_TRUE)
403
  {
404
    xcpMaxDto = responsePacketPtr->data[4] + (responsePacketPtr->data[5] << 8);
405
  }
406
  else
407
  {
408
    xcpMaxDto = responsePacketPtr->data[5] + (responsePacketPtr->data[4] << 8);
409
  }
410 d54d2f07 Thomas Schöpping
411 69661903 Thomas Schöpping
  /* double check size configuration of the master */
412
  assert(XCP_MASTER_TX_MAX_DATA >= xcpMaxCto);
413
  assert(XCP_MASTER_RX_MAX_DATA >= xcpMaxDto);
414 d54d2f07 Thomas Schöpping
415
  /* still here so all went well */
416 69661903 Thomas Schöpping
  return SB_TRUE;
417
} /*** end of XcpMasterSendCmdConnect ***/
418
419
420
/************************************************************************************//**
421
** \brief     Sends the XCP Set MTA command.
422
** \param     address New MTA address for the slave.
423
** \return    SB_TRUE is successfull, SB_FALSE otherwise.
424
**
425
****************************************************************************************/
426
static sb_uint8 XcpMasterSendCmdSetMta(sb_uint32 address)
427
{
428
  sb_uint8 packetData[8];
429
  tXcpTransportResponsePacket *responsePacketPtr;
430 d54d2f07 Thomas Schöpping
431 69661903 Thomas Schöpping
  /* prepare the command packet */
432
  packetData[0] = XCP_MASTER_CMD_SET_MTA;
433
  packetData[1] = 0; /* reserved */
434
  packetData[2] = 0; /* reserved */
435
  packetData[3] = 0; /* address extension not supported */
436 d54d2f07 Thomas Schöpping
437 69661903 Thomas Schöpping
  /* set the address taking into account byte ordering */
438
  XcpMasterSetOrderedLong(address, &packetData[4]);
439 d54d2f07 Thomas Schöpping
440 69661903 Thomas Schöpping
  /* send the packet */
441
  if (XcpTransportSendPacket(packetData, 8, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T1_MS
442
  {
443
    /* cound not set packet or receive response within the specified timeout */
444
    printf("\nno response (set MTA)\n");
445
    return SB_FALSE;
446
  }
447
  /* still here so a response was received */
448
  responsePacketPtr = XcpTransportReadResponsePacket();
449 d54d2f07 Thomas Schöpping
450 69661903 Thomas Schöpping
  /* check if the reponse was valid */
451
  if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) )
452
  {
453
    /* not a valid or positive response */
454
    if (responsePacketPtr->len == 0) {
455
      printf("\nmessage length = 0");
456
    } else {
457
      XcpMasterPrintError(responsePacketPtr->data[1]);
458
    }
459
    printf(" (set MTA)\n");
460
    return SB_FALSE;
461
  }
462 d54d2f07 Thomas Schöpping
463
  /* still here so all went well */
464 69661903 Thomas Schöpping
  return SB_TRUE;
465
} /*** end of XcpMasterSendCmdSetMta ***/
466
467
468
/************************************************************************************//**
469
** \brief     Sends the XCP UPLOAD command.
470
** \param     data Destination data buffer.
471
** \param     length Number of bytes to upload.
472
** \return    SB_TRUE is successfull, SB_FALSE otherwise.
473
**
474
****************************************************************************************/
475
static sb_uint8 XcpMasterSendCmdUpload(sb_uint8 data[], sb_uint8 length)
476
{
477
  sb_uint8 packetData[2];
478
  tXcpTransportResponsePacket *responsePacketPtr;
479
  sb_uint8 data_index;
480 d54d2f07 Thomas Schöpping
481 69661903 Thomas Schöpping
  /* cannot request more data then the max rx data - 1 */
482
  assert(length < XCP_MASTER_RX_MAX_DATA);
483 d54d2f07 Thomas Schöpping
484 69661903 Thomas Schöpping
  /* prepare the command packet */
485
  packetData[0] = XCP_MASTER_CMD_UPLOAD;
486
  packetData[1] = length;
487
488
  /* send the packet */
489
  if (XcpTransportSendPacket(packetData, 2, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T1_MS
490
  {
491
    /* cound not set packet or receive response within the specified timeout */
492
    printf("\nno response (upload)\n");
493
    return SB_FALSE;
494
  }
495
  /* still here so a response was received */
496
  responsePacketPtr = XcpTransportReadResponsePacket();
497 d54d2f07 Thomas Schöpping
498 69661903 Thomas Schöpping
  /* check if the reponse was valid */
499
  if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) )
500
  {
501
    /* not a valid or positive response */
502
    if (responsePacketPtr->len == 0) {
503
      printf("\nmessage length = 0");
504
    } else {
505
      XcpMasterPrintError(responsePacketPtr->data[1]);
506
    }
507
    printf(" (upload)\n");
508
    return SB_FALSE;
509
  }
510 d54d2f07 Thomas Schöpping
511 69661903 Thomas Schöpping
  /* now store the uploaded data */
512
  for (data_index=0; data_index<length; data_index++)
513
  {
514
    data[data_index] = responsePacketPtr->data[data_index+1];
515
  }
516 d54d2f07 Thomas Schöpping
517
  /* still here so all went well */
518 69661903 Thomas Schöpping
  return SB_TRUE;
519
} /*** end of XcpMasterSendCmdUpload ***/
520
521
522
/************************************************************************************//**
523
** \brief     Sends the XCP PROGRAM START command.
524
** \return    SB_TRUE is successfull, SB_FALSE otherwise.
525
**
526
****************************************************************************************/
527
static sb_uint8 XcpMasterSendCmdProgramStart(void)
528
{
529
  sb_uint8 packetData[1];
530
  tXcpTransportResponsePacket *responsePacketPtr;
531
532
  /* prepare the command packet */
533
  packetData[0] = XCP_MASTER_CMD_PROGRAM_START;
534
535
  /* send the packet */
536
  if (XcpTransportSendPacket(packetData, 1, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T3_MS
537
  {
538
    /* cound not set packet or receive response within the specified timeout */
539
    printf("\nno response (program start)\n");
540
    return SB_FALSE;
541
  }
542
  /* still here so a response was received */
543
  responsePacketPtr = XcpTransportReadResponsePacket();
544 d54d2f07 Thomas Schöpping
545 69661903 Thomas Schöpping
  /* check if the reponse was valid */
546
  if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) )
547
  {
548
    /* not a valid or positive response */
549
    if (responsePacketPtr->len == 0) {
550
      printf("\nmessage length = 0");
551
    } else {
552
      XcpMasterPrintError(responsePacketPtr->data[1]);
553
    }
554
    printf(" (program start)\n");
555
    return SB_FALSE;
556
  }
557 d54d2f07 Thomas Schöpping
558 69661903 Thomas Schöpping
  /* store max number of bytes the slave allows for master->slave packets during the
559
   * programming session
560
   */
561
  xcpMaxProgCto = responsePacketPtr->data[3];
562 d54d2f07 Thomas Schöpping
563
  /* still here so all went well */
564 69661903 Thomas Schöpping
  return SB_TRUE;
565
} /*** end of XcpMasterSendCmdProgramStart ***/
566
567
568
/************************************************************************************//**
569
** \brief     Sends the XCP PROGRAM DISCONNECT command.
570
** \return    SB_TRUE is successfull, SB_FALSE otherwise.
571
**
572
****************************************************************************************/
573
static sb_uint8 XcpMasterSendCmdDisconnect(void)
574
{
575
  sb_uint8 packetData[1];
576
  tXcpTransportResponsePacket *responsePacketPtr;
577
578
  /* prepare the command packet */
579
  packetData[0] = XCP_MASTER_CMD_DISCONNECT;
580
581
  /* send the packet */
582
  if (XcpTransportSendPacket(packetData, 1, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE)
583
  {
584
    /* cound not set packet or receive response within the specified timeout */
585
    return SB_FALSE;
586
  }
587
  /* still here so a response was received */
588
  responsePacketPtr = XcpTransportReadResponsePacket();
589 d54d2f07 Thomas Schöpping
590 69661903 Thomas Schöpping
  /* check if the reponse was valid */
591
  if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) )
592
  {
593
    /* not a valid or positive response */
594
    return SB_FALSE;
595
  }
596 d54d2f07 Thomas Schöpping
597
  /* still here so all went well */
598 69661903 Thomas Schöpping
  return SB_TRUE;
599
} /*** end of XcpMasterSendCmdProgramStart ***/
600
601
602
/************************************************************************************//**
603 d54d2f07 Thomas Schöpping
** \brief     Sends the XCP PROGRAM RESET command. Note that this command is a bit
604 69661903 Thomas Schöpping
**            different as in it does not require a response.
605
** \return    SB_TRUE is successfull, SB_FALSE otherwise.
606
**
607
****************************************************************************************/
608
static sb_uint8 XcpMasterSendCmdProgramReset(void)
609
{
610
  sb_uint8 packetData[1];
611
  tXcpTransportResponsePacket *responsePacketPtr;
612
613
  /* prepare the command packet */
614
  packetData[0] = XCP_MASTER_CMD_PROGRAM_RESET;
615
616
  /* send the packet, assume the sending itself is ok and check if a response was
617
   * received.
618
   */
619
  if (XcpTransportSendPacket(packetData, 1, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T5_MS
620
  {
621
    /* probably no response received within the specified timeout, but that is allowed
622
     * for the reset command.
623
     */
624
//    printf("no response");
625
    return SB_TRUE;
626
  }
627
628
/*
629
 * The following part is critical if the user program uses the same connection like the flashing program!
630
 * In this case the bootloader doesn't send any information after the reset command. According to this
631
 * the following code part can be skipped.
632
 */
633
634
//  /* still here so a response was received */
635
//  responsePacketPtr = XcpTransportReadResponsePacket();
636 d54d2f07 Thomas Schöpping
//
637 69661903 Thomas Schöpping
//  /* check if the reponse was valid */
638
//  printf("response not valid\nlength: %u\nresponse: %i", responsePacketPtr->len, *(responsePacketPtr->data));
639
//  if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) )
640
//  {
641
//    /* not a valid or positive response */
642
//    return SB_FALSE;
643
//  }
644
645 d54d2f07 Thomas Schöpping
646
  /* still here so all went well */
647 69661903 Thomas Schöpping
  return SB_TRUE;
648
} /*** end of XcpMasterSendCmdProgramReset ***/
649
650
651
/************************************************************************************//**
652
** \brief     Sends the XCP PROGRAM command.
653
** \param     length Number of bytes in the data array to program.
654
** \param     data Array with data bytes to program.
655
** \return    SB_TRUE is successfull, SB_FALSE otherwise.
656
**
657
****************************************************************************************/
658
static sb_uint8 XcpMasterSendCmdProgram(sb_uint8 length, sb_uint8 data[])
659
{
660
  sb_uint8 packetData[XCP_MASTER_TX_MAX_DATA];
661
  tXcpTransportResponsePacket *responsePacketPtr;
662
  sb_uint8 cnt;
663 d54d2f07 Thomas Schöpping
664 69661903 Thomas Schöpping
  /* verify that this number of bytes actually first in this command */
665
  assert(length <= (xcpMaxProgCto-2) && (xcpMaxProgCto <= XCP_MASTER_TX_MAX_DATA));
666 d54d2f07 Thomas Schöpping
667 69661903 Thomas Schöpping
  /* prepare the command packet */
668
  packetData[0] = XCP_MASTER_CMD_PROGRAM;
669
  packetData[1] = length;
670
  for (cnt=0; cnt<length; cnt++)
671
  {
672
    packetData[cnt+2] = data[cnt];
673
  }
674
675
  /* send the packet */
676
  if (XcpTransportSendPacket(packetData, length+2, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T5_MS
677
  {
678
    /* cound not set packet or receive response within the specified timeout */
679
    printf("\nno response (program)\n");
680
    return SB_FALSE;
681
  }
682
  /* still here so a response was received */
683
  responsePacketPtr = XcpTransportReadResponsePacket();
684 d54d2f07 Thomas Schöpping
685 69661903 Thomas Schöpping
  /* check if the reponse was valid */
686
  if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) )
687
  {
688
    /* not a valid or positive response */
689
    if (responsePacketPtr->len == 0) {
690
      printf("\nmessage length = 0");
691
    } else {
692
      XcpMasterPrintError(responsePacketPtr->data[1]);
693
    }
694
    printf(" (program)\n");
695
    return SB_FALSE;
696
  }
697 d54d2f07 Thomas Schöpping
698
  /* still here so all went well */
699 69661903 Thomas Schöpping
  return SB_TRUE;
700
} /*** end of XcpMasterSendCmdProgram ***/
701
702
703
/************************************************************************************//**
704
** \brief     Sends the XCP PROGRAM MAX command.
705
** \param     data Array with data bytes to program.
706
** \return    SB_TRUE is successfull, SB_FALSE otherwise.
707
**
708
****************************************************************************************/
709
static sb_uint8 XcpMasterSendCmdProgramMax(sb_uint8 data[])
710
{
711
  sb_uint8 packetData[XCP_MASTER_TX_MAX_DATA];
712
  tXcpTransportResponsePacket *responsePacketPtr;
713
  sb_uint8 cnt;
714 d54d2f07 Thomas Schöpping
715 69661903 Thomas Schöpping
  /* verify that this number of bytes actually first in this command */
716
  assert(xcpMaxProgCto <= XCP_MASTER_TX_MAX_DATA);
717 d54d2f07 Thomas Schöpping
718 69661903 Thomas Schöpping
  /* prepare the command packet */
719
  packetData[0] = XCP_MASTER_CMD_PROGRAM_MAX;
720
  for (cnt=0; cnt<(xcpMaxProgCto-1); cnt++)
721
  {
722
    packetData[cnt+1] = data[cnt];
723
  }
724
725
  /* send the packet */
726
  if (XcpTransportSendPacket(packetData, xcpMaxProgCto, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE) //XCP_MASTER_TIMEOUT_T5_MS
727
  {
728
    /* cound not set packet or receive response within the specified timeout */
729
    printf("\nno response (program max)\n");
730
    return SB_FALSE;
731
  }
732
  /* still here so a response was received */
733
  responsePacketPtr = XcpTransportReadResponsePacket();
734 d54d2f07 Thomas Schöpping
735 69661903 Thomas Schöpping
  /* check if the reponse was valid */
736
  if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) )
737
  {
738
    /* not a valid or positive response */
739
    if (responsePacketPtr->len == 0) {
740
      printf("\nmessage length = 0");
741
    } else {
742
      XcpMasterPrintError(responsePacketPtr->data[1]);
743
    }
744
    printf(" (program max)\n");
745
    return SB_FALSE;
746
  }
747 d54d2f07 Thomas Schöpping
748
  /* still here so all went well */
749 69661903 Thomas Schöpping
  return SB_TRUE;
750
} /*** end of XcpMasterSendCmdProgramMax ***/
751
752
753
/************************************************************************************//**
754
** \brief     Sends the XCP PROGRAM CLEAR command.
755
** \return    SB_TRUE is successfull, SB_FALSE otherwise.
756
**
757
****************************************************************************************/
758
static sb_uint8 XcpMasterSendCmdProgramClear(sb_uint32 length)
759
{
760
  sb_uint8 packetData[8];
761
  tXcpTransportResponsePacket *responsePacketPtr;
762
763
  /* prepare the command packet */
764
  packetData[0] = XCP_MASTER_CMD_PROGRAM_CLEAR;
765
  packetData[1] = 0; /* use absolute mode */
766
  packetData[2] = 0; /* reserved */
767
  packetData[3] = 0; /* reserved */
768
769
  /* set the erase length taking into account byte ordering */
770
  XcpMasterSetOrderedLong(length, &packetData[4]);
771
772
773
  /* send the packet */
774
  if (XcpTransportSendPacket(packetData, 8, XCP_MASTER_TIMEOUT_T4_MS) == SB_FALSE)
775
  {
776
    /* cound not set packet or receive response within the specified timeout */
777
    printf("\nno response (program clear)\n");
778
    return SB_FALSE;
779
  }
780
  /* still here so a response was received */
781
  responsePacketPtr = XcpTransportReadResponsePacket();
782 d54d2f07 Thomas Schöpping
783 69661903 Thomas Schöpping
  /* check if the reponse was valid */
784
  if ( (responsePacketPtr->len == 0) || (responsePacketPtr->data[0] != XCP_MASTER_CMD_PID_RES) )
785
  {
786
    /* not a valid or positive response */
787
    if (responsePacketPtr->len == 0) {
788
      printf("\nmessage length = 0");
789
    } else {
790
      XcpMasterPrintError(responsePacketPtr->data[1]);
791
    }
792
    printf(" (program clear)\n");
793
    return SB_FALSE;
794
  }
795 d54d2f07 Thomas Schöpping
796
  /* still here so all went well */
797 69661903 Thomas Schöpping
  return SB_TRUE;
798
} /*** end of XcpMasterSendCmdProgramClear ***/
799
800
801
/************************************************************************************//**
802
** \brief     Stores a 32-bit value into a byte buffer taking into account Intel
803
**            or Motorola byte ordering.
804
** \param     value The 32-bit value to store in the buffer.
805
** \param     data Array to the buffer for storage.
806
** \return    none.
807
**
808
****************************************************************************************/
809
static void XcpMasterSetOrderedLong(sb_uint32 value, sb_uint8 data[])
810
{
811
  if (xcpSlaveIsIntel == SB_TRUE)
812
  {
813
    data[3] = (sb_uint8)(value >> 24);
814
    data[2] = (sb_uint8)(value >> 16);
815
    data[1] = (sb_uint8)(value >>  8);
816
    data[0] = (sb_uint8)value;
817
  }
818
  else
819
  {
820
    data[0] = (sb_uint8)(value >> 24);
821
    data[1] = (sb_uint8)(value >> 16);
822
    data[2] = (sb_uint8)(value >>  8);
823
    data[3] = (sb_uint8)value;
824
  }
825
} /*** end of XcpMasterSetOrderedLong ***/
826
827
828
829
830
831
static void XcpMasterPrintError(sb_uint8 error) {
832
  printf("\nError: ");
833
  switch (error) {
834
    case XCP_ERR_CMD_SYNCH:
835
      printf("command processor synchronization"); break;
836
    case XCP_ERR_CMD_BUSY:
837
      printf("command was not executed"); break;
838
    case XCP_ERR_CMD_UNKNOWN:
839
      printf("unknown or unsupported command"); break;
840
    case XCP_ERR_OUT_OF_RANGE:
841
      printf("parameter out of range"); break;
842
    case XCP_ERR_ACCESS_LOCKED:
843
      printf("protected - seed/key required"); break;
844
    case XCP_ERR_PAGE_NOT_VALID:
845
      printf("cal page not valid"); break;
846
    case XCP_ERR_SEQUENCE:
847
      printf("sequence"); break;
848
    case XCP_ERR_GENERIC:
849
      printf("generic"); break;
850
    default:
851
      printf("!UNKNOWN ERROR!");
852
  }
853
  printf("\n");
854
}
855
856
/*********************************** end of xcpmaster.c ********************************/