File: arduino-cli.html

package info (click to toggle)
arduino 0018%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,704 kB
  • ctags: 8,331
  • sloc: java: 38,036; ansic: 5,841; cpp: 2,867; makefile: 667; perl: 111; sh: 10
file content (350 lines) | stat: -rw-r--r-- 19,095 bytes parent folder | download
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Martin's Atelier: Arduino from the command line</title>
    <link rel="alternate" type="application/atom+xml" href="../../feed.atom" />   
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="robots"      content="all" />
    <meta name="author"      content="Martin Oldfield" />
    <meta name="keywords"    content="Arduino command line Makefile" />
    <meta name="description" content="" />    
    <style type="text/css" media="all">@import "../../std.css";</style>
    <style type="text/css" media="print">@import "../../print.css";</style>
</head>

  <body>
    <div id="wrapper">
      <div id="header">
        <img id="logo_col" src="../../gears.png"/>
        <img id="logo_bw"  src="../../gears_bw.png"/>
        <h1>Arduino from the command line</h1>
      </div>
      <div id="main">
        <h2>Update News</h2>

<p>The current version of this software (0.4) won&#39;t work with versions of the Arduino software before 0018.</p>

<h2>Introduction</h2>

<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>

<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>

<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>

<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>

<ul>
<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>
<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>
<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>
</ul>

<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>

<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>

<pre><code># Arduino 0011 Makefile                                  
# Arduino adaptation by mellis, eighthave, oli.keller    </code></pre>

<p>Thanks then to mellis, eighthavem and oli.keller.</p>

<h2>Installation instructions</h2>

<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>

<pre><code>$ wget http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.4.tar.gz 
$ tar xzvf arduino-mk_0.4.tar.gz 
$ cp arduino-mk-0.4/Arduino.mk /path/to/my/arduino/stuff/Arduino.mk </code></pre>

<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>

<pre><code>$ ls -ltr  
total 8    
drwxr-xr-x   4 mjo  staff  136 15 Feb 19:53 applet        
-rw-r--r--@  1 mjo  staff  384 17 Feb 16:11 CLItest.pde</code></pre>

<p>To this we&#39;ll add a Makefile:</p>

<pre><code>ARDUINO_DIR = /Applications/Arduino.app/Contents/Resources/Java 
                                            
TARGET       = CLItest                            
MCU          = atmega168                          
F_CPU        = 16000000                           
ARDUINO_PORT = /dev/cu.usb*                       
                                            
ARDUINO_LIBS = LiquidCrystal              
                                            
include /path/to/my/arduino/stuff/Arduino.mk</code></pre>

<p>Hopefully these will be self-explanatory but in case they&#39;re not:</p>

<dl>
<dt><span class="caps">ARDUINO</span>_DIR</dt>
<dd>Where you installed the Arduino software. On the Mac this has to point deep inside the Arduino installation /Applications.  </dd>
<dt><span class="caps">TARGET</span></dt>
<dd>The basename used for the final files. Canonically            this would match the .pde file, but you can choose anything!        </dd>
<dt><span class="caps">ARDUINO</span>_LIBS</dt>
<dd>A list of any libraries used by the sketch&mdash;we assume  these are in $(ARDUINO_DIR)/hardware/libraries.                     </dd>
<dt><span class="caps">MCU</span></dt>
<dd>The target processor (atmega168 for the Duemilanove).           </dd>
<dt>F_CPU</dt>
<dd>The target&#39;s clock speed (16000000 for the Duemilanove).      </dd>
<dt><span class="caps">ARDUINO</span>_PORT</dt>
<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>
</dl>


<p>A couple of other options might be useful:</p>

<dl>
<dt><span class="caps">AVRDUDE</span>_ARD_PROGRAMMER</dt>
<dd>The protocol avrdude speaks&mdash;defaults to stk500v1.           </dd>
<dt><span class="caps">AVRDUDE</span>_ARD_BAUDRATE</dt>
<dd>The rate at which we talk to the board&mdash;defaults to 19,200.</dd>
</dl>


<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>

<pre><code>AVR_TOOLS_PATH   = /usr/bin  
AVRDUDE_CONF     = /etc/avrdude.conf</code></pre>

<h2>Building</h2>

<p>If you&#39;re used to Unix then this is easy:</p>

<pre><code>$ make 
...</code></pre>

<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>

<pre><code>$ ls -l  
total 32 
-rw-r--r--@  1 mjo  staff  384 17 Feb 16:11 CLItest.pde  
-rw-r--r--@  1 mjo  staff  202 17 Feb 16:39 Makefile     
drwxr-xr-x   4 mjo  staff  136 15 Feb 19:53 applet       
drwxr-xr-x  21 mjo  staff  714 17 Feb 17:52 build-cli    </code></pre>

<h3>build-cli</h3>

<p>Let&#39;s peek inside the build-cli directory:</p>

<pre><code>$ ls -l build-cli 
total 392 
-rw-r--r--  1 mjo  staff    491 17 Feb 17:52 CLItest.cpp          
-rw-r--r--  1 mjo  staff    583 17 Feb 17:52 CLItest.d            
-rwxr-xr-x  1 mjo  staff  60971 17 Feb 17:52 CLItest.elf          
-rw-r--r--  1 mjo  staff   3994 17 Feb 17:52 CLItest.hex          
-rw-r--r--  1 mjo  staff   5932 17 Feb 17:52 CLItest.o            
-rw-r--r--  1 mjo  staff   6920 17 Feb 17:52 HardwareSerial.o     
-rw-r--r--  1 mjo  staff  12708 17 Feb 17:52 Print.o              
-rw-r--r--  1 mjo  staff   6852 17 Feb 17:52 WInterrupts.o        
-rw-r--r--  1 mjo  staff   4680 17 Feb 17:52 WMath.o              
-rw-r--r--  1 mjo  staff    611 17 Feb 17:52 depends.mk           
-rw-r--r--  1 mjo  staff   5900 17 Feb 17:52 pins_arduino.o       
-rw-r--r--  1 mjo  staff   8476 17 Feb 17:52 wiring.o             
-rw-r--r--  1 mjo  staff   7224 17 Feb 17:52 wiring_analog.o      
-rw-r--r--  1 mjo  staff   8244 17 Feb 17:52 wiring_digital.o     
-rw-r--r--  1 mjo  staff   6544 17 Feb 17:52 wiring_pulse.o       
-rw-r--r--  1 mjo  staff   7424 17 Feb 17:52 wiring_serial.o      
-rw-r--r--  1 mjo  staff   5308 17 Feb 17:52 wiring_shift.o       </code></pre>

<p>Most of the files in here are object files for the wiring library. What about the others ?</p>

<dl>
<dt><span class="caps">CLI</span>test.cpp</dt>
<dd>This is the .pde sketch file with a small main program prepended and a suitable #include prepended. </dd>
<dt><span class="caps">CLI</span>test.d</dt>
<dd>This tracks the dependencies used by <span class="caps">CLI</span>test.pde </dd>
<dt><span class="caps">CLI</span>test.elf</dt>
<dd>This is executable produced by the linker </dd>
<dt><span class="caps">CLI</span>test.hex</dt>
<dd>This is a hex dump of (the code part) of the executable  in a format understood by the Arduino&#39;s bootloader. </dd>
<dt><span class="caps">CLI</span>test.o</dt>
<dd>The object file we got by compiling <span class="caps">CLI</span>test.cpp. </dd>
<dt>depends.mk</dt>
<dd>A single file containing all the dependency relations  (it&#39;s the concatentation of all the .d files).</dd>
</dl>


<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>

<h2>Uploading code</h2>

<p>This is easy:</p>

<pre><code>$ make upload</code></pre>

<h2>Uploading via <span class="caps">ISP</span></h2>

<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>

<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>

<pre><code>ISP_PORT         = /dev/ttyACM0 
ISP_PROG         = -c stk500v2</code></pre>

<p>Then to upload:</p>

<pre><code>$ make ispload</code></pre>

<h3>Fuses</h3>

<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>

<pre><code>ISP_LOCK_FUSE_PRE  = 0x3f 
ISP_LOCK_FUSE_POST = 0xcf 
ISP_HIGH_FUSE      = 0xdf 
ISP_LOW_FUSE       = 0xff 
ISP_EXT_FUSE       = 0x01
</code></pre>

<h2>Growing the project</h2>

<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>

<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>



<h2>Wiring-less development</h2>

<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>

<pre><code>NO_CORE = 1</code></pre>

<h2>Bugs and problems</h2>

<ul>
<li>The Makefile isn&#39;t very elegant.</li>
<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>
<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>
<li>I fear that the Makefile makes unwarranted assumptions about the   hardware: for example, all my Arduino experience has been ATmega 168   related.</li>
<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>
<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>
</ul>

<h2>Changelog</h2>

<h3>2010-05-21, version 0.3</h3>

<ul>
<li>Tidied up the licensing, making it clear that it&#39;s released under <span class="caps">LGPL</span> 2.1.</li>
<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>
<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>
</ul>

<h3>2010-05-24, version 0.4</h3>

<ul>
<li>Tweaked rules for the reset target on Philip Hands&#39; advice. </li>
</ul>
        <div id="link_list">
          <h2>References</h2>
          <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>
        </div>      </div>
           
      <div id="sidebar">
        <div class="listcontainer">
          <h2><a href="../../index/index.html">Martin's Atelier</a></h2>
          <div id="subscribe"><p><a href="../../feed.atom"><img src="../../atom.png" alt="[ Atom Feed ]" title="Atom Feed" /></a></p></div>
        </div>
        
        
        <div class="listcontainer">        
          <h3>Related Articles</h3>
          <ul>
            
            <li><a href="../03/aarduino.html">The AArduino</a><br/>Martin Oldfield, 16 Jun 2009</li>
            
            <li><a href="avrdude-cookbook.html">Avrdude Cookbook</a><br/>Martin Oldfield, 23 Feb 2009</li>
            
            <li><a href="olimex-avrdude-mac.html">MacOS X and the Olimex AVR-ISP500</a><br/>Martin Oldfield, 17 Feb 2009</li>
            
          </ul>
        </div>
        
  
        
        <div class="listcontainer">        
          <h3><a href="../../index/st_subject.html">Related Subjects</a></h3>
          <ul>
            
            <li><a href="../../index/t_Arduino.html">Arduino</a></li>
            
            <li><a href="../../index/t_command line.html">command line</a></li>
            
            <li><a href="../../index/t_Makefile.html">Makefile</a></li>
            
          </ul>
        </div>
        

        
        <div class="listcontainer">        
          <h3><a href="../../index/index.html">Other Articles</a></h3>
          <ul>
            
            <li><a href="../../2010/06/arm-toolchain.html">Getting an ARM toolchain on MacOS 10.6</a><br/>Martin Oldfield, 06 Jun 2010</li>
            
            <li><a href="../../2010/05/bus-pirate.html">The Bus Pirate on MacOS</a><br/>Martin Oldfield, 26 May 2010</li>
            
            <li><a href="../../2010/04/perl-unicode.html">Unicode games with perl, MySQL, and XML</a><br/>Martin Oldfield, 17 Apr 2010</li>
            
            <li><a href="../../2010/01/gps-track-filter.html">Filtering GPS tracks</a><br/>Martin Oldfield, 04 Jan 2010</li>
            
            <li><a href="../01/paris_drink.html">Places to drink in Paris</a><br/>Martin Oldfield, 15 Dec 2009</li>
            
            <li><a href="../01/paris_food.html">Places to eat in Paris</a><br/>Martin Oldfield, 15 Dec 2009</li>
            
            <li><a href="../11/sons-and-daughters.html">Sons and Daughters</a><br/>Martin Oldfield, 06 Dec 2009</li>
            
            <li><a href="../12/garmin-gpx.html">Faking geocaches in Garmin GPX files</a><br/>Martin Oldfield, 06 Dec 2009</li>
            
            <li><a href="../08/macos-pic.html">Playing with PICs on MacOS X</a><br/>Martin Oldfield, 18 Aug 2009</li>
            
            <li>...</li>
          </ul>
        </div>
        

        
        <div class="lastlistcontainer">
          <h3>Bookmark this article</h3>
        <ul class="social">
          
          <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>
          
          <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>
          
          <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>
          
          <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>
          
          <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>
          
        </ul>
        </div>
        

        </div>

        <div id="footer">
          Revised by Martin Oldfield on 07 Jun 2010, original version 17 Feb 2009.<br/>
          Contact: Martin Oldfield, ex-atelier@mjo.tc<br/>
          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.
       </div>

        </div>
        
    </div>
    <script src="http://www.google-analytics.com/urchin.js"
    type="text/javascript">
    </script>
    <script type="text/javascript">
    _uacct = "UA-567180-1";
    urchinTracker();
    </script>
  </body>
</html>