/*H**************************************************************************
* NAME: harddisk.h
*----------------------------------------------------------------------------
* Copyright (c) 2004 Toplow.
*----------------------------------------------------------------------------
* RELEASE:
* REVISION:
*----------------------------------------------------------------------------
* PURPOSE:
*
*
* NOTES:
* Global Variables:
* - gl_sbc_wr_protect: write protect flag for SCSI disk
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h" /* system configuration */
#include
#define FILE0 XBYTE[0x0880] //硬盘数据寄存器
#define FILE1 XBYTE[0x0881] //错误/特征寄存器
#define FILE2 XBYTE[0x0882] //扇区数寄存器
#define FILE3 XBYTE[0x0883] //扇区号寄存器
#define FILE4 XBYTE[0x0884] //柱面号寄存器L
#define FILE5 XBYTE[0x0885] //柱面号寄存器H
#define FILE6 XBYTE[0x0886] //驱动器/磁头寄存器
#define FILE7 XBYTE[0x0887] //状态/命令寄存器
///////////////////位设置///////////////////////////
sbit BSY=ACC^7;
sbit DRDY=ACC^6;
sbit DRQ=ACC^3;
sbit ERR=ACC^0;
/************************************************************
* D7 D6 D5 D4 D3 D2 D1 D0 *
BSY DRDY DWF DSC DRQ CORR IDX ERR *
BSY:驱动器忙; *
DRDY:驱动器准备好; *
DWF:驱动器写失败; *
DSC:寻道结束; *
DRQ:请求服务,驱动器希望通过数据寄存器与CPU交换一字节数据;*
CORR:当可以纠正的读错误发生时,该位置1,数据传输将继续进行 *
IDX:收到综引信号; *
ERR:命令执行出错。 *
*************************************************************/
sbit ARST=P5^3;
sbit INTQ=P5^1;
//uchar status; //从硬盘寄存器返回的状态数据寄存器
uchar buffer0,buffer1; //硬盘16位数据
/*******************************************************
*函数名称:void ReadReg(uchar address) *
*函数用途:读硬盘非数据寄存器内容,并将结果返回 *
********************************************************/
/*void ReadReg(uchar address)
{
status=address;
ACC=status;
}
/*******************************************************
*函数名称:void WriteReg(uchar address,uchar sdata) *
*函数用途:写硬盘非数据寄存器内容 *
********************************************************/
/*void WriteReg(uchar address,uchar sdata)
{XBYTE[0x0881]=address;
}
/*******************************************************
*函数名称:void ReadData() *
*函数用途:读硬盘数据寄存器内容 *
********************************************************/
void ReadData()
{
buffer0=FILE0;
buffer1=DAT16H;
}
/*******************************************************
*函数名称:void WriteData(uchar Data0,uchar Data1) *
*函数用途:写硬盘数据寄存器内容 *
********************************************************/
/*void WriteData(uchar address,uchar data0,uchar data1)
{
DAT16H=data1;
address=data0;
}*/
/*********************************************************
*函数名称:WriteLBA(Uint32 LBA) *
*函数用途:写LBA信息(地址信息) *
**********************************************************/
WriteLBA(Uint32 LBA)
{uchar head,cylinderh,cylinderl,sector;
head=(uchar)(LBA>>24);
cylinderh=(uchar)((LBA&0x00ff0000)>>16);
cylinderl=(uchar)((LBA&0x0000ff00)>>8);
sector=(uchar)(LBA&0x000000ff);
do{ACC=FILE7;}while(BSY);
FILE6=(0xe0|(head&0x0f));
FILE5=cylinderh; //high 2-bit cylinder
FILE4=cylinderl;//low 8-bit cylinder
FILE3=sector; //start sector
FILE2=0x01; //sector counter
}
/*********************************************************
*函数名称:WriteCHS(uint cylinder,uchar head,uchar sector)*
*函数用途:写CHS信息(地址信息) *
**********************************************************/
WriteCHS(uchar head,uchar cylinderh,uchar cylinderl,uchar sector)
{do{ACC=FILE7;}while(BSY);
FILE6=(0xe0|(head&0x0f));
FILE5=cylinderh; //high 2-bit cylinder
FILE4=cylinderl;//low 8-bit cylinder
FILE3=sector; //start sector
FILE2=0x01; //sector counter
}
/*********************************************************
*函数名称:WriteCommand(command) *
*函数用途:写命令 *
**********************************************************/
WriteCommand(command)
{do{ACC=FILE7;}while(BSY);
FILE7=command;
}
/**********************************************************
*函数名称:void DriveID(void) *
*用途:读硬盘ID *
***********************************************************/
void DriveID(void)
{uint i=512;
do{ACC=FILE7;}while(BSY);
FILE7=0xec;
do{ACC=FILE7;}while(BSY|!DRQ);
while(i){ReadData();putch(buffer0);putch(buffer1);i-=2;}
}
/**********************************************************
*函数名称:GetData() *
*用途:将硬盘的返回数据读入BUFFER数组 *
***********************************************************/
void GetData()
{uint i=0;
do{ACC=FILE7;}while(BSY|!DRQ);
if(ERR){LcdWriteCH(0x90,MEG[0],MEG[1]);
LcdWriteCH(0x91,MEG[2],MEG[3]);
}
/* else {LcdWriteCH(0x90,MEG[4],MEG[5]);
LcdWriteCH(0x91,MEG[4],MEG[5]);
LcdWriteCH(0x94,INF[0],INF[1]);
LcdWriteCH(0x95,INF[2],INF[3]);
LcdWriteCH(0x96,INF[4],INF[5]);
}*/
while(i!=512){ReadData();
BUFFER[i++]=buffer0;
BUFFER[i++]=buffer1;
}
}
////////////////////////////////////////////////////////////
void GetTowSector(void)
{uint i=0;
do{ACC=FILE7;}while(BSY|!DRQ);
if(ERR){LcdWriteCH(0x90,MEG[0],MEG[1]);
LcdWriteCH(0x91,MEG[2],MEG[3]);
}
/* else {LcdWriteCH(0x90,MEG[4],MEG[5]);
LcdWriteCH(0x91,MEG[4],MEG[5]);
LcdWriteCH(0x94,INF[0],INF[1]);
LcdWriteCH(0x95,INF[2],INF[3]);
LcdWriteCH(0x96,INF[4],INF[5]);
}*/
while(i!=1024){ReadData();
BUFFER[i++]=buffer0;
BUFFER[i++]=buffer1;
}
}
/**********************************************************
*函数名称:GetLBAData() *
*用途:将硬盘的返回数据读入BUFFER数组 *
***********************************************************/
void GetLBAData(Uint32 LBA)
{uchar head,cylinderh,cylinderl,sector;
head=(uchar)(LBA>>24);
cylinderh=(uchar)((LBA&0x00ff0000)>>16);
cylinderl=(uchar)((LBA&0x0000ff00)>>8);
sector=(uchar)(LBA&0x000000ff);
do{ACC=FILE7;}while(BSY);
FILE6=(0xe0|(head&0x0f));
FILE5=cylinderh; //high 2-bit cylinder
FILE4=cylinderl;//low 8-bit cylinder
FILE3=sector; //start sector
FILE2=0x02; //sector counter
WriteCommand(0x20);
GetTowSector();
BufferFlag=1;
}
/**********************************************************
*函数名称:void IniHard(void) *
*用途:初始化硬盘 *
***********************************************************/
void IniHard(void)
{ARST=0;
delay(0xffff);
ARST=1;
delay(0xffff);
AUXR=0x5c; //设置EXT16=1,使能16-BIT传送模式
do{FILE6=0xa0; ACC=FILE7;}while(!DRDY|BSY);
FILE6=0x20;
FILE2=64;
FILE7=0x91;
do{ACC=FILE7;}while(BSY);
FILE7=0x10;
do{ACC=FILE7;}while(BSY);
}