📄 ide_access.c
字号:
#include "ide_base.h"
/**********************************************************************************
* 名称:Wait_Ready
* 功能:等待硬盘是否准备可以操作
* 入口参数:无
* 出口参数:硬盘准备好返回0,如果硬盘出错返回错误码
***********************************************************************************/
BYTE Wait_Ready(void)
{
BYTE statbyte;
BYTE flag = 0;
while (!flag)
{
statbyte = *pStatus ;// Read Status Register
// printf("%x\n",statbyte);
if (statbyte & IDE_ERROR)
{
statbyte=*pErrorReg; //检查硬盘工作是否有错误
return statbyte; //如果有,读错误寄存器,并返回错误码
}
if (statbyte & IDE_DRDY)
flag = 1; // 检查硬盘是否ready
}
return 0;
}
/**********************************************************************************
* 名称: Wait_ReadyBusy
* 功能:等待硬盘是否应精不忙而且准备好可以操作
* 入口参数:无
* 出口参数:硬盘准备好返回0,如果硬盘出错返回错误码
***********************************************************************************/
BYTE Wait_ReadyBusy(void)
{
BYTE statbyte;
BYTE flag = 0;
while (!flag)
{
statbyte = *pStatus; // Read Status Register
if (statbyte & IDE_ERROR)
{
statbyte=*pErrorReg;
return statbyte;
}
if (((statbyte & IDE_DRDY)!=0 )&&((statbyte & IDE_BUSY)==0))
flag = 1; // Ready bit is in pos 6
}
return 0;
}
/**********************************************************************************
* 名称:Wait_DRQ
* 功能:等待数据传输是否准备好
* 入口参数:无
* 出口参数:硬盘准备好返回0,如果硬盘出错返回错误码
***********************************************************************************/
BYTE Wait_DRQ(void)
{
BYTE statbyte;
BYTE flag = 0;
while (!flag)
{
statbyte = *pStatus; // Read Status Register
if (statbyte & IDE_ERROR)
{
statbyte=*pErrorReg;
return statbyte;
}
if (statbyte & IDE_DRQ)
flag = 1; // Ready bit is in pos 6
}
return 0x00;
}
/**********************************************************************************
* 名称:CheckforError
* 功能:检查是否有错误
* 入口参数:无
* 出口参数:没有错误返回0,如果硬盘出错返回错误码
***********************************************************************************/
BYTE CheckforError(void)
{
BYTE statbyte;
statbyte = *pStatus; // Read Status Register
if (statbyte & 0x01)
{
statbyte= *pErrorReg;
return statbyte; // Is LSB (error bit) is set then return 1
}
else
return 0x00;
}
/**********************************************************************************
* 名称:IdeStandby
* 功能:使硬盘进入Standby状态
* 入口参数:无
* 出口参数:无
***********************************************************************************/
void IdeStandby(void)
{
*pCommand = 0xe0;
}
/**********************************************************************************
* 名称:IdeIdle
* 功能:使硬盘进入Idle状态
* 入口参数:无
* 出口参数:无
***********************************************************************************/
void IdeIdle(void)
{
*pCommand = 0x95;
}
/**********************************************************************************
* 名称:fnIDE_BufferSector
* 功能:读出指定逻辑扇区的全部数据
* 入口参数:WORD *buf 数据地址
* DWORD LBALocation 逻辑扇区地址
* BYTE count 读扇区数最大255,如count=0,读入256个扇区
* 出口参数:操作正常返回0,错误返回1
***********************************************************************************/
int fnIDE_ReadBufferSector(WORD *buf,DWORD LBALocation,BYTE count)
{
WORD i,temp;
BYTE j;
BYTE errorcode;
unsigned short Head, Cyl_H,Cyl_L,SecNum;
Wait_ReadyBusy();
*pDriveHead=((LBALocation >> 24) & 0xFF) | 0xE0;
*pCylinderHigh=(LBALocation >> 16) & 0xFF;
*pCylinderLow=(LBALocation >> 8) & 0xFF;
*pSectorNumber=LBALocation & 0xFF;
*pSectorCount=count;
*pCommand=0x21;
j=count;
temp=0;
do
{
Wait_DRQ();
for(i=0; i < SECTORWORDSIZE; i++)
{
buf[temp+i] = *pDataPort;
}
j--;
temp += SECTORWORDSIZE ;
}while(j!=0);
errorcode = CheckforError();
if (errorcode!=0)
return 1;
return 0;
}
/**********************************************************************************
* 名称:fnIDE_WriteBufferSector
* 功能:向指定逻辑扇区写数据
* 入口参数:WORD *buf 数据地址
* DWORD LBALocation 逻辑扇区地址
* BYTE count 读扇区数最大255,如count=0,读入256个扇区
* 出口:操作正常返回0,错误返回1
***********************************************************************************/
int fnIDE_WriteBufferSector(WORD *buf,DWORD LBALocation,BYTE count)
{
WORD i,temp;
BYTE j;
BYTE errorcode;
Wait_ReadyBusy();
*pDriveHead=((LBALocation >> 24) & 0xFF) | 0xE0;
*pCylinderHigh=(LBALocation >> 16) & 0xFF;
*pCylinderLow=(LBALocation >> 8) & 0xFF;
*pSectorNumber=LBALocation & 0xFF;
*pSectorCount=count;
*pCommand=0x31;
j=count;
temp=0;
do
{
Wait_DRQ();
for(i=0; i < SECTORWORDSIZE; i++)
{
*pDataPort=buf[temp+i];
}
j--;
temp += SECTORWORDSIZE ;
}while(j!=0);
errorcode = CheckforError();
if (errorcode!=0)
return 1;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -