📄 mstick.c
字号:
if(IntStatus & (1<<15)) return 0;
else return 1;
}
int Check_Wp(void)
{
U8 tmps[2];
Mstpc_Read_Status_Reg(0x2, 0x1, tmps);
if(tmps[0] & STATUS_WP) { // Write protect is enabled.
return 1;
}
else return 0; // Not write-protect.
}
int Ms_Page_Write(U32 Block_Addr, U32 Page_Addr, U8 *Write_Buffer)
{
//int i, cnt=0;
int i;
//unsigned short IntStatus1, IntStatus2;
static int offset=0;
U8 Extra_Buffer[EXTRA_SIZE];
U16 Chksum, htmp;
//Uart_Printf("write data.\n");
// Check if write protect...
if(Check_Wp()==1) { // Write protect is enabled.
return -1;
}
// 1. SET R/W address in device TPC
Mstpc_Write_Rw_Reg_Addr(0x0, 0x0, 0x10, 0x6);
// 2. Write_Reg TPC
Mstpc_Write_Param_Reg(Block_Addr, PARAM_BAMD, Page_Addr, DE_AREA);
// make checksum...
for(Chksum=0, i=0; i<ONEPAGE_SIZE; i++)
{
Chksum += Write_Buffer[i];
}
Uart_Printf("Write checksum is [%04x]\n", Chksum);
// Write checksum.
Extra_Buffer[1] = (U8)(Chksum>>8);
Extra_Buffer[0] = (U8)Chksum;
Mstpc_Write_Status_Reg(CHKSUM_REG_START, CHKSUM_SIZE, Extra_Buffer);
// SET_CMD TPC
Mstpc_Set_Command(BLOCK_WRITE);
// Get INT register
Int_Status = Mstpc_Get_Int(1, INT_BREQ);
//Uart_Printf("Page write cnt(%x).\n", Int_Status);
rTP_CMD = WRITE_PAGE_DATA | ONEPAGE_SIZE;
for(i=0; i<ONEPAGE_SIZE; i+=2)
{
htmp = ((U16)(Write_Buffer[i+1]))<<8 | (U8)Write_Buffer[i];
rDAT_FIFO = htmp;
while(!TBE);
}
while(!INT_P_END);
Int_Status = Mstpc_Get_Int(1, INT_CED);
//Uart_Printf("Page write end cnt(%x).\n", Int_Status);
offset++;
}
void Ms_Page_Read(U32 Block_Addr, U32 Page_Addr, U8 *Extra_Buffer, U8 *Read_Buffer)
{
//int i, cnt=0;
int i;
//unsigned short IntStatus1, IntStatus2;
//U16 Calc_Chksum, In_Chksum, htmp;
U16 htmp;
// 1. SET R/W address in device TPC
Mstpc_Write_Rw_Reg_Addr(0x0, 0x00, 0x10, 0x6);
// 2. Write_Reg TPC
Mstpc_Write_Param_Reg(Block_Addr, PARAM_BAMD, Page_Addr, DE_AREA);
// 3. SET_CMD TPC
Mstpc_Set_Command(BLOCK_READ);
// 4. Get_Int or Get status.
#if 1
Int_Status = Mstpc_Get_Int(1, INT_BREQ|INT_CED);
//Uart_Printf("Page read cnt(%x).\n", Int_Status);
Mstpc_Read_Status_Reg(EXTRA_REG_START,EXTRA_SIZE, Extra_Buffer);
#else
// 4. Get INT register
Int_Status = Mstpc_Get_Int(1, INT_BREQ);
//Uart_Printf("Page read cnt(%x).\n", Int_Status);
#endif
rTP_CMD = READ_PAGE_DATA | ONEPAGE_SIZE;
for(i=0; i<ONEPAGE_SIZE; i+=2) {
while(RBE);
htmp = rDAT_FIFO;
Read_Buffer[i+1] = (U8)(htmp>>8); // High byte
Read_Buffer[i] = (U8)htmp; // Low byte
} while(!INT_P_END);
Int_Status = Mstpc_Get_Int(1, INT_CED);
#if 0
Uart_Printf("\nRead Page data is");
for(i=0; i<ONEPAGE_SIZE; i++) {
if(!(i%16)) Uart_Printf("\n%3x: ", i);
Uart_Printf("[%02x]", Read_Buffer[i]);
}
Uart_Printf("\n");
#endif
}
void Ms_Extra_Write(U32 Block_Addr, U8 Page_Addr, U8 *Buffer)
{
//int i, cnt=0;
int i;
//unsigned short IntStatus1, IntStatus2, htmp;
unsigned short htmp;
//U8 Extra_Buffer[EXTRA_SIZE];
// 1. SET R/W address in device TPC
Mstpc_Write_Rw_Reg_Addr(0x0, 0x0, 0x10, 6);
// 2. Write_Reg TPC
Mstpc_Write_Param_Reg(Block_Addr, PARAM_BAMD, Page_Addr, E_AREA);
// 2. Write_Reg TPC
Mstpc_Write_Rw_Reg_Addr(0x0, 0x0, EXTRA_REG_START, EXTRA_SIZE);
// Write_Reg TPC
rTP_CMD = WRITE_REG | EXTRA_SIZE;
// Write extra data.
for(i=0; i<(EXTRA_SIZE+1); i+=2) {
htmp = ((U16)Buffer[i+1])<<8 | (U16)Buffer[i];
rDAT_FIFO = htmp;
while(!TBE);
}
while(!INT_P_END);
// 3. Set_Cmd[BLock_Write].
Mstpc_Set_Command(BLOCK_WRITE);
// 4.5. GET INT.
Int_Status = Mstpc_Get_Int(1, INT_CED);
//Uart_Printf("Extra data read end cnt(%x).\n", Int_Status);
}
void Ms_Extra_Read(U32 Block_Addr, U8 Page_Addr, U8 *Buffer)
{
//int i, cnt=0;
int i;
//unsigned short IntStatus1, IntStatus2, htmp;
unsigned short htmp;
U8 Extra_Buffer[EXTRA_SIZE];
// 1. SET R/W address in device TPC
Mstpc_Write_Rw_Reg_Addr(0x16, 0x09, 0x10, 0x06);
// 2. Write_Reg TPC
Mstpc_Write_Param_Reg(Block_Addr, PARAM_BAMD, Page_Addr, E_AREA);
// 3. SET_CMD TPC
Mstpc_Set_Command(BLOCK_READ);
// 5.Get INT.
Int_Status = Mstpc_Get_Int(1, INT_CED);
//Uart_Printf("Extra data read cnt(%x).\n", Int_Status);
// 7. Read reg
rTP_CMD = READ_REG | EXTRA_SIZE;
for(i=0; i<(EXTRA_SIZE+1); i+=2) {
while(RBE);
htmp = rDAT_FIFO;
Extra_Buffer[i+1] = (U8)(htmp>>8);
Extra_Buffer[i] = (U8)htmp;
}
while(!INT_P_END);
#if 1
Uart_Printf("Extra data is\n");
for(i=0; i<EXTRA_SIZE; i++) {
//if(!(i%8)) Uart_Printf("\n%3x: ", i*2);
Uart_Printf("[%02x]", Extra_Buffer[i]);
}
Uart_Printf("\n");
#endif
}
int Mstpc_Write_Extra_Reg(U8 *Extra_Buffer)
{
//U8 Extra_Buffer[EXTRA_SIZE];
//int i;
// Write_Reg TPC
rTP_CMD = WRITE_REG | EXTRA_SIZE;
rDAT_FIFO = (Extra_Buffer[1]<<8) | (Extra_Buffer[0]<<0);
while(!TBE);
rDAT_FIFO = (Extra_Buffer[3]<<8) | (Extra_Buffer[2]<<0);
while(!TBE);
rDAT_FIFO = (Extra_Buffer[5]<<8) | (Extra_Buffer[4]<<0);
while(!TBE);
rDAT_FIFO = (Extra_Buffer[7]<<8) | (Extra_Buffer[6]<<0);
while(!TBE);
rDAT_FIFO = (0x00<<8) | (Extra_Buffer[8]<<0);
while(!TBE);
while(!INT_P_END);
}
/*
* function name : Ms_Block_Erase
* purpose : Erase a pade data before write to flash
*/
void Ms_Block_Erase(U32 Block_Addr)
{
//int i, cnt=0;
//unsigned short IntStatus;
// 1. SET_R_W_REG_ADS
Mstpc_Write_Rw_Reg_Addr(0x0, 0x1f, 0x10, 0x06);
// 2. Write_Reg
Mstpc_Write_Param_Reg(Block_Addr, PARAM_BAMD, 0, DE_AREA);
// 3. SET_CMD TPC
Mstpc_Set_Command(BLOCK_ERASE);
// 4. Get INT register
Int_Status = Mstpc_Get_Int(1, INT_CED);
//Uart_Printf("Block erase cnt(%x).\n", Int_Status);
}
void __irq IrDA_Mstick_Irq(void)
{
int int_state;
int_state = rSUBSRCPND;
//Uart_Printf("IrDA_Mstick_Irq occurred...\n");
if(int_state & BIT_SUB_MSTICK) {
rINTSUBMSK |= BIT_SUB_MSTICK;
rSUBSRCPND = BIT_SUB_MSTICK;
ClearPending(BIT_IrDA_MSTICK);
Mstick_Sub_Irq();
rINTSUBMSK &= ~BIT_SUB_MSTICK;
} else if (int_state & BIT_SUB_IrDA) {
Uart_Printf("\n\n\nNot allowed interrupt\n\n");
rINTSUBMSK |= BIT_SUB_IrDA;
rSUBSRCPND = BIT_SUB_IrDA;
ClearPending(BIT_IrDA_MSTICK);
IrDA_Sub_Irq();
rINTSUBMSK &= ~BIT_SUB_IrDA;
}
else {
Uart_Printf("IrDA_Mstick Interupt Error!\n");
}
}
void Mstick_Sub_Irq(void)
{
int int_status;
int_status = rINTCTRL_STA;
//Uart_Printf("Mstick intr(%x).\n", int_status);
if(int_status & (1<<7)) {
//Uart_Printf("[INT_INT_P_END]");
}
if(int_status & (1<<6)) {
//Uart_Printf("\nINT_INT_SIF(receive).\n");
}
if (int_status & (1<<5)) {
//Tr_Irq();
}
if(int_status & (1<<4)) {
Stick_Ins_Irq();
}
if(int_status & (1<<1)) {
Uart_Printf("\nINT_CRCerror.\n");
}
if(int_status & (1<<0)) {
Uart_Printf("\nINT_Timeout.\n");
}
}
void Stick_Ins_Irq(void)
{
if(rINS_CON & (1<<4) ) {
Uart_Printf("Stick is inserted.\n");
//if(Check_Wp()) Uart_Printf("Write-protect enabled.\n");
// Must takes 1msec for internal initialization after power supply.
Delay(10);
Ms_Reset();
Stick_Ins = 1;
} else {
Uart_Printf("Stick is extracted.\n");
Stick_Ins = 0;
}
}
void Tr_Irq(void)
{
Uart_Printf("Data transfer request interrupt.\n");
}
void IrDA_Sub_Irq(void)
{
Uart_Printf("IrDA interrupt(int_status).\n");
}
////////////////////////////////////////////////////////
int Mstpc_Write_Param_Reg(U32 Block_Addr, U8 Sys_Param, U8 Page_Addr, U8 Cmd_Param)
{
U8 Block[3];
//Uart_Printf("Param:Block[%x], Page[%x]\n", Block_Addr, Page_Addr);
Block[0] = (unsigned char)(Block_Addr & 0xFF);
Block[1] = (unsigned char)((Block_Addr>>8) & 0xFF);
Block[2] = (unsigned char)((Block_Addr>>16) & 0xFF);
// 2. Write_Reg TPC
rTP_CMD = WRITE_REG | 6;
// Block addr2 | Sys Param.
rDAT_FIFO = (Block[2]<<8) | (Sys_Param<<0);
while(!TBE);
// Block addr0 | Block addr1
rDAT_FIFO = (Block[0]<<8) | (Block[1]<<0);
while(!TBE);
// Page addr | CMD param.
rDAT_FIFO = (Page_Addr<<8) | (Cmd_Param<<0);
while(!TBE);
while(!INT_P_END);
}
int Mstpc_Write_Rw_Reg_Addr(U8 Read_Addr, U8 Read_Size, U8 Write_Addr, U8 Write_Size)
{
// 1. SET R/W address in device TPC
rTP_CMD = SET_RW_REG_ADDRS | 4;
// READ_SIZE | READ_ADDR
rDAT_FIFO = (Read_Size<<8 | Read_Addr<<0);
while(!TBE); // wait until empty
// WRITE_SIZE | WRITE_ADDR
rDAT_FIFO = (Write_Size<<8 | Write_Addr<<0);
while(!TBE);
while(!INT_P_END); // Wait until Protocol end.
}
int Mstpc_Set_Command(U8 Command)
{
rTP_CMD = SET_CMD | 1;
rDAT_FIFO = (0x00<<8) | (Command<<0);
while(!TBE);
while(!INT_P_END);
}
U16 Mstpc_Get_Int(int num, U8 End_Command)
{
U16 int_status=0;
int cnt=0;
cnt=0;
do {
rTP_CMD = GET_INT | num;
while(!INT_P_END); // Data must be read after INT_P_END...
while(RBE);
int_status = (U8)rDAT_FIFO;
if(cnt++>TIME_OUT) break;
} while(!(int_status==End_Command));
if(cnt>TIME_OUT) Uart_Printf("Time out(%d)\n", int_status);
//Uart_Printf("Get int cnt(%d)\n", cnt);
return int_status;
}
int Mstpc_Read_Status_Reg(U8 Status_Addr, U8 Status_Size, U8 *Buffer)
{
//int i, cnt;
int i;
//U16 IntStatus1, htmp;
U16 htmp;
if(Status_Size%1) Status_Size+=1; // Must be read by even number
// Clear buffer
for(i=0; i<Status_Size; i++) {
Buffer[i] = 0x0;
}
// Set R/W register address
Mstpc_Write_Rw_Reg_Addr(Status_Addr, Status_Size, 0x0, 0x0);
// Status read command.
rTP_CMD = READ_REG | (Status_Size);
// Read status data.
for(i=0; i<(Status_Size); i+=2) {
while(RBE);
while(!INT_P_END);
htmp = rDAT_FIFO;
Buffer[i+1] = (U8)(htmp>>8);
Buffer[i] = (U8)htmp;
}
}
/*
* function name : Ms_Attr_Read
* purpose : Read memory stick(device) ATTR information in attribute ROM area
*/
void Ms_Attr_Read(void)
{
int i;
// U16 Attr_Buffer[ATTR_SIZE];
U16 tmps;
U8 Attr_Buffer[ATTR_SIZE];
// Set R/W register address
Mstpc_Write_Rw_Reg_Addr(0x0, 0x0, 0x10, 0x06);
// 2. WRITE_REG TPC
Mstpc_Write_Param_Reg(0, PARAM_ATEN, 0, 0x00);
rTP_CMD = READ_PAGE_DATA | ATTR_SIZE;
for(i=0; i<ATTR_SIZE; i+=2)
{
while(RBE);
tmps = (unsigned short)rDAT_FIFO;
Attr_Buffer[i+1] = (U8)(tmps>>8);
Attr_Buffer[i] = (U8)(tmps);
}
Uart_Printf("ATTR data is \n");
for(i=0; i<ATTR_SIZE; i++) {
if(!(i%16)) Uart_Printf("\n%3x: ", i);
//Uart_Printf("[%02x]", (U8)(Attr_Buffer[i]>>8), (U8)Attr_Buffer[i]);
Uart_Printf("[%02x]", Attr_Buffer[i]);
}
Uart_Printf("\n");
}
int Mstpc_Write_Status_Reg(U8 Status_Addr, U8 Status_Size, U8 *Buffer)
{
//int i, cnt;
int i;
//U16 IntStatus1, htmp;
U16 htmp;
//Status_Size = (Status_Size+1)*2 / 2;
if(Status_Size%2) Status_Size+=1;
// Set R/W register address
Mstpc_Write_Rw_Reg_Addr(0x0, 0x0, Status_Addr, Status_Size);
// Status read command.
rTP_CMD = WRITE_REG | (Status_Size);
// Read status data.
for(i=0; i<Status_Size; i+=2) {
htmp = ((U16)Buffer[i+1])<<8 |(U16)Buffer[i];
rDAT_FIFO = htmp;
while(!TBE);
}
while(!INT_P_END);
}
void Test_Ms_Dma_Rw(void)
{
U32 i, error, offset;
U32 blocknum, pagenum;
U8 Write_Buffer[ONEPAGE_SIZE], Read_Buffer[ONEPAGE_SIZE], Extra_Buffer[EXTRA_SIZE];
U16 In_Chksum, Calc_Chksum;
Uart_Printf("\n<<Memory stick DMA W/R test>>\n");
Uart_Printf("Block number ? ");
blocknum = Uart_GetIntNum();
Uart_Printf("Page number ? ");
pagenum = Uart_GetIntNum();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -