⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 megabootloaderblips.asm

📁 butterfly avrisp for atmel atmega
💻 ASM
📖 第 1 页 / 共 3 页
字号:

;***********************************************
; Atmel AVR Microprocessor 
;  Use this code with Atmel's AVR Studio
;   V3.0
; Dec 06
; Test status
; mega8: 	tested
; mega16:	tested
; mega32:	tested
; mega128: 	tested
; mega168	tested
; m8535:	tested

; HOW TO CONFIGURE THIS FOR A PARTICULAR CHIP
; 1. Edit the .INCLUDE file name for the desired chip, below.
; 2. Edit the crystal clock and baud constants, below.
; 3. If the desired chip is one of the ones with a block of constants, below, you are done.
;    For dual-UART chips, edit the block of constants to choose between UART0 vs. UART1, using the crib sheet table
; 4. IF the desired chip is NOT one of the ones with a block of constants, below, then ...
;    Copy/Paste one of the existing blocks, similar to the chip you wish.
;    Edit the symbol names using the crib-sheet table below or the Atmel data sheet or the include file.
; 5. Assemble this file using AVRstudio with the second generation assembler
; 6. Use your ISP to set the fuses (if not already set) then burn the bootloader .hex file to the chip
; 7. Test the bootloader using the PC side software: BLIPS or AVRProg and a connection to the target
;    chip's serial port via a direct link to PC COMn or a LAN-to-ethernet device or Wireless LAN (use BLIPS on the PC)
; 8. The code as is loops for about 2 seconds after each RESET looking for an escape char from the UART at the.
;	 baud rate setup below.  If it times out, it executes the code at location 0. 
;	    You may want different logic, such as an I/O bit jumper. See code at label "boot_reset:"

;========================================================================================
;
;* This is a modified version of design note #032 'DN032' From http://www.avrfreaks.com
;** Modified by stevech@san.rr.com aka childresss@gmail.com
; 	1. Support BLOCK MODE TRANSFERS FOR FLASH WRITE/READ/VERIFY (EEPROM is byte-at-a-time)
;	2. Support RAMPZ bit for >64K Byte flash chips
;	3. Changed code so that switching chips doesn't affect code, just constants (see below)
;
; Conforms to Atmel's original serial bootloader protocol as in app note AVR109
; PC side can be any that conforms to this protocol, e.g., AVRprog.exe, a command line program
;  or a graphical user interface program such as BLIPS (see AVRFreaks.com)
; Protocol commands supported
;  
;
; ========= Previous authors' comments...
;* It is meant specifically for atmega-8 and avrprog.exe 1.37 or better, or equiv.
;* To write boot with an ISP, set the fuses: Boot Flash Section Size = 256 WORDS; Boot Reset vector enabled

;* The original DN did not seem to work properly with a atmega8, after some debugging it 
;* appeard that the problem was that the code did not re-enable RWW access after page write or erase. Fixed.
;* Notes:
;* Added an esc char test at the beginning of the code. Modify as necessary.
;* I left in the eeprom program counter.
;* Be sure to use avrprog.exe 1.37 or a version that supports atmega8 (the regular download from atmel is 1.33)
;* While debugging I also discovered that avrprog support a baud rate of 115200, seems to work okay.
;* Fuses: BLB11 BOOTSZ1 BOOTRST
;* comments or questions?  carlott@si.rr.com   http://users.rcn.com/carlott/

;********** B O O T L O A D E R **********
;* Assembler : AVR Studio

; the below have #ifdef conditional assembly directives later in the code:
;;;#define ENABLE_READFUSEANDLOCK		; comment-out to omit support fuse and lock bit read/write
;;;#define NONESUCH						; See disabled unecessary code for this


;================ CHIP DEPENDENT CONFIGURATION ITEMS ==============================
;===================PLEASE KEEP THE BLOCKS IN ALPHANUMERIC ORDER BY CHIP NAME======
;==================================================================================

; CHANGE the include file to choose the desired chip's include file.
;   Include file directory: C:\Program Files\Atmel\AVR Tools\AvrAssembler2\Appnotes

.INCLUDE "m128def.inc"	; <<< CHANGE THIS according to chip type <<<<<<<<<<<<<<<<<<<<<<<<<<
						; for example "m128def.inc"

; CHANGE clock 
.equ	clock 			= 16000000	; CPU clock in Hz, given the crystal in use
; CHANGE BAUD. CHANGE UART0 vs. UART1 in one of the blocks of constants, below
.equ	bootldr_baud	= 57600		; Baud rate. Note: PC side software may have limitations on choices

	 ; 
;		as of now, the code is about 486 bytes long; stevech did lots of work to fit in 512 

; these come from the .INCLUDE file
.equ 	SB1 = SIGNATURE_002 	; Atmel chip type Signature byte from include file
.equ 	SB2 = SIGNATURE_001 	; Atmel chip type Signature byte
.equ 	SB3 = SIGNATURE_000 	; Atmel chip type Signature byte (Atmel code)

; Optional Blinking LED while bootloader waits for PC to begin commands
.if	1			; change to 0 to disable blinking LED while boot waits for PC 
.equ	LED_DDR		=DDRB
.equ	LED_PORT	=PORTB
.equ	LED_BITNO	=0
.endif

; CHANGE THIS IF YOU WISH. Controls timeout inside this bootloader waiting for byte from UART
.equ	PCLOOPHZ = ((clock / 5) / 65536)	; Timeout loop's 16 bit counter part, in Hz
.equ	PCTIMEOUT	= PCLOOPHZ * 2	; desire is 0.5Hz outer loop (2 seconds)
							; 8 bits. An incrementing loop count for timeout waiting on PC to send download
							; 75 (decimal) gives 2 seconds with 14.746MHz crystal


;  CHANGE the constants below based on which chip to use. 
;  COPY one of the code blocks, below, to use as a template for a new type of chip.
;  the constants are summarized in the table below, and
;  are detailed in the relevant include file in C:\Program Files\Atmel\AVR Tools\AvrAssembler2\Appnotes



#ifdef _M8DEF_INC_	; is within the chosen include file 
;==========================================================
;==================== mega8 ===============================
;==========================================================
; where this bootloader code begins, Change if this code outgrows 512 bytes
.equ	MYORG =	SECONDBOOTSTART	; word address 0F80 = byte addr 1E00. SET FUSES accordingly, using an ISP device
; UART baud rate setup constant- formula varies by chip type
.equ	UBRRLval = (clock - 8 * bootldr_baud) / (16 * bootldr_baud) ; the 8* part helps rounding

;--- device code and signature bytes for a atmega8 with bootloader
.equ 	DT  	= 0x77	; atmega bootloader

; Chip-dependent names for UART registers (See Table, below)
.set	BAUDL 	=UBRRL		; baud rate low order bits
.set	UARTC	=UCSRB		; control
.set	USTAT	=UCSRA		; status
.set	UDATA	=UDR		; data I/O register
.set	URXC	=RXC		; rx char complete status bit number (data has arrived)
.set	UTXE	=UDRE		; tx data register is empty
.set	UTXC	=TXC		; tx complete status bit number
; values to use in configuring UART
.set	UARTENA	=((1<<RXEN) | (1<<TXEN))	; enable tx,rx for uart 0 or 1 (see table)
.set	SPMCSR	=SPMCR
.MACRO BOOTIDSTRING 
 	.DB "Mega8  ", 0	; change this 7 char string. bootloader ID string, information-only to PC
.ENDMACRO
;=========================================================
#endif


#ifdef _M16DEF_INC_	; is within the chosen include file 
;==========================================================
;==================== mega16 ==============================
;==========================================================
; where this bootloader code begins, Change if this code outgrows 512 bytes
.equ	MYORG =	SECONDBOOTSTART	; word address 0F80 = byte addr 1E00. SET FUSES accordingly, using an ISP device
; UART baud rate setup constant- formula varies by chip type
.equ	UBRRLval = (clock - 8 * bootldr_baud) / (16 * bootldr_baud) ; the 8* part helps rounding

;--- device code and signature bytes for a atmega8 with bootloader
.equ 	DT  	= 0x77	; atmega bootloader

; Chip-dependent names for UART registers (See Table, below)
.set	BAUDL 	=UBRRL		; baud rate low order bits
.set	UARTC	=UCSRB		; control
.set	USTAT	=UCSRA		; status
.set	UDATA	=UDR		; data I/O register
.set	URXC	=RXC		; rx char complete status bit number (data has arrived)
.set	UTXE	=UDRE		; tx data register is empty
.set	UTXC	=TXC		; tx complete status bit number
; values to use in configuring UART
.set	UARTENA	=((1<<RXEN) | (1<<TXEN))	; enable tx,rx for uart 0 or 1 (see table)
;;;;.set	SPMCSR	=SPMCR ; not needed for this chip
.MACRO BOOTIDSTRING 
 	.DB "Mega16 ", 0	; change this 7 char string. bootloader ID string, information-only to PC
.ENDMACRO
;=========================================================
#endif


#ifdef _M32DEF_INC_	; is within the chosen include file <<<<<<<<<<<<<<<<<<<<<<<<<<<
;==========================================================
;==================== mega8 ===============================
;==========================================================
; where this bootloader code begins, Change if this code outgrows 512 bytes
.equ	MYORG =	FIRSTBOOTSTART
; UART baud rate setup constant- formula varies by chip type
.equ	UBRRLval = (clock - 8 * bootldr_baud) / (16 * bootldr_baud) ; the 8* part helps rounding
;--- device code and signature bytes for a atmega8 with bootloader
.equ 	DT  	= 0x7F	; atmega bootloader

; Chip-dependent names for UART registers (See Table, below)
.set	BAUDL 	=UBRRL		; baud rate low order bits
.set	UARTC	=UCSRB		; control
.set	USTAT	=UCSRA		; status
.set	UDATA	=UDR		; data I/O register
.set	URXC	=RXC		; rx char complete status bit number (data has arrived)
.set	UTXE	=UDRE		; tx data register is empty
.set	UTXC	=TXC		; tx complete status bit number
; values to use in configuring UART
.set	UARTENA	=((1<<RXEN) | (1<<TXEN))	; enable tx,rx for uart 0 or 1 (see table)
.set	SPMCSR	=SPMCR
.MACRO BOOTIDSTRING 
 	.DB "Mega32 ", 0	; change this 7 char string. bootloader ID string, information-only to PC
.ENDMACRO
;=========================================================
#endif



#ifdef _M128DEF_INC_	; is within the chosen include file
;==========================================================
;==================== mega128 =============================
;==========================================================
; UART baud rate setup constant- formula varies by chip type
; where this bootloader code begins, Change if this code outgrows 512 bytes
.equ	MYORG =	FIRSTBOOTSTART ; NOTE! This chip doesn't support a boot memory area as small as 512Bytes; see fuses for it
.equ	UBRRLval = (clock - 8 * bootldr_baud) / (16 * bootldr_baud) ;the 8* part helps rounding
;--- device code and signature bytes for a atmega8 with bootloader
.equ 	DT  	= 0x44	; atmega bootloader
; Chip-dependent names for UART0 registers (See Table, below)
.set	BAUDL 	=UBRR0L		; baud rate low order bits
.set	UARTC	=UCSR0B		; control
.set	USTAT	=UCSR0A		; status
.set	UDATA	=UDR0		; data I/O register
.set	URXC	=RXC0		; rx char complete status bit number (data has arrived)
.set	UTXE	=UDRE0		; tx data register is empty
.set	UTXC	=TXC0		; tx complete status bit number
; values to use in configuring UART0
.set	UARTENA	=((1<<RXEN0) | (1<<TXEN0))	; enable tx,rx for uart 0 or 1 (see table)
.MACRO BOOTIDSTRING 
 	.DB "Mega128", 0	; change this 7 char string.  bootloader ID string, information-only to PC

.ENDMACRO
;=========================================================
#endif



#ifdef _M168DEF_INC_	; is within the chosen include file 
;==========================================================
;==================== meg168 ==============================
;==========================================================
; UART baud rate setup constant- formula varies by chip type
; where this bootloader code begins, Change if this code outgrows 512 bytes
.equ	MYORG =	SECONDBOOTSTART ; Use ISP to set fuses for 512 byte boot size (256 words)
.equ	UBRRLval = (clock - 8 * bootldr_baud) / (16 * bootldr_baud) ; the 8* part helps rounding

;--- device code and signature bytes for a atmega8 with bootloader
.equ 	DT  	= 0x77	; atmega bootloader

; Chip-dependent names for UART registers (See Table, below)
.set	BAUDL 	=UBRR0L		; baud rate low order bits
.set	UARTC	=UCSR0B		; control
.set	USTAT	=UCSR0A		; status
.set	UDATA	=UDR0		; data I/O register
.set	URXC	=RXC0		; rx char complete status bit number (data has arrived)
.set	UTXE	=UDRE0		; tx data register is empty
.set	UTXC	=TXC0		; tx complete status bit number
; values to use in configuring UART
.set	UARTENA	=((1<<RXEN0) | (1<<TXEN0))	; enable tx,rx for uart 0 or 1 (see table)
.MACRO BOOTIDSTRING 
 	.DB "Mega168", 0	; change this 7 char string. bootloader ID string, information-only to PC
.ENDMACRO
.set	SPMEN	=SELFPRGEN		;SPMEN to SELFPRGEN
.set	EEMWE	=EEMPE			;EEMWE to EEMPE
.set	EEWE	=EEPE			;EEWE to EEPE
;=========================================================
#endif


#ifdef _M8535DEF_INC_ ; is within the chosen include file <<<<<<<<<<<<<<<<<<<<<<<<<<<
;==========================================================
;=================== mega8535 =============================
;==========================================================
; where this bootloader code begins, Change if this code outgrows 512 bytes
.equ MYORG = SECONDBOOTSTART
; UART baud rate setup constant- formula varies by chip type
.equ UBRRLval = (clock - 8 * bootldr_baud) / (16 * bootldr_baud) ; the 8* part helps rounding
;--- device code and signature bytes for a atmega8 with bootloader
.equ DT = 0x64 ; atmega bootloader

; Chip-dependent names for UART registers (See Table, below)
.set BAUDL =UBRRL 	; baud rate low order bits
.set UARTC =UCSRB 	; control
.set USTAT =UCSRA 	; status
.set UDATA =UDR 	; data I/O register
.set URXC =RXC 		; rx char complete status bit number (data has arrived)
.set UTXE =UDRE 	; tx data register is empty
.set UTXC =TXC 		; tx complete status bit number
; values to use in configuring UART
.set UARTENA =((1<<RXEN) | (1<<TXEN)) ; enable tx,rx for uart 0 or 1 (see table)
.set SPMCSR =SPMCR
.MACRO BOOTIDSTRING
.DB "M8535  ", 0 ; change this 7 char string. bootloader ID string, information-only to PC
.ENDMACRO 
#endif



;==========================================================================================
; ASSUME THAT UART baud rate setup constant is always less than 256
.if HIGH(UBRRLval!=0)
.error "UBRRLval high register setup code needs to be added."

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -