Stampduino: Programming

After setting up the hardware, now we have to program the Stampduino. The chips I bought came pre-programmed with Atmel’s bootloader, but if we want to program it through the Arduino IDE, then we’ll need to burn the Arduino bootloader onto it. In order to do this, you’ll need a ISP (in system programming) programmer; in my case, I had an Atmel ICE on hand.

ISP, as it turns out, is very similar to the SPI interface. It has a MOSI, MISO, SCK and chip select. This is used to load the bootloader into the microcontroller’s non-volatile memory as well as read data from the chip. Note that chip select for ISP is the reset pin for the microcontroller; by holding the chip in reset, the chip can be put into a mode that will write provided data to its flash. ATMEGA32U4’s pinout is shown below:

ATMEGA32U4 pinout, ISP pins highlighted

The Atmel ICE has the following pinout:

Atmel ICE AVR port pinout; relevant ATMEGA32U4 pins are written in the table by me

Connect the mini-squid to the Stampduino as indicated in the table above. My setup is pretty crude, but it looks like this:

Powered Stampduino, hooked up to Atmel ICE via squid cable

Now, the Stampduino is ready to be programmed by the Atmel ICE. Do the following steps:

  1. Power up the Stampduino by hooking it up to a USB cable.
  2. Open Atmel Studio 7.0, and open a brand new project (or an example project). The project doesn’t matter; what matters is that you have a project open.
  3. Open Device Programmer (Tools > Device Programming).
  4. Set Tool to Atmel-ICE, Device to Atmega32U4, and Interface to ISP. Then click apply.
  5. Click the read button next to Device Signature to make sure you can talk to the device to be programmed.
  6. Click the memories tab, then the … button under Flash (32 KB). Since we want to burn the Arduino Micro bootloader onto the chip, we’ll need to select that file during this step. When you installed the Arduino IDE, the .hex file that contains the code for the bootloader should have been installed on your computer as well. For me, I found it at C:\Program Files (x86)\Arduino\hardware\arduino\avr\bootloaders\caterina\Caterina-Genuino-Micro.hex. Once the file is selected, hit the Program button.
  7. Now, the bootloader is on the microcontroller, but the chip (probably) isn’t configured properly; to fix this, you need to set the chip’s fuses. To do this, click the Fuses tab and adjust the settings as shown below. Then, click Program.
ATMEGA32U4 fuse settings

I programmed the EXTENDED fuses to 0xFB, but when I read the fuse settings out of an Arduino Micro, I got the screenshot above. My guess is they both work, but according to the datasheet, the high nibble of the EXTENDED fuse byte should be all high:

Extended Fuse Byte from ATMEGA32U4 datasheet

Now the Stampduino is good to go! You should now be able to program the device as if it were an Arduino Micro.

To program application to the Stampduino, do the following:

  1. Open Arduino IDE
  2. Select target board as Arduino Micro (Tools > Board > Arduino/Genuino Micro)
  3. Select the port that the Stampduino is on (Tools > Port > COMXX (Arduino/Genuino Micro)
  4. Hit the Upload button

One weird thing I noticed is that the very first time you program the Stampduino after it has a bootloader burned onto it, it changes COM ports. It doesn’t do it again until you re-burn the bootloader. Strange.

One inconvenience when writing code for the Stampduino is the pin number:

Arduino Micro schematic

Remeber that the IDE thinks the board is an Arduino Micro. So that means if you write to pin 11 in the IDE, that means you’re writing to PB7 on the Stampduino. Pin 10 is PB6, pin 9 is PB5, etc. Now, writing to pins 11, 10 and 9 in your application code when you mean PB7, PB6 and PB5 would be extremely confusing to future historians reading the code, so I wrote header file to address this problem:

Stampduino.h

Instead of writing digitalWrite(11, HIGH) when you want to set PB7 high, now you can write digitalWrite(PAD_B7, HIGH) instead, which is much clearer. Note that PAD_E2 cannot be written to in the Arduino Micro, so it’s not easily accessible to the Stampduino either. You can get around this by using the old fashioned method of writing directly to registers.

Leave a comment

Design a site like this with WordPress.com
Get started