~ubuntu-branches/debian/experimental/arduino/experimental

« back to all changes in this revision

Viewing changes to mjo/arduino-cli.html

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2012-03-11 18:19:42 UTC
  • mfrom: (1.1.5) (5.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120311181942-be2clnbz1gcehixb
Tags: 1:1.0.1~rc1+dfsg-1
New upstream release, experimental.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2
 
<html xmlns="http://www.w3.org/1999/xhtml">
3
 
  <head>
4
 
    <title>Martin's Atelier: Arduino from the command line</title>
5
 
    <link rel="alternate" type="application/atom+xml" href="../../feed.atom" />   
6
 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7
 
    <meta name="robots"      content="all" />
8
 
    <meta name="author"      content="Martin Oldfield" />
9
 
    <meta name="keywords"    content="Arduino command line Makefile" />
10
 
    <meta name="description" content="" />    
11
 
    <style type="text/css" media="all">@import "../../std.css";</style>
12
 
    <style type="text/css" media="print">@import "../../print.css";</style>
13
 
</head>
14
 
 
15
 
  <body>
16
 
    <div id="wrapper">
17
 
      <div id="header">
18
 
        <img id="logo_col" src="../../gears.png"/>
19
 
        <img id="logo_bw"  src="../../gears_bw.png"/>
20
 
        <h1>Arduino from the command line</h1>
21
 
      </div>
22
 
      <div id="main">
23
 
        <h2>Update News</h2>
24
 
 
25
 
<p>The current version of this software (0.4) won&#39;t work with versions of the Arduino software before 0018.</p>
26
 
 
27
 
<h2>Introduction</h2>
28
 
 
29
 
<p>The <a href="http://www.arduino.cc/">Arduino</a><span class="link_list"><sup>1</sup></span> has done much to popularize microcontrollers for the casual tinkerer. Its success suggests that there&#39;s considerable value in combining a standard microcontroller (the ATmega) and a <span class="caps">GCC </span>based toolchain into an easily digesible package. For myself, it&#39;s certainly easier to just install the latest release of the Arduino software than worry about building my own cross-compilers, particularly when it&#39;s all new to me and consequently somewhat confusing.</p>
30
 
 
31
 
<p>After working through the toy tutorials though, I found myself wishing that writing code for the Arduino were more like writing other C programs. In my case, that means editing it with emacs then building it with make. I must emphasize that I&#39;m not criticizing the Arduino <span class="caps">IDE</span>: there&#39;s nothing wrong with it beyond it not being emacs...</p>
32
 
 
33
 
<p>It turns out that others have been along this path before: the Arduino website has a hopeful sounding <a href="http://www.arduino.cc/en/Hacking/CommandLine">Arduino from the Command Line</a><span class="link_list"><sup>2</sup></span> article. In turn this points you at the Makefile shipped as part of the Arduino software distribution: hardware/cores/arduino/Makefile.</p>
34
 
 
35
 
<p>This didn&#39;t really fit quite what I wanted to do though, so I wrote my own Makefile. You might wonder why I should embark on such a task. Well:</p>
36
 
 
37
 
<ul>
38
 
<li>I was keen that all of my objects and random other files were   completely separate from the main Arduino stuff in the applet   directory.</li>
39
 
<li>Although I wanted to be able to build Arduino sketches, I also   wanted a suitable jumping-off point for code which didn&#39;t use   wiring. In other words, to regard the Arduino software as a   convenient way to get the <span class="caps">AVR GCC </span>toolchain.</li>
40
 
<li>Rather than dumping a big Makefile in each sketch directory, I   wanted to have a few definitions in the directory which then   included a large project-independent file from elsewhere.</li>
41
 
</ul>
42
 
 
43
 
<p>Finally, one of the things I enjoy about writing code for microcontrollers is the sense of continuity between the hardware datasheets published by the chip manufacturer and the code I write (by contrast if you&#39;re writing code on Linux there&#39;s a vast gulf between the code executing printf and stuff appearing on the screen). Writing my own Makefile seemed a good way to make sure I understood what was going on.</p>
44
 
 
45
 
<p>So to the Makefile. Obviously it owes a great debt to the people who wrote the Makefile shipped with the Arduino <span class="caps">IDE </span>and here&#39;s the credit list from that file:</p>
46
 
 
47
 
<pre><code># Arduino 0011 Makefile                                  
48
 
# Arduino adaptation by mellis, eighthave, oli.keller    </code></pre>
49
 
 
50
 
<p>Thanks then to mellis, eighthavem and oli.keller.</p>
51
 
 
52
 
<h2>Installation instructions</h2>
53
 
 
54
 
<p>You&#39;ll need to download <a href="http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.4.tar.gz">the tarball containing the Makefile,</a><span class="link_list"><sup>3</sup></span>, unpack it, and then copy the Makefile somewhere sensible:</p>
55
 
 
56
 
<pre><code>$ wget http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.4.tar.gz 
57
 
$ tar xzvf arduino-mk_0.4.tar.gz 
58
 
$ cp arduino-mk-0.4/Arduino.mk /path/to/my/arduino/stuff/Arduino.mk </code></pre>
59
 
 
60
 
<p>The next step is to create a small Makefile for the sketch you actually want to build. The typical Arduino sketch directory looks like this:</p>
61
 
 
62
 
<pre><code>$ ls -ltr  
63
 
total 8    
64
 
drwxr-xr-x   4 mjo  staff  136 15 Feb 19:53 applet        
65
 
-rw-r--r--@  1 mjo  staff  384 17 Feb 16:11 CLItest.pde</code></pre>
66
 
 
67
 
<p>To this we&#39;ll add a Makefile:</p>
68
 
 
69
 
<pre><code>ARDUINO_DIR = /Applications/Arduino.app/Contents/Resources/Java 
70
 
                                            
71
 
TARGET       = CLItest                            
72
 
MCU          = atmega168                          
73
 
F_CPU        = 16000000                           
74
 
ARDUINO_PORT = /dev/cu.usb*                       
75
 
                                            
76
 
ARDUINO_LIBS = LiquidCrystal              
77
 
                                            
78
 
include /path/to/my/arduino/stuff/Arduino.mk</code></pre>
79
 
 
80
 
<p>Hopefully these will be self-explanatory but in case they&#39;re not:</p>
81
 
 
82
 
<dl>
83
 
<dt><span class="caps">ARDUINO</span>_DIR</dt>
84
 
<dd>Where you installed the Arduino software. On the Mac this has to point deep inside the Arduino installation /Applications.  </dd>
85
 
<dt><span class="caps">TARGET</span></dt>
86
 
<dd>The basename used for the final files. Canonically            this would match the .pde file, but you can choose anything!        </dd>
87
 
<dt><span class="caps">ARDUINO</span>_LIBS</dt>
88
 
<dd>A list of any libraries used by the sketch&mdash;we assume  these are in $(ARDUINO_DIR)/hardware/libraries.                     </dd>
89
 
<dt><span class="caps">MCU</span></dt>
90
 
<dd>The target processor (atmega168 for the Duemilanove).           </dd>
91
 
<dt>F_CPU</dt>
92
 
<dd>The target&#39;s clock speed (16000000 for the Duemilanove).      </dd>
93
 
<dt><span class="caps">ARDUINO</span>_PORT</dt>
94
 
<dd>The port where the Arduino can be found (only needed when  uploading) If this expands to several ports, the first will be used.</dd>
95
 
</dl>
96
 
 
97
 
 
98
 
<p>A couple of other options might be useful:</p>
99
 
 
100
 
<dl>
101
 
<dt><span class="caps">AVRDUDE</span>_ARD_PROGRAMMER</dt>
102
 
<dd>The protocol avrdude speaks&mdash;defaults to stk500v1.           </dd>
103
 
<dt><span class="caps">AVRDUDE</span>_ARD_BAUDRATE</dt>
104
 
<dd>The rate at which we talk to the board&mdash;defaults to 19,200.</dd>
105
 
</dl>
106
 
 
107
 
 
108
 
<p>If you&#39;re using the toolchain provided by the system rather than bundled with the Arduino software (as I think is the case on Linux) then you&#39;ll have to add some more paths:</p>
109
 
 
110
 
<pre><code>AVR_TOOLS_PATH   = /usr/bin  
111
 
AVRDUDE_CONF     = /etc/avrdude.conf</code></pre>
112
 
 
113
 
<h2>Building</h2>
114
 
 
115
 
<p>If you&#39;re used to Unix then this is easy:</p>
116
 
 
117
 
<pre><code>$ make 
118
 
...</code></pre>
119
 
 
120
 
<p>The output is pretty verbose, but I think it should be obvious if it worked. After building you&#39;ll see a new directory has been created which contains all the object files: build-cli.</p>
121
 
 
122
 
<pre><code>$ ls -l  
123
 
total 32 
124
 
-rw-r--r--@  1 mjo  staff  384 17 Feb 16:11 CLItest.pde  
125
 
-rw-r--r--@  1 mjo  staff  202 17 Feb 16:39 Makefile     
126
 
drwxr-xr-x   4 mjo  staff  136 15 Feb 19:53 applet       
127
 
drwxr-xr-x  21 mjo  staff  714 17 Feb 17:52 build-cli    </code></pre>
128
 
 
129
 
<h3>build-cli</h3>
130
 
 
131
 
<p>Let&#39;s peek inside the build-cli directory:</p>
132
 
 
133
 
<pre><code>$ ls -l build-cli 
134
 
total 392 
135
 
-rw-r--r--  1 mjo  staff    491 17 Feb 17:52 CLItest.cpp          
136
 
-rw-r--r--  1 mjo  staff    583 17 Feb 17:52 CLItest.d            
137
 
-rwxr-xr-x  1 mjo  staff  60971 17 Feb 17:52 CLItest.elf          
138
 
-rw-r--r--  1 mjo  staff   3994 17 Feb 17:52 CLItest.hex          
139
 
-rw-r--r--  1 mjo  staff   5932 17 Feb 17:52 CLItest.o            
140
 
-rw-r--r--  1 mjo  staff   6920 17 Feb 17:52 HardwareSerial.o     
141
 
-rw-r--r--  1 mjo  staff  12708 17 Feb 17:52 Print.o              
142
 
-rw-r--r--  1 mjo  staff   6852 17 Feb 17:52 WInterrupts.o        
143
 
-rw-r--r--  1 mjo  staff   4680 17 Feb 17:52 WMath.o              
144
 
-rw-r--r--  1 mjo  staff    611 17 Feb 17:52 depends.mk           
145
 
-rw-r--r--  1 mjo  staff   5900 17 Feb 17:52 pins_arduino.o       
146
 
-rw-r--r--  1 mjo  staff   8476 17 Feb 17:52 wiring.o             
147
 
-rw-r--r--  1 mjo  staff   7224 17 Feb 17:52 wiring_analog.o      
148
 
-rw-r--r--  1 mjo  staff   8244 17 Feb 17:52 wiring_digital.o     
149
 
-rw-r--r--  1 mjo  staff   6544 17 Feb 17:52 wiring_pulse.o       
150
 
-rw-r--r--  1 mjo  staff   7424 17 Feb 17:52 wiring_serial.o      
151
 
-rw-r--r--  1 mjo  staff   5308 17 Feb 17:52 wiring_shift.o       </code></pre>
152
 
 
153
 
<p>Most of the files in here are object files for the wiring library. What about the others ?</p>
154
 
 
155
 
<dl>
156
 
<dt><span class="caps">CLI</span>test.cpp</dt>
157
 
<dd>This is the .pde sketch file with a small main program prepended and a suitable #include prepended. </dd>
158
 
<dt><span class="caps">CLI</span>test.d</dt>
159
 
<dd>This tracks the dependencies used by <span class="caps">CLI</span>test.pde </dd>
160
 
<dt><span class="caps">CLI</span>test.elf</dt>
161
 
<dd>This is executable produced by the linker </dd>
162
 
<dt><span class="caps">CLI</span>test.hex</dt>
163
 
<dd>This is a hex dump of (the code part) of the executable  in a format understood by the Arduino&#39;s bootloader. </dd>
164
 
<dt><span class="caps">CLI</span>test.o</dt>
165
 
<dd>The object file we got by compiling <span class="caps">CLI</span>test.cpp. </dd>
166
 
<dt>depends.mk</dt>
167
 
<dd>A single file containing all the dependency relations  (it&#39;s the concatentation of all the .d files).</dd>
168
 
</dl>
169
 
 
170
 
 
171
 
<p>You may recall the <span class="caps">TARGET </span>variable we specified in the Makefile above: it&#39;s that which sets the base name for e.g. <span class="caps">CLI</span>test.elf. However <span class="caps">CLI</span>test.cpp, <span class="caps">CLI</span>test.o and <span class="caps">CLI</span>test.d take their name from the .pde sketch file.</p>
172
 
 
173
 
<h2>Uploading code</h2>
174
 
 
175
 
<p>This is easy:</p>
176
 
 
177
 
<pre><code>$ make upload</code></pre>
178
 
 
179
 
<h2>Uploading via <span class="caps">ISP</span></h2>
180
 
 
181
 
<p>If you&#39;re using target hardware which doesn&#39;t have a bootloader then you might want to use <span class="caps">ISP </span>to upload the code. Though you&#39;ll obviously need some extra hardware to do this.</p>
182
 
 
183
 
<p>Assuming that avrdude supports your programmer though, you&#39;ll only need to make a few changes to the Makefile to tell avrdude where it can find the programmer and how to talk to it:</p>
184
 
 
185
 
<pre><code>ISP_PORT         = /dev/ttyACM0 
186
 
ISP_PROG         = -c stk500v2</code></pre>
187
 
 
188
 
<p>Then to upload:</p>
189
 
 
190
 
<pre><code>$ make ispload</code></pre>
191
 
 
192
 
<h3>Fuses</h3>
193
 
 
194
 
<p>You might need to change the fuse settings when programming, though some care needs to be taken here or you might irreversibly change the chip. You can set the fuse settings thus (the values below are the defaults):</p>
195
 
 
196
 
<pre><code>ISP_LOCK_FUSE_PRE  = 0x3f 
197
 
ISP_LOCK_FUSE_POST = 0xcf 
198
 
ISP_HIGH_FUSE      = 0xdf 
199
 
ISP_LOW_FUSE       = 0xff 
200
 
ISP_EXT_FUSE       = 0x01
201
 
</code></pre>
202
 
 
203
 
<h2>Growing the project</h2>
204
 
 
205
 
<p>There a couple of obvious things to do now. You might want to edit the sketch. That&#39;s easy: just edit the .pde file and run make again.</p>
206
 
 
207
 
<p>Alternatively you might want to add some more source files to the project. That&#39;s easy too: the Makefile understands C, C++ and assembler files in the source directory (with .c, .cpp, and .s extensions). Everything <strong>should</strong> just work.</p>
208
 
 
209
 
 
210
 
 
211
 
<h2>Wiring-less development</h2>
212
 
 
213
 
<p>Finally you might want to develop code which isn&#39;t linked against the Wiring library. There&#39;s some scope for this: just set NO_CORE in the Makefile e.g.</p>
214
 
 
215
 
<pre><code>NO_CORE = 1</code></pre>
216
 
 
217
 
<h2>Bugs and problems</h2>
218
 
 
219
 
<ul>
220
 
<li>The Makefile isn&#39;t very elegant.</li>
221
 
<li>Handling Arduino libraries isn&#39;t very clean. For one thing I&#39;m   relying on them being compiled in a compatible way by the Arduino   environment which is probably a bit risky. Secondly it&#39;s a bit lazy   to get the user to specify which libraries are being used manually.</li>
222
 
<li>When compiling the sketch file, the compiler actually sees the .cpp   file derived from it. Accordingly the line numbers of any errors   will be wrong (but not by that much).</li>
223
 
<li>I fear that the Makefile makes unwarranted assumptions about the   hardware: for example, all my Arduino experience has been ATmega 168   related.</li>
224
 
<li>The Makefile doesn&#39;t do some of the things that the Makefile   distributed with the Arduino software does e.g. generating <span class="caps">COFF   </span>files. I worry that some of these might be important.</li>
225
 
<li>This hasn&#39;t been used very much yet, even by me. I&#39;m writing this   now as much for my benefit as anyone else&#39;s, though I&#39;d be delighted   to know if anyone else finds it useful.</li>
226
 
</ul>
227
 
 
228
 
<h2>Changelog</h2>
229
 
 
230
 
<h3>2010-05-21, version 0.3</h3>
231
 
 
232
 
<ul>
233
 
<li>Tidied up the licensing, making it clear that it&#39;s released under <span class="caps">LGPL</span> 2.1.</li>
234
 
<li><a href="http://hands.com/~phil/">Philip Hands</a><span class="link_list"><sup>4</sup></span> sent me some code to reset the   Arduino by dropping <span class="caps">DTR </span>for 100ms, and I added it.</li>
235
 
<li>Tweaked the Makefile to handle version 0018 of the Arduino software   which now includes main.cpp. Accordingly we don&#39;t need to&mdash;and   indeed must not&mdash;add main.cxx to the .pde sketch file. The paths   seem to have changed a bit too.</li>
236
 
</ul>
237
 
 
238
 
<h3>2010-05-24, version 0.4</h3>
239
 
 
240
 
<ul>
241
 
<li>Tweaked rules for the reset target on Philip Hands&#39; advice. </li>
242
 
</ul>
243
 
        <div id="link_list">
244
 
          <h2>References</h2>
245
 
          <ul><li>1. http://www.arduino.cc/</li><li>2. http://www.arduino.cc/en/Hacking/CommandLine</li><li>3. http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.4.tar.gz</li><li>4. http://hands.com/~phil/</li></ul>
246
 
        </div>      </div>
247
 
           
248
 
      <div id="sidebar">
249
 
        <div class="listcontainer">
250
 
          <h2><a href="../../index/index.html">Martin's Atelier</a></h2>
251
 
          <div id="subscribe"><p><a href="../../feed.atom"><img src="../../atom.png" alt="[ Atom Feed ]" title="Atom Feed" /></a></p></div>
252
 
        </div>
253
 
        
254
 
        
255
 
        <div class="listcontainer">        
256
 
          <h3>Related Articles</h3>
257
 
          <ul>
258
 
            
259
 
            <li><a href="../03/aarduino.html">The AArduino</a><br/>Martin Oldfield, 16 Jun 2009</li>
260
 
            
261
 
            <li><a href="avrdude-cookbook.html">Avrdude Cookbook</a><br/>Martin Oldfield, 23 Feb 2009</li>
262
 
            
263
 
            <li><a href="olimex-avrdude-mac.html">MacOS X and the Olimex AVR-ISP500</a><br/>Martin Oldfield, 17 Feb 2009</li>
264
 
            
265
 
          </ul>
266
 
        </div>
267
 
        
268
 
  
269
 
        
270
 
        <div class="listcontainer">        
271
 
          <h3><a href="../../index/st_subject.html">Related Subjects</a></h3>
272
 
          <ul>
273
 
            
274
 
            <li><a href="../../index/t_Arduino.html">Arduino</a></li>
275
 
            
276
 
            <li><a href="../../index/t_command line.html">command line</a></li>
277
 
            
278
 
            <li><a href="../../index/t_Makefile.html">Makefile</a></li>
279
 
            
280
 
          </ul>
281
 
        </div>
282
 
        
283
 
 
284
 
        
285
 
        <div class="listcontainer">        
286
 
          <h3><a href="../../index/index.html">Other Articles</a></h3>
287
 
          <ul>
288
 
            
289
 
            <li><a href="../../2010/06/arm-toolchain.html">Getting an ARM toolchain on MacOS 10.6</a><br/>Martin Oldfield, 06 Jun 2010</li>
290
 
            
291
 
            <li><a href="../../2010/05/bus-pirate.html">The Bus Pirate on MacOS</a><br/>Martin Oldfield, 26 May 2010</li>
292
 
            
293
 
            <li><a href="../../2010/04/perl-unicode.html">Unicode games with perl, MySQL, and XML</a><br/>Martin Oldfield, 17 Apr 2010</li>
294
 
            
295
 
            <li><a href="../../2010/01/gps-track-filter.html">Filtering GPS tracks</a><br/>Martin Oldfield, 04 Jan 2010</li>
296
 
            
297
 
            <li><a href="../01/paris_drink.html">Places to drink in Paris</a><br/>Martin Oldfield, 15 Dec 2009</li>
298
 
            
299
 
            <li><a href="../01/paris_food.html">Places to eat in Paris</a><br/>Martin Oldfield, 15 Dec 2009</li>
300
 
            
301
 
            <li><a href="../11/sons-and-daughters.html">Sons and Daughters</a><br/>Martin Oldfield, 06 Dec 2009</li>
302
 
            
303
 
            <li><a href="../12/garmin-gpx.html">Faking geocaches in Garmin GPX files</a><br/>Martin Oldfield, 06 Dec 2009</li>
304
 
            
305
 
            <li><a href="../08/macos-pic.html">Playing with PICs on MacOS X</a><br/>Martin Oldfield, 18 Aug 2009</li>
306
 
            
307
 
            <li>...</li>
308
 
          </ul>
309
 
        </div>
310
 
        
311
 
 
312
 
        
313
 
        <div class="lastlistcontainer">
314
 
          <h3>Bookmark this article</h3>
315
 
        <ul class="social">
316
 
          
317
 
          <li><a href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fmjo.tc%2Fatelier%2F2009%2F02%2Farduino-cli.html" title="Facebook"><img src="../../social/facebook.png" alt="[ facebook ]"/></a></li>
318
 
          
319
 
          <li><a href="http://del.icio.us/post?url=http%3A%2F%2Fmjo.tc%2Fatelier%2F2009%2F02%2Farduino-cli.html&title=Arduino+from+the+command+line" title="Delicious"><img src="../../social/delicious.png" alt="[ delicious ]"/></a></li>
320
 
          
321
 
          <li><a href="http://digg.com/submit?url=http%3A%2F%2Fmjo.tc%2Fatelier%2F2009%2F02%2Farduino-cli.html&title=Arduino+from+the+command+line" title="Digg"><img src="../../social/digg.png" alt="[ digg ]"/></a></li>
322
 
          
323
 
          <li><a href="http://reddit.com/submit?url=http%3A%2F%2Fmjo.tc%2Fatelier%2F2009%2F02%2Farduino-cli.html&title=Arduino+from+the+command+line" title="Reddit"><img src="../../social/reddit.png" alt="[ reddit ]"/></a></li>
324
 
          
325
 
          <li><a href="http://www.stumbleupon.com?url=http%3A%2F%2Fmjo.tc%2Fatelier%2F2009%2F02%2Farduino-cli.html&title=Arduino+from+the+command+line" title="Stumbleupon"><img src="../../social/stumbleupon.png" alt="[ stumbleupon ]"/></a></li>
326
 
          
327
 
        </ul>
328
 
        </div>
329
 
        
330
 
 
331
 
        </div>
332
 
 
333
 
        <div id="footer">
334
 
          Revised by Martin Oldfield on 07 Jun 2010, original version 17 Feb 2009.<br/>
335
 
          Contact: Martin Oldfield, ex-atelier@mjo.tc<br/>
336
 
          This work is available under the <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>, or the <a href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">LGPL</a> version 2.1 or later.
337
 
       </div>
338
 
 
339
 
        </div>
340
 
        
341
 
    </div>
342
 
    <script src="http://www.google-analytics.com/urchin.js"
343
 
    type="text/javascript">
344
 
    </script>
345
 
    <script type="text/javascript">
346
 
    _uacct = "UA-567180-1";
347
 
    urchinTracker();
348
 
    </script>
349
 
  </body>
350
 
</html>