Menu

[r25]: / trunk / uploader / src / main.cpp  Maximize  Restore  History

Download this file

209 lines (186 with data), 5.5 kB

  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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <direct.h>
extern "C" {
#include "syspil.h"
#include "processpil.h"
}
#include "sketchbuild.h"
using namespace std;
char* appdir;
int main(int argc, char* argv[])
{
char* filepath;
const char* serial = 0;
char *p;
int ret = 0;
int boardIndex = 0;
int ispBaudrate = 0;
char* corefile = 0;
char* libfile = 0;
BOARD_CONFIG* board = boards;
CArduinoBuilder builder;
cout << "Arduino Compiler & Uploader Version " << BUILDER_VERSION << endl << "(C)2013 Developed by Stanley Huang <stanleyhuangyc@gmail.com>" << endl << "Distributed under GPL license" << endl << endl;
if (argc < 2) {
int i;
cout << "Command line syntax:" << endl << argv[0] << " [sketch/hex file] [board type] [serial port/usbasp] [MCU frequency in MHz]" << endl << endl
<< "Board types:" << endl;
for (i = 0; boards[i].name; i++) {
cout << (i + 1) << " - " << boards[i].name << endl;
}
cout << endl;
return -1;
}
// get app dir
appdir = strdup(argv[0]);
p = strrchr(appdir, '/');
if (!p) p = strrchr(appdir, '\\');
if (p)
*(p + 1) = 0;
else
*appdir = 0;
SetCurrentDirectory(appdir);
#ifdef WIN32
// set toolchain path
char* pathenv = new char[4096];
GetEnvironmentVariable("PATH", pathenv, 4096 - strlen(appdir) - 16);
strcat(pathenv, ";");
strcat(pathenv, appdir);
strcat(pathenv, "bin");
SetEnvironmentVariable("PATH", pathenv);
delete pathenv;
#endif
filepath = argv[1];
p = filepath + strlen(filepath) - 1;
if (*p == '.') *(p--) = 0;
if (*p == '/' || *p == '\\') *p = 0;
if (IsDir(filepath)) {
// building core and library mode
builder.hexfile = 0;
} else {
builder.hexfile = new char[strlen(filepath) + 5];
sprintf(builder.hexfile, "%s.hex", filepath);
}
if (argc > 2) {
int boardCount;
for (boardCount = 0; boards[boardCount].name; boardCount++);
if (isdigit(argv[2][0])) {
boardIndex = atoi(argv[2]);
boardIndex--;
} else {
for (boardIndex = 0; boardIndex < boardCount; boardIndex++) {
if (strstr(boards[boardIndex].id, argv[2])) break;
}
}
if (boardIndex < 0 || boardIndex >= boardCount) {
cout << "No valid board selected." << endl;
return -2;
}
}
board = boards + boardIndex;
if (argc > 3) {
serial = argv[3];
if (!strcmp(serial, "-")) serial = 0;
}
if (argc > 4)
builder.freq =atoi(argv[4]);
else
builder.freq = 16;
if (argc > 5)
ispBaudrate = atoi(argv[5]);
if (argc > 6) libfile = argv[6];
if (argc > 7) corefile = argv[7];
if (libfile) {
// create library file dir
char *s = _strdup(libfile);
char *p = strrchr(s, '/');
if (!p) p = strrchr(s, '\\');
if (p) {
*p = 0;
if (!IsDir(s)) _mkdir(s);
}
free(s);
}
// initialize builder
if (boardIndex >= 0) {
builder.board = boards + boardIndex;
}
if (!strstr(filepath, ".hex") && !strstr(filepath, ".HEX")) {
char* buildDir = new char [strlen(filepath) + 1];
if (builder.hexfile) {
strcpy(buildDir, filepath);
p = strrchr(buildDir, '/');
if (!p) p = strrchr(buildDir, '\\');
if (p) {
*p = 0;
} else {
strcpy(buildDir, ".");
}
remove(builder.hexfile);
} else {
strcpy(buildDir, filepath);
}
if (strstr(filepath, ".elf")) {
builder.proc.flags = SF_REDIRECT_STDOUT;
if (!builder.ConvertELFtoHEX(filepath)) {
builder.ConsoleOutput("Error generating HEX file");
ret = ERROR_SIZE;
}
} else {
builder.ConsoleOutput("Build Target: %s ", builder.board->name);
builder.ConsoleOutput("(MCU: %s)\r\n", builder.board->mcu);
builder.sketchPath = filepath;
builder.buildDir = buildDir;
if (corefile) {
builder.corefile = corefile;
} else if (IsDir("cores")) {
builder.corefile = new char[128];
sprintf(builder.corefile, "cores/core_%s_%d.a", builder.board->id, builder.freq);
} else {
builder.corefile = 0;
}
builder.libfile = libfile;
ret = builder.BuildSketch();
builder.proc.iRetCode = ret;
if (builder.hexfile && !IsFileExist(builder.hexfile)) {
cout << endl << "Error occurred during compiliation" << endl;
if (ret == 0) ret = -1;
}
cout << endl;
}
if (ret == SUCCESS && serial) {
char* eepfile = 0;
builder.ConsoleOutput("Starting upload for %s ", builder.board->name);
builder.ConsoleOutput("via %s...\r\n", serial);
builder.proc.flags = SF_REDIRECT_STDERR;
if (builder.target.eepBytes > 0) {
eepfile = new char[strlen(builder.hexfile) + 5];
sprintf(eepfile, "%s.eep", builder.hexfile);
}
builder.proc.flags = SF_ALLOC | SF_REDIRECT_STDERR;
ret = builder.UploadToArduino(eepfile, serial, ispBaudrate, "bin/");
if (ret == 0) {
do {
while (ShellRead(&builder.proc, 3000) > 0) cout << builder.proc.buffer;
} while (ShellWait(&builder.proc, 0) == 0);
ret = builder.proc.iRetCode;
}
ShellClean(&builder.proc);
if (eepfile) delete eepfile;
}
delete buildDir;
if (!corefile && builder.corefile) delete builder.corefile;
} else if (serial) {
ret = builder.UploadToArduino(0, serial, ispBaudrate, "bin/");
}
if (builder.hexfile) free(builder.hexfile);
free(appdir);
#ifdef _DEBUG
printf("Press ENTER to exit...");
getchar();
#endif
return ret;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.