DSP2812读写SST39VF800A之C++程序源码
DSP2812读写SST39VF800A之C++程序源码
/*-----------------------------------------------------
DSP2812读写SST39VF800A之C++程序源码
本演示程序在Study-2812增强版上通过运行测试
------------------------------------------------------*/
#include "flash.h"
FlashObj::FlashObj(void)
{
Init();
Test();
}
void FlashObj::Init(void)
{
Active = false;
Busy = false;
MakerId = 0;
DeviceId = 0;
FlashReset();
ReadId();
Test();
}
void FlashObj::FlashReset(void)
{
Busy = true;
FLASH[0x5555] = 0xaa;
FLASH[0x2AAA] = 0x55;
FLASH[0x5555] = 0xf0;
Wait(FLASH[0x5555]);
Busy = false;
}
void FlashObj::ReadId(void)
{
Busy = true;
FLASH[0x5555] = 0xaa;
FLASH[0x2AAA] = 0x55;
FLASH[0x5555] = 0x90;
Wait(FLASH[0x5555]);
MakerId = FLASH[0x0000] & 0xff;
DeviceId = FLASH[0x0001] & 0xff;
//SST39VF800A MakerId=0xBF DeviceId=0x81
Active = (MakerId == 0xBF) && (DeviceId == 0x81);
FlashReset();
Busy = false;
}
void FlashObj::ChipErase(void)
{
Busy = true;
FLASH[0x5555] = 0xaa;
FLASH[0x2AAA] = 0x55;
FLASH[0x5555] = 0x80;
FLASH[0x5555] = 0xaa;
FLASH[0x2AAA] = 0x55;
FLASH[0x5555] = 0x10;
Wait(FLASH[0x5555]);
Busy = false;
}
void FlashObj::SectorErase(unsigned int SectorNum)
{
Busy = true;
FLASH[0x5555] = 0xaa;
FLASH[0x2AAA] = 0x55;
FLASH[0x5555] = 0x80;
FLASH[0x5555] = 0xaa;
FLASH[0x2AAA] = 0x55;
FLASH[SectorSize * SectorNum] = 0x30;
Wait(FLASH[0x5555]);
Busy = false;
}
void FlashObj::BlockErase(unsigned int BlockNum)
{
Busy = true;
FLASH[0x5555] = 0xaa;
FLASH[0x2AAA] = 0x55;
FLASH[0x5555] = 0x80;
FLASH[0x5555] = 0xaa;
FLASH[0x2AAA] = 0x55;
FLASH[BlockSize * BlockNum] = 0x50;
Wait(FLASH[0x5555]);
Busy = false;
}
void FlashObj::Write(unsigned int & address, unsigned int val)
{
Busy = true;
FLASH[0x5555] = 0xaa;
FLASH[0x2AAA] = 0x55;
FLASH[0x5555] = 0xa0;
address = val;
Wait(address);
Busy = false;
}
void FlashObj::Wait(unsigned int & address)
{
unsigned int temp, val;
Busy = true;
do
{
_delay_loop_(1);
val = address;
_delay_loop_(1);
temp = address;
}
while(((val ^ temp) & BIT6) != 0);
Busy = false;
}
void FlashObj::Test(void)
{
Busy = true;
if (Active)//SST39VF800A测试正常
{
FlashReset();
ChipErase();
FlashReset();
Write(FLASH[0x0000], 0x1234);
Write(FLASH[0x5555], 0x1234);
Write(FLASH[0x2aaa], 0xabcd);
FlashReset();
SectorErase(0);
FlashReset();
Write(FLASH[0x0000], 0x8888);
FlashReset();
BlockErase(0);
Write(FLASH[0x0000], 0x6666);
FlashReset();
}
Busy = false; |