Statistics
| Branch: | Tag: | Revision:

amiro-blt / Host / Source / SerialBoot / main.c @ 43464dd5

History | View | Annotate | Download (32.8 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 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
/****************************************************************************************
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 d54d2f07 Thomas Schöpping
#include "stdlib.h"                                   /* ascii to integral conversion  */
46 69661903 Thomas Schöpping
47
48
/****************************************************************************************
49
* Function prototypes
50
****************************************************************************************/
51
static void     DisplayProgramInfo(void);
52
static void     DisplayProgramUsage(void);
53
static void     DisplayProgramInput(void);
54
static void     printErrors(void);
55
static sb_uint8 ParseCommandLine(sb_int32 argc, sb_char *argv[]);
56
static sb_int32 startFlashMode(void);
57
static sb_int32 flashProgram(void);
58
static sb_int32 startUserProgram(void);
59
static sb_int32 originalMainPart(void);
60
static sb_int32 openSerialPort(void);
61
static sb_int32 openSerialPortConnect(sb_uint8 start);
62
static sb_int32 closeSerialPort(void);
63
static sb_int32 buildConnection(sb_uint8 start);
64
static sb_int32 closeConnection(sb_file *hSrecord);
65
static sb_int32 closeConnectionWithReset(sb_file *hSrecord);
66
static sb_int32 prepareProgrammingSession(sb_file *hSrecord, tSrecordParseResults *fileParseResults);
67
static sb_int32 programCode(sb_file *hSrecord, tSrecordParseResults *fileParseResults, tSrecordLineParseResults *lineParseResults);
68
69
/****************************************************************************************
70
* Macro definitions
71
****************************************************************************************/
72
/** \brief Program return code if all went ok. */
73
#define PROG_RESULT_OK    (0)
74
75
/** \brief Program return code if an error occurred. */
76
#define PROG_RESULT_ERROR (1)
77
78
/* mode definitions of the program status */
79
#define PROGSTATE_ORIGIN           0
80
#define PROGSTATE_STARTFLASHMODE   1
81
#define PROGSTATE_RESET            2
82
#define PROGSTATE_FLASHPROGRAM     3
83
#define PROGSTATE_SETFLAGS         4
84
85
/* error definitions */
86
#define ERROR_NO                   0
87
#define ERROR_FILE                 1
88
#define ERROR_DEVICE               2
89
#define ERROR_UNKNOWN              255
90
91
/* maximal space for flashing programs */
92
#define MAX_COUNT_FLASHING_PROGRAMS 5
93
94
95
/****************************************************************************************
96
* Local data declarations
97
****************************************************************************************/
98 03906dc3 Thomas Schöpping
/** \brief Name of the serial device, such as COM4, /dev/ttyUSB0 or /dev/ttyAMiRo0. */
99 d54d2f07 Thomas Schöpping
static sb_char serialDeviceName[32];
100 69661903 Thomas Schöpping
101
/** \brief Id of the bluetooth device, such as 01:23:45:67:89:AB. */
102 d54d2f07 Thomas Schöpping
static sb_char bluetoothID[18];
103 69661903 Thomas Schöpping
104
/** \brief Serial communication speed in bits per second. */
105 d54d2f07 Thomas Schöpping
static sb_uint32 serialBaudrate;
106 69661903 Thomas Schöpping
107
/** \brief Name of the S-record file. */
108 d54d2f07 Thomas Schöpping
static sb_char *srecordFileName;
109 69661903 Thomas Schöpping
110
/* program status */
111
static sb_uint8 prog_state = 1;
112
113
/* id of flashing target */
114 d54d2f07 Thomas Schöpping
static sb_uint32 flashingTargetID = 0;
115 69661903 Thomas Schöpping
116
/* list of program names */
117
static sb_char *srecordFileNames[MAX_COUNT_FLASHING_PROGRAMS];
118
119
/* list of target ids */
120 d54d2f07 Thomas Schöpping
static sb_uint32 flashingTargetIDs[MAX_COUNT_FLASHING_PROGRAMS];
121 69661903 Thomas Schöpping
122
/* list of errors */
123
static sb_uint8 errors[MAX_COUNT_FLASHING_PROGRAMS];
124
125
/* last error */
126
static sb_uint8 errorDetected = ERROR_NO;
127
128
/* program counter */
129
static sb_uint8 countFlashingPrograms = 0;
130
131
/* RTS register value */
132
static sb_uint8 registerRTSvalue = 0;
133
134
/* Type of communication is about UART, not Bluetooth */
135
static sb_uint8 comIsUart = SB_TRUE;
136
137
138
/************************************************************************************//**
139
** \brief     Program entry point.
140
** \param     argc Number of program parameters.
141
** \param     argv array to program parameter strings.
142
** \return    0 on success, > 0 on error.
143
**
144
****************************************************************************************/
145
sb_int32 main(sb_int32 argc, sb_char *argv[])
146
{
147
  /* disable buffering for the standard output to make sure printf does not wait until
148
   * a newline character is detected before outputting text on the console.
149
   */
150
  setbuf(stdout, SB_NULL);
151
152
  /* inform user about the program */
153
  DisplayProgramInfo();
154
155
  /* start out by making sure program was started with the correct parameters */
156
  if (ParseCommandLine(argc, argv) == SB_FALSE)
157
  {
158
    /* parameters invalid. inform user about how this program works */
159
    DisplayProgramUsage();
160
    return PROG_RESULT_ERROR;
161
  }
162
163
  DisplayProgramInput();
164
165
  if (openSerialPortConnect(0) == PROG_RESULT_ERROR) {
166
    return PROG_RESULT_ERROR;
167
  }
168
169
  /* initialize errors */
170
  sb_uint8 errorIdx;
171
  for (errorIdx=0; errorIdx<MAX_COUNT_FLASHING_PROGRAMS; errorIdx++) {
172
    errors[errorIdx] = ERROR_NO;
173
  }
174
175
  flashingTargetID = 0;
176
  if (startFlashMode() == PROG_RESULT_ERROR) {
177
    return PROG_RESULT_ERROR;
178
  }
179
180
  sb_uint8 flashIdx;
181
  for (flashIdx=0; flashIdx<countFlashingPrograms; flashIdx++) {
182
    errorDetected = ERROR_NO;
183
    flashingTargetID = flashingTargetIDs[flashIdx];
184
    srecordFileName = srecordFileNames[flashIdx];
185 d54d2f07 Thomas Schöpping
    printf("\nFlash %i: Flashing %s on device 0x%08X\n\n", flashIdx+1, srecordFileName, flashingTargetID);
186
187 69661903 Thomas Schöpping
    if (flashProgram() == PROG_RESULT_ERROR) {
188
      closeSerialPort();
189
      openSerialPortConnect(0);
190
    }
191
    errors[flashIdx] = errorDetected;
192
  }
193
194
  flashingTargetID = 0;
195
  if (startUserProgram() == PROG_RESULT_ERROR) {
196
    return PROG_RESULT_ERROR;
197
  }
198
199
  printErrors();
200
201
202
  return PROG_RESULT_OK;
203
} /*** end of main ***/
204
205
206
/************************************************************************************//**
207
** \brief     Outputs information to the user about this program.
208
** \return    none.
209
**
210
****************************************************************************************/
211
static void DisplayProgramInfo(void)
212
{
213
  printf("---------------------------------------------------------------------------\n");
214 ac7f321b Thomas Schöpping
  printf("SerialBoot version 1.01. Performs firmware updates via the serial port     \n");
215 4cce70a8 Thomas Schöpping
  printf("for a microcontroller based system that runs the OpenBLT bootloader.       \n");
216
  printf("                                                                           \n");
217
  printf("Copyright (c) by Feaser  http://www.feaser.com                             \n");
218 69661903 Thomas Schöpping
  printf("---------------------------------------------------------------------------\n");
219 4cce70a8 Thomas Schöpping
  printf("This tool was modified for the 'Autonomous Mini Robot' - AMiRo.            \n");
220 94886d84 Thomas Schöpping
  printf("Copyright (c) 2016..2020  Marvin Barther, Thomas Schoepping, and Stefan    \n");
221 4cce70a8 Thomas Schöpping
  printf("                          Herbrechtsmeier                                  \n");
222
  printf("This is free software; see the source for copying conditions. There is NO  \n");
223<