Electronics, Embedded Systems, and Software are my breakfast, lunch, and dinner.
Nov 08, 2020
There is an odd famine of information about this particular subject available in text form. If you google "usbasp tpi" and things like it, you'll find posts on the avr forums filled with incorrect information and schematics. This post is an attempt to remedy this.
I intend to show exactly the following, no more and no less:
TPI is the Tiny Programming Interface found on "somewhat newer" AVR microcontrollers (i.e. made in the last decade or so). There is yet a newer one which has shown up on AVRs post-Microchip acquisition (UPDI) which isn't very publicly documented, but there are utilities like pyupdi which aim to support that interface without a programmer.
TPI is quite a bit smaller than the older ISP interface. Rather than looking like an SPI port, it has just three lines:
The timing of these three lines is described in the part datasheets and isn't complex. It is fairly slow, but since the devices that have TPI don't usually have much program flash, it doesn't really make a difference. The logical details on how the interface works are also documented in the part datasheets, but that logic has already made its way into avrdude.
If you're like me, you bought your USBASP a decade ago from ebay. It likely has very old firmware which isn't capable of TPI programming just yet. Even the "new" usbasps found on ebay have very old firmware. So, you'll need to update the firmware to the newest version (from 2011!!!!) which gave TPI support. There are a few guides on this process, but one text-based one can be found here: https://blog.podkalicki.com/how-to-update-avr-usbasp-firmware-to-latest-version/
I'll summarize here what you need to do:
I want to focus this guide mostly on actually hooking up TPI on a schematic level, so I don't have pictures or anything else here.
This should be easy to find information on, but it really isn't for some reason. Here's the basics:
That's it really. However, some details would probably be useful. The pinout of the standard usbasp ISP headers are shown on the right (note: VTG = VCC at Target, so it's the programmer-supplied voltage).
I used an ATTiny20 in my design, so I have mine wired up like this (note that I tend to leave pins 4 and 6 disconnected since on some programmers they are disconnected):
Aside from the connections, the next thing to worry about is that 5V connection. This isn't 2011 anymore and voltages are getting lower and lower. However, flash still requires upwards of 12V to program properly (gotta get those electrons to jump!) and a charge pump that works at 3.3V is 2x or 4x larger than one that needs 5V. So, it makes sense to me that the designers of the ATTiny20 would have made that tradeoff and required a programming voltage of 5V (4.5V minimum, but you should really use 5V) rather than trying to fit a giant charge pump on an already highly-opmized die.
So, you need to program at 5V, but your circuit uses different voltages. Here are some tips I have for resolving this situation:
If you've got everything wired up properly and the voltage levels are correct, you can run avrdude and get the following beautiful greeting message:
And if you're even more lucky, reflashing will work too!
While I was researching using TPI with my usbasp, I came across a post where someone said something along the lines of "why waste your money on a 6-pin microncontroller? Just use <insert older AVR here>". Despite the obvious downside of needing to use a new programming interface, I think there is one niche where these cannot be beat:
The TPI-only AVRs are BY FAR the cheapest "name brand" microcontrollers you can buy. Between these and the PIC10 family, there is nothing cheaper until you start looking at some chinese suppliers (there's a $0.03-in-single-quantity microcontroller that has become somewhat popular). I chose the ATTiny20 for my project because it was even cheaper than a couple discrete single gate ICs . I needed to do some OR-ing logic which I would have normally accomplished with discrete parts. Each of those in single quantity is like $0.25 and I would have needed at least 4 or 5. The ATTiny20 I picked was $0.53 . It was cheaper to use a microcontroller than discrete logic gates! What a world we live in.
The older ISP AVRs (especially ATMega8 and friends) are losing relevance in the market when faced with pressure from the ARM Cortex-M based microcontrollers. They not only have better peripherals generally, but they also require both less power and have better tooling (compiler, standard programming tools, etc) than the equivalent AVRs. I would never pick an ATMega328 for a new product unless I had to use an existing binary or something. Even in my personal projects, I strongly prefer ARM Cortex-M microcontrollers because they're easier to program and debug without requiring expensive single-vendor hardware.
The intention in this post was to communicate how to program TPI-based AVRs without too much hassle using the usbasp. If I've got a detail missing, something wrong, or this post helped you out, feel free to drop a comment!