The article describes how to quickly launch a
USB bootloader for an ATmega32 microcontroller using the example of an
usbasploader bootloader from Objective Development.
The USB bootloader technology provides a single opportunity - no need for a special programmer to replace the software (
firmware ) in the device - just connect to a computer via USB. There is no need to carry a programmer with you anymore, as a computer and USB are everywhere. If your breadboard model is equipped with a bootloader, you can save money on buying a programmer or time to make it - this is important for beginners.
A lot of bootloaders have been made for AVR - see [
1 ]. For the article, I chose usbasploader [
2 ] because it is compatible with the very popular
USBasp programmer. This allows you to reflash the firmware both under Linux and under Windows using popular programs (see [3]) avrdude, eXtreme Burner - AVR, Khazama AVR Programmer and even from the BASCOM-AVR programming environment (the BASCOM-AVR programming system - a small miracle worthy of a separate article). In addition, usbasploader comes with all sources, is well documented and easily adapts to the needs of the user (more on this later).
')
The usbasploader loader works very simply - when connected to a USB, it pretends to be a USBasp programmer. Therefore, all programs that support it will overwrite the firmware in your device, as if they are using a USBasp programmer. The bootloader is located in the higher addresses of the flash memory of the microcontroller's programs, and writes the user program to lower addresses (usually starting at address 0), that is, when the microcontroller is reflashed, the bootloader does not overwrite. After the end of the recording, the bootloader transfers control to the user program. Now in more detail, how it works with ATmega microcontrollers, on the example of
ATmega32 .
For ATmega32 usbasploader compiled so that it is located in flash from the address 7000h (recall that the address space of ATmega32 program memory is 0000h..7FFFh, and the command address is a multiple of two bytes, i.e. the command addresses are in the range 0000h..37FFh) . For the user program, the space is 0000h..6FFFh (28672 bytes). The usbasploader loader uses the ability of the ATmega32 microcontroller to start from the address located in the higher memory addresses of programs (several fixed addresses are available that are selected by jumpers - fuses, see [
4 ]). So that when power is turned on and reset, control is always transferred to the address 0x7000, you must program the jumpers (fuses, fuse-bits)
BOOTSZ0 and
BOOTSZ1 to the appropriate state (4000h bytes or 2048 words of the program code should be allocated to the bootloader), and also program the jumper
BOOTRST . After this, the code when resetting or turning on will start not from address 0, but from address 3800h in the words of AVR commands, or from address 7000h in flash bytes (recall that the minimum size of an AVR command is two bytes).
Having received control after a reset, the usbasploader code checks the condition of its activation, that is, the signal by which it should start working as a USBasp programmer. Usually such a signal is the closure of some legs to the ground. For the prototyping board, I chose the ATmega32
PB5 microcontroller as such a foot. This leg is also the MOSI signal outputted to the connector, so the jumper between pins 4 and 6 of the U1 ISP connector is very convenient to send a log to PB5. 0. So, the usbasploader code checks the level on the PB5 port, and if there is a log. 0 (the jumper between legs 4 and 6 of the U1 ISP connector is installed), then work begins as a USBasp programmer. That is, when the jumper is installed, the bootloader is activated when the power is turned on, and if it is connected to a computer, a USB USB device will appear on the computer. If there is no jumper at the time of power-on (the logger 1 is read by the microcontroller on the PB5), usbasploader immediately transfers control to the user program (to address 0). Here is a simple startup algorithm.
In usbasploader it is possible to change the behavior of the bootloader, for this it is enough to edit three functions (they are very simple and are in the bootloaderconfig.h file) -
bootLoaderInit ,
bootLoaderCondition ,
bootLoaderExit . The function assignment is almost obvious by name. The bootLoaderInit function is intended to configure the environment in which the condition for activating the bootloader can be monitored:
static inline void bootLoaderInit( void ) <br>{<br> DDRB |= (1 << PB0); // <br> // PB5 (MOSI, 4 U1 ISP) pull-up <br> PORTB |= (1 << PB5)|(1 << PB0); <br>}
The code is very simple - it only connects the pull-up internal pull-up resistor on the PB5 leg to determine if there is a jumper between pins 4 and 6 of the U1 ISP connector, and lights the red LED on the layout. The bootLoaderCondition function is designed to check whether or not there is a jumper between pins 4 and 6:
static inline uint8_t bootLoaderCondition() <br>{<br> if (!(PINB & (1 << PB5))) <br> {<br> return 1;<br> } <br> else <br> {<br> // no boot loader <br> return 0;<br> }<br>}
If the jumper is, then PB5 leg reads like a log. 0, and the bootLoaderCondition function returns 1 (which means the bootloader is working). If there is no jumper, the function will return 0, which means inactivity for the bootloader (control is immediately transferred to the address 0 - to the user program). The bootLoaderExit function does not do anything for me, only turns off the red LED of the layout:
static inline void bootLoaderExit( void ) <br>{<br> PORTB &= ~(1 << PB0); // <br>}
The code for the bootLoaderInit, bootLoaderCondition, bootLoaderExit functions can be considered as an example - they can and should be altered to suit your needs, and then usbasploader will work exactly as you want. In conclusion, I will describe the process step by step on the Windows platform (it is assumed that you have already installed AVRStudio and WinAVR environment. If not, read the instructions on how to install them, refer to [
6 ]).
[
How to embed usbasploader into your project ]
1 . You need to download the latest version of usbasploader (see [
2 ]), for example
USBaspLoader 2009-03-20.zip . If you have a prototype AVR-USB-MEGA16 card, then I suggest downloading the version from the link [
5 ] - everything is ready there, and you can skip steps 2, 3, 4. You unpack in any convenient folder.
2 Edit the settings in the Makefile. There you need to change:
a) the definition of F_CPU is the frequency in Hz at which the microcontroller operates. Domostimy frequencies are 12, 15, 16, 16.5 and 20 MHz.
b) determining DEVICE for your type of microcontroller.
c) address (format hexadecimal, units in bytes) download code usbasploader BOOTLOADER_ADDRESS.
d) (optional, if you are flashing a chip from a non-Makefile), check and, if necessary, correct the definitions of FUSEOPT and LOCKOPT, as well as the definition of AVRDUDE.
3 Edit bootloaderconfig.h. There you need to check and, if necessary, change:
a) USB_CFG_IOPORTNAME macro - the letter of the name of the port to which the USB signals D- and D + are connected.
b) USB_CFG_DMINUS_BIT and USB_CFG_DPLUS_BIT macros are the numbers of the ports with which the D- and D + signals are connected. The D + signal must be connected to the INT0 interrupt leg.
c) the function code bootLoaderInit, bootLoaderExit and the bootLoaderCondition macro.
4 Recompile the project, to do this, type make. At the command prompt, you will see something like:

After successful compilation, you will receive the main.bin and main.hex files in the project root folder - ready firmware for usbasploader. By the way, in the folder hexfiles there are already several compiled versions of firmware for crystals ATmega8, ATmega88, ATmega168, on different frequencies of quartz.
5 It is necessary using the programmer to flash usbasploader code into the chip, install the fyuse correctly. The meaning of this operation is that the usbasploader code must be put in the upper memory area (at BOOTLOADER_ADDRESS), and the fyuzas are installed in such a way that the bootloader code started running (when I reset or turned on) (I already wrote about this). For details on fusion, see link [
4 ] and datasheet to your microcontroller. For an ATmega32 microcontroller, for example, fuses should be set as follows:
LOW FUSE BYTE : 0x
CFHIGH FUSE BYTE : 0x
D8 (you can also 0x98 to enable JTAG debugging)
LOCKOPT BYTE : 0x
EFThe AVR-USB-MEGA16 development board can be purchased with usbasploader already stitched and fused, so steps 1, 2, 3, 4, 5 need not be done.
6 It is necessary to connect the flashed prototype board to the computer via USB. If nothing is messed up, the layout will be defined in the system as a new device and Windows will ask for a driver. The driver can be downloaded from the page [
2 ], or taken from the archive by reference [
5 ]. For Linux, the driver is not needed.
7 We need one of the programs that work with the USBasp programmer (see references [3]). For Linux users, avrdude is suitable, and for Windows users, the choice is very wide. I recommend Khazama AVR Programmer with a very simple and user-friendly interface.


[
How to work with usbasploader using the example of the AVR-USB-MEGA16 and Khazama AVR Programmer layout ]
1 . Place a jumper between legs 4 and 6 of the U1 ISP connector.
2 Connect the mockup to the computer via USB. A red LED will light up on the layout, and a USBasp programmer will be detected in the Windows system.
3 Run the Khazama AVR Programmer. In the settings, remove the option to clear the crystal memory (Command -> Program Options -> uncheck the Erase Chip checkbox). Select your chip from the drop-down list. Download the hex file of the firmware (via the menu File -> Load FLASH file to Buffer). Press the large Auto Program button that starts programming. A crystal is programmed very quickly, in a few seconds.

After programming, the red LED goes out, and your program starts from address 0 (which you have just recorded).
4 Remove the jumper between legs 4 and 6 of the U1 ISP connector.
UPD100711 :
wrote an article about another USB bootloader -
BootloadHID . The source code and style of work is very similar to USBASPloader. It differs in that it requires a special program on the computer, but it is easier to integrate it into younger chips (starting with ATmega8), since the requirements for the size of the bootloader section are reduced.
[
Links ]
1 .
Boot loaders for AVR microcontrollers .
2 Home page of usbasploader .
3 Programs for working with the USBasp programmer -
AVRDUDE ,
BASCOM-AVR ,
Khazama AVR Programmer ,
eXtreme Burner-AVR .
4 Engbedded Atmel AVR® Fuse Calculator - AVR jumper calculator.
5 My version is usbasploader, sharpened for a AVR-USB-MEGA16 mock-up board with an ATmega32 microcontroller (a project for AVRStudio with sources and compiled versions for 12 MHz, 16 MHz quartz).
6 USB device development - how to get started with AVR USB (V-USB) and libusb libraries .
7 USBasp bootloader with XOR-encryption .