📄 ata.c
字号:
#include <string.h>
#include "option.h"
#include "def.h"
#include "2410lib.h"
#include "ata.h"
U16 Drv_Cylinders;
U16 Drv_Heads;
U16 Drv_Sectors;
U32 Drv_Total_Addr_Sectors;
U8 Drv_Name[Drv_NameLong];
U8 Read_Sectors(U8 Drv,U32 LBA,U8 *buf)
{
U16 DATA_TEMP;
U32 i;
// Set the Sector Count //
Sector_Count=0x01;
// Decode the LBA address //
LBA3=Drv|(LBA/16777216);LBA=LBA%16777216;
LBA2=LBA/65536; LBA=LBA%65536;
LBA1=LBA/256; LBA=LBA%256;
LBA0=LBA;
// Wait for RDY and Not BUSY //
while ((Status_Reg & 0xC0)!=0x40);
// Issue the Read Sectors with Retry ATA command //
Command_Reg=Read_Sectors_Retry;
// Wait for DRQ and NOT BUSY //
while ((Status_Reg & 0x88)!=0x08);
// Driver is Ready for read out data //
for(i=0;i<(HDD_Sector_Size/2);i++)
{
DATA_TEMP=Data_Reg_re;
*buf=DATA_TEMP%256;
buf++;
*buf=DATA_TEMP/256;
buf++;
}
// Judge the DRQ //
if((Status_Reg & 0x08)==0x08)
return(ATA_FAIL); // If the DRQ is still High,There is some error //
else
return(ATA_OK); // ATA OK //
}
U8 Write_Sectors(U8 Drv,U8 Sec_Num,U32 LBA,U8 *buf)
{
U16 DATA_TEMP;
U32 i,k;
// Decode the LBA address //
LBA3=Drv|(LBA/16777216);LBA=LBA%16777216;
LBA2=LBA/65536; LBA=LBA%65536;
LBA1=LBA/256; LBA=LBA%256;
LBA0=LBA;
// Set the Sector Count //
Sector_Count=Sec_Num;
// Wait for RDY
while ((Status_Reg & 0xC0)!=0x40);
// Issue the Read Sectors with Retry ATA command //
Command_Reg=Write_Sectors_Retry;
// Wait for DRQ and NOT BUSY
while ((Status_Reg & 0x88)!=0x08);
// Write Out Sec_Num*HDD_Sector_Size Bytes Data to HDD //
for(k=0;k<Sec_Num;k++)
{
// Driver is Ready for read out data //
for(i=0;i<(HDD_Sector_Size/2);i++)
{
DATA_TEMP=*buf;
buf++;
Data_Reg_wr=(*buf*256)+DATA_TEMP;
buf++;
}
}
// Judge the DRQ //
if((Status_Reg & 0x08)==0x08)
return(ATA_FAIL); // If the DRQ is still High,There is some error //
else
return(ATA_OK); // ATA OK //
}
void Set_StandBy_Immediate(U8 DRV)
{
SDH=0xA0+(DRV&0x10);
Command_Reg=Standby_Immediate;
}
void Set_Idle_Immediate(U8 DRV)
{
SDH=0xA0+(DRV&0x10);
Command_Reg=Idle_Immediate;
}
void Disp_Sector_Buf(U8 Drv,U32 LBA,U8 *Sec_Buf)
{
U32 i;
Read_Sectors(Drv,LBA,Sec_Buf);
Uart_Printf("\n*---------------------Sector %8d is show as following: ---------*",LBA);
for(i=0;i<HDD_Sector_Size;i=i+16)
{
Uart_Printf("\nBYTE%4x:%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x%4x",\
i, *(Sec_Buf+i+0), *(Sec_Buf+i+1), *(Sec_Buf+i+2), *(Sec_Buf+i+3), *(Sec_Buf+i+4), *(Sec_Buf+i+5), *(Sec_Buf+i+6), *(Sec_Buf+i+7),\
*(Sec_Buf+i+8), *(Sec_Buf+i+9), *(Sec_Buf+i+10), *(Sec_Buf+i+11), *(Sec_Buf+i+12), *(Sec_Buf+i+13), *(Sec_Buf+i+14), *(Sec_Buf+i+15));
}
}
void Identify_Drv(U8 DRV)
{
U8 state_temp;
U8 *Drv_Info_Buf;
U8 *Drv_Info_Buf_Temp;
U32 i;
U16 DATA_TEMP;
Drv_Info_Buf =(U8 *)malloc(HDD_Sector_Size);
Drv_Info_Buf_Temp =Drv_Info_Buf;
// while(1)
// {
// Sector_Number=0xaa;
// }
SDH=0xA0+(DRV&0x10); // Selcet the Driver //
Command_Reg=Identify_Drive;
// Wait for DRQ and NOT BUSY //
do
{
state_temp=Status_Reg;
}while ((state_temp & 0x88)!=0x08);
// Driver is Ready for read out data //
for(i=0;i<(HDD_Sector_Size/2);i++)
{
DATA_TEMP=Data_Reg_re;
*Drv_Info_Buf=DATA_TEMP%256;Drv_Info_Buf++;
*Drv_Info_Buf=DATA_TEMP/256;Drv_Info_Buf++;
}
Drv_Cylinders =*(Drv_Info_Buf_Temp+Drv_Cylinders_Offset)+(*(Drv_Info_Buf_Temp+Drv_Cylinders_Offset+1))*256;
Drv_Heads =*(Drv_Info_Buf_Temp+Drv_Heads_Offset) +(*(Drv_Info_Buf_Temp+Drv_Heads_Offset+1))*256;
Drv_Sectors =*(Drv_Info_Buf_Temp+Drv_Sectors_Offset) +(*(Drv_Info_Buf_Temp+Drv_Sectors_Offset+1))*256;
Drv_Total_Addr_Sectors=*(Drv_Info_Buf_Temp+Drv_Total_Addr_Sectors_Offset)\
+(*(Drv_Info_Buf_Temp+Drv_Total_Addr_Sectors_Offset+1))*256\
+(*(Drv_Info_Buf_Temp+Drv_Total_Addr_Sectors_Offset+2))*256*256\
+(*(Drv_Info_Buf_Temp+Drv_Total_Addr_Sectors_Offset+3))*256*256*256;
for (i=0;i<Drv_NameLong;i=i+2)
{
Drv_Name[i]=*(Drv_Info_Buf_Temp+Drv_Name_Offset+i+1);
Drv_Name[i+1]=*(Drv_Info_Buf_Temp+Drv_Name_Offset+i);
}
Uart_Printf("\n*The Drv_Cylinders is:%4d,The Drv_Heads is:%4d,The Drv_Sectors is:%4d*\n* The Driver's Type is:",Drv_Cylinders,Drv_Heads,Drv_Sectors);
for(i=0;i<Drv_NameLong;i++)
Uart_Printf("%c",Drv_Name[i]);
Uart_Printf(" *");
free(Drv_Info_Buf);
}
void ATA_Test(void)
{
U8 Sector_Buf[HDD_Sector_Size];
U8 aa;
U32 LBA=0;
U32 newlba=0;
Identify_Drv(Current_DRV);
Uart_Printf("\n* - :Point to prev sector *");
Uart_Printf("\n* + :Point to next sector *");
Uart_Printf("\n* S(s):Set HDD to StandBy *");
Uart_Printf("\n* Q(q):Quit *");
while(1)
{
Disp_Sector_Buf(Current_DRV,LBA,Sector_Buf);
// while(!(GetKey(&aa))&&(!((aa==1)||(aa==2)||(aa==F1)||(aa==F2)||(aa==F3)||(aa==F4))));
aa= Uart_Getch();
if ((aa=='q')||(aa=='Q')) break;
if ((aa=='S')||(aa=='S'))
{
Set_StandBy_Immediate(Current_DRV);
Uart_Printf("\n*---------------------The Driver is Set to StandBy now!-----------------*");
break;
}
switch(aa)
{
case '-':
if(LBA==0)
LBA=Drv_Total_Addr_Sectors-1;
else
LBA--;
break;
case '+':
LBA++;
if(LBA==Drv_Total_Addr_Sectors)
LBA=0;
break;
case 'J':
case 'j':
// Set_Idle_Immediate(Current_DRV);
// Uart_Printf("\n*---------------------The Driver is Set to IDLE now!--------------------*");
Uart_Printf("\n*-----------------------------------------------------------------------*");
Uart_Printf("\n* 1~10:Number Key *");
Uart_Printf("\n* F3 :Enter *");
Uart_Printf("\n* F4 :Quit *");
Uart_Printf("\n*-----------------------------------------------------------------------*\n");
while(1)
{
// while(!(GetKey(&aa)));
// if(aa==F4) break;
// if(aa==F3)
{
if(newlba<Drv_Total_Addr_Sectors)
{
LBA=newlba;
newlba=0; //reset newlba;
}
else
{
newlba=0;
Uart_Printf("\n* Invalid Data *");
Uart_Printf("\n*************************************************************************");
}
break;
}
if(aa==10)
aa=0;
newlba=newlba*10+aa;
Uart_Printf("NewLBA=%8d\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",newlba);
}
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -