📄 msdef_i.c
字号:
#include <stdio.h>
#include "s1c33209.h"
#include "datatype.h"
#include "msdef.h"
extern unsigned short currentblockno;
extern unsigned char WriteBackLPTable;
extern const unsigned short LBIndex[];
extern const unsigned short crc16tab[256];
//extern struct PhyFmt PhyicalFormat;
extern unsigned short NumOfBlock; //总的块数
extern unsigned short OffsetPBR; //引导扇区的偏移
extern unsigned short PosOfLPTable; //逻辑物理映像表所在的块数
extern void SendTPC(unsigned char tpc);
extern void SendCRC(unsigned short crc);
extern void JudgeBusy(void);
extern unsigned short updcrc(unsigned char * buf, unsigned short len);
extern void SendCMD(unsigned char cmd);
extern void SetRWRegAddr(unsigned char RStartAddr, unsigned char RNum, unsigned char WStartAddr, unsigned char WNum);
extern unsigned char ReadReg(void);
extern void WriteReg(unsigned char data);
extern unsigned char GetINT(void);
extern unsigned char WaitForINT();
extern unsigned char WritePageData(unsigned char *buf);
extern void ReadPageData(unsigned char *buf);
extern unsigned char WriteBlockData(unsigned char *buf,unsigned long blockindex, unsigned char* tmp);
extern unsigned char ReadBlockData(unsigned char *buf,unsigned long blockindex);
extern void ReadExtraDataArea(unsigned char *buf,unsigned long pageindex,unsigned short num);
extern void SendByte(unsigned char n);
extern void EraseBlocks(unsigned short block,unsigned short num);
extern int IsDisable(unsigned short Block); //物理块
extern int M_WriteSectors(unsigned char* Buffer, unsigned long StartSec, unsigned long Sectors);
extern int M_ReadSectors(unsigned char* Buffer, unsigned long StartSec, unsigned long Sectors);
extern int ReadLogicBlock(unsigned char * buf, unsigned short logicBlock);
extern int WriteLogicBlock(unsigned char * buf, unsigned short logicBlock);
extern long FindUnusedBlock(unsigned short logicBlock);
extern int CreateLPTable();
extern unsigned short GetLogicBlockIndex(unsigned short logicBlock);
extern long GetPhyBlock(unsigned short logicBlock);
extern int GetOffsetPBR();
extern int ParseBootArea(void); //分析MemoryStick的引导区,取得NumOfBlock、NumOfUserBlock、OffsetPBR、NumOfHeads、SecPerTrack值;返回:0正确;-1错误
extern int GetLPTable(void); //读出逻辑物理映像表,取PosOfLPTable值;返回:0正确;-1错误 //没有判断LPTABLE是否有效
extern int WritePacket(unsigned char TPC, unsigned char * data, unsigned short length, unsigned short CRC);
extern int ReadPacket(unsigned char TPC, unsigned char * data, unsigned short length);
int ParseBootArea(void) //分析MemoryStick的引导区,取得NumOfBlock、NumOfUserBlock、OffsetPBR、NumOfHeads、SecPerTrack值;返回:0正确;-1错误
{
unsigned long i;
unsigned char * boot;
unsigned char tmp[9];
ReadExtraDataArea(tmp, 0, 1);
// CLR_BS;
//// SDIO_IN;
// CLR_SCLK;
if (tmp[0] != 0xC0 && tmp[1] != 0xFB)
{
return -1; //Invaild MemoryStick
}
boot = (unsigned char*)malloc(0x4000);
// //ReadSectors(PhyicalFormat.LPtable, 0, 32);
ReadBlockData(boot, 0);
EraseBlocks(0, 2);
WriteBlockData(boot, 0, tmp);
WriteBlockData(boot, 1, tmp);
// ReadBlockData(boot, 0);
// ReadBlockData(boot, 0);
// WriteBlockData(boot, 0, tmp);
// WriteBlockData(boot, 1, tmp);
tmp[1] = boot[415+5];
tmp[0] = boot[415+6];
NumOfBlock = *((unsigned short*)tmp);
// for (i=0; i<100; i++)
// {
// asm("nop\n");
// asm("nop\n");
// asm("nop\n");
// asm("nop\n");
// }
//tmp[1] = boot[415+7];
//tmp[0] = boot[415+8];
//PhyicalFormat.NumOfUserBlock = *((unsigned short*)tmp);
//tmp[0] = boot[1280+6];
//tmp[1] = boot[1280+7];
//PhyicalFormat.NumOfHeads = *((unsigned short*)tmp);
//tmp[0] = PhyicalFormat.LPtable[1280+2];
//tmp[1] = PhyicalFormat.LPtable[1280+3];
//PhyicalFormat.NumberCylinders = *((unsigned short*)tmp);
//tmp[0] = boot[1280+12];
//tmp[1] = boot[1280+13];
//PhyicalFormat.SecPerTrack = *((unsigned short*)tmp);
// memcpy((unsigned char*)DISTABLE_ADDR, boot+512, 512);
free(boot);
return 0;
}
int GetLPTable(void) //读出逻辑物理映像表,取PosOfLPTable值;返回:0正确;-1错误 //没有判断LPTABLE是否有效
{
unsigned short i;
unsigned short StartAddr, EndAddr;
unsigned char tmp[9];
if (NumOfBlock == 0x0800)
{
StartAddr = 1536;
EndAddr = 2047;
}
else
if (NumOfBlock == 0x1000)
{
StartAddr = 3584;
EndAddr = 4095;
}
else
if (NumOfBlock == 0x2000)
{
StartAddr = 7680;
EndAddr = 8191;
}
else
{
return -1; //no support
}
for (i=StartAddr; i<=EndAddr; i++) //Seek for LPTable
{
//ResetWatchDog();
ReadExtraDataArea(tmp, i<<5/*i*32*/, 1);
if ((tmp[0] & 0x80) && tmp[2] == 0xFF && tmp[3] == 0xFF)
{
PosOfLPTable = i;
}
if ((tmp[0] != 0) && (!(tmp[1] & 0x08)))
{
if (IsDisable(i)) //Is Disable Block
{
continue;
}
PosOfLPTable = i;
break;
}
}
if (PosOfLPTable == 0)
{
return -1;
}
// tmp[0] = 0x28;
// tmp[1] = 0xF7;
// tmp[2] = 0xA5;
// tmp[3] = 0xAA;
// EraseBlocks(PosOfLPTable, 1);
// WriteBlockData((unsigned char*)LPTABLE_ADDR, PosOfLPTable, tmp);
ReadExtraDataArea(tmp, PosOfLPTable<<5/*i*32*/, 1);
if ((tmp[0] != 0) && (!(tmp[1] & 0x08)))
{
}
else
{ //正常情况下不会执行以下指令
tmp[0] = 0x28;
tmp[1] = 0xF7;
tmp[2] = 0xA5;
tmp[3] = 0xA5;
EraseBlocks(PosOfLPTable, 1);
WriteBlockData((unsigned char*)LPTABLE_ADDR, PosOfLPTable, tmp);
ReadExtraDataArea(tmp, PosOfLPTable<<5/*i*32*/, 1);
ReadBlockData((unsigned char*)LPTABLE_ADDR, PosOfLPTable);
}
return 0;
}
int GetOffsetPBR()
{
//int i;
unsigned char * tmp;
unsigned short PBRBlock;
tmp = (unsigned char*)malloc(0x4000);
//PBRBlock = PhyicalFormat.LPTable[0];
PBRBlock = *((unsigned short*)LPTABLE_ADDR);
if (IsDisable(PBRBlock)) //进行PBR块有效的判断
{
free(tmp);
return -1;
}
//ReadSectors(tmp, PBRBlock*32, 2);
ReadBlockData(tmp, PBRBlock);
if (tmp[0] == 00) //判断是MBR还是PBR
{
unsigned char longbuf[4];
longbuf[0] = tmp[0x1c6];
longbuf[1] = tmp[0x1c7];
longbuf[2] = tmp[0x1c8];
longbuf[3] = tmp[0x1c9];
OffsetPBR = (unsigned short)(*((unsigned long*)longbuf));
}
else
{
OffsetPBR = 0;
}
free(tmp);
return 0;
}
/**********************************************************************
* Function Name : void SendTPC(unsigned char tpc)
* Description : Send TPC to memory stick
* Input Para : tpc:TPC
* Ouput Para :None
* Remark :
***********************************************************************/
void SendTPC(unsigned char tpc)
{
unsigned char j;
for( j=0x80; j!=1; j>>=1 )
{
if( j&tpc ) //***************?
{
SET_SDIO;
}
else
{
CLR_SDIO;
}
CLR_SCLK;
SET_SCLK;
}
CLR_BS;
if( tpc&0x01 ) //****************
{
SET_SDIO;
}
else
{
CLR_SDIO;
}
CLR_SCLK;
SET_SCLK;
/*SendByte(tpc);
CLR_BS;
CLR_SCLK;
SET_SCLK;*/
}
/**********************************************************************
* Function Name :void SendCRC(unsigned short crc)
* Description :Send CRC to Memory stick
* Input Para :crc:CRC16 value
* Ouput Para :None
* Remark :
***********************************************************************/
void SendCRC(unsigned short crc)
{
unsigned short j;
for( j=0x8000; j!=1; j>>=1 )
{
if( j&crc ) //***************?
{
SET_SDIO;
}
else
{
CLR_SDIO;
}
CLR_SCLK;
SET_SCLK;
}
if( crc&0x01 ) //****************
{
SET_SDIO;
}
else
{
CLR_SDIO;
}
SET_BS;
CLR_SCLK;
SET_SCLK;
}
/**********************************************************************
* Function Name : void JudgeBusy(void)
* Description : Reset Memory stick
* Input Para :None
* Ouput Para :None
* Remark :
***********************************************************************/
void JudgeBusy(void)
{
SDIO_IN;
while( GET_SDIO ) //1: ready,0:busy**************???????????????
{
CLR_SCLK;
SET_SCLK;
}
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
}
/**********************************************************************
* Function Name : unsigned short Crc16(unsigned char *Buf,unsigned char BufLen)
* Description : calculate the CRC16 value of data buf
* Input Para :Buf:pointe the data buf,BufLen:the data buf len include the two bytes 0
* Ouput Para :CRC16
* Remark :
***********************************************************************/
unsigned short updcrc(unsigned char * buf, unsigned short len)
{
//return Crc16(buf,len);
register unsigned short crc = 0L;
register unsigned char *cp = buf;
register unsigned short cnt = len;
while(cnt--)
{
crc = (crc<<8) ^ crc16tab[(crc>>8) ^ *cp++];
}
return crc;
}
/**********************************************************************
* Function Name :void SendCMD(unsigned char cmd)
* Description :Send command to Memory stick
* Input Para :cmd: the command
* Ouput Para :None
* Remark :
***********************************************************************/
void SendCMD(unsigned char cmd)
{
unsigned char i;
unsigned short Crctemp;
unsigned char buf[1];
buf[0] = cmd;
Crctemp = updcrc( buf,1);
WritePacket(SET_CMD, buf, 1, Crctemp);
// CLR_SCLK;
// CLR_BS;
//
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
//
// SET_BS;
// SET_SCLK;
//
// SDIO_OUT;
//
// SendTPC(SET_CMD);
// //Data
// SendByte(cmd);
//
// //Send CRC16
// SendCRC( Crctemp );
//
// JudgeBusy();
//
// CLR_SCLK;
// CLR_BS;
}
/**********************************************************************
* Function Name : void SetRWRegAddr(unsigned char RStartAddr,unsigned char RNum,unsigned char WStartAddr,unsigned char WNum)
* Description : Set the Register address accessed by WRITE_REG and READ_REG
* Input Para :RStartAddr: Starting address for READ_REG
* RNum:The number of Registers to be read consecutively
* WStartAddr: Starting address for WRITE_REG
* WNum:The number of Registers to be write consecutively
* Ouput Para :None
* Remark :
***********************************************************************/
void SetRWRegAddr(unsigned char RStartAddr, unsigned char RNum, unsigned char WStartAddr, unsigned char WNum)
{
unsigned char i;
unsigned char buf[4];
unsigned short crc = 0;
buf[0] = RStartAddr;
buf[1] = RNum;
buf[2] = WStartAddr;
buf[3] = WNum;
crc = (crc<<8) ^ crc16tab[(crc>>8) ^ RStartAddr];
crc = (crc<<8) ^ crc16tab[(crc>>8) ^ RNum];
crc = (crc<<8) ^ crc16tab[(crc>>8) ^ WStartAddr];
crc = (crc<<8) ^ crc16tab[(crc>>8) ^ WNum];
WritePacket(SET_RW_REG_ADRS, buf, 4, crc);
/* CLR_SCLK;
CLR_BS;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_SCLK;
CLR_SCLK;
SET_BS;
SET_SCLK;
SDIO_OUT;
//Send TPC
SendTPC(SET_RW_REG_ADRS);
//send data
SendByte(RStartAddr);
SendByte(RNum);
SendByte(WStartAddr);
SendByte(WNum);
SendCRC( crc );
JudgeBusy();
CLR_BS;*/
}
/**********************************************************************
* Function Name : void ReadReg(unsigned char RStartAddr,unsigned char RNum)
* Description : Set the Register address accessed by WRITE_REG and READ_REG
* Input Para :
*
* Ouput Para :None
* Remark :
***********************************************************************/
unsigned char ReadReg(void)
{
unsigned char i,data;
unsigned short crc;
ReadPacket(READ_REG, &data, 1);
// CLR_SCLK;
// CLR_BS;
//
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
//
// SET_BS;
// SET_SCLK;
//
// SDIO_OUT;
//
// //Send TPC
// SendTPC(READ_REG);
//
// JudgeBusy();
//
// SET_BS;
//
// CLR_SCLK;
// SET_SCLK;
//
// for( i=0x80,data=0; i!=0; i>>=1 )
// {
// CLR_SCLK;
// SET_SCLK;
// if( GET_SDIO )
// {
// data |= i;
// }
// }
// //16 bits CRC
// for( i=0x80,crc=0; i!=0; i>>=1 )
// {
// CLR_SCLK;
// SET_SCLK;
// if( GET_SDIO )
// {
// crc |= i;
// }
// }
// crc <<= 8;
// for( i=0x80; i!=1; i>>=1 )
// {
// CLR_SCLK;
// SET_SCLK;
// if( GET_SDIO )
// {
// crc |= i;
// }
// }
// //LSB of CRC
// CLR_BS;
// CLR_SCLK;
// SET_SCLK;
// if( GET_SDIO )
// {
// crc |= 1;
// }
//
// CLR_SCLK;
return data;
}
/**********************************************************************
* Function Name : void ReadReg(unsigned char RStartAddr,unsigned char RNum)
* Description : Set the Register address accessed by WRITE_REG and READ_REG
* Input Para :
*
* Ouput Para :None
* Remark :
***********************************************************************/
unsigned char GetINT(void)
{
unsigned char i,data;
unsigned short crc;
ReadPacket(GET_INT, &data, 1);
return data;
// CLR_SCLK;
// CLR_BS;
//
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
// SET_SCLK;
// CLR_SCLK;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -