一.实现原理
1.将芯片高地址处的2K字节(地址0xF800 ~ 0xFFFF)FLASH存储器用于存放BOOT程序,BOOT项目prm文件的ROM地址范围如下:
2.通过在FLASH块保护功能将BOOT程序保护起来,NVPROT设置0xF6(保护块开始地址为0xF600+0x200=0xF800)
3.NV0PT寄存器的FNORED设置0,从而开启向量重定位功能,使用户程序的中断向量重新定位到0xF7C6~0xF7FF之间
4.用户程序向量地址设置为0xF7C6~0xF7FF
用户程序prm文件ROM地址范围如下:
5.单片机复位时进入BOOT程序,BOOT程序通过跳转至0xF7FE处的用户程序复位地址处,实现用户程序的执行
二.读写FLASH的汇编程序
HCS08的写FLASH操作必须在RAM中执行,此段程序用汇编语言实现,为了在C项目中调用,引出了如下三个接口函数:
void FlashPageErase(uint16_t);//擦除页(512字节)
void FlashPageWrite(uint16_t);//指定地址开始写入64字节数据
void AppReset(void); //跳转到应用程序
实现此三个函数的汇编程序如下:
;函数导出声明
XDEF FlashPageErase
XDEF FlashPageWrite
XDEF AppReset
; include derivative specific macros
Include 'MC9S08AC16.inc'
XREF g_Buffer '全局变量声明
MY_ZEROPAGE: SECTION SHORT
ADRS: DS.W 1
ADRR: DS.W 1
LEN: DS.B 1
STAT: DS.B 1
STACK: DS.W 1
DEFAULT_ROM: SECTION
FlashPageErase:
STHX ADRS
lda #(mFSTAT_FPVIOL+mFSTAT_FACCERR) ;mask
sta FSTAT ;abort any command and clear errors
mov #EraseSubSize, STAT ;length of flash erase routine to STAT
tsx
sthx STACK
ldhx #EraseSubEnd-1 ;point at last byte to move to stack
bra DoOnStack ;execute prog code from stack RAM
FlashPageWrite:
STHX ADRS
LDA #64
STA LEN
LDHX #g_Buffer
STHX ADRR
lda #(mFSTAT_FPVIOL+mFSTAT_FACCERR) ;mask
sta FSTAT ;abort any command and clear errors
mov #ProgSubSize, STAT ;length of flash prog routine to STAT
tsx
sthx STACK
ldhx #ProgSubEnd-1 ;point at last byte to move to stack
; bra DoOnStack ;execute prog code from stack RAM
; fallthru to this routine
;*******************************************************************************************
DoOnStack:
lda ,x ;read from flash
psha ;move onto stack
aix #-1 ;next byte to move
dbnz STAT, DoOnStack
tsx ;point to sub on stack
jmp ,x ;execute the sub on the stack (will return on it's own)
;*******************************************************************************************
EraseSub:
ldhx ADRS ;get flash address
sta 0,x ;write to flash; latch addr and data
lda #mPageErase ;get flash command
sta FCMD ;write the flash command
lda #mFSTAT_FCBEF ;mask to initiate command
sta FSTAT ;[pwpp] register command
nop ;[p] want min 4~ from w cycle to r
nop
nop
nop
ChkDoneErase:
lda FSTAT ;[prpp] so FCCF is valid
lsla ;FCCF now in MSB
bpl ChkDoneErase ;loop if FCCF = 0
ldhx STACK
txs
rts
EraseSubEnd:
EraseSubSize: equ (*-EraseSub)
;*******************************************************************************************
ProgSub:
lda FSTAT ;check FCBEF
and #mFSTAT_FCBEF ;mask it
beq ProgSub ;loop if not empty
ldhx ADRR
lda 0,x
aix #1
sthx ADRR
ldhx ADRS ;get flash address
sta 0,x ;write to flash; latch addr and data
aix #1
sthx ADRS
lda #mBurstProg ;get flash command
sta FCMD ;write the flash command
lda #mFSTAT_FCBEF ;mask to initiate command
sta FSTAT ;[pwpp] register command
dbnz LEN,ProgSub ;all bytes in a row?
ChkDoneProg:
lda FSTAT ;[prpp] so FCCF is valid
lsla ;FCCF now in MSB
bpl ChkDoneProg ;loop if FCCF = 0
ldhx STACK
txs
rts
ProgSubEnd:
ProgSubSize: equ (*-ProgSub)
;*******************************************************************************************
AppReset:
LDHX #$F7FE ; there should be relocated reset vector of the new app.
;LDA ,X
LDHX ,X
JMP ,X ; jump to relocated application!
三.源代码下载
上位机程序及BOOT程序源代码请参考本站CAS-BOOT项目。
|
|