September 7, 2019

Convoy S2+ firmware - Arduino as a device programmer

You can buy a dedicated USB dongle to do this for you for about $20. What you want is the USBasp. But I want to get things done today, and people just grab an Arduino and set it up to be an AVR programmer.

Install Arduino

I did this years ago and hated it, but it may be less of a mess these days. I go the the arduino download page, dodge their solicitation to contribute money, select linux and 64 bit and download arduino-1.8.9-linux64.tar.xz, which is a bloated 180M tarball.

I make a directory /opt/arduino and untar the mess there. This yields /opt/arduino/arduino-1.8.9. As per the instructions, I cd into that directory, and run:

./install.sh
Adding desktop shortcut, menu item and file associations for Arduino IDE...
rm: cannot remove '/usr/local/bin/arduino': No such file or directory
Removing symlink failed. Hope that's OK. If not then rerun as root with sudo.

rm: cannot remove '/usr/local/bin/arduino': No such file or directory
Removing symlink failed. Hope that's OK. If not then rerun as root with sudo.

 done!
I am running this as "tom" on my system, and /usr/local/bin is owned by root, so of course the script cannot screw around there. They never told me it might want or need to. In spite of all that, I do have an Arduino icon on my desktop now, and it does seem to launch the Arduino GUI.

Use Arduino

The first thing is to get the GUI set up to work with my board. I have two choices in my hands. One is an ancient Arduino Duemilanove, mine is based on the Atmega 328 chip. The other is a Sparkfun Pro Micro, based on the Atmel Atmega 32U4 chip. I don't see the Duemilanove in the current list, so I am going to try to work with the Pro Micro. Mine has a 16 Mhz crystal, so apparently it is the 5 volt variant, which is good. I follow these instructions on the Sparkfun product page: I paste the URL for the sparkfun boards, then use the board manager under tools and install the sparkfun collection. Using File -- Examples -- ArduinoISP gives me a "sketch" for the ArduinoISP that I need! Using locate on my system, I find the sketch itself at /opt/arduino/arduino-1.8.9/examples/11.ArduinoISP/ArduinoISP. It is 719 lines of C++.

Try the hardware

I find a cable, and plug my Pro Micro into my linux machine. Nothing shows up in the logs about a USB device, but the LED on the micro starts blinking. So no doubt I loaded some blink demo independent of any kind of a boot loader on this and it won't work as-is with the Arduino GUI.

The Duemilanove looks more promising. I plug it in using the same cable and see:

Sep  7 16:23:38 trona kernel: usb 2-1.8: new full-speed USB device number 3 using ehci-pci
Sep  7 16:23:38 trona kernel: usb 2-1.8: New USB device found, idVendor=0403, idProduct=6001, bcdDevice= 6.00
Sep  7 16:23:38 trona kernel: usb 2-1.8: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Sep  7 16:23:38 trona kernel: usb 2-1.8: Product: FT232R USB UART
Sep  7 16:23:38 trona kernel: usb 2-1.8: Manufacturer: FTDI
Sep  7 16:23:38 trona kernel: usb 2-1.8: SerialNumber: A6008bvC
Sep  7 16:23:38 trona kernel: usbcore: registered new interface driver ftdi_sio
Sep  7 16:23:38 trona kernel: usbserial: USB Serial support registered for FTDI USB Serial Device
Sep  7 16:23:38 trona kernel: ftdi_sio 2-1.8:1.0: FTDI USB Serial Device converter detected
Sep  7 16:23:38 trona kernel: usb 2-1.8: Detected FT232RL
Sep  7 16:23:38 trona kernel: usb 2-1.8: FTDI USB Serial Device converter now attached to ttyUSB0
Sep  7 16:23:38 trona mtp-probe[5515]: checking bus 2, device 3: "/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.8"
The question now is what I can select for a board in the Arduino GUI to get this to work. Both the Uno and the Nano are based on the AT328 chip, like my Duemilanove. The Nano is a tiny form factor that could be placed on a breadboard.

Actually Tools -- Board has a menu that offers me the Duemilanove, so all this is moot. However the Uno is essentially the modern version of the Duemilanove.

I select File -- Examples -- Basics -- Blink. Then Sketch -- Compile/Verify -- Upload. This works, and the LED labeled "L" is now blinking slowly.

Now I select File -- Examples -- ArduinoISP and do the same. This works fine and I get the following messages:

Sketch uses 4402 bytes (14%) of program storage space. Maximum is 30720 bytes.
Global variables use 482 bytes (23%) of dynamic memory, leaving 1566 bytes for local variables. Maximum is 2048 bytes.
Now the question is how to wire this up to my 8 pin clip that attaches to the ATTiny13A. The sketch has a bunch of comments as follows:
Pin 13 on my board is SCK
Pin 12 on my board is MISO
Pin 11 on my board is MOSI
Pin 10 should be connected to RESET
pin 9 may be an LED to show "heartbeat" that the "programmer" is running.
pin 8 may be an LED to show "error" if something goes wrong.
pin 7 may be an LED to show communication with the slave.
The SPI interface pins are found on the ICSP header, which will have MISO, MOSI, SCK, Gnd, Vcc. On some boards (including my Duemilanove) these pins are also on 11, 12, and 13.

I wire up an LED to pin 9, and indeed it is not just blinking, but "breathing". This confirms that the Duemilanove is actually running the ISP code, not just lurking in the bootloader.

Now we can make the connections from the Arduino to the 8 pin clip that will go to the ATTiny.

Arduino 5v  -- pin 8
Arduino Gnd -- pin 4
Arduino D10 -- pin 1
Arduino D11 -- pin 5
Arduino D12 -- pin 6
Arduino D13 -- pin 7

Get avrdude and give it a go

This is available as a Fedora package, so:
su
dnf install avrdude
This gives me version 6.3-15 for Fedora 30.

Here are some examples of avrdude command lines that use the usbasp device that I do not have.

avrdude -p attiny13 -c usbasp -U flash:w:main.hex:i -F -P usb
It is apparently entirely possible to program the ATTiny13a using only the Arduino GUI as per the following. Note that we do NOT want to load a bootloader into the ATtiny13, so ignore any instructions that tell you to do that. That might be fine for easy experimenting with the arduino GUI, but will use some space in flash we are not willing to give up.
Feedback? Questions? Drop me a line!

Tom's Light Info / tom@mmto.org