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

📄 ide.c

📁 在arm realview下的s3c2410开发板测试工程及代码
💻 C
字号:
#include "def.h"
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"

#include "ide.h"


#define IDE_Tacs	(0x0)	// 0clk
#define IDE_Tcos	(0x3)	// 4clk
#define IDE_Tacc	(0x7)	// 14clk
#define IDE_Tcoh	(0x1)	// 1clk
#define IDE_Tah		(0x0)	// 0clk
#define IDE_Tacp	(0x3)	// 6clk
#define IDE_PMC		(0x0)	// normal(1data)


void IDE_InitBoard(void);
void IDE_InitInterrupt(void);
void IDE_REG_Wr(U8 index, U8 data);
U8 IDE_REG_Rd(U8 index);
int IDE_ReadStatus(U8 index);

void IDE_Wr(void);
void IDE_Rd(void);

void __irq IsrIDE(void);



void Test_IDE(void)
{
    U8 c;
    Uart_Printf("[Test IDE]\n");
    IDE_InitBoard();
    //while(1)
    //	IDE_REG_Wr(0,0x5a);
    
	Uart_Printf("write data to ide disk,please input 'y'\n");
        c=Uart_Getch();
        if(c=='y')
        	IDE_Wr();
	Uart_Printf("\nRead data from ide disk,please input 'y'\n");
        c=Uart_Getch();
	if(c=='y')	
		IDE_Rd();
	Uart_Printf("\nQuit IDE test!\n\n");
    
}

//写数据到硬盘中
void IDE_Wr(void)
{
	U8 startSector;//起始扇区
	U8 sectorNum;//扇区数目
	U8 lowCyliner;//柱面号,低位
	U8 highCyliner;//柱面号,高位
	U8 head;//磁头
	
	U8 status;
	U16 data;
	int i;
	
	startSector=1;
	sectorNum=1;
	lowCyliner=1;
	highCyliner=0;
	head=0xa0;//1010,0,000:master,0 head
	Uart_Printf("\nWrite data to ide disk,please input start sector, sector num, low cyliner, hingh cyliner\n");
	Uart_Printf("\nplease input start sector=");
	startSector=Uart_GetIntNum();
	Uart_Printf("\nplease input sector num=");
	sectorNum=Uart_GetIntNum();
	Uart_Printf("\nplease input low cyliner=");
	lowCyliner=Uart_GetIntNum();
	Uart_Printf("\nplease input high cyliner=");
	highCyliner=Uart_GetIntNum();
	Uart_Printf("\nWrite data to ide disk\n start sector=%d", startSector);
	Uart_Printf("\nsector num=%d",sectorNum);
	Uart_Printf("\nlow cyliner=%d",lowCyliner);
	Uart_Printf("\nhingh cyliner=%d\n",highCyliner);

	//读硬盘的状态
	i=0;
	while(IDE_ReadStatus(BIT_IDE_BSY))
	{
		i++;
		Delay(10);
		if(i>10000)
		{
			Uart_Printf("Disk is error,please check it!!\n");
			break;
		}
	}
	
	//写地址	
	IDE_REG_Wr(rIDE_SECTOR_NUM,sectorNum);//写扇区数目
	IDE_REG_Wr(rIDE_START_SECTOR,startSector);//写起始扇区号
	IDE_REG_Wr(rIDE_LOW_CYLINDER,lowCyliner);//写低柱面号
	IDE_REG_Wr(rIDE_HIGHT_CYLINDER,highCyliner);//写高柱面号
	IDE_REG_Wr(rIDE_HEAD_DEVICE,head);//写磁头
	
	//读硬盘的状态
	i=0;
	while(IDE_ReadStatus(BIT_IDE_RDY))
	{
		//busy
		i++;
		Delay(10);
		if(i>10000)
		{
			Uart_Printf("Disk is error,please check it!!\n");
			break;
		}
	}	
	
	//写命令
	IDE_REG_Wr(rIDE_STATUS,0x30);//写磁头
	while(!IDE_ReadStatus(BIT_IDE_DRQ));//等待驱动器准备好
	
	//写数据
	data=0xaeae;//16位
	Uart_Printf("\nplease input data data=0%x");
	data=Uart_GetIntNum();
	for(i=0;i<512;i++)
	{
		Uart_Printf("*");
		IDE_REG_Wr(rIDE_DATA,data);
	}
	
	//等待驱动器写成功
	while(IDE_ReadStatus(BIT_IDE_BSY));
	
	//i/o方式读状态
	status=((rGPGDAT& 0x200)>>9);//第9位
	
	//读出错位
	if(IDE_ReadStatus(BIT_IDE_ERR))
		Uart_Printf("data is error to write to disk!!\n");
	else
		Uart_Printf("data is ok to write to disk!!\n");
		
	return; 
	
	
}
//写数据到硬盘中
void IDE_Rd(void)
{
	U8 startSector;//起始扇区
	U8 sectorNum;//扇区数目
	U8 lowCyliner;//柱面号,低位
	U8 highCyliner;//柱面号,高位
	U8 head;//磁头
	
	U8 status;
	U16 data[512];
	int i;
	
	startSector=1;
	sectorNum=1;
	lowCyliner=1;
	highCyliner=0;
	head=0xa0;//1010,0,000:master,0 head
	Uart_Printf("\nRead data from ide disk,please input start sector, sector num, low cyliner, hingh cyliner\n");
	Uart_Printf("\nplease input start sector=");
	startSector=Uart_GetIntNum();
	Uart_Printf("\nplease input sector num=");
	sectorNum=Uart_GetIntNum();
	Uart_Printf("\nplease input low cyliner=");
	lowCyliner=Uart_GetIntNum();
	Uart_Printf("\nplease input high cyliner=");
	highCyliner=Uart_GetIntNum();
	Uart_Printf("\nWrite data to ide disk\n start sector=%d", startSector);
	Uart_Printf("\nsector num=%d",sectorNum);
	Uart_Printf("\nlow cyliner=%d",lowCyliner);
	Uart_Printf("\nhingh cyliner=%d\n",highCyliner);
	

	//写地址	
	IDE_REG_Wr(rIDE_SECTOR_NUM,sectorNum);//写扇区数目
	IDE_REG_Wr(rIDE_START_SECTOR,startSector);//写起始扇区号
	IDE_REG_Wr(rIDE_LOW_CYLINDER,lowCyliner);//写低柱面号
	IDE_REG_Wr(rIDE_HIGHT_CYLINDER,highCyliner);//写高柱面号
	IDE_REG_Wr(rIDE_HEAD_DEVICE,head);//写磁头
	

	//写命令
	IDE_REG_Wr(rIDE_STATUS,IDE_CMD_READ);//写磁头
	while(!IDE_ReadStatus(BIT_IDE_DRQ));//等待驱动器准备好
	
	//i/o方式读状态
	status=((rGPGDAT& 0x200)>>9);//第9位	
	
	//读出错位
	if(IDE_ReadStatus(BIT_IDE_ERR))
		Uart_Printf("data is error to write to disk!!\n");
	else
		Uart_Printf("data is ok to write to disk!!\n");	
		
	//读数据
	for(i=0;i<512;i++)
	{
		data[i]=IDE_REG_Rd(rIDE_DATA);
		Uart_Printf("\ndata[%d]=0%x",i,data[i]);
		
	}
	

	return ; 
	
	
}

int IDE_ReadStatus(U8 index)
{
	int i;
	U8 status;
	//busy
	status=IDE_REG_Rd(rIDE_STATUS);
	
	switch(index)
	{
		case 0:
			status=(status | 0xfe); //ERROR
			break;
		case 1:
			status=(status | 0xfd); //IDX
			break;
		case 2:
			status=(status | 0xfb); //CORR
			break;
		case 3:
			status=(status | 0xf7); //DRQ
			break;
		case 4:
			status=(status | 0xef); //dsc
			break;
		case 5:
			status=(status | 0xdf); //dwf
			break;
		case 6:
			status=(status | 0xbf); //drdy
			break;
		case 7:
			status=(status | 0x7f); //bsy
			break;
	}
	if(status==0xff)
		return 1;//1
	else
		return 0;//0
}





void IDE_InitBoard(void)
{
//Initialize S3C2410X for IDE
#ifdef INTERRUPT
    rGPGCON=rGPGCON&~(3<<6)|(2<<6);	//EINT17(GPG9),中断方式
#else
    rGPGCON=rGPGCON&~(3<<6);	//i/o方式
#endif    
	    

    rBWSCON=rBWSCON&~(0xf<<16)|(0x9<<8);	  //nGCS4=nUB/nLB(nSBHE),nWAIT,16-bit

    rBANKCON4=((IDE_Tacs<<13)+(IDE_Tcos<<11)+(IDE_Tacc<<8)+(IDE_Tcoh<<6)\
		+(IDE_Tah<<4)+(IDE_Tacp<<2)+(IDE_PMC));
}


void IDE_InitInterrupt(void)
{
    //EINT71
   

    rEXTINT2=rEXTINT2&~(7<<4)|(4<<4);//EINT17,rising edge


    pISR_EINT8_23=(U32)IsrIDE;		//nINT17
    rSRCPND = BIT_EINT8_23; //to clear the previous pending states
    rINTPND = BIT_EINT8_23;

    rINTMSK=~(BIT_EINT8_23);  
 }




void IDE_REG_Wr(U8 index, U8 data)
{
    //nREG=L
    (*(volatile unsigned char *)(IDE_BASE_ADDRESS+index))=data;
    
}


U8 IDE_REG_Rd(U8 index)
{
    //nREG=L
    

    return (*(volatile unsigned char *)(IDE_BASE_ADDRESS+index));
}




void __irq IsrIDE(void)	//nINT_P_DEV
{
    rEINTPEND=(1<<5); //EINTPEND[5] is cleared.
    ClearPending(BIT_EINT8_23);
    Uart_Printf("ide interrupt\n");
}



⌨️ 快捷键说明

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