Full Version : New Mega32 Bootloader (AVR ASM)
avr >>ASSMBLER ROUTINES >>New Mega32 Bootloader (AVR ASM)


AVR_Admin- 05-16-2006
New AtMega32 Bootloader by Agner

QUOTE
I have made a new bootloader for ATMega32 which I am using for teaching a course in microprocessor technology. It is based on the code published by Peter Dannegger in 2003 with various modifications. It sends the hex file and possibly eep file from a Windows PC to an AVR prototype board through an RS232 cable or USB cable.

I am posting it here including source code in case somebody wants to use it.

I am using the Olimex prototype board (http://www.olimex.com/dev/avr-p40-usb-8535.html) but you may use any other prototype board. Modifications may be needed if the clock frequency is not 10 MHz.


Bootloader for AVR ATMega32
2006-02-12 Agner Fog

This is a bootloader for AVR microcontrollers based on the code by Peter Dannegger, Nov. 2003. It is modified to run from Windows.

There are two parts:
1.avrupload.exe: Runs on a Windows computer. Sends .hex file to flash memory and .eep file to EEPROM through RS232 connection. C++ source code and exe file in AVRupload_source.zip.

2.m32boot.hex: Runs on AVR. Receives data from avrupload.exe and stores them in flash and EEPROM. Asm source code and .hex file in M32boot_source.zip.

The main differences to the code published by Peter Dannegger are:

avrupload runs from Windows. The old version works only under DOS.

automatic detection of COM port number.

a timeout limit for COM port search can be specified. This is necessary when avrupload is called from a make utility.

There is no automatic baud rate detection in m32boot.hex. The connection speed is 19200 Baud 8N1. It assumes that the clock speed is 10 MHz. Modifications are needed if you have another clock speed.

The bootloader is designed for the Olimex prototype board, which has a pushbutton connected to port B bit 4, active low. This button escapes out of the bootloader and into the previously loaded program.

The prototype board is available from Olimex. See http://www.olimex.com/dev/avr-p40-usb-8535.html

The prototype board has a USB to RS232 converter chip. It connects to a PC through a USB cable, which also serves as power supply. You may use any other prototype board as long as you connect a pushbutton to port B bit 4, active low. A LED connected to port B bit 0, active low, shows when the bootloader is activated.

I am using this prototype board for teaching purposes. I have put a 26-pin connector on the prototype board for access to power, ground, and the port bits that are not already in use. My students connect an LCD display, a shift register for SPI, and other devices to this connector.

I have included the instructions and examples that I give to my students. The file template.c has #define switches for setting up timers and external interrupts. There are also driver functions for LCD display, SPI output, and serial communication with PC.

File list:
readme.doc: this file
AVRupload_source.zip: source code and exe file for avrupload
M32boot_source.zip: source code and hex file for m32boot
avr-p40-usb-sch.gif: schematic diagram of Olimex prototype board
AVRTemplate.zip: template C program and various driver functions
example1.zip: test example for serial communication with PC
example2.zip: test example for LCD display
programAVR.doc: instructions for my students for installing compiler etc.
CODE

;*************************************************************************
;*                                              *
;*    ATMEGA Bootloader                                *
;*                                              *
;*************************************************************************
; This is a bootloader for Atmel Atmega32 microcontroller kit.
; Can be used with AVR-P40-USB-8535 prototype board from www.olimex.com.
; Originally programmed by Peter Dannegger, Nov. 2003.
; Modified by Agner Fog, Sep. 2005.

; Build instructions:
; =====================
; Build with AVR Studio 4.
; Upload to AVR by JTAG.

; Set the following fuses:
; On-Chip Debug Enabled
; JTAG Interface Enabled
; Serial Program downloading enabled
; Boot Flash section size = 512 words
; Boot Reset vector Enabled
; Brown-out detection level at VCC=4.0V or 2.7V
; Ext. Crystal/Resonator High Freq.; Start-up time: 16K + 64 ms

; Use instructions:
; =====================
; Use the accompanying program AVRupload.exe to upload from PC using
; RS232 serial interface or USB cable with USB to serial converter on
; the prototype board.

; Baud rate = 19200 Baud, 8 data bits, no parity, 1 stop bit.

.nolist
.include"m32defr.inc"
.include"bootload.inc"
.list
;
.equ signature = 0x1E9502;Mega32
;.equ signature = 0x1E9501;Mega323
;.equ signature = 0x1E9403;Mega16
;.equ signature = 0x1E9404;Mega162
;.equ signature = 0x1E9307;Mega8
;
.equ base = SECONDBOOTSTART
.org base
rjmp init
.org base + OVF1addr
rjmp OVF1int
.org base + URXCaddr
rjmp URXCint
.org base + NOINTaddr;end of interrupt vector table
init:
.include"init.inc"
;
;------------------------------------------------------------------------
; sbi LED_DDR, LED0 ;test LED


; rcall set_baud ;set baud rate
rcall uart_init ;set baud rate 19200

ldi     a0, 1
out     DDRB, a0
cbi     PORTB, 0    ; turn on LED

  ; Timeout. Not implemented
; ldi     timeout, $ff

main:  ; command loop
;   dec     timeout
; breq  timeout_exit

   sbis PINB,4             ; check pushbutton
   rjmp    button_pressed     ; go to user program if button pressed

   cbr  flags, 1<<f_error;clear error
rcall rx_command     ;Command received ?
brcc main
rcall exec_command ;Command execute
ldi     a0, key_failed ;execution failed
sbrs flags, f_error ;test
ldi     a0, key_done ;execution successful
sbrc flags, f_enable
rcall putchar      ;send command execution status
rjmp main

button_pressed:
; Wait till button is up again to avoid short circuit in case..
; user progam has PORTB.4 as output
   sbis PINB,4
rjmp    button_pressed
; debounce:
   ldi     a1, $ff
debounce_loop:
   sbis PINB,4
rjmp    button_pressed
   ldi     a0, $ff
inner_delay_loop:
   dec     a0
breq    inner_delay_loop
   dec     a1
breq    debounce_loop

; Debounce finished. Jump to user program
jmp     userprog
;-------------------------------------------------------------------------
;.include"uart.inc"
.include"uart19200.inc"
.include"commexec.inc"
.include"asc2hex.inc"
.include"commands.inc"
.include"read.inc"
.include"program.inc"
.include"userprog.inc"
.include"abaud.inc"
;-------------------------------------------------------------------------

Link: http://www.avrfreaks.net/index.php?name=PN...fd0263618081174


Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.