Statistics
| Branch: | Tag: | Revision:

amiro-blt / Host / Source / SerialBoot / main.c @ 69661903

History | View | Annotate | Download (32.075 KB)

1 69661903 Thomas Schöpping
/************************************************************************************//**
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\n");
214
  printf("Copyright (c) by Feaser  http://www.feaser.com\n");
215
  printf("---------------------------------------------------------------------------\n");
216
  printf("This tool was modified for the 'Autonomous Mini Robot' - AMiRo.\n");
217
  printf("Copyright (c) 2016 by Marvin Barther, Thomas Schoepping, and Stefan\n");
218
  printf("Herbrechtsmeier.\n");
219
  printf("This is free software; see the source for copying conditions. There is NO\n");
220
  printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
221
  printf("The development of this software was supported by the Excellence Cluster\n");
222
  printf("EXC 227 Cognitive Interaction Technology. The Excellence Cluster EXC 227 is\n");
223
  printf("a grant of the Deutsche Forschungsgemeinschaft (DFG) in the context of the\n");
224
  printf("German Excellence Initiative.\n");
225
  printf("---------------------------------------------------------------------------\n");
226
} /*** end of DisplayProgramInfo ***/
227
228
229
/************************************************************************************//**
230
** \brief     Outputs information to the user about how to use this program.
231
** \return    none.
232
**
233
****************************************************************************************/
234
static void DisplayProgramUsage(void)
235
{
236<