Hardware projects

An AVR Programmer for the C64

So there I was, tinkering away on a microcontroller project, when all of a sudden my AVR programmer stopped working.

My trusty old AVR Dragon had fallen.

The AVR family of microcontrollers are In-System Programmable (ISP): While they are held in reset, it's possible to upload new code to the on-chip flash memory over a three-wire serial protocol. However, as simple and well-documented as that protocol is, modern computers can't speak it because they lack general-purpose signals to connect to the outside world. Hence the need for a programmer (also known as a probe) that goes between the USB port of a computer and the microcontroller.

Such programmers are available off the shelf, and the simple models are quite cheap. But this malfunction happened on a Friday night and I was already knee-deep in my project. Ordering a new programmer would have meant putting everything on hold for a couple of days and ruining the whole weekend.

Normally, when you want to talk to some hardware via a custom protocol, you might do it using code running on a microcontroller. Only in this case I had a chicken-and-egg problem, because how would I get that code into a microcontroller without a programmer?

A machine that can talk to the world

So I looked around the room. If there had been, say, a Raspberry Pi system on my desk, my problem would have been solved. That's because recent versions of the avrdude tool can run the AVR ISP protocol over the GPIO pins (General-Purpose Input/Output) of that machine. But all my Raspberries were tied up in other builds, tucked away inside enclosures and running ancient software.

Instead my eyes fell on the good old Commodore 64. It's a capable system, and an absolute joy to program. It has plenty of signals that can be used for general-purpose I/O, exposed over a wide range of connectors. And I have a cartridge (a 1541 Ultimate) that lets me load software from a USB thumb drive, so it's easy to transfer data into the machine.

I also knew I had a 9-pin D-Sub connector with a terminal block somewhere, so I dug it out and plugged it into Joystick Port 2. Then I hooked it up to my project:

Reality is messy, but the ISP connection itself is straightforward:

The resistors protect the C64 in case the AVR puts out strong signals while running—as mine does in this project, incidentally.

To keep cost and complexity down, the Commodore 64 was designed with the joystick ports and the keyboard matrix sharing signals. During normal operation, the C64 scans the keyboard sixty times per second, and during this scanning the lines of Port 2 are pulled to ground one at a time. As a side-effect this will periodically reset the AVR chip, so my software (on the C64) disables that interrupt.

A minimal implementation

My program just goes through the motions of the ISP protocol as documented in the AVR datasheets. 253 lines of assembly code produce a 381-byte executable—or rather, part of an executable. You then concatenate this program with the binary firmware image you want to flash into the AVR chip. Thus, on Linux, if you have the firmware in flash.hex in IntelHex format:

avr-objcopy -I ihex flash.hex -O binary flash.bin
cat lft-avrprg64-1.0.prg flash.bin >combined.prg

And then you launch combined.prg on the C64. The program will change the border colour to indicate status:

PurpleChip has responded. Flashing.
YellowVerifying.
RedVerification failed.
GreenSuccess!

Upon reaching the green state, the program puts the three communication lines in a high-impedance state and takes the Reset line high. This allows the newly flashed firmware to run until the C64 is rebooted, at which point the system interrupt handler kicks in to reset the AVR again.

Download

Final remarks

The current version of the software only programs the flash area, but one could easily add support for updating the EEPROM area and fuse bits.

I'm not sure if anybody besides myself will find a use for the program. But the moral of the story is: Don't let a broken tool ruin your weekend! Take a look around the room and hack something together.

Posted Friday 19-Jul-2024 06:35

Discuss this page

Disclaimer: I am not responsible for what people (other than myself) write in the forums. Please report any abuse, such as insults, slander, spam and illegal material, and I will take appropriate actions. Don't feed the trolls.

Jag tar inget ansvar för det som skrivs i forumet, förutom mina egna inlägg. Vänligen rapportera alla inlägg som bryter mot reglerna, så ska jag se vad jag kan göra. Som regelbrott räknas till exempel förolämpningar, förtal, spam och olagligt material. Mata inte trålarna.

Anonymous
Fri 19-Jul-2024 12:41
If not having a c64, trying to use gpio ports on a pc could be fun as well. You could find existing gpios in a pc or laptop and bring them out of the case to then be able to do gpio stuff if needed. ;)
L3P3
Anonymous
Sat 20-Jul-2024 07:28
One morning when I woke up my fridge was warm. Managed to by-pass the thermostat and cobble together a temporary replacement using and old laptop, a temperature probe and a USB-controlled power outlet. Worked until I managed to get a new thermostat. Since then, my wife has been much less negative on me keeping "nice to haves".

Solving problems MacGyver gives you a nice warm feeling in your stomach.
Anonymous
Sun 28-Jul-2024 21:03
I've been following your projects since 2008. It's amazing! Greetings from Ukraine.
Anonymous
Wed 7-Aug-2024 10:17
"Don't let a broken tool ruin your weekend! Take a look around the room and hack something together"

Chapeau bas! Couldn't say it any better :-))