~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to src/processing/app/debug/AvrdudeUploader.java

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
 
2
 
 
3
/*
 
4
  AvrdudeUploader - uploader implementation using avrdude
 
5
  Part of the Arduino project - http://www.arduino.cc/
 
6
 
 
7
  Copyright (c) 2004-05
 
8
  Hernando Barragan
 
9
 
 
10
  This program is free software; you can redistribute it and/or modify
 
11
  it under the terms of the GNU General Public License as published by
 
12
  the Free Software Foundation; either version 2 of the License, or
 
13
  (at your option) any later version.
 
14
 
 
15
  This program is distributed in the hope that it will be useful,
 
16
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
  GNU General Public License for more details.
 
19
 
 
20
  You should have received a copy of the GNU General Public License
 
21
  along with this program; if not, write to the Free Software Foundation,
 
22
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
  
 
24
  $Id: AvrdudeUploader.java 804 2009-12-18 16:05:52Z dmellis $
 
25
*/
 
26
 
 
27
package processing.app.debug;
 
28
 
 
29
import processing.app.Base;
 
30
import processing.app.Preferences;
 
31
import processing.app.Serial;
 
32
 
 
33
import java.io.*;
 
34
import java.util.*;
 
35
import java.util.zip.*;
 
36
import javax.swing.*;
 
37
import gnu.io.*;
 
38
 
 
39
 
 
40
public class AvrdudeUploader extends Uploader  {
 
41
  public AvrdudeUploader() {
 
42
  }
 
43
 
 
44
  // XXX: add support for uploading sketches using a programmer
 
45
  public boolean uploadUsingPreferences(String buildPath, String className, boolean verbose)
 
46
  throws RunnerException {
 
47
    this.verbose = verbose;
 
48
    Map<String, String> boardPreferences = Base.getBoardPreferences();
 
49
    String uploadUsing = boardPreferences.get("upload.using");
 
50
    if (uploadUsing == null) {
 
51
      // fall back on global preference
 
52
      uploadUsing = Preferences.get("upload.using");
 
53
    }
 
54
    if (uploadUsing.equals("bootloader")) {
 
55
      return uploadViaBootloader(buildPath, className);
 
56
    } else {
 
57
      // XXX: this needs to handle programmers in other targets.
 
58
      Collection params = getProgrammerCommands(Base.getTarget().getName(), uploadUsing);
 
59
      params.add("-Uflash:w:" + buildPath + File.separator + className + ".hex:i");
 
60
      return avrdude(params);
 
61
    }
 
62
  }
 
63
  
 
64
  private boolean uploadViaBootloader(String buildPath, String className)
 
65
  throws RunnerException {
 
66
    Map<String, String> boardPreferences = Base.getBoardPreferences();
 
67
    List commandDownloader = new ArrayList();
 
68
    String protocol = boardPreferences.get("upload.protocol");
 
69
    
 
70
    // avrdude wants "stk500v1" to distinguish it from stk500v2
 
71
    if (protocol.equals("stk500"))
 
72
      protocol = "stk500v1";
 
73
    commandDownloader.add("-c" + protocol);
 
74
    commandDownloader.add(
 
75
      "-P" + (Base.isWindows() ? "\\\\.\\" : "") + Preferences.get("serial.port"));
 
76
    commandDownloader.add(
 
77
      "-b" + Integer.parseInt(boardPreferences.get("upload.speed")));
 
78
    commandDownloader.add("-D"); // don't erase
 
79
    commandDownloader.add("-Uflash:w:" + buildPath + File.separator + className + ".hex:i");
 
80
 
 
81
    if (boardPreferences.get("upload.disable_flushing") == null ||
 
82
        boardPreferences.get("upload.disable_flushing").toLowerCase().equals("false")) {
 
83
      flushSerialBuffer();
 
84
    }
 
85
 
 
86
    return avrdude(commandDownloader);
 
87
  }
 
88
  
 
89
  public boolean burnBootloader(String target, String programmer) throws RunnerException {
 
90
    return burnBootloader(getProgrammerCommands(target, programmer));
 
91
  }
 
92
  
 
93
  private Collection getProgrammerCommands(String targetName, String programmer) {
 
94
    Target target = Base.targetsTable.get(targetName);
 
95
    Map<String, String> programmerPreferences = target.getProgrammers().get(programmer);
 
96
    List params = new ArrayList();
 
97
    params.add("-c" + programmerPreferences.get("protocol"));
 
98
    
 
99
    if ("usb".equals(programmerPreferences.get("communication"))) {
 
100
      params.add("-Pusb");
 
101
    } else if ("serial".equals(programmerPreferences.get("communication"))) {
 
102
      params.add("-P" + (Base.isWindows() ? "\\\\.\\" : "") + Preferences.get("serial.port"));
 
103
      if (programmerPreferences.get("speed") != null) {
 
104
        params.add("-b" + Integer.parseInt(programmerPreferences.get("speed")));
 
105
      }
 
106
    }
 
107
    // XXX: add support for specifying the port address for parallel
 
108
    // programmers, although avrdude has a default that works in most cases.
 
109
    
 
110
    if (programmerPreferences.get("force") != null &&
 
111
        programmerPreferences.get("force").toLowerCase().equals("true"))
 
112
      params.add("-F");
 
113
    
 
114
    if (programmerPreferences.get("delay") != null)
 
115
      params.add("-i" + programmerPreferences.get("delay"));
 
116
    
 
117
    return params;
 
118
  }
 
119
  
 
120
  protected boolean burnBootloader(Collection params)
 
121
  throws RunnerException {
 
122
    Map<String, String> boardPreferences = Base.getBoardPreferences();
 
123
    List fuses = new ArrayList();
 
124
    fuses.add("-e"); // erase the chip
 
125
    fuses.add("-Ulock:w:" + boardPreferences.get("bootloader.unlock_bits") + ":m");
 
126
    if (boardPreferences.get("bootloader.extended_fuses") != null)
 
127
      fuses.add("-Uefuse:w:" + boardPreferences.get("bootloader.extended_fuses") + ":m");
 
128
    fuses.add("-Uhfuse:w:" + boardPreferences.get("bootloader.high_fuses") + ":m");
 
129
    fuses.add("-Ulfuse:w:" + boardPreferences.get("bootloader.low_fuses") + ":m");
 
130
 
 
131
    if (!avrdude(params, fuses))
 
132
      return false;
 
133
 
 
134
    try {
 
135
      Thread.sleep(1000);
 
136
    } catch (InterruptedException e) {}
 
137
    
 
138
    Target t;
 
139
    String bootloaderPath = boardPreferences.get("bootloader.path");
 
140
    
 
141
    if (bootloaderPath.indexOf(':') == -1) {
 
142
      t = Base.getTarget(); // the current target (associated with the board)
 
143
    } else {
 
144
      String targetName = bootloaderPath.substring(0, bootloaderPath.indexOf(':'));
 
145
      t = Base.targetsTable.get(targetName);
 
146
      bootloaderPath = bootloaderPath.substring(bootloaderPath.indexOf(':') + 1);
 
147
    }
 
148
    
 
149
    File bootloadersFile = new File(t.getFolder(), "bootloaders");
 
150
    File bootloaderFile = new File(bootloadersFile, bootloaderPath);
 
151
    bootloaderPath = bootloaderFile.getAbsolutePath();
 
152
    
 
153
    List bootloader = new ArrayList();
 
154
    bootloader.add("-Uflash:w:" + bootloaderPath + File.separator +
 
155
                   boardPreferences.get("bootloader.file") + ":i");
 
156
    bootloader.add("-Ulock:w:" + boardPreferences.get("bootloader.lock_bits") + ":m");
 
157
 
 
158
    return avrdude(params, bootloader);
 
159
  }
 
160
  
 
161
  public boolean avrdude(Collection p1, Collection p2) throws RunnerException {
 
162
    ArrayList p = new ArrayList(p1);
 
163
    p.addAll(p2);
 
164
    return avrdude(p);
 
165
  }
 
166
  
 
167
  public boolean avrdude(Collection params) throws RunnerException {
 
168
    List commandDownloader = new ArrayList();
 
169
    commandDownloader.add("avrdude");
 
170
 
 
171
    // Point avrdude at its config file since it's in a non-standard location.
 
172
    if (Base.isLinux()) {
 
173
      // ???: is it better to have Linux users install avrdude themselves, in
 
174
      // a way that it can find its own configuration file?
 
175
      commandDownloader.add("-C" + Base.getHardwarePath() + "/tools/avrdude.conf");
 
176
    } else {
 
177
      commandDownloader.add("-C" + Base.getHardwarePath() + "/tools/avr/etc/avrdude.conf");
 
178
    }
 
179
 
 
180
    if (verbose || Preferences.getBoolean("upload.verbose")) {
 
181
      commandDownloader.add("-v");
 
182
      commandDownloader.add("-v");
 
183
      commandDownloader.add("-v");
 
184
      commandDownloader.add("-v");
 
185
    } else {
 
186
      commandDownloader.add("-q");
 
187
      commandDownloader.add("-q");
 
188
    }
 
189
    commandDownloader.add("-p" + Base.getBoardPreferences().get("build.mcu"));
 
190
    commandDownloader.addAll(params);
 
191
 
 
192
    return executeUploadCommand(commandDownloader);
 
193
  }
 
194
}