?? flash.c
字號:
//文件名 FLASH.C
//目 的 FLASH處理
//Copyright (c) 2006-2007 東方久樂公司電子研究所
//編 寫 文洪奎 2007-01-15
//修 改
//修改原因
#include "flash.h"
#include <start08.h>
#include "global.h"
unsigned char const ProgCrc @ 0x0EFFF = 152;
unsigned char const Memory1[10][128] @ 0x0C000 = {
{53,0x0ff,250,1,1,50,20,0,84,0,84,44,64,54,64}, //System
// crc st1 st2 sl1 sl2 I1 I2
{168,0 ,0 ,0 ,0 ,0 ,0,30 ,0 ,30 ,0x02E,0x0E0,0x02E,0x0E0},//Record0
{208,0 ,0 ,0 ,0 ,0 ,0,30 ,0 ,30 ,0x032,0x0C8,0x032,0x0C8},//Record1
{32 ,0 ,0 ,0 ,0 ,0 ,0,30 ,0 ,30 ,0x03A,0x098,0x03A,0x098},//Record2
{158,0 ,0 ,0 ,0 ,50 ,0,30 ,0 ,30 ,0x032,0x0C8,0x032,0x0C8},//Record3
{108,0 ,0 ,0 ,0 ,100,0,30 ,0 ,30 ,0x032,0x0C8,0x032,0x0C8},//Record4
{58 ,0 ,0 ,0 ,0 ,150,0,30 ,0 ,30 ,0x032,0x0C8,0x032,0x0C8},//Record5
{8 ,0 ,0 ,0 ,0 ,200,0,30 ,0 ,30 ,0x032,0x0C8,0x032,0x0C8},//Record6
{214,0 ,0 ,0 ,0 ,250,0,30 ,0 ,30 ,0x032,0x0C8,0x032,0x0C8},//Record7
{192,0 ,0 ,0 ,1 ,45 ,0,30 ,0 ,30 ,0x032,0x0C8,0x032,0x0C8},//Record8
};
//C000 - C07F 系統記錄
//C080 - C0FF 記錄零(默認)
//C100 - C17F 記錄一
//C180 - C1FF 記錄二
//C200 - C27F 記錄三
//C280 - C2FF 記錄四
//C300 - C37F 記錄五
//C380 - C3FF 記錄六
//C400 - C47F 記錄七
//C480 - C4FF 記錄八
/************說明*********************
程序分為非運行停電狀態和運行停電狀態和完成停電狀態
完成停電狀態
**************************************/
/// FLCR 0xFE08
/// 7 6 5 4 3 2 1 0
/// X X X X HVEN MASS ERASE PGM
/// 0 0 0 0 0 0 0 0
volatile unsigned char d1; //延時用
volatile CXSTR _SADD; //flash程序的源地址
volatile CXSTR _TADD; //flash程序的目標地址
volatile unsigned char nLength; //程序長度
extern SYSTEMSTR mSystem;
extern RECORDSTR mRecord;
unsigned char PragRam[0x60] @ 0x0200;
void FlashProc(void) //FLASH操作主程序
{
_asm
{
//;***************************************************************
f_0001:
bra f_write
//;-----------------H:X = Erase Address--------A change-------------
f_erase:
lda #$2 //;H:X = Erase Address
sta FLCR //;1->Erase
sta ,x // Write ony data to any FLASH address within the page address
f_eflash0:
lda #3
bsr f_delay //;等待10uS
lda #$0a
sta FLCR //;1->HVEN 1->ERASE
lda #200 //1ms
bsr f_delay
bra f_end
//;--------------------------------------------------------------
f_write:
lda #$01 //;2
sta FLCR //;3 1->pgm
lda FLBPR //;3 read flash protect reg
sta ,x //;2
lda #3 //10us
bsr f_delay //;25
lda #$09 //;2
sta FLCR //;3 1->hven
lda #1 //5us
bsr f_delay //;13
f_1010: //Write data to the FLASH address to be programmed
ldhx sAdd //3
lda ,x //2
aix #1 //2
sthx sAdd //4
ldhx tAdd //3
sta ,x //2
aix #1 //2
sthx tAdd //4
lda #9 //30us
bsr f_delay //
dbnz nLength,f_1010
f_1020:
//;-----------------------------------------------------------------
f_end: ;FLASH程序結束
lda #$08 //;2
sta FLCR //;3 ;0->pgm 0->erase
lda #2
bsr f_delay //;13;等待5us以上
clra //;1
sta FLCR //;3 ;0->hven
brn f_end //;3
rts //;4 26
//;-----------A Change--------------flash program 2-----------------
// ;延時子程序
// 總線頻率8MHz
// d2為輸入參數 5us單位
//
f_delay: mov #8,d1 //;5
f_1410: dbnz d1,f_1410 //;5
dbnza f_delay //;5
rts //;4
}
}
void InitFlash(void) //上載FLASH程序到RAM空間
{
unsigned char i;
unsigned char *far t;
unsigned char *far s;
s = (unsigned char *far)FlashProc;
t = (unsigned char *far)PragRam;
for(i = 0; i < 0x60; i++)
{
*t = *s;
t++;
s++;
}
}
void Erase(void) //FLASH頁擦除程序
{
FLBPR = 0x14;
_asm
{
pshh
pshx
psha
ldhx tAdd
sei
jsr $202
cli
pula
pulx
pulh
}
FLBPR = 0x00;
}
void Program(void) //FLASH頁寫入程序
{
FLBPR = 0x14;
_asm
{
pshh
pshx
psha
ldhx tAdd
sei
jsr $200
cli
pula
pulx
pulh
}
FLBPR = 0x00;
}
//擦除記錄
void EraseRecord(unsigned char nRecord)
{
tAdd = nRecord * 128 + 0x0C080;
Erase();
}
//讀記錄到當前存
unsigned char ReadRecord(unsigned char nRecord)
{
unsigned char i;
unsigned char *far t;
unsigned char *far s;
unsigned char crc;
s = (unsigned char *far) 0x0C080;
for(i = 0; i < nRecord ; i++)
s += 128;
t = (unsigned char *far) &mRecord;
crc = 0;
for(i = 0 ; i < sizeof(mRecord) ; i++)
{
*t = *s;
crc += *t;
t++;
s++;
}
if(crc)
{
mRecord.uCRC = 0;
mRecord.Reserve = 0;
mRecord.iCur0 = 13000; //2000.0 mA
mRecord.iCur1 = 13000; //3000.0 mA
mRecord.sTime0 = 0; //0.0 ms
mRecord.sTime1 = 0; //10.0ms
mRecord.lTime0 = 30; // 2.0ms
mRecord.lTime1 = 30; //3.0ms
mRecord.Res0Up = 4000; //4.0ohm
mRecord.Res0Down = 1000; //1.0ohm
mRecord.Res1Up = 4000; //4.0ohm
mRecord.Res1Down = 1000; //1.0ohm
}
return crc;
}
//寫內存記錄到FLASH
unsigned char WriteRecord(unsigned char nRecord)
{
unsigned char crc;
unsigned char i;
unsigned char *far s;
unsigned char *far t;
tAdd = nRecord * 128 + 0x0C080;
sAdd =(unsigned int) &mRecord;
s = (unsigned char *far) &mRecord;
crc = 0;
for(i = 1; i < sizeof(mRecord) ; i++)
{
crc += *(s + i);
}
*s = -crc;
nLength = sizeof(mRecord);
Program();
s = (unsigned char *far) &mRecord;
t = (unsigned char *far) 0x0C080;
t = t + nRecord * 128;
for(i = 0; i < sizeof(mRecord) ; i++)
{
if(*s != *t)
return 1;
s++;
t++;
}
return 0;
}
unsigned char ReadSystem(void)
{
unsigned char i;
unsigned char *far t;
unsigned char *far s;
unsigned char crc;
s = (unsigned char *far)0x0C000;
t = (unsigned char *far)&mSystem;
crc = 0;
for(i = 0 ; i < sizeof(mSystem) ; i++)
{
*t = *s;
crc += *t;
t++;
s++;
}
if(crc)
{
mSystem.uCRC = 83;
mSystem.bLight = 0x0FF; //背景燈開
mSystem.ld = 250; //亮度
mSystem.TargetType = 1; //低電平觸發
mSystem.OutType = 1; //
mSystem.KeyDelay = 50; //鍵盤延時 50
mSystem.KeyRepeat = 20; //鍵盤重復 20
mSystem.I1_O = 0;
mSystem.I1_S = 84;
mSystem.I2_O = 0;
mSystem.I2_S = 84;
mSystem.R1_O = 44;
mSystem.R1_S = 65;
mSystem.R2_O = 54;
mSystem.R2_S = 65;
mSystem.C1_O = 0;
mSystem.C1_S = 0;
mSystem.C2_O = 0;
mSystem.C2_S = 0;
}
return crc;
}
unsigned char WriteSystem(void)
{
unsigned char crc;
unsigned char i;
unsigned char *far s;
unsigned char *far t;
tAdd = 0x0C000;
sAdd = (unsigned int) &mSystem;
s = (unsigned char *far) &mSystem;
crc = 0;
for(i = 1; i < sizeof(mSystem) ; i++)
{
crc += *(s + i);
}
*s = -crc;
nLength = sizeof(mSystem);
Program();
s = (unsigned char *far) &mSystem;
t = (unsigned char *far) 0x0C000;
for(i = 0; i < sizeof(mSystem) ; i++)
{
if(*s != *t)
return 1;
s++;
t++;
}
return 0;
}
void EraseSystem(void)
{
tAdd = 0x0C000;
Erase();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -