*** Major update of article and code in 2019 ***
USBasp is a free, open source firmware and reference design for implementing a USB AVR programmer. It uses simple circuitry and an ATmega8 or ATmega*8 for interfacing with the USB host. A variety of inexpensive boards (~$1.50 US in 2019) based on the USBasp project can be purchased from aliexpress and eBay. Re-programming one of these boards can often be an effective solution for a one-off project where the cost of a full development board or the time consumed designing and assembling a custom board is prohibitive. They can even be made to work with Arduino. This article focuses on the betemcu.cn board pictured here. Note that there are at least two versions of this board.
V-USB — a firmware-only implementation of a low-speed USB device for Atmel AVR microcontrollers.
in-circuit programmer — a device for burning code and changing fuse settings on a microcontroller which remains in its application circuit.
USBasp – a USB in-circuit programmer for Atmel AVR microcontrollers that utilizes V-USB.
bootloader — a bit of firmware capable of burning code onto the same microcontroller on which it resides. A bootloader allows new code to be uploaded to a microcontroller without an external programmer.
USBaspLoader – a USB bootloader that follows the USBasp protocol.
It’s possible to erase the USBasp firmware and load a new program onto the betemcu.cn board via the 10 pin header. First insert a jumper wire into J2 (the two holes circled in yellow in the picture above). Doing this connects RST on the 10 pin header to PC6 (RESET). Now you can use a second programmer to burn the code. However if you put USBaspLoader onto the betemcu.cn board you can use AVRDUDE or Arduino to upload programs directly over USB without having to use an external programmer. Once you have the USBaspLoader on the board there is still enough room left to re-upload the USBasp firmware so that the betemcu.cn board can function as an external programmer again. I call this Project Ouroboros because it’s a play on the word asp and the seeming recursiveness of using USBaspLoader to put USBasp onto a USBasp programmer.
First you’ll need a betemcu.cn USBasp programmer (hereafter betemcu board) and a second, external programmer (hereafter primary programmer) capable of programming AVR microcontrollers over SPI. Next you should test out your betemcu board to make sure it’s not defective. Before you load new code onto your betemcu board you should use the primary programmer and AVRDUDE to attempt to save the betemcu board’s factory installed firmware to your computer and discover its original fuse settings. Doing this will allow you test whether the device is still functional by attempting to restore it to a known good state by writing back the original firmware and fuse settings. The following command will try to save the betemcu board’s firmware and display its fuse settings. The
-c programmer-id option is the ID of the primary programmer. If the betemcu board’s lock bits are programmed to prevent reading off the firmware then factory_firmware.hex will be filled with junk data. There are a variety of these USBasp programmers created by different groups, and not all of them lock the boards so it’s worth trying to save the factory firmware.
avrdude -p atmega8 -c programmer-id -v -U flash:r:factory_firmware.hex:i
factory_firmware.hex junk data:
Fuse settings for burning code to the betemcu board with an external programmer
If you are unfamiliar with microcontroller configuration fuses, then please visit the relevant articles in the Links section to learn more about them. You should also use a fuse calculator to double check fuse settings before applying them. The original settings for my betemcu board were
hfuse = 0xD9 and
lfuse = 0xFF but these leave CKOPT unprogrammed which should be set for crystals faster than 8 MHz. The makefile for USBasp uses
hfuse = 0xC9 and
lfuse = 0xEF but these settings didn’t work for me because the start-up delay isn’t long enough, in fact they (recoverably) bricked the betemcu board. Setting the fuses to
hfuse = 0xC1 and
lfuse = 0xBF sets a proper start-up delay and enables CKOPT. These also set a brown-out detection level of 2.7 V which is necessary if you want to use the onboard 3.3 V regulator for power. From here on I use a fuse setting that sets the brown-out detection level appropriate for a 3.3V board, if your board is 5V this fuse setting won’t hurt your board, but it won’t properly detect a brown-out condition.
high fuse: 0xC1
low fuse: 0xBF (3.3V board)
low fuse: 0x3F (5V board)
avrdude -p atmega8 -c programmer-id -U hfuse:w:0xC1:m -U lfuse:w:0xBF:m
avrdude -p atmega8 -c programmer-id -v -U flash:w:betemcu_blink.hex
To restore the original factory firmware (make sure factory_firmware.hex isn’t junk data!):
avrdude -p atmega8 -c programmer-id -v -U flash:w:factory_firmware.hex
If you don’t have the original factory firmware you can substitute it with a precompiled hex from the Fischl USBasp package, although it will not be exactly the same as the original firmware (see the Nota Bene section below):
avrdude -p atmega8 -c programmer-id -v -U flash:w:usbasp.atmega8.2011-05-28.hex
Hardware modifications required to run vanilla USBaspLoader on the betemcu board
The unmodified version of USBaspLoader connects PD2 and PD4 to the USB data plus and data minus lines. However the betemcu board connects PB1 and PD2 to USB data plus and PB0 to USB data minus. Therefore the USBaspLoader code needs to be changed to use the same pins. Here’s a snippet of
bootloaderconfig.h with the changes already made.
#define USB_CFG_IOPORTNAME B
/* This is the port where the USB bus is connected. When you configure it to
* "B", the registers PORTB, PINB and DDRB will be used.
#define USB_CFG_DMINUS_BIT 0
/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
* This may be any bit in the port.
#define USB_CFG_DPLUS_BIT 1
/* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.
* This may be any bit in the port. Please note that D+ must also be connected
* to interrupt pin INT0!
Vanilla USBaspLoader is set up so that it will only enter self-programming mode (i.e. listen for a program being uploaded and write it to the flash) if PD7 is held low after pulling the reset pin low (i.e. an external reset) occurs. Therefore you will have to make a couple of modifications to the betemcu board. First you’ll want to add a normally open pushbutton connected to the reset pin and ground for producing an external reset. Soldering the pushbutton connection at the reset pin’s pull-up resistor is easier than soldering it directly to the pin.
For the second modification you’ll have to determine how you want to signal the bootloader to wait for a program. The typical method is to use a jumper to connect PD7 to ground. The NC (not connected) pin of the 10 pin header is conveniently located 0.1″ from one of the header’s ground pins so running a mod wire from PD7 to NC is a simple way to add a jumper header. However, if you plan on interfacing the betemcu board with another circuit through the 10 pin header by using a 10 pin IDC you will want to add the jumper header somewhere else on the board because of the difficulty of inserting and removing the 10 pin IDC while the USB connector is plugged in. Using a pushbutton connected to PD7 and ground instead of a jumper is also an option, but you’ll have to keep the pushbutton pressed until AVRDUDE has finished.
Here’s a picture of my modified board. At first I was using a jumper to connect PC2 (orange wire to NC) to ground, but later I decided to add a second pushbutton to signal the bootloader to wait for a program to be uploaded. The brown and blue wires are for I2C and not a necessary modification.
The pin used to signal the bootloader can be easily changed by modifying
bootloaderconfig.h. If you want to use a different pin make certain it’s not already being used for another function by consulting USBasp schematic and by carefully examining the betemcu board. Keep in mind that pins at the corners of the ATmega8’s package are easier to solder to.
Compiling a new hex file from the USBaspLoader source also requires a few changes to the
Makefile. I’ve created a package of the vanilla version of USBaspLoader preconfigured to work with the betemcu board. I’ve also included a pre-compiled hex in case you have difficulty compiling. You can download these files from the GitHub repo at the end of this article.
Alternate version of USBaspLoader
I wasn’t happy with using a jumper (or pressing and holding a pushbutton) to signal the bootloader to enter self-programming mode (SPM). So I’ve made some changes to the vanilla version and packaged them as an alternate version of USBaspLoader. ***I added an option that compiles the bootloader so that it always enters SPM after every power cycle or reset and uses a timeout to exit the bootloader, which makes hardware modifications to the board unnecessary.*** The alternate version can also be configured so that you can enter self-programming mode by pressing and releasing a pushbutton, which is a more typical method used by other USB development boards with microcontrollers that have a HWB pin. I made a few more modifications to the code to utilize the betemcu board’s LEDs. Here’s a sample of some of the changes I made (look at
main.c to view all the changes):
#define TIMEOUT_ENABLED 1
/* If TIMEOUT_ENABLED is defined to 1 then the bootloader will always load
* and stay active until the programmer closes the connection or the time
* out period has elapsed. Since the bootloader always loads there is no
* need for a jumper on the bootLoaderCondition() pin. Costs ~108 bytes.
#define TIMEOUT_DURATION 5
/* The number of seconds the bootloader waits before exiting if no activity
* has occurred during the timeout interval. If TIMEOUT_ENABLED is defined
* to 0 this define will be ignored. Maximum value is 255 seconds.
#define POWER_ON_RESET_LOAD 1
/* If POWER_ON_RESET_LOAD is defined as 1, then the bootloader will load
* when an external reset or a power on reset is detected. When this option
* is paired with TIMEOUT_ENABLED defined as 1 then zero hardware
* modifications to the board are required. If POWER_ON_RESET_LOAD is defined
* as 0 then the bootloader only loads if an external reset is detected, but
* this requires adding a reset button to the board. Costs ~6 bytes.
#define USING_PUSHBUTTON 1
/* If USING_PUSHBUTTON is defined to 1, then a press and release of a
* pushbutton on the bootLoaderCondition() pin can be used to signal that
* you wish to enter the bootloader. If USING_PUSHBUTTON is defined to 0,
* then you must use a jumper or the pushbutton must be pressed and held
* for the duration of the programming (failing to do so will result in
* the bootloader exiting prematurely).
If you’d like to add a reset button without making a modification to the board, you can easily do so by soldering a pushbutton to a female header. To use it put the female headers onto the RST and GND header pins and insert a jumper wire in J2.
Fuse settings for burning USBaspLoader to the betemcu board with an external programmer
The USBaspLoader hex file for an ATmega8 at 12 MHz is around 1900 bytes so the 2 kB (1024 words) bootloader section is required. These fuses set a boot flash section size of 1024 words (2048 bytes) and a start-up delay of 64 ms.
high fuse: 0xC0
low fuse: 0xBF (3.3V board)
low fuse: 0x3F (5V board)
avrdude -p atmega8 -c programmer-id -U hfuse:w:0xC0:m -U lfuse:w:0xBF:m
avrdude -p atmega8 -c programmer-id -v -U flash:w:alternate_USBaspLoader_2019-02-18_mega8_12MHz_betemcu_pushbutton.hex
If you don’t want to add a SPM jumper or button, you can use the timeout bootloader.
avrdude -p atmega8 -c programmer-id -U hfuse:w:0xC0:m -U lfuse:w:0xBF:m
avrdude -p atmega8 -c programmer-id -v -U flash:w:alternate_USBaspLoader_2019-02-18_mega8_12MHz_betemcu_timeout.hex
Flashing a program with USBaspLoader
Now that you have the alternate USBaspLoader on your betemcu board you can flash new programs to it directly over USB. To instruct your board to enter self-programming mode, start by pressing and holding the program button, next press and release the reset button, then release the program button. The green LED should turn on indicating the board is ready for a program upload. The bootloader doesn’t run when the board is powered on; it needs a reset to run. Upload vusb_mouse_example.hex found in my GitHub repo by using the following command. If the upload is successful you should see the mouse cursor move in a circle.
avrdude -p atmega8 -c usbasp -v -U flash:w:vusb_mouse_example.hex
You can also upload the original USBasp firmware onto the board to through the bootloader to restore its ability to program other AVR microcontrollers.
With USBaspLoader on the board it is now compatible with the Arduino programming environment. You must add some entries to boards.txt to be able to upload a sketch within the Arduino IDE. Follow this link for an explanation of the variables used in boards.txt. Put the following at the top of boards.txt:
usbasploader_board.name=USBaspLoader w/ ATmega8 at 12MHz
Then under Tools->Board select
"USBaspLoader w/ ATmega8 at 12MHz". It is normal to get this message:
"avrdude: warning: cannot set sck period. please check for usbasp firmware update." Note that you’ll need to connect a USB to serial adapter to RX and TX if you want to send data over the UART (i.e. Serial.print()). Also note that the ATmega8 is missing some of the features the ATmega*8 series of chips have. For example, the ATmega8 doesn’t have PWM capability on Timer0. Therefore, this board won’t work with some Arduino sketches.
You may also need to a
pins_arduino.h to your Arduino files for it to work with your board properly. The file
tinyusbboard-betemcu/pins_arduino.h found in this file archive might be useful to you, but I haven’t tried it out.
You can also upload the sketch from the command line with the following command. Notice that you don’t need to set any fuses, in fact the bootloader is incapable of changing fuses.
avrdude -p atmega8 -c usbasp -v -U flash:w:betemcu_blink.ino.hex
Summary of Methods to Enter Programming Mode
External programmer: connect the holes of J2 together for the duration of the program upload.
Vanilla USBaspLoader: connect PD7 to ground, bring the reset pin low, upload program, disconnect PD7 from ground.
Alternate USBaspLoader: 1) cycle the power or bring the reset pin low then upload program before bootloader times out. 2) press and hold PD7 pushbutton, reset, release PD7 pushbutton, upload program
A note on the error message: “avrdude: warning: cannot set sck period. please check for usbasp firmware update.”
If you are using a hardware USBasp programmer and you receive that error message then it indicates you are using an older version of the USBasp firmware that can’t automatically set the sck period. This doesn’t necessarily mean you should overwrite the firmware that came with your board with the newest version of USBasp. Since the firmwares on these boards are customized by the board makers some users have found that overwriting the original firmware resulted in a board that lost some of its capabilities. Follow this link for a discussion on this topic. As a result of that discussion some updated firmwares were created that address some of the flaws in the official Fischl USBasp firmware
If you are using USBaspLoader then you should ignore that error message since a bootloader is incapable of setting the sck period; indeed, it doesn’t make sense for it to even try to.