⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ata.c

📁 AT91RM9200-DK的cf卡读写程序
💻 C
字号:
#define U16 unsigned short
#define U32 unsigned long
#define U8  unsigned char

#include <string.h>
#include <stdarg.h>
#include <stdio.h>

#include "ata.h"

#define CLRSCREEN	                "\033[2J"

extern void AT91F_DBGU_Printk(char *);
extern void AT91F_DBGU_Printf(char *fmt,...);
extern char AT91F_DBGU_Getc(void);

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);
  //while(!Error_Reg);
  // Driver is Ready for read out data //	
  for(i=0;i<(HDD_Sector_Size/2);i++)
  {
    DATA_TEMP=Data_Reg;
    *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=(*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;
  U8 Buf[HDD_Sector_Size];  
//0  Read_Sectors(Drv,LBA,Sec_Buf);
  
  Read_Sectors(Drv,LBA,Buf);  

  AT91F_DBGU_Printf("\n\n\r*------------------  Sector: %5d is show as following: ---------------*\n",LBA);



//  for(i=0;i<HDD_Sector_Size;i++)
//   AT91F_DBGU_Printf("%s",Buf[i]);

  AT91F_DBGU_Printk((char *)Buf);
/*  
  for(i=0;i<HDD_Sector_Size;i=i+16)
  {
    AT91F_DBGU_Printf("\n\rBYTE%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 Buf[HDD_Sector_Size];
  U8 *Drv_Info_Buf;
  U8 *Drv_Info_Buf_Temp;
  U32 i;
  U16 DATA_TEMP;
  Drv_Info_Buf      =(U8 *)Buf;
  Drv_Info_Buf_Temp =Drv_Info_Buf;
	
  SDH=0xA0+(DRV&0x10);			// Selcet the Driver //
  Command_Reg=Identify_Drive;
	
  // 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;
    *Drv_Info_Buf=DATA_TEMP%256;Drv_Info_Buf++;
    *Drv_Info_Buf=DATA_TEMP/256;Drv_Info_Buf++;
  }
/*  
  		#define Drv_Cylinders_Offset    2
		#define Drv_Heads_Offset		6
		#define Drv_Sectors_Offset		12
		#define Drv_Total_Addr_Sectors_Offset 120	
		#define Drv_NameLong			40
		#define Drv_Name_Offset			54
*/

//0 
		#define Firmware_NameLong			8
		#define Firmware_Name_Offset			46

		#define Firmware1_NameLong			20
		#define Firmware1_Name_Offset			20		
//0
 
  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))<<8)\
                       +((*(Drv_Info_Buf_Temp+Drv_Total_Addr_Sectors_Offset+2))<<16)\
                       +((*(Drv_Info_Buf_Temp+Drv_Total_Addr_Sectors_Offset+3))<<24);
  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);
  }
    
  
AT91F_DBGU_Printf("\n\rDevice type NO: 0x%x",*(Drv_Info_Buf_Temp+0)+((*(Drv_Info_Buf_Temp+1))<<8));
AT91F_DBGU_Printf("\n\rDrv_Total_Addr_Sectors: %d",Drv_Total_Addr_Sectors);


  AT91F_DBGU_Printf("\n\n\r*The Drv_Cylinders is:%5d,The Drv_Heads is:%3d,The Drv_Sectors is:%3d *",Drv_Cylinders,Drv_Heads,Drv_Sectors);
  AT91F_DBGU_Printf("\n\r*The Driver's Type is:");  
  for(i=0;i<Drv_NameLong;i++)
   AT91F_DBGU_Printf("%c",Drv_Name[i]);      
  AT91F_DBGU_Printf("          *");   
//0 
AT91F_DBGU_Printf("\n\r*    Firmware_Name is:");  
  for (i=0;i<Firmware_NameLong;i=i+2)
  {
    Drv_Name[i]=*(Drv_Info_Buf_Temp+Firmware_Name_Offset+i+1);
    Drv_Name[i+1]=*(Drv_Info_Buf_Temp+Firmware_Name_Offset+i);
  } 
  for(i=0;i<Drv_NameLong;i++)
   AT91F_DBGU_Printf("%c",Drv_Name[i]); 
  AT91F_DBGU_Printf("          *");    
//0 

//0 
AT91F_DBGU_Printf("\n\r*         ACSII序列号:");  
  for (i=0;i<Firmware1_NameLong;i=i+2)
  {
    Drv_Name[i]=*(Drv_Info_Buf_Temp+Firmware1_Name_Offset+i+1);
    Drv_Name[i+1]=*(Drv_Info_Buf_Temp+Firmware1_Name_Offset+i);
  } 
  for(i=0;i<Drv_NameLong;i++)
   AT91F_DBGU_Printf("%c",Drv_Name[i]); 
  AT91F_DBGU_Printf("          *");    
//0    
      
}
void ATA_Test(void)
{
  char Sector_Buf[HDD_Sector_Size];
//0  
  U32 i;
  U32 myLBA=24012;  
//0  
  U8 aa;
  U32 LBA=0;
  U32 newlba=0;  
loop:  
  Identify_Drv(Current_DRV);
  AT91F_DBGU_Printf("\n\r*                  - :Point to prev sector                              *");
  AT91F_DBGU_Printf("\n\r*                  + :Point to next sector                              *");
  AT91F_DBGU_Printf("\n\r*                S(s):Set HDD to StandBy                                *");
  AT91F_DBGU_Printf("\n\r*                J(j):Jump To Specific Sector(LBA)                      *");
  AT91F_DBGU_Printf("\n\r*                Z(z):Write a sentence to a Sector:%8d             *",myLBA);  
//  AT91F_DBGU_Printf("\n\r*                     F(f):Use FAT32 tools                              *");
//  AT91F_DBGU_Printf("\n\r*                     Q(q):Quit                                         *");
  while(1)
  {
    Disp_Sector_Buf(Current_DRV,LBA,(U8 *)Sector_Buf);
//    while(!(  GetKey(&aa))&&(!((aa==1)||(aa==2)||(aa==F1)||(aa==F2)||(aa==F3)||(aa==F4))   ));
    aa= AT91F_DBGU_Getc();
//	aa='+';
    switch(aa)
    {
    case 'q':
    case 'Q':    
		goto loop;
    case 'S':
    case 's':    
      Set_StandBy_Immediate(Current_DRV);
      AT91F_DBGU_Printf("\n\r*---------------------The Driver is Set to StandBy now!-----------------*");
      break;
    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 'z': 
    case 'Z':             	
 	//* Write Page 1
        sprintf(Sector_Buf,"\n\rCongratulations!!  \
        This sentence is written in your ATA Device... \
        欢迎使用深圳英贝德公司的产品,希望我们的努力能带给你帮助!\
        祝你好运连连!!!!!\
        更多帮助请登陆(http://www.szembed.com!!!!!)\n\r"); 
		Write_Sectors(Current_DRV,0x01,myLBA,(U8 *)Sector_Buf); 
		for(i=0;i<HDD_Sector_Size;i++) 	Sector_Buf[i] = 0x00;
		Read_Sectors(Current_DRV,myLBA,(U8 *)Sector_Buf);
		AT91F_DBGU_Printk("\n\r");		
		AT91F_DBGU_Printk(Sector_Buf);		    
            
      break;      	
    case 'J':
    case 'j':
    Set_Idle_Immediate(Current_DRV);
    AT91F_DBGU_Printf("\n\r*---------------------Jump To Display Specific Sector(LBA)!--------------------*");
      AT91F_DBGU_Printf("\n\r*-----------------------------------------------------------------------*");
      AT91F_DBGU_Printf("\n\r*                          1~9:    Number Key                              *");
      AT91F_DBGU_Printf("\n\r*                          Enter:  Confirm                                   *");
      AT91F_DBGU_Printf("\n\r*                          Q  :    Quit                                    *");
      AT91F_DBGU_Printf("\n\r*-----------------------------------------------------------------------*\n");
      
      while(1)
      {
//        while(!(GetKey(&aa)));
		aa= AT91F_DBGU_Getc();
        if((aa=='Q')||(aa=='q')) break;        
        if((aa=='\r'))
        {
          if(newlba<Drv_Total_Addr_Sectors)
          {
            LBA=newlba;
            newlba=0;            //reset newlba;
          }
          else
          {
            newlba=0;
            AT91F_DBGU_Printf("\n\r*                          Invalid Data                                 *");
            AT91F_DBGU_Printf("\n\r*************************************************************************");						
          }
          break;
        }
        if(aa==10)
          aa=0;
        aa-=48;
        newlba=newlba*10+aa;
        AT91F_DBGU_Printk(CLRSCREEN);
        AT91F_DBGU_Printf("\n\rNewLBA=%8d",newlba);
      }
      break;
    }
  	
  }
  goto loop;  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -