Dragino-chipKIT-Yun Yum Yum...

My latest acquisition, the Dragino Yun Shield is actually quite a nice bit of kit. It's the Linux portion of an Arduino Yun placed on a shield, so you can attach it to any board of your choosing.

It provides both a UART and an SPI connection to whatever it is plugged in to. And that includes chipKIT boards.

For those of you not familiar with the Arduino Yun, it's a combination MIPS SoC (Atheros) like you get in most home WiFi routers coupled with an Atmel microcontroller (32U4). Personally I always felt it was such a shame that a board as cool as the Yun should be crippled with a piece of Atmel like that. But hey ho. And then the Dragino arrived and all was forgiven. Gone is the Atmel, and in its place is the lovely PIC32MX695F512L on a chipKIT WF32. Or any other chipKIT board of your choice that you should happen to be drooling over at the time.

The Dragino, like the Yun, runs a custom version of OpenWRT called Linino. It's basically a cut-down version of Linux with a few tools on it for programming and communicating with Arduino microcontrollers. It doesn't, of course, have any support for chipKIT boards, which is what I have been spending my time rectifying over the past three or four days.

So you plug the Dragino in to your board of choice, hoping to just get right on and make it sing and dance. But it's not quite as straight forward as all that. There's a few things you need to know first:

Power

The Dragino is thirsty. So you need to give it plenty of power. It's been designed to draw its power direct from the VIN pin of the board it's connected to. That means on normal Arduino boards, and on many chipKIT boards, you have to power it through the barrel jack, or nothing at all will happen. Some other chipKIT boards, though, like the WF32, have a set of jumpers that route the power around the place.  These can allow the 5V from the USB or other locations directly through to the VIN pin, which is enough for the Dragino to run happily.

But don't go and power it from the normal USB port right away, because there's another catch there.

Reset

The Dragino uses the RESET pin of the 6-pin SPI header on Arduino boards to reset the board. However, most chipKIT boards don't have that RESET pin connected to anything. So you will need to manually wire the RESET pin of the 6-pin header up to the RESET pin over by the power pins. A simple jumper wire on the Dragino from the pin on the 6-pin header that is closest to the reset pin by the power will do the job.

UART

When chipKIT boards that have an FT232 chip on them (which is most of them) have power applied to the USB port the FT232 chip activates. That then takes over the UART pins of the board. And that causes a conflict. With both the FT232 and the Dragino trying to use the UART pins to communicate neither of them manage to do it. And that means that you won't be able to program your board through the USB without disconnecting the Dragino. And that's a major drag. Also, the Dragino won't be able to communicate with the PIC32, and that is even more of a major drag.

Still, if you log in to the Dragino through the network (using ssh as you would with an Yun) it is possible to disable the Dragino's UART connection (thanks to the cunning use of a level shifter with Output Enable) so that the chipKIT becomes programmable:

# echo 0 > /sys/class/gpio/gpio24/value

And then you can turn it on again with:

# echo 1 > /sys/class/gpio/gpio24/value

But now, thanks to the work I have been doing for the past few days (most of which was working out how to do what I wanted to do) there is a better way.

Pic32prog

I have built a special custom version of pi32prog - the program used to upload firmware to chipKIT boards - that runs on the Dragino. It took some doing, and a few bugs were discovered along the way (not least the fact that pic32prog doesn't like running on big-endian systems). Add to that some slight strange things about the Dragino (and Yun) UART interface that threw me somewhat for a while, and you can understand why it took 4 days to get working.

Still, there is now a special pic32prog package for the Dragino available:

You can either install that using the Dragino web interface, or from the command line:

# opkg install http://uecide.org/pic32prog_2.0.189-1-f2988c6_ar71xx.ipk

That should give you everything you need to program a chipKIT board (that uses the UART bootloader, so not the Lenny which uses the USB bootloader) from the command line of the Dragino - including a handy script that deals with all the foibles of the UART interface.

# run-pic32prog Blink.hex

That script does a number of things:

  • Disable the login on the UART (used for the Bridge library)
  • Enable the UART if it's disabled
  • Reset the chipKIT board
  • Run pic32prog
  • Restore the UART state to what it was before
  • Re-enable login on the UART

UECIDE

(Note: this section is out of date now. UECIDE has moved on and improved since writing this part.)

It is possible to couple this with UECIDE to make the programming completely transparent. One click can compile your sketch, copy the HEX file to the Dragino, and execute run-pic32prog to program the board. And all without even having to be in the same room as the chipKIT.

You do need to make some small changes to the Dragino though so that UECIDE can find it, and then too you need to make some changes to UECIDE so that it knows what to do with it.

Fist off let's advertise what we have. That means making an Avahi service. Avahi is the mDNS server of Linux and is the system used to find services that are on a local network. So let's make the service. This is a file called /etc/avahi/services/chipkit.service and contains:

<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
        <name replace-wildcards="yes">%h</name>
        <service>
                <type>_chipkit._tcp</type>
                <port>22</port>
                <txt-record>board=wf32</txt-record>
                <txt-record>distro_version=0.1</txt-record>
        </service>
</service-group>

Replace the "board=wf32" with the name of the board you have attached to the Dragino. It doesn't have to be the real name, it just has to be something that identifies the board type. This allows you to have different Draginos with different chipKIT boards on them and it can work out which is which. You will want to remember that name since you will be using it in a moment.

Save that file then restart the avahi service:

# /etc/init.d/avahi-daemon restart

Now the fact that the Dragino has a chipKIT attached to it should be discoverable. So now you need to change some things in UECIDE to make it try and find the Dragino.  That means finding the "board.txt" file for your board. You can start by opening the "data" directory of UECIDE (there is an option within the Help menu in the Debug sub-menu to get you there). The board will be in the "boards" folder, then a sub-folder within that. Once you have found the board (for instance the WF32 is in boards/chipKIT/chipkit-wf32) you need to tweak the board.txt file.  Add to the bottom of it:

sketch.upload=avrdude::ssh::pic32prog::pickit2

mdns.model.key=board
mdns.model.value=wf32
mdns.version=distro_version
mdns.service=_chipkit._tcp.local.
mdns.programmer=ssh
mdns.class=ssh

board.user=root

upload.ssh.name=Upload via Dragino Yun Shield at ${ip}
upload.ssh.using=script
upload.ssh.quiet=-qq
upload.ssh.script.0=__builtin_echo::Uploading firmware image...
upload.ssh.script.1=__builtin_scp::${build.path}/${filename}.hex::${board.user}@${ip}:/tmp/${filename}.hex
upload.ssh.script.2=__builtin_echo::Programming target...
upload.ssh.script.3=__builtin_ssh::${board.user}@${ip}::run-pic32prog /tmp/${filename}.hex
upload.ssh.script.4=__builtin_echo::Done</pre>

The entry "mdns.model.value=wf32" needs to be changed to match the name you chose in the Avahi service. Now if you use the "Rebuild internal structures" of the Debug menu or UECIDE, or close it and re-open it (or of course load it if you didn't have it running at the time) it should now identify the Dragino in the Hardware->Devices menu. You will also get a new programmer "Upload via Dragino Yun Shield at 192.168.0.38" (or similar). Select that, and you're good to go.

Load your sketch, compile it, and upload away. The first time you upload it will ask for the password for the Dragino. Enter it, optionally telling it to remember (if you don't it will forget it when you quit UECIDE). The file should upload and program automatically:

Compiling...
  • Preprocessing...
  • Compiling sketch...
  • Compiling core...
    ‣ api
  • Compiling libraries...
  • Linking sketch...
Compiling done.
Memory usage
  • Program size: 6212 bytes
  • Memory size: 652 bytes
  • Compilation took 0.365 seconds
Uploading firmware...
Uploading firmware image... 
Programming target... 
Programmer for Microchip PIC32 microcontrollers, Version 2.0
    Copyright: (C) 2011-2015 Serge Vakulenko
      Adapter: STK500v2 Bootloader
 Program area: 1d000000-1d1fffff
    Processor: Bootloader
 Flash memory: 2048 kbytes
  Boot memory: 80 kbytes
         Data: 6212 bytes
        Erase: done
Program flash: ........one
 Verify flash: ........### done
 Program rate: 3084 bytes per second
Done

Enjoy.


Top 6 Things Every Engineer Needs