amiro-blt / Target / Source / xcp.c @ f7d2c786
History | View | Annotate | Download (52.392 KB)
| 1 |
/************************************************************************************//** |
|---|---|
| 2 |
* \file Source\xcp.c
|
| 3 |
* \brief XCP 1.0 protocol core source file.
|
| 4 |
* \ingroup Core
|
| 5 |
* \internal
|
| 6 |
*----------------------------------------------------------------------------------------
|
| 7 |
* C O P Y R I G H T
|
| 8 |
*----------------------------------------------------------------------------------------
|
| 9 |
* Copyright (c) 2011 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 "boot.h" /* bootloader generic header */ |
| 38 |
|
| 39 |
#include <helper.h> |
| 40 |
|
| 41 |
/****************************************************************************************
|
| 42 |
* Defines
|
| 43 |
****************************************************************************************/
|
| 44 |
/** \brief XCP protocol layer version number (16-bit). */
|
| 45 |
#define XCP_VERSION_PROTOCOL_LAYER (0x0100) |
| 46 |
|
| 47 |
/** \brief XCP transport layer version number (16-bit). */
|
| 48 |
#define XCP_VERSION_TRANSPORT_LAYER (0x0100) |
| 49 |
|
| 50 |
/* XCP packet identifiers */
|
| 51 |
/** \brief Command response packet identifier. */
|
| 52 |
#define XCP_PID_RES (0xff) |
| 53 |
/** \brief Error packet identifier. */
|
| 54 |
#define XCP_PID_ERR (0xfe) |
| 55 |
|
| 56 |
/* XCP error codes */
|
| 57 |
/** \brief Cmd processor synchronization error code. */
|
| 58 |
#define XCP_ERR_CMD_SYNCH (0x00) |
| 59 |
/** \brief Command was not executed error code. */
|
| 60 |
#define XCP_ERR_CMD_BUSY (0x10) |
| 61 |
/** \brief Unknown or unsupported command error code. */
|
| 62 |
#define XCP_ERR_CMD_UNKNOWN (0x20) |
| 63 |
/** \brief Parameter out of range error code. */
|
| 64 |
#define XCP_ERR_OUT_OF_RANGE (0x22) |
| 65 |
/** \brief Protected error code. Seed/key required. */
|
| 66 |
#define XCP_ERR_ACCESS_LOCKED (0x25) |
| 67 |
/** \brief Cal page not valid error code. */
|
| 68 |
#define XCP_ERR_PAGE_NOT_VALID (0x26) |
| 69 |
/** \brief Sequence error code. */
|
| 70 |
#define XCP_ERR_SEQUENCE (0x29) |
| 71 |
/** \brief Generic error code. */
|
| 72 |
#define XCP_ERR_GENERIC (0x31) |
| 73 |
|
| 74 |
/* XCP command codes */
|
| 75 |
/** \brief CONNECT command code. */
|
| 76 |
#define XCP_CMD_CONNECT (0xff) |
| 77 |
/** \brief DISCONNECT command code. */
|
| 78 |
#define XCP_CMD_DISCONNECT (0xfe) |
| 79 |
/** \brief GET_STATUS command code. */
|
| 80 |
#define XCP_CMD_GET_STATUS (0xfd) |
| 81 |
/** \brief SYNCH command code. */
|
| 82 |
#define XCP_CMD_SYNCH (0xfc) |
| 83 |
/** \brief GET_ID command code. */
|
| 84 |
#define XCP_CMD_GET_ID (0xfa) |
| 85 |
/** \brief GET_SEED command code. */
|
| 86 |
#define XCP_CMD_GET_SEED (0xf8) |
| 87 |
/** \brief UNLOCK command code. */
|
| 88 |
#define XCP_CMD_UNLOCK (0xf7) |
| 89 |
/** \brief SET_MTA command code. */
|
| 90 |
#define XCP_CMD_SET_MTA (0xf6) |
| 91 |
/** \brief UPLOAD command code. */
|
| 92 |
#define XCP_CMD_UPLOAD (0xf5) |
| 93 |
/** \brief SHORT_UPLOAD command code. */
|
| 94 |
#define XCP_CMD_SHORT_UPLOAD (0xf4) |
| 95 |
/** \brief BUILD_CHECKSUM command code. */
|
| 96 |
#define XCP_CMD_BUILD_CHECKSUM (0xf3) |
| 97 |
/** \brief DOWNLOAD command code. */
|
| 98 |
#define XCP_CMD_DOWNLOAD (0xf0) |
| 99 |
/** \brief DOWNLOAD_MAX command code. */
|
| 100 |
#define XCP_CMD_DOWLOAD_MAX (0xee) |
| 101 |
/** \brief SET_CALPAGE command code. */
|
| 102 |
#define XCP_CMD_SET_CAL_PAGE (0xeb) |
| 103 |
/** \brief GET_CALPAGE command code. */
|
| 104 |
#define XCP_CMD_GET_CAL_PAGE (0xea) |
| 105 |
/** \brief PROGRAM_START command code. */
|
| 106 |
#define XCP_CMD_PROGRAM_START (0xd2) |
| 107 |
/** \brief PROGRAM_CLEAR command code. */
|
| 108 |
#define XCP_CMD_PROGRAM_CLEAR (0xd1) |
| 109 |
/** \brief PROGRAM command code. */
|
| 110 |
#define XCP_CMD_PROGRAM (0xd0) |
| 111 |
/** \brief PROGRAM_RESET command code. */
|
| 112 |
#define XCP_CMD_PROGRAM_RESET (0xcf) |
| 113 |
/** \brief PROGRAM_PREPARE command code. */
|
| 114 |
#define XCP_CMD_PROGRAM_PREPARE (0xcc) |
| 115 |
/** \brief PROGRAM_MAX command code. */
|
| 116 |
#define XCP_CMD_PROGRAM_MAX (0xc9) |
| 117 |
|
| 118 |
|
| 119 |
/****************************************************************************************
|
| 120 |
* Type definitions
|
| 121 |
****************************************************************************************/
|
| 122 |
/** \brief Struture type for grouping XCP internal module information. */
|
| 123 |
typedef struct |
| 124 |
{
|
| 125 |
blt_int8u connected; /**< connection established */
|
| 126 |
#if (BOOT_GATE_ENABLE > 0) |
| 127 |
blt_int32u other_connection; /**< connection to other device established */
|
| 128 |
#endif
|
| 129 |
#if (BOOTLOADER_OF_MAIN_DEVICE > 0) |
| 130 |
blt_bool wasMainConnection; /**< connection to serialboot (0x00) established */
|
| 131 |
#endif
|
| 132 |
blt_int8u oldData[BOOT_COM_RX_MAX_DATA]; /**< old packet data buffer */
|
| 133 |
blt_int8u protection; /**< protection state */
|
| 134 |
blt_int8u s_n_k_resource; /**< for seed/key sequence */
|
| 135 |
blt_int8u ctoData[BOOT_COM_RX_MAX_DATA]; /**< cto packet data buffer */
|
| 136 |
blt_int8u ctoPending; /**< cto transmission pending flag */
|
| 137 |
blt_int16s ctoLen; /**< cto current packet length */
|
| 138 |
blt_int32u mta; /**< memory transfer address */
|
| 139 |
} tXcpInfo; |
| 140 |
|
| 141 |
|
| 142 |
/****************************************************************************************
|
| 143 |
* Function prototypes
|
| 144 |
****************************************************************************************/
|
| 145 |
/* transport layer specific functions */
|
| 146 |
#if (BOOT_GATE_ENABLE > 0) |
| 147 |
static void XcpTransmitPacket(blt_int8u *data, blt_int16s len, blt_bool fromGate); |
| 148 |
#else
|
| 149 |
static void XcpTransmitPacket(blt_int8u *data, blt_int16s len); |
| 150 |
#endif /* BOOT_GATE_ENABLE > 0 */ |
| 151 |
|
| 152 |
/* application specific functions */
|
| 153 |
static blt_int8u XcpComputeChecksum(blt_int32u address, blt_int32u length,
|
| 154 |
blt_int32u *checksum); |
| 155 |
|
| 156 |
#if (XCP_SEED_KEY_PROTECTION_EN == 1) |
| 157 |
static blt_int8u XcpGetSeed(blt_int8u resource, blt_int8u *seed);
|
| 158 |
static blt_int8u XcpVerifyKey(blt_int8u resource, blt_int8u *key, blt_int8u len);
|
| 159 |
#endif
|
| 160 |
|
| 161 |
/* general utility functions */
|
| 162 |
static void XcpProtectResources(void); |
| 163 |
static void XcpSetCtoError(blt_int8u error); |
| 164 |
|
| 165 |
/* XCP command processors */
|
| 166 |
static void XcpCmdConnect(blt_int8u *data, blt_int16s dataLength); |
| 167 |
static void XcpCmdDisconnect(blt_int8u *data); |
| 168 |
static void XcpCmdGetStatus(blt_int8u *data); |
| 169 |
static void XcpCmdSynch(blt_int8u *data); |
| 170 |
static void XcpCmdGetId(blt_int8u *data); |
| 171 |
static void XcpCmdSetMta(blt_int8u *data); |
| 172 |
static void XcpCmdUpload(blt_int8u *data); |
| 173 |
static void XcpCmdShortUpload(blt_int8u *data); |
| 174 |
static void XcpCmdBuildCheckSum(blt_int8u *data); |
| 175 |
#if (XCP_SEED_KEY_PROTECTION_EN == 1) |
| 176 |
static void XcpCmdGetSeed(blt_int8u *data); |
| 177 |
static void XcpCmdUnlock(blt_int8u *data); |
| 178 |
#endif
|
| 179 |
#if (XCP_RES_CALIBRATION_EN == 1) |
| 180 |
static void XcpCmdDownload(blt_int8u *data); |
| 181 |
static void XcpCmdDownloadMax(blt_int8u *data); |
| 182 |
#endif
|
| 183 |
#if (XCP_RES_PAGING_EN == 1) |
| 184 |
static void XcpCmdSetCalPage(blt_int8u *data); |
| 185 |
static void XcpCmdGetCalPage(blt_int8u *data); |
| 186 |
#endif
|
| 187 |
#if (XCP_RES_PROGRAMMING_EN == 1) |
| 188 |
static void XcpCmdProgramMax(blt_int8u *data); |
| 189 |
static void XcpCmdProgram(blt_int8u *data); |
| 190 |
static void XcpCmdProgramStart(blt_int8u *data); |
| 191 |
static void XcpCmdProgramClear(blt_int8u *data); |
| 192 |
static void XcpCmdProgramReset(blt_int8u *data); |
| 193 |
static void XcpCmdProgramPrepare(blt_int8u *data); |
| 194 |
#endif
|
| 195 |
static void portedTransmission(blt_int8u *data); |
| 196 |
|
| 197 |
|
| 198 |
/****************************************************************************************
|
| 199 |
* Hook functions
|
| 200 |
****************************************************************************************/
|
| 201 |
#if (XCP_RES_PAGING_EN == 1) |
| 202 |
extern blt_int8u XcpCalSetPageHook(blt_int8u segment, blt_int8u page);
|
| 203 |
extern blt_int8u XcpCalGetPageHook(blt_int8u segment);
|
| 204 |
#endif
|
| 205 |
|
| 206 |
#if (XCP_CONNECT_MODE_HOOK_EN == 1) |
| 207 |
extern blt_bool XcpConnectModeHook(blt_int8u mode);
|
| 208 |
#endif
|
| 209 |
|
| 210 |
#if (XCP_SEED_KEY_PROTECTION_EN == 1) |
| 211 |
extern blt_int8u XcpGetSeedHook(blt_int8u resource, blt_int8u *seed);
|
| 212 |
extern blt_int8u XcpVerifyKeyHook(blt_int8u resource, blt_int8u *key, blt_int8u len);
|
| 213 |
#endif
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
/****************************************************************************************
|
| 218 |
* External functions
|
| 219 |
****************************************************************************************/
|
| 220 |
#if (BOOT_COM_ENABLE == 0) |
| 221 |
/* in case no internally supported communication interface is used, a custom
|
| 222 |
* communication module can be added. In order to use the XCP protocol in the custom
|
| 223 |
* communication module, this hook function needs to be implemented. In the XCP protocol
|
| 224 |
* is not needed, then simply remove the xcp.c source from the project.
|
| 225 |
*/
|
| 226 |
extern void XcpTransmitPacketHook(blt_int8u *data, blt_int16u len); |
| 227 |
#endif
|
| 228 |
|
| 229 |
|
| 230 |
/****************************************************************************************
|
| 231 |
* Local constants
|
| 232 |
****************************************************************************************/
|
| 233 |
/** \brief String buffer with station id. */
|
| 234 |
static const blt_int8s xcpStationId[] = XCP_STATION_ID_STRING; |
| 235 |
|
| 236 |
|
| 237 |
/****************************************************************************************
|
| 238 |
* Local data definitions
|
| 239 |
****************************************************************************************/
|
| 240 |
/** \brief Local variable for storing XCP internal module info. */
|
| 241 |
static tXcpInfo xcpInfo;
|
| 242 |
|
| 243 |
|
| 244 |
/************************************************************************************//** |
| 245 |
** \brief Initializes the XCP driver. Should be called once upon system startup.
|
| 246 |
** \return none
|
| 247 |
**
|
| 248 |
****************************************************************************************/
|
| 249 |
void XcpInit(void) |
| 250 |
{
|
| 251 |
/* reset xcp module info */
|
| 252 |
xcpInfo.connected = 0;
|
| 253 |
#if (BOOT_GATE_ENABLE > 0) |
| 254 |
xcpInfo.other_connection = 0;
|
| 255 |
#endif
|
| 256 |
#if (BOOTLOADER_OF_MAIN_DEVICE > 0) |
| 257 |
xcpInfo.wasMainConnection = BLT_FALSE; |
| 258 |
#endif
|
| 259 |
xcpInfo.mta = 0;
|
| 260 |
xcpInfo.ctoPending = 0;
|
| 261 |
xcpInfo.ctoLen = 0;
|
| 262 |
xcpInfo.s_n_k_resource = 0;
|
| 263 |
xcpInfo.protection = 0;
|
| 264 |
} /*** end of XcpInit ***/
|
| 265 |
|
| 266 |
|
| 267 |
/************************************************************************************//** |
| 268 |
** \brief Obtains information about the XCP connection state.
|
| 269 |
** \return BLT_TRUE is an XCP connection is established, BLT_FALSE otherwise.
|
| 270 |
**
|
| 271 |
****************************************************************************************/
|
| 272 |
blt_bool XcpIsConnected(void)
|
| 273 |
{
|
| 274 |
#if (BOOT_GATE_ENABLE > 0) |
| 275 |
if (xcpInfo.connected == 0 && xcpInfo.other_connection == 0) |
| 276 |
#else
|
| 277 |
if (xcpInfo.connected == 0) |
| 278 |
#endif
|
| 279 |
{
|
| 280 |
return BLT_FALSE;
|
| 281 |
} |
| 282 |
return BLT_TRUE;
|
| 283 |
} /*** end of XcpIsConnected ***/
|
| 284 |
|
| 285 |
|
| 286 |
#if (BOOTLOADER_OF_MAIN_DEVICE > 0) |
| 287 |
/************************************************************************************//** |
| 288 |
** \brief Obtains information about the XCP main connection state.
|
| 289 |
** \return BLT_TRUE is an XCP connection to main was established, BLT_FALSE otherwise.
|
| 290 |
**
|
| 291 |
****************************************************************************************/
|
| 292 |
blt_bool XcpWasConnectedToMain(void)
|
| 293 |
{
|
| 294 |
return xcpInfo.wasMainConnection;
|
| 295 |
} /*** end of XcpWasConnectedToMain ***/
|
| 296 |
#endif
|
| 297 |
|
| 298 |
|
| 299 |
/************************************************************************************//** |
| 300 |
** \brief Informs the core that a pending packet transmission was completed by
|
| 301 |
** the transport layer.
|
| 302 |
** \return none
|
| 303 |
**
|
| 304 |
****************************************************************************************/
|
| 305 |
void XcpPacketTransmitted(void) |
| 306 |
{
|
| 307 |
/* reset packet transmission pending flag */
|
| 308 |
xcpInfo.ctoPending = 0;
|
| 309 |
} /*** end of XcpPacketTransmitted ***/
|
| 310 |
|
| 311 |
|
| 312 |
/************************************************************************************//** |
| 313 |
** \brief Informs the core that a new packet was received by the transport layer.
|
| 314 |
** \param data Pointer to byte buffer with packet data.
|
| 315 |
** \param dataLength Number of data bytes that need to be transmitted.
|
| 316 |
** \return none
|
| 317 |
**
|
| 318 |
****************************************************************************************/
|
| 319 |
#if (BOOT_GATE_ENABLE > 0) |
| 320 |
void XcpPacketReceived(blt_int8u *data, blt_int16s dataLength, blt_bool fromGate)
|
| 321 |
#else
|
| 322 |
void XcpPacketReceived(blt_int8u *data, blt_int16s dataLength)
|
| 323 |
#endif /* BOOT_GATE_ENABLE > 0 */ |
| 324 |
{
|
| 325 |
/* was this a connect command? */
|
| 326 |
if (data[0] == XCP_CMD_CONNECT) |
| 327 |
{
|
| 328 |
/* process the connect command */
|
| 329 |
XcpCmdConnect(data, dataLength); |
| 330 |
} |
| 331 |
/* only continue if connected */
|
| 332 |
else if (xcpInfo.connected == 1) |
| 333 |
{
|
| 334 |
switch (data[0]) |
| 335 |
{
|
| 336 |
case XCP_CMD_UPLOAD:
|
| 337 |
XcpCmdUpload(data); |
| 338 |
break;
|
| 339 |
case XCP_CMD_SHORT_UPLOAD:
|
| 340 |
XcpCmdShortUpload(data); |
| 341 |