-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.c
248 lines (229 loc) · 6.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* STM32ISP - CLI tool for ST STM32F10x In-System Programming
*
* copyright (c) 2011 BA5AG ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32.h"
#include "hexfile.h"
#include "memmap.h"
#include "term.h"
typedef struct {
char* portName;
int baudrate;
char* fileName;
int isBin;
int reset;
int bootp;
int termBaud;
char* logFile;
int isDetect;
int isTerm;
int isEcho;
int isLine;
int isVerbose;
} Config;
static void prompt()
{
printf(
"Syntax: stm32isp [Options] <file> <comport> <baudrate>\n"
"\n"
"Example: stm32isp test.hex /dev/ttyS0 115200\n"
"\n"
"Options: -bin for uploading binary file\n"
" -reset [-]DTR|RTS reset control line, default DTR\n"
" -bootp [-]DTR|RTS bootp control line, default RTS\n"
" -term <baud> for starting terminal after upload with buadrate, default 9600\n"
" -detectonly detect chip set only\n"
" -termonly for starting terminal without an upload\n"
" -noreset do not reset the board in termonly mode\n"
" -echo local echo in term\n"
" -line local line edit in term\n"
" -verbose for more information\n"
" -log <file name> for enabling logging of terminal output to <file name>\n"
);
}
static int parsePin(char* s)
{
int ret = 0;
if ( strstr(s, "DTR") ) {
ret = DTR;
} else if ( strstr(s, "RTS") ) {
ret = RTS;
}
if ( s[0] == '-' )
ret = -ret;
return ret;
}
static void dumpOptions(Config *p)
{
printf("Options:\n");
printf("\tfile name:%s\n", p->fileName);
printf("\tfile type:%s\n", p->isBin?"bin":"hex");
printf("\tserial port:%s\n", p->portName);
printf("\tbaud rate:%d\n", p->baudrate);
if ( p->reset >0 ) {
printf("\treset use:%s%s\n", p->reset<0?"-":"", abs(p->reset)==DTR?"DTR":"RTS");
}
if ( p->bootp >0 ) {
printf("\tbootp use:%s%s\n", p->bootp<0?"-":"", abs(p->bootp)==DTR?"DTR":"RTS");
}
if ( p->termBaud >0 ) {
printf("\tterm baud:%d\n", p->termBaud);
} else {
printf("\tterm no\n");
}
if ( p->isDetect ) {
printf("\tdetect chip set only\n");
}
if ( p->isTerm ) {
printf("\tterm only\n");
}
if ( p->isEcho ) {
printf("\tlocal echo in term\n");
}
if ( p->isLine ) {
printf("\tlocal line edit in term\n");
}
if ( p->isVerbose ) {
printf("\tin verbose mode\n");
}
}
static int verifyCondig(Config *p)
{
int ret = 1;
if ( p->baudrate <1200 || p->baudrate >115200 ) {
printf("Not valid baudrate:%d\n", p->baudrate);
ret = 0;
} else if ( p->termBaud >115200 ) {
printf("Not valid baudrate:%d for term\n", p->termBaud);
ret = 0;
} else if ( p->isDetect && p->isTerm ) {
ret = 0;
}
return ret;
}
static int parseOptions(int argc, char* argv[], Config* p)
{
int i;
int argidx = 0;
int ret = 0;
memset(p, 0, sizeof(Config)); // init the config
for ( i=1; i< argc; i++ ) {
if ( argv[i][0] == '-' ) { // an option
if ( strcmp(argv[i], "-bin") ==0 ) {
p->isBin = 1;
} else if ( strcmp(argv[i], "-reset") ==0 ) {
p->reset = parsePin(argv[i+1]);
i++;
} else if ( strcmp(argv[i], "-bootp") ==0 ) {
p->bootp = parsePin(argv[i+1]);
i++;
} else if ( strcmp(argv[i], "-term") ==0 ) {
sscanf(argv[i+1], "%d", &(p->termBaud));
i++;
} else if ( strcmp(argv[i], "-detectonly") ==0 ) {
p->isDetect = 1;
} else if ( strcmp(argv[i], "-termonly") ==0 ) {
p->isTerm = 1;
} else if ( strcmp(argv[i], "-echo") ==0 ) {
p->isEcho = 1;
} else if ( strcmp(argv[i], "-line") ==0 ) {
p->isLine = 1;
} else if ( strcmp(argv[i], "-verbose") ==0 ) {
p->isVerbose = 1;
} else if ( strcmp(argv[i], "-log") ==0 ) {
p->logFile = argv[i+1];
i++;
} else {
printf("Unknown option: %s\n", argv[i]);
ret = 0;
break;
}
} else {
switch ( argidx ) {
case 0:
p->fileName = argv[i];
break;
case 1:
p->portName = argv[i];
break;
case 2:
sscanf(argv[i], "%d", &(p->baudrate));
break;
}
argidx ++;
}
}
if ( argidx == 3 ) {
if ( p->isVerbose ) {
dumpOptions(p);
}
ret = 1;
}
return ret;
}
int main(int argc, char *argv[])
{
Config config;
memset((void*)&config, 0, sizeof(config));
config.baudrate = 115200;
config.termBaud = 9600;
config.bootp = RTS;
config.reset = DTR;
printf(
"Portable command line ISP for STM32F10x family\n"
"Version 0.2 compiled for Apple MacOS X: "__DATE__ " " __TIME__ "\n"
"Copyright (c) by Weng Kai, 2011-2018, Email: [email protected]\n"
"\n");
if ( parseOptions(argc, argv, &config) ==0 || verifyCondig(&config) ==0 ) {
prompt();
exit(-1);
}
// term(config.portName,19200,config.bootp, config.reset, config.isEcho, config.isLine);
if ( !config.isTerm ) {
if ( stm32Init(config.portName, config.baudrate, config.bootp, config.reset) == STM_OK ) {
stm32CmdGet();
// stm32GetVersion();
stm32GetID();
if ( !config.isDetect ) {
MemMap mp;
if ( readHexFile(config.fileName, &mp) ) {
stm32Erase();
stm32Write(&mp);
stm32Verify(&mp);
stm32Go(0x08000000);
stm32Close();
mmapDelete(&mp);
printf("STM32isp finished.\n");
} else {
printf("Can not open file %s.\n", config.fileName);
}
}
} else {
printf("Can not connect to board.\n");
}
}
if ( config.termBaud >0 ) {
printf("Starting term.\n");
term(config.portName,config.termBaud,config.bootp, config.reset, config.isEcho, config.isLine);
}
return 0;
}