📄 bootm8v.bas
字号:
'-------------------------------------------------------------------------------
'IMPORTANT : Look at BOOTLOADER.BAS which is simpler
'-------------------------------------------------------------------------------
'------------------------------------------------------------------------------------
'name : bootM8.bas
'copyright : (c) 1995-2005, MCS Electronics
'purpose : demonstration of genereric bootloader program with verify
'micro : Mega8
'suited for demo : yes
'commercial addon needed : no
'use in simulator : not possible
'
'Include at end of regular program
'
'Bootloader Code:
'The fusebits need to be set for 128 bytes for the boot code,
'starting at $F80
'
'Fusebits: Boot Application Bootloader End of Boot Reset
'BOOTSZ1 BOOTSZ0 Size Flash Section Flash Section Application Address
' 1 1 128 $000 - $F7F $F80 - $FFF $F7F $F80
'
'Standard Intel hex file can be sent: (spaces added for readability)
'All record sizes must be even, AVR uses WORDs, not bytes
'
' :Size Address Type Data Bytes Checksum
' :10 00 00 00 26 C0 B3 C0 B3 C0 B3 C0 B3 C0 C5 C0 C5 C0 D0 C0 A4
' :10 00 10 00 DB C0 E4 C0 ED C0 30 31 32 33 34 35 36 37 38 39 E7
' -
' -
' :10 05 30 00 55 DF 08 95 57 E5 57 DF 52 E5 55 DF 54 E5 53 DF A2
' :10 05 40 00 5C E2 51 DF 53 2F 52 95 49 DF 53 2F 47 DF 01 D0 33
' :0B 05 50 00 08 95 5D E0 48 DF 5A E0 46 DF 08 95 A2 DC
' :00 00 00 01 FF
'
'------------------------------------------------------------------------------------
$regfile = "M8def.dat" 'Set the chip type to ATmega8
.equ Ramend = $45f 'RAM ending location to set up stack pointer
.equ Pagesize = 32 'Flash Memory Page Size, in WORDS
'Variable Definitions:
!.def Tmp_reg = R16 'Temporary register for calculations etc.
!.def Hex_reg = R17 'Hex calculation register
!.def Ser_reg = R18 'Serial character buffer register
!.def SPM_reg = R19 'Temporary register for SPM register settings
!.def Rec_size = R20 'Number of data bytes in this Hex file line
!.def Byte_cnt = R21 'Byte Counter
!.def Chk_sum = R22 'Checksum storage
!.def Lo_Byte = R23 'Hex file low (even) data byte
!.def Hi_Byte = R24 'Hex file high (odd) data byte
Print "Bootloader Installed"
' jmp $f00 'Dummy Start code for Simulator
End 'End Program
'******************************************************************************
'Start of Bootloader Code Area
'$boot = $f00 'Set boot vector to F00 - CORRECT VALUE
$boot = $eff 'Set boot vector to EFF and add a NOP, for BASCOM Programmer
nop
Disable Interrupts 'no interrupts allowed during bootloader programming
_chk_for_bootload: 'Check for bootload, this one uses hardware, Port D.5
cbi DDRD,5 'Clear the data direction bit for input
cbi DDRD,6 'Clear the data direction bit for input
sbi PORTD,5 'Set the pull-up
sbi PORTD,6 'Set the pull-up
clt 'Clear the T bit, assume VERIFY mode selected
ldi ser_reg, asc("P") 'Load "P" to show PROGRAMING enabled
sbis PIND,5 'Do next if PROGRAMMING pin is LOW
!set 'Set the T bit indicating PROGRAMMING mode selected
brts _bootloader_start: 'And then go run the bootloader, otherwise...
ldi ser_reg, asc("V") 'Load "V" to show VERIFY enabled
sbic PIND,6 'Skip next if VERIFY pin is LOW, continue with T clear
jmp $0000 'Must be HIGH too, neither PROG or VERF, run normal code
_bootloader_start: 'Otherwise, run the bootloader
ldi tmp_reg,hbyte(RAMEND) 'Load temp reg with the top of SRAM value
!out SPH,tmp_reg 'Move out to stack pointer low byte
ldi tmp_reg,lbyte(RAMEND) 'Load temp reg with the top of SRAM value
!out SPL,tmp_reg 'Move out to stack pointer low byte
ldi tmp_reg,25 'Load the temp register with USART baud rate low
!out UBRRL,tmp_reg 'Set up the USART
ldi tmp_reg,$00 'Load the temp register with USART baud rate high
!out UBRRH,tmp_reg 'Set up the USART
' ldi tmp_reg,$00 'Load the temp register with USART settings
!out UCSRA,tmp_reg 'Set up the USART
ldi tmp_reg,$18 'Load the temp register with USART settings
!out UCSRB,tmp_reg 'Set up the USART
ldi tmp_reg,$86 'Load the temp register with USART settings
!out UCSRC,tmp_reg 'Set up the USART
_send_boot_msg: 'Send a bootloader started message
' ldi ser_reg, asc("B") 'Load "B" to show bootloader enabled
rcall _send_ser 'Call routine to send a character
_read_lines: 'Read in the lines from serial port to SRAM
rcall _receive_hex_line 'Receive a single line from the UART into SRAM
_parse_line: 'Decode the current hex line
ldi XH,$01 'Point to start of line, high byte, uses $0100
ldi XL,$00 'Point to start of line, low byte
clr chk_sum 'Clear the checksum register for this line
_read_header:
ld tmp_reg,x+ 'Get first character, should be ":"
cpi tmp_reg, asc(":") 'Compare with ":" to send as error flag
breq _header_ok 'Fine, read the next record
_header_err: 'Not ":", send error character
ldi ser_reg, asc("?") 'Header error "What ???"
rcall _send_ser 'Call routine to send a character
_header_ok: 'Fine, read the next record
_read_record_size: 'Read the data byte count for this line
rcall _char_to_byte 'Call routine to convert two characters to byte
mov rec_size,hex_reg 'Save number of bytes in this line
mov byte_cnt,hex_reg 'Save number of bytes in this line
tst rec_size 'Test if record size is zero for this line
brne _read_address 'Not the final line, continue, next is address field
rjmp _write_current_page 'Final line, write current page exit to main program
_read_address: 'Read the address high byte and low bytes into ZH/ZL
rcall _char_to_byte 'Call routine to convert two characters to byte
mov ZH,hex_reg 'Load ZH with page address high byte
rcall _char_to_byte 'Call routine to convert two characters to byte
mov ZL,hex_reg 'Load ZL with page address low byte
_read_record_type: 'Read the record type for this line, add to checksum
rcall _char_to_byte 'Call routine to convert two characters to byte
_read_data_pairs: 'Read the rest of the data bytes
rcall _char_to_byte 'Call routine to convert two characters to byte
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -