Jonathan Thomson's web journal

Project Ouroboros — Reflashing a betemcu USBasp Programmer August 18, 2011

Filed under: Electronics — jethomson @ 11:40 pm

*** Major update of article and code in 2019 ***

 

Introduction
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.

 

Terminology
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.

 

Getting started
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:
:2000000000000101020203030404050506060707080809090A0A0B0B0C0C0D0D0E0E0F0FF0
:2000200010101111121213131414151516161717181819191A1A1B1B1C1C1D1D1E1E1F1FD0
:2000400020202121222223232424252526262727282829292A2A2B2B2C2C2D2D2E2E2F2FB0
:2000600030303131323233333434353536363737383839393A3A3B3B3C3C3D3D3E3E3F3F90
:2000800040404141424243434444454546464747484849494A4A4B4B4C4C4D4D4E4E4F4F70
:2000A00050505151525253535454555556565757585859595A5A5B5B5C5C5D5D5E5E5F5F50
:2000C00060606161626263636464656566666767686869696A6A6B6B6C6C6D6D6E6E6F6F30
:2000E00070707171727273737474757576767777787879797A7A7B7B7C7C7D7D7E7E7F7F10
:2001000080808181828283838484858586868787888889898A8A8B8B8C8C8D8D8E8E8F8FEF
:2001200090909191929293939494959596969797989899999A9A9B9B9C9C9D9D9E9E9F9FCF
:20014000A0A0A1A1A2A2A3A3A4A4A5A5A6A6A7A7A8A8A9A9AAAAABABACACADADAEAEAFAFAF
:20016000B0B0B1B1B2B2B3B3B4B4B5B5B6B6B7B7B8B8B9B9BABABBBBBCBCBDBDBEBEBFBF8F
:20018000C0C0C1C1C2C2C3C3C4C4C5C5C6C6C7C7C8C8C9C9CACACBCBCCCCCDCDCECECFCF6F
:2001A000D0D0D1D1D2D2D3D3D4D4D5D5D6D6D7D7D8D8D9D9DADADBDBDCDCDDDDDEDEDFDF4F
:2001C000E0E0E1E1E2E2E3E3E4E4E5E5E6E6E7E7E8E8E9E9EAEAEBEBECECEDEDEEEEEFEF2F
:2001E000F0F0F1F1F2F2F3F3F4F4F5F5F6F6F7F7F8F8F9F9FAFAFBFBFCFCFDFDFEFEFFFF0F
...

 

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 bootloaderconfig.h and 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.

 

Arduino
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

usbasploader_board.vid.0=0x16c0
usbasploader_board.pid.0=0x05dc

usbasploader_board.upload.tool=avrdude
usbasploader_board.upload.protocol=usbasp

usbasploader_board.bootloader.tool=avrdude
usbasploader_board.bootloader.unlock_bits=0x3F
usbasploader_board.bootloader.lock_bits=0x0F

usbasploader_board.build.f_cpu=12000000L
usbasploader_board.build.board=AVR_USBASPLOADER_BOARD
usbasploader_board.build.core=arduino
usbasploader_board.build.variant=standard

usbasploader_board.menu.cpu.atmega8=ATmega8

usbasploader_board.menu.cpu.atmega8.upload.maximum_size=6144
#usbasploader_board.menu.cpu.atmega8.upload.maximum_data_size=
usbasploader_board.menu.cpu.atmega8.upload.speed=115200
usbasploader_board.upload.disable_flushing=true

usbasploader_board.menu.cpu.atmega8.bootloader.low_fuses=0xBF
usbasploader_board.menu.cpu.atmega8.bootloader.high_fuses=0xC0
usbasploader_board.menu.cpu.atmega8.bootloader.extended_fuses=0
#usbasploader_board.menu.cpu.atmega8.bootloader.file=

usbasploader_board.menu.cpu.atmega8.build.mcu=atmega8

 

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

 

Nota Bene
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.

 

Code
Project Ouroboros on GitHub

 

Links
AVR Fuses HOWTO Guide
LadyAda’s AVR Tutorial — Fuses
A very helpful fuse calculator
I used the original USBaspLoader code, but here is a fork that might be useful to you

Advertisements