Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Enough of the STK500 protocol in Erlang to control the Arduino boot loader
Erlang Processing Makefile Shell
tree: c75384c0f5

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
doc
examples/counter
include
src
.gitignore
Makefile
README.md
rebar.config
start.sh

README.md

stk500 is an Erlang implementation of enough of the STK500 protocol to talk to an Arduino.

Example

% Open the serial device attached to the Arduino
% The Diecimila uses 19200
{ok,FD} = stk500:open("/dev/ttyUSB0", [{speed, 19200}]),

% Read a hex file generated by the Arduino IDE
%
% hex_file/1 returns a list of binaries as read from the
% file (16 bytes). It's faster to use 128 bytes chunks.

Hex = stk500:hex_file("doc/counter.cpp.hex"),
Bytes = stk500:chunk(Hex, 128),

ok = stk500:load(FD, Bytes),

stk500:close(FD).

doc/counter.cpp.hex is a compiled hex file for testing, generated from the example in examples/counter/counter.pde. To call the example code from Erlang:

1> counter:run(). % loops adding 1 to a counter

2> {ok, FD} = counter:start("/dev/ttyUSB0").
3> counter:read(FD).
{ok, 0}

4> counter:incr(FD).
ok
5> counter:incr(FD, 5).
6> counter:read(FD).
{ok, 6}

7> counter:set(FD, 3).
ok
8> counter:read(FD).
{ok, 3}

Note on Protocol Dumping

The simplest way to dump the serial communications is by using strace. For example, on an Ubuntu system, to see how the Arduino IDE is calling avrdude:

# as root
cd /usr/share/arduino/hardware/tools

mv avrdude avrdude.dist

cat<<EOF>avrdude
#!/bin/sh

set -e

mkdir /tmp/arduino-$$
strace -e read=3 -e write=3 -v -o /tmp/arduino-$$/dump \
-e trace=open,close,read,write,ioctl \
/usr/share/arduino/hardware/tools/avrdude.dist $@
EOF

An example protocol dump is doc/protocol_trace.txt.

Resources

The STK500 protocol documentation can be found here:

http://www.atmel.com/Images/doc2525.pdf
Something went wrong with that request. Please try again.