📄 bootload.asm
字号:
; *****************************************************************************
; Software License Agreement
;
; The software supplied herewith by Microchip Technology
; Incorporated (the 揅ompany? for its PICmicro?Microcontroller is
; intended and supplied to you, the Company抯 customer, for use
; solely and exclusively on Microchip PICmicro Microcontroller
; products. The software is owned by the Company and/or its
; supplier, and is protected under applicable copyright laws. All
; rights are reserved. Any use in violation of the foregoing
; restrictions may subject the user to criminal sanctions under
; applicable laws, as well as to civil liability for the breach of
; the terms and conditions of this license.
;
; THIS SOFTWARE IS PROVIDED IN AN 揂S IS?CONDITION. NO WARRANTIES,
; WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
; TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
; PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
; IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
; CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
;
;
; Bootloader for the PIC18F252/452
;
; DESCRIPTION:
; This is an example of a bootloader routine for the PIC18FX52. The
; PICDEM2 was used for development. A 10MHz clock was selected.
;
; INSTRUCTIONS:
; Press button RA4 and reset on the PICDEM2. Release reset. Then
; release button RA4. Now the device is waiting for a text data stream
; to be parced and loaded into the part. //按下A4和DEM2上的复位键,先释放复位键,然后在释放A4按纽,
则单片机等待一个数据流,进入引导模式。
;
; Use 'Hyperterm' to download the HEX file to be bootloaded. Set
; the terminal for 57.6kb, 8, N, 1, and enable XON/XOFF. Use the
; 'Send Text File' option to transfer the HEX file. The HEX file
; must be INTEL32 HEX format. //使用windows的超级终端
;
; RB0 will light if an error occured. //错误发生RB0亮
; RB1 will light if the code loaded successfully and an EOL was received.//下载代码成功并收到一个结束标志RB1亮
; RB2 will toggle for each HEX line recieved.//每一个HEX记录收到则RB1触发
;
;
; GENERAL INFO:
; When writing code to be bootloaded, be aware of the re-mapping of
; the reset and interrupt vectors. //注意重新映射复位和中断向量
;
; Memory Map
; -----------------
; | 0x0000 |
; | |
; | Boot Block | (this program) //bootload代码区
; | |
; | 0x0200 | Re-mapped Reset Vector //复位向量重新映射
; | 0x0208 | Re-mapped High Priority Interrupt Vector //高优先级中断向量重新映射
; | 0x0218 | Re-mapped Low Priority Interrupt Vector //低优先级中断向量重新映射
; | |
; | | |
; | |
; | Code Space | //用户代码区
; | |
; | | |
; | |
; | 0x7FFE |
; -----------------
; *****************************************************************************
; *****************************************************************************
#include P18F452.INC ; Standard include
; *****************************************************************************
; *****************************************************************************
#define BRG_VAL D'10' ; Desired baud rate //串口波特率发生器 值为10
; *****************************************************************************
; *****************************************************************************
_BOOTLOAD UDATA_ACS
DATACNT res 1 ; Storage area for INHX32
ADDRESS_H res 1
ADDRESS_L res 1
RECORD res 1
DATA_BUFF res 0x10 //16字节数据缓存区
LAST_BYTE res 1
CHKSUM res 1 ; Checksum accumulator //按照HEX的一个记录格式定义
COUNTER res 1 ; General counter
TEMP1 res 1 ; Temp storage
RXDATA res 1 ; Receive storage area
ADDRESS_UH res 1 ; Upper word for 32-bit HEX
ADDRESS_UL res 1
; *****************************************************************************
; *****************************************************************************
_V_RESET CODE 0x0000
btfss PORTA, 4 ; Enter bootloader if button
bra Setup ; RA4 is pressed.
bra 0x0200 //检测RA4
; *****************************************************************************
; *****************************************************************************
_V_INT_HIGH CODE 0x0008
bra 0x0208 ; Re-map Interrupt vector
; *****************************************************************************
; *****************************************************************************
_V_INT_LOW CODE 0x0018
bra 0x0218 ; Re-map Interrupt vector
; *****************************************************************************
; *****************************************************************************
_V_ERROR CODE 0x001A ; Bootload error vector
GeneralErr
bsf PORTB, 0 ; Indicate some error
bra $ //错误指示
EndOfRecord
bsf PORTB, 1 ; Indicate successful write
bra $ //成功完成烧写
; *****************************************************************************
; *****************************************************************************
_V_SETUP CODE
Setup
btfss PORTA, 4 ; Wait for button release
bra Setup
clrf LATB
clrf TRISB
bcf TRISC, 6 ; Setup tx pin
bsf TRISC, 7 ; Setup rx pin
movlw b'10010000' ; Setup rx and tx
movwf RCSTA
movlw b'00100110'
movwf TXSTA
movlw BRG_VAL ; Setup the baud rate
movwf SPBRG
movf RCREG, W ; Empty the buffer
movf RCREG, W //初始化串口
; *****************************************************************************
; *****************************************************************************
; This section reads and parses one line of INTEL32 HEX. 读并解析一个HEX记录
;
StartOfLine //此函数为保存并解析一个记录的开始
movlw D'17' ; XON, enable data stream
rcall WrRS232
btg LATB, 2
WaitForStart
rcall GetChar ; Look for a start of line
xorlw ':'
bnz WaitForStart ; Go back for another character
rcall GetByte ; Get the data count
movwf CHKSUM
movwf DATACNT ; Set the data counter
addlw 0x03 //数据长度加上两字节地址长度和一个字节的数据格式长度为3
movwf COUNTER
//一个HEX记录格式: { :(开始)数据长度(充满16字节) 起始地址 数据格式 数据区 效验和}
lfsr 0, ADDRESS_H //数据长度过后是数据存放FLASH中的起始地址
GetNextDat ; Get the data
rcall GetByte
addwf CHKSUM, F
movwf POSTINC0
decfsz COUNTER, F
bra GetNextDat
rcall GetByte ; Get the checksum
movwf LAST_BYTE
movlw D'19' ; XOFF, disable data stream
rcall WrRS232
; *****************************************************************************
; *****************************************************************************
; This tests, writes, and verifies the line of HEX data.
;
; Notes:
; The HEX data is assumed to be padded to fill a 16 byte
; INHX32 line.
;
; Programming is limited to program memory space only.
;
; Not all record types are decoded.
TestChkSum ; Examine the checksum
movf LAST_BYTE, W
negf CHKSUM
xorwf CHKSUM, W
bnz GeneralErr ; Stop if an error is detected //效验和的计算:一行的所有数据相加的和取补就是chksum的 值
TestRecordType ; Check the record type
movlw 0x00
xorwf RECORD, W
bz NormalDataRecord ; If a normal record then process
movlw 0x01
xorwf RECORD, W
bz EndOfRecord ; IF EOF then finish
movlw 0x04
xorwf RECORD, W
bz ExtendedRecord ; If extended then adjust
bra StartOfLine ; Otherwise, undefined record type...
ExtendedRecord ; Read in upper word of 32-bit address
movff DATA_BUFF, ADDRESS_UH
movff DATA_BUFF + 1, ADDRESS_UL
bra StartOfLine
NormalDataRecord ; Normal data record
movf ADDRESS_UH ; Get the next line if too high
bnz StartOfLine
movlw 0x20 ; Write ID locations?
xorwf ADDRESS_UL, W
bz WriteIDLocations
movlw 0x30 ; Write Config locations?
xorwf ADDRESS_UL, W
bz WriteConfig
movlw 0xF0 ; Write Data EEPROM?
xorwf ADDRESS_UL, W
bz WriteEEPROM
btfsc ADDRESS_H, 7 ; Get the next line if too high
bra StartOfLine
movlw 0x02 ; Prevent writing to boot area
subwf ADDRESS_H, W
bnc StartOfLine
EraseRow ; Erase row if pointing to first byte in row
rcall SetPointers //设置指针
movf ADDRESS_L, W ; Check for row all ignment
andlw b'00111111'
bnz TestForBlankMem
movlw b'10010100' ; Setup erase
movwf EECON1
rcall StartWrite ; Erase the row
TestForBlankMem ; Do not write unless blank
rcall SetPointers
TestNext
tblrd *+ ; Read
comf TABLAT ; Test for blank (0xFF)
bnz GeneralErr
decfsz COUNTER
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -