FlashRomObj::FlashRomObj(void)
{
// SREG->SWWSR = 0x3f09;
SREG->SWWSR = 0x7f09;
// SREG->SWCR = 0;
// _delay_loop_(10);
FlashRomInit();
}
void FlashRomObj::FlashRomInit(void)
{
MakerId = 0;
DeviceId = 0;
FlashReset();
if (!FlashId()) return;
FlashReset();
ChipErase();
FlashReset();
FlashWrite(&FlashRom8000, 0x12);
FlashReset();
FlashWrite(&FlashRom8001, 0x34);
FlashReset();
//return;
if ((FlashRom8000 & 0xff) == 0x12) {
// FlashWrite(&FlashRomStart, 0xff55);
__nop();
__nop();
}
else {
__nop();
__nop();
// FlashWrite(&FlashRomStart, 0xffaa);
}
if ((FlashRom8001 & 0xff) == 0x34) {
// FlashWrite(&FlashRomStart, 0xff55);
__nop();
__nop();
}
else {
__nop();
__nop();
// FlashWrite(&FlashRomStart, 0xffaa);
}
FlashReset();
}
void FlashRomObj::FlashReset(void)
{
FlashRom5555 = 0xaa;
__nop();
__nop();
FlashRom2AAA = 0x55;
__nop();
__nop();
FlashRom5555 = 0xf0;
FlashWait();
}
bool FlashRomObj::FlashId(void)
{
bool flag;
FlashRom5555 = 0xaa;
__nop();
__nop();
FlashRom2AAA = 0x55;
__nop();
__nop();
FlashRom5555 = 0x90;
FlashWait();
MakerId = FlashRomMaker & 0xff;
__nop();
__nop();
DeviceId = FlashRomDevice & 0xff;
__nop();
__nop();
flag = (MakerId == 0xbf) && (DeviceId == 0x80);
FlashReset();
return flag;
}
void FlashRomObj::ChipErase(void)
{
FlashRom5555 = 0xaa;
__nop();
__nop();
FlashRom2AAA = 0x55;
__nop();
__nop();
FlashRom5555 = 0x80;
__nop();
__nop();
FlashRom5555 = 0xaa;
__nop();
__nop();
FlashRom2AAA = 0x55;
__nop();
__nop();
FlashRom5555 = 0x10;
FlashWait();
}
void FlashRomObj::FlashWrite(unsigned int * address, unsigned int val)
{
FlashRom5555 = 0xaa;
__nop();
__nop();
FlashRom2AAA = 0x55;
__nop();
__nop();
FlashRom5555 = 0xa0;
__nop();
__nop();
*address = val;
FlashWait();
}
void FlashRomObj::FlashWait(void)
{
unsigned int temp, val;
__nop();
__nop();
val = FlashRomStart;
do {
temp = FlashRomStart;
__nop();
__nop();
}
while(((val ^ temp) & (1 << BIT7)) != 0);
}
文章评论(0条评论)
登录后参与讨论