-
Notifications
You must be signed in to change notification settings - Fork 1
/
p1load.c
306 lines (279 loc) · 8.79 KB
/
p1load.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include "port.h"
#include "ploader.h"
#include "osint.h"
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
/* port prefix */
#if defined(CYGWIN) || defined(WIN32) || defined(MINGW)
#define PORT_PREFIX ""
#elif defined(LINUX)
#ifdef RASPBERRY_PI
#define PORT_PREFIX "ttyAMA"
#else
#define PORT_PREFIX "ttyUSB"
#endif
#elif defined(MACOSX)
#define PORT_PREFIX "cu.usbserial-"
#else
#define PORT_PREFIX ""
#endif
/* defaults */
#define BAUD_RATE 115200
/* constants */
#define HUB_MEMORY_SIZE 32768
static PL_state state;
static void Usage(void);
static uint8_t *ReadEntireFile(char *name, long *pSize);
int main(int argc, char *argv[])
{
char actualPort[PATH_MAX], *var, *val, *port, *p;
int baudRate, baudRate2, verbose, terminalMode, pstMode, i;
int loadType = LOAD_TYPE_RUN;
int loadTypeOptionSeen = FALSE;
int actionSpecified = FALSE;
char *file = NULL;
long imageSize;
uint8_t *image;
/* initialize */
baudRate = baudRate2 = BAUD_RATE;
verbose = terminalMode = pstMode = FALSE;
port = NULL;
/* initialize the loader port */
InitPortState(&state);
/* process the position-independent arguments */
for (i = 1; i < argc; ++i) {
/* handle switches */
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'b':
if (argv[i][2])
p = &argv[i][2];
else if (++i < argc)
p = argv[i];
else
Usage();
if (*p != ':')
baudRate = baudRate2 = atoi(p);
if ((p = strchr(p, ':')) != NULL) {
if (*++p != ':' && *p != '\0')
baudRate2 = atoi(p);
}
break;
case 'D':
if (argv[i][2])
p = &argv[i][2];
else if (++i < argc)
p = argv[i];
else
Usage();
if ((var = strtok(p, "=")) != NULL) {
if ((val = strtok(NULL, "")) != NULL) {
if (strcmp(var, "reset") == 0)
use_reset_method(val);
else
Usage();
}
else
Usage();
}
else
Usage();
break;
case 'e':
if (!loadTypeOptionSeen) {
loadTypeOptionSeen = TRUE;
loadType = 0;
}
loadType |= LOAD_TYPE_EEPROM;
break;
case 'p':
if (argv[i][2])
port = &argv[i][2];
else if (++i < argc)
port = argv[i];
else
Usage();
#if defined(CYGWIN) || defined(WIN32) || defined(LINUX)
if (isdigit((int)port[0])) {
#if defined(CYGWIN) || defined(WIN32)
static char buf[10];
sprintf(buf, "COM%d", atoi(port));
port = buf;
#endif
#if defined(LINUX)
static char buf[64];
sprintf(buf, "/dev/%s%d", PORT_PREFIX, atoi(port));
port = buf;
#endif
}
#endif
#if defined(MACOSX)
if (port[0] != '/') {
static char buf[64];
sprintf(buf, "/dev/%s-%s", PORT_PREFIX, port);
port = buf;
}
#endif
break;
case 'P':
ShowPorts(&state, PORT_PREFIX);
actionSpecified = TRUE;
break;
case 'r':
if (!loadTypeOptionSeen) {
loadTypeOptionSeen = TRUE;
loadType = 0;
}
loadType |= LOAD_TYPE_RUN;
break;
case 'T':
pstMode = TRUE;
// fall through
case 't':
terminalMode = TRUE;
actionSpecified = TRUE;
break;
case 'v':
verbose = TRUE;
break;
case '?':
/* fall through */
default:
Usage();
break;
}
}
/* handle the input filename */
else {
if (file)
Usage();
file = argv[i];
actionSpecified = TRUE;
}
}
/* complain about nothing to do */
if (!actionSpecified) {
printf("error: must specify either a file to load or -t\n");
return 1;
}
/* open the serial port */
if (file || terminalMode) {
switch (InitPort(&state, PORT_PREFIX, port, baudRate, verbose, actualPort)) {
case CHECK_PORT_OK:
printf("Found propeller version %d on %s\n", state.version, actualPort);
break;
case CHECK_PORT_OPEN_FAILED:
printf("error: opening serial port '%s'\n", port);
perror("Error is ");
return 1;
case CHECK_PORT_NO_PROPELLER:
if (port)
printf("error: no propeller chip on port '%s'\n", port);
else
printf("error: can't find a port with a propeller chip\n");
return 1;
}
}
/* check for a file to load */
if (file) {
/* read the entire file into a buffer */
if (!(image = ReadEntireFile(file, &imageSize))) {
printf("error: reading '%s'\n", file);
return 1;
}
/* make sure the file isn't too big for hub memory */
if (imageSize > HUB_MEMORY_SIZE) {
printf("error: image too big for hub memory\n");
return 1;
}
/* load the file from the memory buffer */
printf("Loading '%s' (%ld bytes)\n", file, imageSize);
switch (PL_LoadSpinBinary(&state, loadType, image, imageSize)) {
case LOAD_STS_OK:
printf("OK\n");
break;
case LOAD_STS_ERROR:
printf("Error\n");
return 1;
case LOAD_STS_TIMEOUT:
printf("Timeout\n");
return 1;
default:
printf("Internal error\n");
return 1;
}
}
/* enter terminal mode if requested */
if (terminalMode) {
printf("[ Entering terminal mode. Type ESC or Control-C to exit. ]\n");
fflush(stdout);
if (baudRate2 != baudRate)
serial_baud(baudRate2);
terminal_mode(FALSE, pstMode);
}
return 0;
}
/* Usage - display a usage message and exit */
static void Usage(void)
{
printf("\
p1load - a simple loader for the propeller - %s, %s\n\
usage: p1load\n\
[ -b baud ] baud rate (default is %d)\n\
[ -D var=val ] set variable value\n\
[ -e ] write a bootable image to EEPROM\n\
[ -p port ] serial port (default is to auto-detect the port)\n\
[ -P ] list available serial ports\n\
[ -r ] run the program after loading (default)\n\
[ -t ] enter terminal mode after running the program\n\
[ -T ] enter PST-compatible terminal mode\n\
[ -v ] verbose output\n\
[ -? ] display a usage message and exit\n\
file file to load\n", VERSION, __DATE__, BAUD_RATE);
#ifdef RASPBERRY_PI
printf("\
\n\
This version supports resetting the Propeller with a GPIO pin with option: -Dreset=gpio,pin,level\n\
where \"pin\" is the GPIO number to use and \"level\" is the logic level, 0 or 1. This defaults to\n\
GPIO 17 and level 0.\n\
");
#endif
exit(1);
}
/* ReadEntireFile - read an entire file into an allocated buffer */
static uint8_t *ReadEntireFile(char *name, long *pSize)
{
uint8_t *buf;
long size;
FILE *fp;
/* open the file in binary mode */
if (!(fp = fopen(name, "rb")))
return NULL;
/* get the file size */
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
/* allocate a buffer for the file contents */
if (!(buf = (uint8_t *)malloc(size))) {
fclose(fp);
return NULL;
}
/* read the contents of the file into the buffer */
if (fread(buf, 1, size, fp) != size) {
free(buf);
fclose(fp);
return NULL;
}
/* close the file ad return the buffer containing the file contents */
*pSize = size;
fclose(fp);
return buf;
}