amiro-blt / Host / Source / SerialBoot / main.c @ 6feb42c8
History | View | Annotate | Download (32.331 KB)
| 1 |
/************************************************************************************//** |
|---|---|
| 2 |
* \file main.c
|
| 3 |
* \brief SerialBoot command line demonstration program for OpenBLT.
|
| 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 |
/****************************************************************************************
|
| 36 |
* Include files
|
| 37 |
****************************************************************************************/
|
| 38 |
#include <assert.h> /* assertion module */ |
| 39 |
#include <sb_types.h> /* C types */ |
| 40 |
#include <stdio.h> /* standard I/O library */ |
| 41 |
#include <string.h> /* string library */ |
| 42 |
#include "xcpmaster.h" /* XCP master protocol module */ |
| 43 |
#include "srecord.h" /* S-record file handling */ |
| 44 |
#include "timeutil.h" /* time utility module */ |
| 45 |
|
| 46 |
|
| 47 |
/****************************************************************************************
|
| 48 |
* Function prototypes
|
| 49 |
****************************************************************************************/
|
| 50 |
static void DisplayProgramInfo(void); |
| 51 |
static void DisplayProgramUsage(void); |
| 52 |
static void DisplayProgramInput(void); |
| 53 |
static void printErrors(void); |
| 54 |
static sb_uint8 ParseCommandLine(sb_int32 argc, sb_char *argv[]);
|
| 55 |
static sb_int32 startFlashMode(void); |
| 56 |
static sb_int32 flashProgram(void); |
| 57 |
static sb_int32 startUserProgram(void); |
| 58 |
static sb_int32 originalMainPart(void); |
| 59 |
static sb_int32 openSerialPort(void); |
| 60 |
static sb_int32 openSerialPortConnect(sb_uint8 start);
|
| 61 |
static sb_int32 closeSerialPort(void); |
| 62 |
static sb_int32 buildConnection(sb_uint8 start);
|
| 63 |
static sb_int32 closeConnection(sb_file *hSrecord);
|
| 64 |
static sb_int32 closeConnectionWithReset(sb_file *hSrecord);
|
| 65 |
static sb_int32 prepareProgrammingSession(sb_file *hSrecord, tSrecordParseResults *fileParseResults);
|
| 66 |
static sb_int32 programCode(sb_file *hSrecord, tSrecordParseResults *fileParseResults, tSrecordLineParseResults *lineParseResults);
|
| 67 |
|
| 68 |
/****************************************************************************************
|
| 69 |
* Macro definitions
|
| 70 |
****************************************************************************************/
|
| 71 |
/** \brief Program return code if all went ok. */
|
| 72 |
#define PROG_RESULT_OK (0) |
| 73 |
|
| 74 |
/** \brief Program return code if an error occurred. */
|
| 75 |
#define PROG_RESULT_ERROR (1) |
| 76 |
|
| 77 |
/* mode definitions of the program status */
|
| 78 |
#define PROGSTATE_ORIGIN 0 |
| 79 |
#define PROGSTATE_STARTFLASHMODE 1 |
| 80 |
#define PROGSTATE_RESET 2 |
| 81 |
#define PROGSTATE_FLASHPROGRAM 3 |
| 82 |
#define PROGSTATE_SETFLAGS 4 |
| 83 |
|
| 84 |
/* error definitions */
|
| 85 |
#define ERROR_NO 0 |
| 86 |
#define ERROR_FILE 1 |
| 87 |
#define ERROR_DEVICE 2 |
| 88 |
#define ERROR_UNKNOWN 255 |
| 89 |
|
| 90 |
/* maximal space for flashing programs */
|
| 91 |
#define MAX_COUNT_FLASHING_PROGRAMS 5 |
| 92 |
|
| 93 |
|
| 94 |
/****************************************************************************************
|
| 95 |
* Local data declarations
|
| 96 |
****************************************************************************************/
|
| 97 |
/** \brief Name of the serial device, such as COM4 or /dev/ttyUSB0. */
|
| 98 |
static sb_char serialDeviceName[32]; |
| 99 |
|
| 100 |
/** \brief Id of the bluetooth device, such as 01:23:45:67:89:AB. */
|
| 101 |
static sb_char bluetoothID[18]; |
| 102 |
|
| 103 |
/** \brief Serial communication speed in bits per second. */
|
| 104 |
static sb_uint32 serialBaudrate;
|
| 105 |
|
| 106 |
/** \brief Name of the S-record file. */
|
| 107 |
static sb_char *srecordFileName;
|
| 108 |
|
| 109 |
/* program status */
|
| 110 |
static sb_uint8 prog_state = 1; |
| 111 |
|
| 112 |
/* id of flashing target */
|
| 113 |
static sb_uint8 flashingTargetID = 0; |
| 114 |
|
| 115 |
/* list of program names */
|
| 116 |
static sb_char *srecordFileNames[MAX_COUNT_FLASHING_PROGRAMS];
|
| 117 |
|
| 118 |
/* list of target ids */
|
| 119 |
static sb_uint8 flashingTargetIDs[MAX_COUNT_FLASHING_PROGRAMS];
|
| 120 |
|
| 121 |
/* list of errors */
|
| 122 |
static sb_uint8 errors[MAX_COUNT_FLASHING_PROGRAMS];
|
| 123 |
|
| 124 |
/* last error */
|
| 125 |
static sb_uint8 errorDetected = ERROR_NO;
|
| 126 |
|
| 127 |
/* program counter */
|
| 128 |
static sb_uint8 countFlashingPrograms = 0; |
| 129 |
|
| 130 |
/* RTS register value */
|
| 131 |
static sb_uint8 registerRTSvalue = 0; |
| 132 |
|
| 133 |
/* Type of communication is about UART, not Bluetooth */
|
| 134 |
static sb_uint8 comIsUart = SB_TRUE;
|
| 135 |
|
| 136 |
|
| 137 |
/************************************************************************************//** |
| 138 |
** \brief Program entry point.
|
| 139 |
** \param argc Number of program parameters.
|
| 140 |
** \param argv array to program parameter strings.
|
| 141 |
** \return 0 on success, > 0 on error.
|
| 142 |
**
|
| 143 |
****************************************************************************************/
|
| 144 |
sb_int32 main(sb_int32 argc, sb_char *argv[]) |
| 145 |
{
|
| 146 |
/* disable buffering for the standard output to make sure printf does not wait until
|
| 147 |
* a newline character is detected before outputting text on the console.
|
| 148 |
*/
|
| 149 |
setbuf(stdout, SB_NULL); |
| 150 |
|
| 151 |
/* inform user about the program */
|
| 152 |
DisplayProgramInfo(); |
| 153 |
|
| 154 |
/* start out by making sure program was started with the correct parameters */
|
| 155 |
if (ParseCommandLine(argc, argv) == SB_FALSE)
|
| 156 |
{
|
| 157 |
/* parameters invalid. inform user about how this program works */
|
| 158 |
DisplayProgramUsage(); |
| 159 |
return PROG_RESULT_ERROR;
|
| 160 |
} |
| 161 |
|
| 162 |
DisplayProgramInput(); |
| 163 |
|
| 164 |
if (openSerialPortConnect(0) == PROG_RESULT_ERROR) { |
| 165 |
return PROG_RESULT_ERROR;
|
| 166 |
} |
| 167 |
|
| 168 |
/* initialize errors */
|
| 169 |
sb_uint8 errorIdx; |
| 170 |
for (errorIdx=0; errorIdx<MAX_COUNT_FLASHING_PROGRAMS; errorIdx++) { |
| 171 |
errors[errorIdx] = ERROR_NO; |
| 172 |
} |
| 173 |
|
| 174 |
flashingTargetID = 0;
|
| 175 |
if (startFlashMode() == PROG_RESULT_ERROR) {
|
| 176 |
return PROG_RESULT_ERROR;
|
| 177 |
} |
| 178 |
|
| 179 |
sb_uint8 flashIdx; |
| 180 |
for (flashIdx=0; flashIdx<countFlashingPrograms; flashIdx++) { |
| 181 |
errorDetected = ERROR_NO; |
| 182 |
flashingTargetID = flashingTargetIDs[flashIdx]; |
| 183 |
srecordFileName = srecordFileNames[flashIdx]; |
| 184 |
printf("\nFlash %i: Flashing %s on device %i\n\n", flashIdx+1, srecordFileName, flashingTargetID); |
| 185 |
if (flashProgram() == PROG_RESULT_ERROR) {
|
| 186 |
closeSerialPort(); |
| 187 |
openSerialPortConnect(0);
|
| 188 |
} |
| 189 |
errors[flashIdx] = errorDetected; |
| 190 |
} |
| 191 |
|
| 192 |
flashingTargetID = 0;
|
| 193 |
if (startUserProgram() == PROG_RESULT_ERROR) {
|
| 194 |
return PROG_RESULT_ERROR;
|
| 195 |
} |
| 196 |
|
| 197 |
printErrors(); |
| 198 |
|
| 199 |
|
| 200 |
return PROG_RESULT_OK;
|
| 201 |
} /*** end of main ***/
|
| 202 |
|
| 203 |
|
| 204 |
/************************************************************************************//** |
| 205 |
** \brief Outputs information to the user about this program.
|
| 206 |
** \return none.
|
| 207 |
**
|
| 208 |
****************************************************************************************/
|
| 209 |
static void DisplayProgramInfo(void) |
| 210 |
{
|
| 211 |
printf("---------------------------------------------------------------------------\n");
|
| 212 |
printf("SerialBoot version 1.00. Performs firmware updates via the serial port \n");
|
| 213 |
printf("for a microcontroller based system that runs the OpenBLT bootloader. \n");
|
| 214 |
printf(" \n");
|
| 215 |
printf("Copyright (c) by Feaser http://www.feaser.com \n");
|
| 216 |
printf("---------------------------------------------------------------------------\n");
|
| 217 |
printf("This tool was modified for the 'Autonomous Mini Robot' - AMiRo. \n");
|
| 218 |
printf("Copyright (c) 2016..2018 Marvin Barther, Thomas Schoepping, and Stefan \n");
|
| 219 |
printf(" Herbrechtsmeier \n");
|
| 220 |
printf("This is free software; see the source for copying conditions. There is NO \n");
|
| 221 |
printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
|
| 222 |
printf("The development of this software was supported by the Excellence Cluster \n");
|
| 223 |
printf("EXC 227 Cognitive Interaction Technology. The Excellence Cluster EXC 227 is\n");
|
| 224 |
printf("a grant of the Deutsche Forschungsgemeinschaft (DFG) in the context of the \n");
|
| 225 |
printf("German Excellence Initiative. \n");
|
| 226 |
printf("---------------------------------------------------------------------------\n");
|
| 227 |
} /*** end of DisplayProgramInfo ***/
|
| 228 |
|
| 229 |
|
| 230 |
/************************************************************************************//** |
| 231 |
** \brief Outputs information to the user about how to use this program.
|
| 232 |
** \return none.
|
| 233 |
**
|
| 234 |
****************************************************************************************/
|
| 235 |
static void DisplayProgramUsage(void) |
| 236 |
{
|
| 237 |
printf("Usage: SerialBoot -d<device> -b<baudrate> <s-record file>/(-T<flash-id> <s-record file>)\n\n");
|
| 238 |
printf("Example 1: SerialBoot -h\n");
|
| 239 |
#ifdef PLATFORM_WIN32
|
| 240 |
printf("Example 2: SerialBoot -dCOM4 -b57600 -T3 firmware.srec\n");
|
| 241 |
printf("Example 3: SerialBoot -dCOM4 -b57600 -T0 firm1.srec -T3 firm2.srec\n");
|
| 242 |
printf("Example 4: SerialBoot -dCOM4 -b57600 firmware.srec\n\n");
|
| 243 |
printf("WARNING: SerialBoot is not really functional with windows! Please use\n");
|
| 244 |
printf(" the Linux version.\n\n");
|
| 245 |
printf(" -> In all four cases it connects to COM4 and configures a communication\n");
|
| 246 |
#else
|
| 247 |
printf("Example 2: SerialBoot -d/dev/ttyUSB0 -b57600 -T3 firmware.srec\n");
|
| 248 |
printf("Example 3: SerialBoot -d/dev/ttyUSB0 -b57600 -T0 firm1.srec -T3 firm2.srec\n");
|
| 249 |
printf("Example 4: SerialBoot -d/dev/ttyUSB0 -b57600 firmware.srec\n\n");
|
| 250 |
printf(" -> In all four cases it connects to ttyUSB0 and configures a communication\n");
|
| 251 |
#endif
|
| 252 |
printf(" speed of 57600 bits/second. The bootloaders will be set into flashing\n");
|
| 253 |
printf(" mode and will be reset after flashing the last binary file.\n");
|
| 254 |
printf(" -> In the first example this help will be opened\n");
|
| 255 |
printf(" -> In the second example the binary file 'firmware.srec' will be flashed on\n");
|
| 256 |
printf(" device 0x03.\n");
|
| 257 |
printf(" -> In the third example the binary file 'firm1.srec' will be flashed on\n");
|
| 258 |
printf(" device 0x00 and the other binary file 'firm2.srec' on device 0x03.\n");
|
| 259 |
printf(" -> In the last example the standard configuration is shown. The binary file\n");
|
| 260 |
printf(" 'firmware.srec' will be flashed on device 0x00. It equals the\n");
|
| 261 |
printf(" configuration '-T0 firmware.srec'.\n");
|
| 262 |
printf(" -> Don't forget that you always have to give the parameters -d and -b.\n");
|
| 263 |
printf(" -> There is also the parameter -a for giving an address for bluetooth\n");
|
| 264 |
printf(" communication, but this part is not functional yet.\n");
|
| 265 |
printf("--------------------------------------------------------------------------------\n");
|
| 266 |
} /*** end of DisplayProgramUsage ***/
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
static void DisplayProgramInput(void) { |
| 271 |
#ifdef PLATFORM_WIN32
|
| 272 |
printf("WARNING: SerialBoot is designed for Linux. While using Windows the correct\n");
|
| 273 |
printf(" functionality can not be guaranteed! Please use Linux for using best\n");
|
| 274 |
printf(" performance and savety.\n");
|
| 275 |
printf("\n--------------------------------------------------------------------------------\n\n");
|
| 276 |
#endif
|
| 277 |
printf("\nOpen device at %s @ %u bits/s and RTS=%i\n\n", serialDeviceName, serialBaudrate, registerRTSvalue);
|
| 278 |
printf("Programming will start immediately in following order:\n");
|
| 279 |
sb_uint8 idx; |
| 280 |
for (idx=0; idx<countFlashingPrograms; idx++) { |
| 281 |
printf(" %i) device %i: %s\n", idx+1, flashingTargetIDs[idx], srecordFileNames[idx]); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
/************************************************************************************//** |
| 287 |
** \brief Parses the command line arguments. A fixed amount of arguments is expected.
|
| 288 |
** The program should be called as:
|
| 289 |
** SerialBoot -d[device] -b[baudrate] [s-record file]
|
| 290 |
** \param argc Number of program parameters.
|
| 291 |
** \param argv array to program parameter strings.
|
| 292 |
** \return SB_TRUE on success, SB_FALSE otherwise.
|
| 293 |
**
|
| 294 |
****************************************************************************************/
|
| 295 |
static sb_uint8 ParseCommandLine(sb_int32 argc, sb_char *argv[])
|
| 296 |
{
|
| 297 |
sb_uint8 paramIdx; |
| 298 |
sb_uint8 paramAfound = SB_FALSE; |
| 299 |
sb_uint8 paramDfound = SB_FALSE; |
| 300 |
sb_uint8 paramBfound = SB_FALSE; |
| 301 |
sb_uint8 paramTfound = SB_FALSE; |
| 302 |
sb_uint8 paramRTSfound = SB_FALSE; |
| 303 |
sb_uint8 paramXfound = SB_FALSE; |
| 304 |
sb_uint8 srecordfound = SB_FALSE; |
| 305 |
|
| 306 |
/* make sure the right amount of arguments are given */
|
| 307 |
if (argc < 4 || argc > 14) |
| 308 |
{
|
| 309 |
return SB_FALSE;
|
| 310 |
} |
| 311 |
|
| 312 |
/* loop through all the command lina parameters, just skip the 1st one because this
|
| 313 |
* is the name of the program, which we are not interested in.
|
| 314 |
*/
|
| 315 |
for (paramIdx=1; paramIdx<argc; paramIdx++) |
| 316 |
{
|
| 317 |
/* is this the device name? */
|
| 318 |
if ( (argv[paramIdx][0] == '-') && (argv[paramIdx][1] == 'd') && (paramDfound == SB_FALSE) && (paramTfound == SB_FALSE) && (paramAfound == SB_FALSE)) |
| 319 |
{
|
| 320 |
/* copy the device name and set flag that this parameter was found */
|
| 321 |
strcpy(serialDeviceName, &argv[paramIdx][2]);
|
| 322 |
paramDfound = SB_TRUE; |
| 323 |
comIsUart = SB_TRUE; |
| 324 |
} |
| 325 |
/* is this the bluetooth id? */
|
| 326 |
/*
|
| 327 |
else if ( (argv[paramIdx][0] == '-') && (argv[paramIdx][1] == 'a') && (paramDfound == SB_FALSE) && (paramTfound == SB_FALSE) && (paramAfound == SB_FALSE))
|
| 328 |
{
|
| 329 |
if ((argv[paramIdx][4] != ':') || (argv[paramIdx][7] != ':') || (argv[paramIdx][10] != ':') || (argv[paramIdx][13] != ':') || (argv[paramIdx][16] != ':')) {
|
| 330 |
printf("The bluetooth ID %s has not the format xx:xx:xx:xx:xx:xx\n", &argv[paramIdx][2]);
|
| 331 |
return SB_FALSE;
|
| 332 |
}
|
| 333 |
// copy the bluetooth id and set flag that this parameter was found
|
| 334 |
strcpy(serialDeviceName, &argv[paramIdx][2]);
|
| 335 |
paramAfound = SB_TRUE;
|
| 336 |
comIsUart = SB_FALSE;
|
| 337 |
// TODO accept bluetooth communication
|
| 338 |
printf("Bluetooth communication is not available yet!");
|
| 339 |
return SB_FALSE;
|
| 340 |
}
|
| 341 |
*/
|
| 342 |
/* is this the baudrate? */
|
| 343 |
else if ( (argv[paramIdx][0] == '-') && (argv[paramIdx][1] == 'b') && (paramBfound == SB_FALSE) && (paramTfound == SB_FALSE) ) |
| 344 |
{
|
| 345 |
/* extract the baudrate and set flag that this parameter was found */
|
| 346 |
sscanf(&argv[paramIdx][2], "%u", &serialBaudrate); |
| 347 |
paramBfound = SB_TRUE; |
| 348 |
} |
| 349 |
/* is this the RTS flag? */
|
| 350 |
else if ( (argv[paramIdx][0] == '-') && (argv[paramIdx][1] == 'R') && (argv[paramIdx][2] == 'T') && (argv[paramIdx][3] == 'S') && (paramRTSfound == SB_FALSE) && (paramTfound == SB_FALSE) ) |
| 351 |
{
|
| 352 |
/* extract RTS flag [0,1] and set that this parameter was found */
|
| 353 |
sb_char setValue[2];
|
| 354 |
strcpy(setValue, &argv[paramIdx][4]);
|
| 355 |
if (setValue[0] == 0x0) { |
| 356 |
registerRTSvalue = 0;
|
| 357 |
} else {
|
| 358 |
registerRTSvalue = ((int)setValue[0])-48; |
| 359 |
} |
| 360 |
if (registerRTSvalue > 1 || registerRTSvalue < 0 || setValue[1] != 0x0) { |
| 361 |
registerRTSvalue = 0;
|
| 362 |
printf("Not allowed value for RTS register. RTS=[0,1]\n");
|
| 363 |
return SB_FALSE;
|
| 364 |
} |
| 365 |
paramRTSfound = SB_TRUE; |
| 366 |
} |
| 367 |
/* is this the target number? */
|
| 368 |
else if ( (argv[paramIdx][0] == '-') && (argv[paramIdx][1] == 'T') && (paramTfound == SB_FALSE) && (paramXfound == SB_FALSE) && (paramDfound == SB_TRUE) && (paramBfound == SB_TRUE) ) |
| 369 |
{
|
| 370 |
if (countFlashingPrograms >= MAX_COUNT_FLASHING_PROGRAMS) {
|
| 371 |
printf("too many programs to flash (-T)\n");
|
| 372 |
return SB_FALSE;
|
| 373 |
} |
| 374 |
/* prog status has to be set in flashing mode */
|
| 375 |
paramTfound = SB_TRUE; |
| 376 |
prog_state = PROGSTATE_FLASHPROGRAM; |
| 377 |
sb_char choosenID[128];
|
| 378 |
strcpy(choosenID, &argv[paramIdx][2]);
|
| 379 |
/* Transform ascii numbers in string to an integer */
|
| 380 |
flashingTargetID = 0;
|
| 381 |
int idx=0; |
| 382 |
while (choosenID[idx] != 0x00) { |
| 383 |
if (flashingTargetID > 25 || ((int)choosenID[idx])-48 < 0 || ((int)choosenID[idx])-48 > 9) { |
| 384 |
flashingTargetID = 0;
|
| 385 |
printf("given flashing id too high\n");
|
| 386 |
return SB_FALSE;
|
| 387 |
} |
| 388 |
flashingTargetID *= 10;
|
| 389 |
if (flashingTargetID >= 250 && ((int)choosenID[idx])-48 > 5) { |
| 390 |
printf("given flashing id too high\n");
|
| 391 |
return SB_FALSE;
|
| 392 |
} |
| 393 |
flashingTargetID += ((int)choosenID[idx])-48; |
| 394 |
idx++; |
| 395 |
} |
| 396 |
flashingTargetIDs[countFlashingPrograms] = flashingTargetID; |
| 397 |
} |
| 398 |
/* is this a srec file (a parameter without "-" in the beginning) */
|
| 399 |
else if (argv[paramIdx][0] != '-') |
| 400 |
{
|
| 401 |
if (countFlashingPrograms >= MAX_COUNT_FLASHING_PROGRAMS) {
|
| 402 |
printf("too many programs to flash (without -T)\n");
|
| 403 |
return SB_FALSE;
|
| 404 |
} |
| 405 |
/* srecord file has been found */
|
| 406 |
if (paramTfound == SB_FALSE) {
|
| 407 |
paramTfound = SB_TRUE; |
| 408 |
flashingTargetIDs[countFlashingPrograms] = 0;
|
| 409 |
prog_state = PROGSTATE_ORIGIN; |
| 410 |
} |
| 411 |
srecordFileNames[countFlashingPrograms] = &(argv[paramIdx][0]);
|
| 412 |
countFlashingPrograms++; |
| 413 |
paramTfound = SB_FALSE; |
| 414 |
srecordfound = SB_TRUE; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
/* verify if all parameters were found */
|
| 419 |
if ( ( ((paramDfound == SB_FALSE) || (paramBfound == SB_FALSE)) && (paramAfound == SB_FALSE)) || (srecordfound == SB_FALSE) )
|
| 420 |
{
|
| 421 |
printf("-a, -d, -b or srecord file missing, remember that you can only choose -a or -d\n");
|
| 422 |
return SB_FALSE;
|
| 423 |
} |
| 424 |
|
| 425 |
/* still here so the parsing was successful */
|
| 426 |
return SB_TRUE;
|
| 427 |
} /*** end of ParseCommandLine ***/
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
static void printErrors(void) { |
| 434 |
sb_uint8 detected = 0;
|
| 435 |
sb_uint8 errorIdx; |
| 436 |
for (errorIdx=0; errorIdx<MAX_COUNT_FLASHING_PROGRAMS; errorIdx++) { |
| 437 |
if (errors[errorIdx] != ERROR_NO) {
|
| 438 |
detected++; |
| 439 |
} |
| 440 |
} |
| 441 |
if (detected == 0) { |
| 442 |
printf("\nEvery firmware successfully updated.\nSerialBoot finished without any errors.\n\n");
|
| 443 |
} else {
|
| 444 |
if (detected < countFlashingPrograms) {
|
| 445 |
printf("\n%i ERRORs detected!\nOnly in the following %i cases SerialBoot updated the firmware:\n", detected, countFlashingPrograms-detected);
|
| 446 |
for (errorIdx=0; errorIdx<countFlashingPrograms; errorIdx++) { |
| 447 |
if (errors[errorIdx] == ERROR_NO) {
|
| 448 |
printf(" * Flash %i: %s on device %i\n", errorIdx+1, srecordFileNames[errorIdx], flashingTargetIDs[errorIdx]); |
| 449 |
} |
| 450 |
} |
| 451 |
printf("In the following %i cases SerialBoot could not update the firmware:\n", detected);
|
| 452 |
} else {
|
| 453 |
printf("\nERRORs detected!\nEvery firmware update by SerialBoot failed:\n");
|
| 454 |
} |
| 455 |
for (errorIdx=0; errorIdx<countFlashingPrograms; errorIdx++) { |
| 456 |
if (errors[errorIdx] != ERROR_NO) {
|
| 457 |
printf(" * ERROR in Flash %i: ", errorIdx+1); |
| 458 |
switch (errors[errorIdx]) {
|
| 459 |
case ERROR_FILE:
|
| 460 |
printf("The file %s couldn't be read!\n", srecordFileNames[errorIdx]); break; |
| 461 |
case ERROR_DEVICE:
|
| 462 |
printf("No connection to device %i!\n", flashingTargetIDs[errorIdx]); break; |
| 463 |
default:
|
| 464 |
printf("Couldn't specify the error. Please check output above!\n"); break; |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
printf("Please check the errors before trying to update again.\n\n");
|
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
static sb_int32 startFlashMode(void) { |
| 477 |
|
| 478 |
/* -------------------- start flashing mode ---------------------------------------- */
|
| 479 |
printf("Set main device on flashing mode\n");
|
| 480 |
|
| 481 |
/* -------------------- open connection -------------------------------------------- */
|
| 482 |
if (buildConnection(1) == PROG_RESULT_ERROR) { |
| 483 |
return PROG_RESULT_ERROR;
|
| 484 |
} |
| 485 |
|
| 486 |
//printf("close...");
|
| 487 |
|
| 488 |
/* -------------------- close connection ------------------------------------------- */
|
| 489 |
if(closeConnection(NULL) == PROG_RESULT_ERROR) { |
| 490 |
return PROG_RESULT_ERROR;
|
| 491 |
} |
| 492 |
|
| 493 |
/* all done */
|
| 494 |
printf("Main device is now in flashing mode!\n\n");
|
| 495 |
|
| 496 |
return PROG_RESULT_OK;
|
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
static sb_int32 flashProgram(void) { |
| 502 |
sb_file hSrecord; |
| 503 |
tSrecordParseResults fileParseResults; |
| 504 |
tSrecordLineParseResults lineParseResults; |
| 505 |
|
| 506 |
/* -------------------- start the firmware update procedure ------------------------ */
|
| 507 |
printf("Starting firmware update for \"%s\" on device %i (0x", srecordFileName, flashingTargetID);
|
| 508 |
if (flashingTargetID < 16) |
| 509 |
{
|
| 510 |
printf("0");
|
| 511 |
} |
| 512 |
printf("%X)\nUsing %s @ %u bits/s\n", flashingTargetID, serialDeviceName, serialBaudrate);
|
| 513 |
|
| 514 |
/* -------------------- prepare programming session -------------------------------- */
|
| 515 |
if (prepareProgrammingSession(&hSrecord, &fileParseResults) == PROG_RESULT_ERROR) {
|
| 516 |
errorDetected = ERROR_FILE; |
| 517 |
return PROG_RESULT_ERROR;
|
| 518 |
} |
| 519 |
|
| 520 |
/* -------------------- open connection -------------------------------------------- */
|
| 521 |
if (buildConnection(0) == PROG_RESULT_ERROR) { |
| 522 |
errorDetected = ERROR_DEVICE; |
| 523 |
return PROG_RESULT_ERROR;
|
| 524 |
} |
| 525 |
|
| 526 |
/* -------------------- programming code ------------------------------------------- */
|
| 527 |
if (programCode(&hSrecord, &fileParseResults, &lineParseResults) == PROG_RESULT_ERROR) {
|
| 528 |
errorDetected = ERROR_UNKNOWN; |
| 529 |
return PROG_RESULT_ERROR;
|
| 530 |
} |
| 531 |
|
| 532 |
/* -------------------- close connection ------------------------------------------- */
|
| 533 |
if (closeConnection(&hSrecord) == PROG_RESULT_ERROR) {
|
| 534 |
errorDetected = ERROR_UNKNOWN; |
| 535 |
return PROG_RESULT_ERROR;
|
| 536 |
} |
| 537 |
|
| 538 |
/* -------------------- close the S-record file ------------------------------------ */
|
| 539 |
SrecordClose(hSrecord); |
| 540 |
printf("Closed S-record file \"%s\"\n", srecordFileName);
|
| 541 |
|
| 542 |
/* all done */
|
| 543 |
printf("Firmware successfully updated!\n\n");
|
| 544 |
|
| 545 |
return PROG_RESULT_OK;
|
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
static sb_int32 startUserProgram(void) { |
| 554 |
/* -------------------- start the user program ------------------------------------- */
|
| 555 |
printf("Resetting all using %s @ %u bits/s\n", serialDeviceName, serialBaudrate);
|
| 556 |
|
| 557 |
/* -------------------- open connection -------------------------------------------- */
|
| 558 |
if (buildConnection(0) == PROG_RESULT_ERROR) { |
| 559 |
return PROG_RESULT_ERROR;
|
| 560 |
} |
| 561 |
|
| 562 |
/* -------------------- close connection and reset --------------------------------- */
|
| 563 |
if (closeConnectionWithReset(NULL) == PROG_RESULT_ERROR) { |
| 564 |
return PROG_RESULT_ERROR;
|
| 565 |
} |
| 566 |
|
| 567 |
/* all done */
|
| 568 |
printf("User programs have been started!\n\n");
|
| 569 |
|
| 570 |
return PROG_RESULT_OK;
|
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
static sb_int32 originalMainPart(void) { |
| 577 |
sb_file hSrecord; |
| 578 |
tSrecordParseResults fileParseResults; |
| 579 |
tSrecordLineParseResults lineParseResults; |
| 580 |
|
| 581 |
/* -------------------- start the firmware update procedure ------------------------ */
|
| 582 |
printf("Starting firmware update for \"%s\" using %s @ %u bits/s\n", srecordFileName, serialDeviceName, serialBaudrate);
|
| 583 |
|
| 584 |
/* -------------------- prepare programming session -------------------------------- */
|
| 585 |
if (prepareProgrammingSession(&hSrecord, &fileParseResults) == PROG_RESULT_ERROR) {
|
| 586 |
return PROG_RESULT_ERROR;
|
| 587 |
} |
| 588 |
|
| 589 |
/* -------------------- open serial port and open connection ----------------------- */
|
| 590 |
if (openSerialPortConnect(0) == PROG_RESULT_ERROR) { |
| 591 |
return PROG_RESULT_ERROR;
|
| 592 |
} |
| 593 |
|
| 594 |
/* -------------------- open connection -------------------------------------------- */
|
| 595 |
// if (buildConnection(0) == PROG_RESULT_ERROR) {
|
| 596 |
// return PROG_RESULT_ERROR;
|
| 597 |
// }
|
| 598 |
|
| 599 |
/* -------------------- programming code ------------------------------------------- */
|
| 600 |
if (programCode(&hSrecord, &fileParseResults, &lineParseResults) == PROG_RESULT_ERROR) {
|
| 601 |
return PROG_RESULT_ERROR;
|
| 602 |
} |
| 603 |
|
| 604 |
/* -------------------- close connection and reset --------------------------------- */
|
| 605 |
if (closeConnectionWithReset(&hSrecord) == PROG_RESULT_ERROR) {
|
| 606 |
return PROG_RESULT_ERROR;
|
| 607 |
} |
| 608 |
|
| 609 |
/* -------------------- close the S-record file ------------------------------------ */
|
| 610 |
SrecordClose(hSrecord); |
| 611 |
printf("Closed S-record file \"%s\"\n", srecordFileName);
|
| 612 |
|
| 613 |
/* all done */
|
| 614 |
printf("Firmware successfully updated!\n\n");
|
| 615 |
|
| 616 |
return PROG_RESULT_OK;
|
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
static sb_int32 openSerialPort(void) { |
| 624 |
/* -------------------- Open the serial port --------------------------------------- */
|
| 625 |
// printf("Opening serial port %s...", serialDeviceName);
|
| 626 |
if (XcpMasterInit(serialDeviceName, serialBaudrate, comIsUart) == SB_FALSE)
|
| 627 |
{
|
| 628 |
printf("ERROR\n");
|
| 629 |
return PROG_RESULT_ERROR;
|
| 630 |
} |
| 631 |
// printf("OK\n");
|
| 632 |
return PROG_RESULT_OK;
|
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
static sb_int32 closeSerialPort(void) { |
| 640 |
/* -------------------- close the serial port -------------------------------------- */
|
| 641 |
XcpMasterDeinit(); |
| 642 |
return PROG_RESULT_OK;
|
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
static sb_int32 openSerialPortConnect(sb_uint8 start) {
|
| 650 |
/* -------------------- Connect to XCP slave --------------------------------------- */
|
| 651 |
// printf("Connecting to bootloader...");
|
| 652 |
sb_uint8 connections = 0;
|
| 653 |
if (XcpMasterInit(serialDeviceName, serialBaudrate, comIsUart) == SB_FALSE) {
|
| 654 |
printf("Could not open serial port\n");
|
| 655 |
} else {
|
| 656 |
if (XcpMasterConnect(0) == SB_FALSE) { |
| 657 |
connections++; |
| 658 |
/* no response. prompt the user to reset the system */
|
| 659 |
if (flashingTargetID == 0 && start > 0) { |
| 660 |
printf("Please connect main device to serial port %s...", serialDeviceName);
|
| 661 |
} else {
|
| 662 |
printf("Try to connect to device...");
|
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
/* now keep retrying until we get a response */
|
| 667 |
sb_uint16 timeoutIdx = 0;
|
| 668 |
sb_uint16 timeoutEnd = 60;
|
| 669 |
sb_uint8 noConnectionYet = SB_TRUE; |
| 670 |
while (noConnectionYet == SB_TRUE) {
|
| 671 |
if (XcpMasterInit(serialDeviceName, serialBaudrate, comIsUart) == SB_FALSE) {
|
| 672 |
if (timeoutIdx == timeoutEnd) {
|
| 673 |
printf("\nTIMEOUT in building connection. Please start serial port of the main device ...");
|
| 674 |
} |
| 675 |
} else {
|
| 676 |
if (XcpMasterConnect(0) == SB_FALSE) { |
| 677 |
if (timeoutIdx == timeoutEnd) {
|
| 678 |
printf("\nTIMEOUT in reset. Please reset the main device ...");
|
| 679 |
} |
| 680 |
} else {
|
| 681 |
noConnectionYet = SB_FALSE; |
| 682 |
} |
| 683 |
} |
| 684 |
/* delay a bit to not pump up the CPU load */
|
| 685 |
TimeUtilDelayMs(20);
|
| 686 |
timeoutIdx++; |
| 687 |
} |
| 688 |
if (connections > 0) { |
| 689 |
printf("OK\n");
|
| 690 |
} |
| 691 |
|
| 692 |
return PROG_RESULT_OK;
|
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
static sb_int32 buildConnection(sb_uint8 start) {
|
| 699 |
/* -------------------- Connect to XCP slave --------------------------------------- */
|
| 700 |
// printf("Connecting to bootloader...");
|
| 701 |
sb_uint8 connections = 0;
|
| 702 |
if (XcpMasterConnect(flashingTargetID) == SB_FALSE) {
|
| 703 |
connections++; |
| 704 |
/* no response. prompt the user to reset the system */
|
| 705 |
if (flashingTargetID == 0 && start > 0) { |
| 706 |
printf("Please connect main device to serial port %s...", serialDeviceName);
|
| 707 |
} else if (flashingTargetID == 0) { |
| 708 |
printf("Try to connect to device...");
|
| 709 |
} else {
|
| 710 |
printf("Try to connect to device 0x");
|
| 711 |
if (flashingTargetID < 16) { |
| 712 |
printf("0");
|
| 713 |
} |
| 714 |
printf("%X...", flashingTargetID);
|
| 715 |
} |
| 716 |
} |
| 717 |
/* now keep retrying until we get a response */
|
| 718 |
sb_uint16 timeoutIdx = 0;
|
| 719 |
sb_uint16 timeoutEnd = 50;
|
| 720 |
while (XcpMasterConnect(flashingTargetID) == SB_FALSE) {
|
| 721 |
if (timeoutIdx == timeoutEnd) {
|
| 722 |
printf("\nTIMEOUT: No connection possible.\n\n");
|
| 723 |
return PROG_RESULT_ERROR;
|
| 724 |
} |
| 725 |
/* delay a bit to not pump up the CPU load */
|
| 726 |
TimeUtilDelayMs(20);
|
| 727 |
timeoutIdx++; |
| 728 |
} |
| 729 |
if (connections > 0) { |
| 730 |
printf("OK\n");
|
| 731 |
} |
| 732 |
|
| 733 |
return PROG_RESULT_OK;
|
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
static sb_int32 closeConnection(sb_file *hSrecord) {
|
| 740 |
/* -------------------- Disconnect from XCP slave and perform software reset ------- */
|
| 741 |
// printf("Performing software reset...");
|
| 742 |
if (XcpMasterDisconnect() == SB_FALSE)
|
| 743 |
{
|
| 744 |
printf("ERROR\n");
|
| 745 |
if (hSrecord != NULL) { |
| 746 |
SrecordClose(*hSrecord); |
| 747 |
} |
| 748 |
return PROG_RESULT_ERROR;
|
| 749 |
} |
| 750 |
// printf("OK\n");
|
| 751 |
|
| 752 |
return PROG_RESULT_OK;
|
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
static sb_int32 closeConnectionWithReset(sb_file *hSrecord) {
|
| 759 |
/* -------------------- Disconnect from XCP slave ---------------------------------- */
|
| 760 |
printf("Resetting...");
|
| 761 |
if (XcpMasterProgramReset() == SB_FALSE)
|
| 762 |
{
|
| 763 |
printf("ERROR\n");
|
| 764 |
XcpMasterDisconnect(); |
| 765 |
if (hSrecord != NULL) { |
| 766 |
SrecordClose(*hSrecord); |
| 767 |
} |
| 768 |
return PROG_RESULT_ERROR;
|
| 769 |
} |
| 770 |
printf("OK\n");
|
| 771 |
|
| 772 |
closeSerialPort(); |
| 773 |
printf("Closed serial port %s\n", serialDeviceName);
|
| 774 |
|
| 775 |
return PROG_RESULT_OK;
|
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
static sb_int32 prepareProgrammingSession(sb_file *hSrecord, tSrecordParseResults *fileParseResults) {
|
| 782 |
/* -------------------- validating the S-record file ------------------------------- */
|
| 783 |
printf("Checking formatting of S-record file \"%s\"...", srecordFileName);
|
| 784 |
if (SrecordIsValid(srecordFileName) == SB_FALSE)
|
| 785 |
{
|
| 786 |
printf("ERROR\n\n");
|
| 787 |
return PROG_RESULT_ERROR;
|
| 788 |
} |
| 789 |
printf("OK\n");
|
| 790 |
|
| 791 |
/* -------------------- opening the S-record file ---------------------------------- */
|
| 792 |
printf("Opening S-record file \"%s\"...", srecordFileName);
|
| 793 |
if ((*hSrecord = SrecordOpen(srecordFileName)) == SB_NULL)
|
| 794 |
{
|
| 795 |
printf("ERROR\n\n");
|
| 796 |
return PROG_RESULT_ERROR;
|
| 797 |
} |
| 798 |
printf("OK\n");
|
| 799 |
|
| 800 |
/* -------------------- parsing the S-record file ---------------------------------- */
|
| 801 |
printf("Parsing S-record file \"%s\"...", srecordFileName);
|
| 802 |
SrecordParse(*hSrecord, fileParseResults); |
| 803 |
printf("OK\n");
|
| 804 |
printf("-> Lowest memory address: 0x%08x\n", fileParseResults->address_low);
|
| 805 |
printf("-> Highest memory address: 0x%08x\n", fileParseResults->address_high);
|
| 806 |
printf("-> Total data bytes: %u\n", fileParseResults->data_bytes_total);
|
| 807 |
|
| 808 |
return PROG_RESULT_OK;
|
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
static sb_int32 programCode(sb_file *hSrecord, tSrecordParseResults *fileParseResults, tSrecordLineParseResults *lineParseResults) {
|
| 815 |
/* -------------------- Prepare the programming session ---------------------------- */
|
| 816 |
printf("Initializing programming session...");
|
| 817 |
if (XcpMasterStartProgrammingSession() == SB_FALSE)
|
| 818 |
{
|
| 819 |
printf("ERROR\n");
|
| 820 |
XcpMasterDisconnect(); |
| 821 |
SrecordClose(*hSrecord); |
| 822 |
return PROG_RESULT_ERROR;
|
| 823 |
} |
| 824 |
printf("OK\n");
|
| 825 |
|
| 826 |
/* -------------------- Erase memory ----------------------------------------------- */
|
| 827 |
printf("Erasing %u bytes starting at 0x%08x...", fileParseResults->data_bytes_total, fileParseResults->address_low);
|
| 828 |
if (XcpMasterClearMemory(fileParseResults->address_low, (fileParseResults->address_high - fileParseResults->address_low)) == SB_FALSE)
|
| 829 |
{
|
| 830 |
printf("ERROR\n");
|
| 831 |
XcpMasterDisconnect(); |
| 832 |
SrecordClose(*hSrecord); |
| 833 |
return PROG_RESULT_ERROR;
|
| 834 |
} |
| 835 |
printf("OK\n");
|
| 836 |
|
| 837 |
/* -------------------- Program data ----------------------------------------------- */
|
| 838 |
printf("Programming data. Please wait...");
|
| 839 |
/* loop through all S-records with program data */
|
| 840 |
while (SrecordParseNextDataLine(*hSrecord, lineParseResults) == SB_TRUE)
|
| 841 |
{
|
| 842 |
if (XcpMasterProgramData(lineParseResults->address, lineParseResults->length, lineParseResults->data) == SB_FALSE)
|
| 843 |
{
|
| 844 |
printf("ERROR\n");
|
| 845 |
XcpMasterDisconnect(); |
| 846 |
SrecordClose(*hSrecord); |
| 847 |
return PROG_RESULT_ERROR;
|
| 848 |
} |
| 849 |
} |
| 850 |
printf("OK\n");
|
| 851 |
|
| 852 |
/* -------------------- Stop the programming session ------------------------------- */
|
| 853 |
printf("Finishing programming session...");
|
| 854 |
if (XcpMasterStopProgrammingSession() == SB_FALSE)
|
| 855 |
{
|
| 856 |
printf("ERROR\n");
|
| 857 |
XcpMasterDisconnect(); |
| 858 |
SrecordClose(*hSrecord); |
| 859 |
return PROG_RESULT_ERROR;
|
| 860 |
} |
| 861 |
printf("OK\n");
|
| 862 |
|
| 863 |
return PROG_RESULT_OK;
|
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
/*********************************** end of main.c *************************************/
|