Menu

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

Download this file

133 lines (125 with data), 3.6 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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include "ctb-0.16/ctb.h"
extern "C" {
#include "syspil.h"
#include "processpil.h"
}
#include "sketchbuild.h"
using namespace std;
vector<string> ports;
static string SearchNewPort()
{
DWORD tick = GetTickCount();
vector<string> newports;
bool found = true;
do {
newports.clear();
ctb::GetAvailablePorts( newports );
for( int i = 0; i < (int)newports.size(); i++) {
// look for port in previous ports list
found = false;
for (int j = 0; j < ports.size(); j++) {
if (newports[i].compare(ports[j]) == 0) {
found = true;
break;
}
}
if (!found) {
return newports[i];
}
}
newports.clear();
msleep(1000);
} while (GetTickCount() - tick < 10000 && found);
return "";
}
static string ResetLeonardo(const char* port)
{
string uploadPort;
ctb::SerialPort serialPort;
char devname[64];
sprintf(devname, "\\\\.\\%s", port);
if (serialPort.Open(devname, 1200) != 0) {
return "";
}
//serialPort.ClrLineState(ctb::LinestateDtr);
serialPort.Close();
uploadPort = SearchNewPort();
return uploadPort;
}
int CArduinoBuilder::UploadToArduino(const char* eepfile, const char* serial, int baudrate, const char* avrpath)
{
char cmd[512];
char *p;
int ret = 0;
ports.clear();
progress = 0;
do {
string uploadPort;
if (!IsFileExist(hexfile)) {
ConsoleOutput("%s does not exist", hexfile);
break;
}
if (!serial) {
ConsoleOutput("No serial port is specified.\r\n");
break;
}
// enumerate serial ports
if( !ctb::GetAvailablePorts( ports )) {
ConsoleOutput("No serial port is accessible.\r\n");
break;
}
if (!strcmp(board->mcu, "atmega32u4") && stricmp(serial, "usbasp") && !baudrate) {
ConsoleOutput("Forcing reset using 1200bps open/close on %s... ", serial);
uploadPort = ResetLeonardo(serial);
if (!uploadPort.empty()) {
ConsoleOutput("%s found!\r\n", uploadPort.c_str());
} else {
ConsoleOutput("no new port found\r\n");
proc.iRetCode = -1;
break;
}
}
p = cmd;
#ifdef WIN32
p += sprintf(p, "\"%savrdude.exe\" -C %savrdude.conf", avrpath, avrpath);
if (!stricmp(serial, "usbasp")) {
p += sprintf(p, " -cusbasp -Pusb");
} else if (baudrate) {
// ArduinoISP
p += sprintf(p, " -cstk500v1 -P\\\\.\\%s -b%d", serial, baudrate);
} else if (!strcmp(board->mcu, "atmega32u4")) {
p += sprintf(p, " -cavr109 -P\\\\.\\%s -b%d -D", uploadPort.c_str(), board->baudrate);
} else if (!strcmp(board->mcu, "atmega2560")) {
p += sprintf(p, " -cwiring -P\\\\.\\%s -b%d -D", serial, board->baudrate);
} else {
p += sprintf(p, " -carduino -P\\\\.\\%s -b%d -D", serial, board->baudrate);
}
#else
p += sprintf(p, "%savrdude -C /etc/avrdude.conf", avrpath);
if (!strcmp(serial, "usbasp")) {
p += sprintf(p, " -cusbasp -Pusb");
} else if (!strcmp(board->mcu, "atmega32u4")) {
p += sprintf(p, " -cavr109 -P%s -b%d", uploadPort, board->baudrate);
} else if (!strcmp(board->mcu, "atmega2560")) {
p += sprintf(p, " -cwiring -P%s -b%d", serial, board->baudrate);
} else {
p += sprintf(p, " -carduino -P%s -b%d", serial, board->baudrate);
}
#endif
p += sprintf(p, " -V -p%s -Uflash:w:\"%s\":i", board->mcu, hexfile);
if (eepfile) {
p += sprintf(p, " -Ueeprom:w:\"%s\":i", eepfile);
}
if (ShellExec(&proc, cmd) != 0) {
break;
}
//ResetLeonardo(serial.c_str());
return 0;
} while(0);
return -1;
}
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.