;************************************************************** ;* avrboot.asm - Assembler implementation of Atmel ATmega series ;* self programming for use with AvrProg.exe. ;* ;* Copyright (C) 2002, Mikkel Holm Olsen (mho@ostenfeld.dtu.dk) ;************************************************************** .INCLUDE "m163def.inc" ;************************************************************** ;* Modify these to fit your needs ;************************************************************** ; Remember to chech the datasheet to make sure the baudrate works ; with the selected colck frequency .EQU CLOCK_FREQ = 1843200 ; Clock Frequency in Hz ; Wait for 5 sec for UART data .EQU INIT_TIMEOUT = 5000 ; (ms) Watch out for overflow if >5 sec ; UART Baud Rate. .EQU BAUDRATE = 19200 ; Device - ATmega163 .EQU DEVICE_ID = 0x66 ; Only one supported device .EQU DEV_ID_0 = 0x1E ; Manufacturer is Atmel .EQU DEV_ID_1 = 0x94 ; 16K Flash .EQU DEV_ID_2 = 0x02 ; ATmega163 ;************************************************************** ;* No user servicable parts below this point ;************************************************************** .EQU UBRR_VAL = (CLOCK_FREQ/(16*BAUDRATE))-1 .EQU TMR_VAL = $10000-(1000*CLOCK_FREQ/1024*INIT_TIMEOUT) .EQU VERSION_HW_H = '1' ; Hardware version 1.0 .EQU VERSION_HW_L = '0' .EQU VERSION_SW_H = '1' ; Software version 1.0 .EQU VERSION_SW_L = '0' .EQU MYBOOTSTART = SECONDBOOTSTART ; Register definitions .DEF tmp =r16 ; Parameters/General purpose .DEF tmph =r17 .DEF utmp =r18 ; UART receive register .DEF addrl =r24 .DEF addrh =r25 .ESEG .ORG (E2END-1) .DB 0x00, 0x00 ; Counter for number of programmings .CSEG .ORG 0x0000 ; Interrupt Vectors INT_VECTORS: jmp DUMMY_RESET DUMMY_RESET: nop rjmp DUMMY_RESET .ORG MYBOOTSTART RESET: cli in tmp, MCUSR cpi tmp, 0x02 ; Reset by H/W reset pin breq PROGRAMMING cbr tmp, 1 out MCUSR, tmp rjmp (FLASHEND+1) ; Since PC wraps, we can do with a rjmp instead of jmp PROGRAMMING: ldi tmp, Low(RAMEND) ; Set stackpointer to last addr out SPL, tmp ; - stack grows downwards ldi tmp, High(RAMEND) ; - variables are at lower addrs out SPH, tmp ; Setup port D (UART) ldi tmp, 0b00000010 ; TX is output, others are input out PORTD, tmp out DDRD, tmp ldi tmp, High(UBRR_VAL) ; Setup baud rate generator according out UBRRHI, tmp ; to value defined in the top of this ldi tmp, Low(UBRR_VAL) ; file (found from Table 27 in the out UBRR, tmp ; the AtMega163 datasheet) ldi tmp, 0b00000000 ; No special features out UCSRA, tmp ; (normal UART speed, No MPCM) ldi tmp, (1<