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

📄 ata.c

📁 可移到ucos上的文件系统
💻 C
字号:
/*! \file ata.c \brief IDE-ATA hard disk interface driver. */
//*****************************************************************************
//
// File Name	: 'ata.c'
// Title		: IDE-ATA interface driver for hard disks
// Author		: Pascal Stang
// Date			: 11/22/2000
// Revised		: 4/19/2003
// Version		: 0.3
// Target MCU	: Atmel AVR Series
// Editor Tabs	: 4
//
// NOTE: This code is currently below version 1.0, and therefore is considered
// to be lacking in some functionality or documentation, or may not be fully
// tested.  Nonetheless, you can expect most functions to work.
//
// This code is distributed under the GNU Public License
//		which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************

#include "option.h"
#include "def.h"
#include "44b.h"
#include "44blib.h"
#include "ata.h"

//extern void Delay(int time);
//#define DEBUG_ATA	1

// global variables

// drive information
typeDriveInfo ataDriveInfo;


void ataInit(void)
{

}

void ataDriveInit(void)
{
	U8 i;
	unsigned char* buffer = (U8 *)malloc(HDD_Sector_Size);

	// read drive identity
	Uart_Printf("\r\nScanning IDE interface...\r\n");
	// Wait for drive to be ready
	ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);
	// issue identify command
	ataWriteByte(ATA_REG_CMDSTATUS1, 0xEC);
	// wait for drive to request data transfer
	ataStatusWait(ATA_SR_DRQ, ATA_SR_DRQ);
	Delay(200);
	// read in the data
	ataReadDataBuffer(buffer, HDD_Sector_Size);

	// set local drive info parameters
	ataDriveInfo.cylinders =		*( ((unsigned int*) buffer) + ATA_IDENT_CYLINDERS );
	ataDriveInfo.heads =			*( ((unsigned int*) buffer) + ATA_IDENT_HEADS );
	ataDriveInfo.sectors =			*( ((unsigned int*) buffer) + ATA_IDENT_SECTORS );
	ataDriveInfo.LBAsupport =		*( ((unsigned int*) buffer) + ATA_IDENT_FIELDVALID );
	ataDriveInfo.sizeinsectors =	*( (unsigned long*) (buffer + ATA_IDENT_LBASECTORS*2) );
	// copy model string
	for(i=0; i<40; i+=2)
	{
		// correct for byte order
		ataDriveInfo.model[i  ] = buffer[(ATA_IDENT_MODEL*2) + i + 1];
		ataDriveInfo.model[i+1] = buffer[(ATA_IDENT_MODEL*2) + i    ];
	}
	// terminate string
	ataDriveInfo.model[40] = 0;

	// process and print info
	if(ataDriveInfo.LBAsupport)
	{
		// LBA support
		Uart_Printf("Drive 0: %dMB\n", ataDriveInfo.sizeinsectors/(1000000/512) );
		Uart_Printf("LBA mode -- MODEL: ");
	}
	else
	{
		// CHS, no LBA support
		// calculate drive size
		ataDriveInfo.sizeinsectors = (unsigned long) ataDriveInfo.cylinders*
												ataDriveInfo.heads*ataDriveInfo.sectors;
		Uart_Printf("Drive 0: %dMB\n", ataDriveInfo.sizeinsectors/(1000000/512) );
		Uart_Printf("CHS mode C=%d H=%d S=%d -- MODEL:\n", ataDriveInfo.cylinders, ataDriveInfo.heads, ataDriveInfo.sectors );
	}
	// print model information	
	Uart_Printf("%s\n",ataDriveInfo.model);

	// initialize local disk parameters
	//ataDriveInfo.cylinders = ATA_DISKPARM_CLYS;
	//ataDriveInfo.heads = ATA_DISKPARM_HEADS;
	//ataDriveInfo.sectors = ATA_DISKPARM_SECTORS;

}

void ataDiskErr(void)
{
	unsigned char b;

	b = ataReadByte(ATA_REG_ERROR);	
	Uart_Printf("ATA Error: %u\n",b); 
}

void ataSetDrivePowerMode(U8 DriveNo, U8 mode, U8 timeout)
{
	// select drive
	ataDriveSelect(DriveNo);
	// Wait for drive to be ready
	ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);

	// set mode
	switch(mode)
	{
	case ATA_DISKMODE_SPINDOWN:		ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SPINDOWN); break;
	case ATA_DISKMODE_SPINUP:		ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SPINUP); break;
	case ATA_DISKMODE_SETTIMEOUT:
		ataWriteByte(ATA_REG_SECCOUNT, timeout);
		ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_IDLE_5SU);
		break;
	case ATA_DISKMODE_SLEEP:		ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SLEEP); break;
	default:
		break;
	}
}

void ataPrintSector( U8 *Buffer)
{
	U8 i;
	U16 j;
	U8 *buf;
	U8 s;

	buf = Buffer;
	
	// print the low order address indicies
	Uart_Printf("     00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F  0123456789ABCDEF\r\n");
	Uart_Printf("     -----------------------------------------------  ---- ASCII -----\r\n");
	
	// print the data
	for(j=0; j<0x20; j++)
	{
		// print the high order address index for this line
		Uart_Printf("%u",j<<4);
		Uart_Printf(" ");

		// print the hex data
		for(i=0; i<0x10; i++)
		{
			Uart_Printf("%u",buf[(j<<4)+i]);
			Uart_Printf(" ");
		}
		
		// leave some space
		Uart_Printf(" ");

		// print the ascii data
		for(i=0; i<0x10; i++)
		{
			s = buf[(j<<4)+i]; 
			// make sure character is printable
			if(s >= 0x20)
			{
				Uart_Printf("%u",s);
			}
			else
			{
				Uart_Printf(" ");
			}

		}
		Uart_Printf("\n");
	}
}

void ataReadDataBuffer(U8 *Buffer, U16 numBytes)
{
	unsigned int i;
	U16 DATA_TEMP;

	// read data from drive
	for(i=0;i<(numBytes/2);i++)
	{
		DATA_TEMP=*((volatile unsigned char*) ATA_REG_BASE + ATA_REG_DATA);
		*Buffer++ = DATA_TEMP&0xff;
		*Buffer++ = (DATA_TEMP>>8)&0xff;
	}
}

void ataWriteDataBuffer(U8 *Buffer, U16 numBytes)
{
	unsigned int i;
	U16 DATA_TEMP;

	// write data to drive
    for(i=0;i<(numBytes/2);i++)
    {
      DATA_TEMP=*Buffer;
      Buffer++;
      *((volatile unsigned char*) ATA_REG_BASE + ATA_REG_DATA) = (((*Buffer)<<8)&0xff00)+DATA_TEMP;
      Buffer++;
    }		
}

U8 ataStatusWait(U8 mask, U8 waitStatus)
{
	register U8 status;

	Delay(100);

	// wait for desired status
	while( ((status = ataReadByte(ATA_REG_CMDSTATUS1)) & mask) == waitStatus );

	return status;
}


unsigned char ataReadSectorsCHS(	unsigned char Drive, 
											unsigned char Head, 
											unsigned int Track,
											unsigned char Sector,
											unsigned int numsectors,
											unsigned char *Buffer)
{
  	unsigned char temp;

	// Wait for drive to be ready
	temp = ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);

  	// Prepare parameters...
	ataWriteByte(ATA_REG_HDDEVSEL, 0xA0+(Drive ? 0x10:00)+Head); // CHS mode/Drive/Head
	ataWriteByte(ATA_REG_CYLHI, Track>>8);  		// MSB of track
	ataWriteByte(ATA_REG_CYLLO, Track);     		// LSB of track
  	ataWriteByte(ATA_REG_STARTSEC, Sector);    	// sector
	ataWriteByte(ATA_REG_SECCOUNT, numsectors);	// # of sectors

	// Issue read sector command...
  	ataWriteByte(ATA_REG_CMDSTATUS1, 0x21);

	// Wait for drive to be ready
	temp = ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);

	if (temp & ATA_SR_ERR)
	{
		Uart_Printf("RD ERR\r\n");
		return 1;
	}

	// Wait for drive to request data transfer
	ataStatusWait(ATA_SR_DRQ, 0);

	// read data from drive
	ataReadDataBuffer(Buffer, (HDD_Sector_Size)*numsectors);

	// Return the error bit from the status register...
	temp = ataReadByte(ATA_REG_CMDSTATUS1);	// read status register

	return (temp & ATA_SR_ERR) ? 1:0;
}


unsigned char ataWriteSectorsCHS(unsigned char Drive, 
											unsigned char Head, 
											unsigned int Track,
											unsigned char Sector,
											unsigned int numsectors,
											unsigned char *Buffer)
{
  	unsigned char temp;

	// Wait for drive to be ready
	temp = ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);

  	// Prepare parameters...
	ataWriteByte(ATA_REG_HDDEVSEL, 0xA0+(Drive ? 0x10:00)+Head); // CHS mode/Drive/Head
	ataWriteByte(ATA_REG_CYLHI, Track>>8);  		// MSB of track
	ataWriteByte(ATA_REG_CYLLO, Track);     		// LSB of track
  	ataWriteByte(ATA_REG_STARTSEC, Sector);    	// sector
	ataWriteByte(ATA_REG_SECCOUNT, numsectors);	// # of sectors

	// Issue write sector command
  	ataWriteByte(ATA_REG_CMDSTATUS1, 0x31);

	//Delay(100);

	// Wait for drive to request data transfer
	ataStatusWait(ATA_SR_DRQ, 0);

	// write data to drive
	ataWriteDataBuffer(Buffer, (HDD_Sector_Size)*numsectors);
	
	// Wait for drive to finish write
	temp = ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);

	// check for errors
	if (temp & ATA_SR_ERR)
	{
		Uart_Printf("WR ERR\r\n");
		return 1;
	}

	// Return the error bit from the status register...
	return (temp & ATA_SR_ERR) ? 1:0;
}

unsigned char ataReadSectorsLBA(	unsigned char Drive, 
											unsigned long lba,
											unsigned int numsectors,
                            		unsigned char *Buffer)
{
  	unsigned int cyl, head, sect;
  	unsigned char temp;

#ifdef DEBUG_ATA
	Uart_Printf("ATA LBA read %u %u %u\n",lab,numsectors,(unsigned int)Buffer);
#endif

	sect = (int) ( lba & 0x000000ffL );
	lba = lba >> 8;
	cyl = (int) ( lba & 0x0000ffff );
	lba = lba >> 16;
	head = ( (int) ( lba & 0x0fL ) ) | ATA_HEAD_USE_LBA;

	temp = ataReadSectorsCHS( Drive, head, cyl, sect, numsectors, Buffer );

	if(temp)
		ataDiskErr();
	return temp;
}

unsigned char ataWriteSectorsLBA(	unsigned char Drive, 
												unsigned long lba,
												unsigned int numsectors,
                            			unsigned char *Buffer)
{
	unsigned int cyl, head, sect;
	unsigned char temp;

#ifdef DEBUG_ATA
	Uart_Printf("ATA LBA read %u %u %u\n",lab,numsectors,(unsigned int)Buffer);
#endif

	sect = (int) ( lba & 0x000000ffL );
	lba = lba >> 8;
	cyl = (int) ( lba & 0x0000ffff );
	lba = lba >> 16;
	head = ( (int) ( lba & 0x0fL ) ) | ATA_HEAD_USE_LBA;

	temp = ataWriteSectorsCHS( Drive, head, cyl, sect, numsectors, Buffer );

	if(temp)
		ataDiskErr();
	return temp;
}                            		


unsigned char ataReadSectors(	unsigned char Drive, 
										unsigned long lba,
										unsigned int numsectors,
                            	unsigned char *Buffer)
{
  	unsigned int cyl, head, sect;
  	unsigned char temp;

	// check if drive supports native LBA mode
	if(ataDriveInfo.LBAsupport)
	{
		// drive supports using native LBA
		temp = ataReadSectorsLBA(Drive, lba, numsectors, Buffer);
	}
	else
	{
		// drive required CHS access
		#ifdef DEBUG_ATA
			// do this defore destroying lba
			Uart_Printf("ATA LBA for CHS read: LBA=%u\n",lba);
		#endif

		// convert LBA to pseudo CHS
		// remember to offset the sector count by one
		sect = (U8) (lba % ataDriveInfo.sectors)+1;
		lba = lba / ataDriveInfo.sectors;
		head = (U8) (lba % ataDriveInfo.heads);
		lba = lba / ataDriveInfo.heads;
		cyl = (U16) lba;

		#ifdef DEBUG_ATA
			Uart_Printf("C:H:S=%u:%u:%u\n",cyl,head,sect);
		#endif

		temp = ataReadSectorsCHS( Drive, head, cyl, sect, numsectors, Buffer );
	}

	if(temp)
		ataDiskErr();
	return temp;
}


unsigned char ataWriteSectors(unsigned char Drive, 
										unsigned long lba,
										unsigned int numsectors,
                            	unsigned char *Buffer)
{
  	unsigned int cyl, head, sect;
  	unsigned char temp;

	// check if drive supports native LBA mode
	if(ataDriveInfo.LBAsupport)
	{
		// drive supports using native LBA
		temp = ataWriteSectorsLBA(Drive, lba, numsectors, Buffer);
	}
	else
	{
		// drive required CHS access
		#ifdef DEBUG_ATA
			// do this defore destroying lba
			Uart_Printf("ATA LBA for CHS write: LAB=%u\n",lba);
		#endif

		// convert LBA to pseudo CHS
		// remember to offset the sector count by one
		sect = (U8) (lba % ataDriveInfo.sectors)+1;
		lba = lba / ataDriveInfo.sectors;
		head = (U8) (lba % ataDriveInfo.heads);
		lba = lba / ataDriveInfo.heads;
		cyl = (U16) lba;

		#ifdef DEBUG_ATA
			Uart_Printf("C:H:S=%u:%u:%u\n",cyl,head,sect);
		#endif

		temp = ataWriteSectorsCHS( Drive, head, cyl, sect, numsectors, Buffer );
	}

	if(temp)
		ataDiskErr();
	return temp;
}                            		

void ataDriveSelect(U8 DriveNo)
{
	ataWriteByte(ATA_REG_HDDEVSEL, 0xA0+(DriveNo ? 0x10:00)); // Drive selection
}
 
//----------------------------------------------------------------------------
// Set drive mode (STANDBY, IDLE)
//----------------------------------------------------------------------------
/*#define STANDBY 0
#define IDLE    1
#define SLEEP   2 
*/ 

/*
unsigned char SetMode(unsigned char DriveNo, unsigned char Mode, unsigned char PwrDown) 
{
  WriteBYTE(CMD, 6, 0xA0 + (DriveNo ? 0x10:0x00)); // Select drive
  WriteBYTE(CMD, 2, (PwrDown ? 0x01:0x00)); // Enable automatic power down
  switch (Mode) 
  {
    case STANDBY: WriteBYTE(CMD,7, 0xE2); break;
    case IDLE:    WriteBYTE(CMD,7, 0xE3); break;
    // NOTE: To recover from sleep, either issue a soft or hardware reset !
    // (But not on all drives, f.ex seagate ST3655A it's not nessecary to reset
    // but only to go in Idle mode, But on a Conner CFA170A it's nessecary with
    // a reset)
    case SLEEP:   WriteBYTE(CMD,7, 0xE6); break;
  }
  Timer10mSec=10000;
  while ((ReadBYTE(CMD,7) & 0xC0)!=0x40 && Timer10mSec); // Wait for DRDY & NOT BUSY 
  if (Timer10mSec==0) return 0xFF;                       //   or timeout
 
  // Return the error register...
  return ReadBYTE(CMD, 1);
}

*/

U8 ataReadByte(U8 reg)
{
	register U8 ret;
	//sbi(MCUCR, SRW);			// enable RAM waitstate
	ret = *((volatile unsigned char*) ATA_REG_BASE + reg);
	//cbi(MCUCR, SRW);			// disable RAM waitstate
	return ret;
}

void ataWriteByte(U8 reg, U8 data)
{
	//sbi(MCUCR, SRW);			// enable RAM waitstate
	*((volatile unsigned char*) ATA_REG_BASE + reg) = data;
	//cbi(MCUCR, SRW);			// disable RAM waitstate
}

 
void ataShowRegisters(unsigned char DriveNo) 
{ 
	ataWriteByte(ATA_REG_HDDEVSEL, 0xA0 + (DriveNo ? 0x10:0x00)); // Select drive
	
	Uart_Printf("R0: DATALOW  = 0x%u\n",ataReadByte(ATA_REG_DATA));
	Uart_Printf("R1: ERROR    = 0x%u\n",ataReadByte(ATA_REG_ERROR));
	Uart_Printf("R2: SECT CNT = 0x%u\n",ataReadByte(ATA_REG_SECCOUNT));
	Uart_Printf("R3: SECT NUM = 0x%u\n",ataReadByte(ATA_REG_STARTSEC));
	Uart_Printf("R4: CYL LOW  = 0x%u\n",ataReadByte(ATA_REG_CYLLO));
	Uart_Printf("R5: CYL HIGH = 0x%u\n",ataReadByte(ATA_REG_CYLHI));
	Uart_Printf("R6: HEAD/DEV = 0x%u\n",ataReadByte(ATA_REG_HDDEVSEL));
	Uart_Printf("R7: CMD/STA  = 0x%u\n",ataReadByte(ATA_REG_CMDSTATUS1));
} 

unsigned char ataSWReset(void)
{
	ataWriteByte(ATA_REG_HDDEVSEL, 0x06);	// SRST and nIEN bits
	Delay(10);	// 10uS delay
	ataWriteByte(ATA_REG_HDDEVSEL, 0x02);	// nIEN bits
	Delay(10);	// 10 uS delay
   
   while( (ataReadByte(ATA_REG_CMDSTATUS1) & 0xC0) != 0x40 ); // Wait for DRDY and not BSY
    
  	return ataReadByte(ATA_REG_CMDSTATUS1) + ataReadByte(ATA_REG_ERROR);
}

/*
unsigned char ATA_Idle(unsigned char Drive)
{

  WriteBYTE(CMD, 6, 0xA0 + (Drive ? 0x10:0x00)); // Select drive
  WriteBYTE(CMD,7, 0xE1);

  while ((ReadBYTE(CMD,7) & 0xC0)!=0x40); // Wait for DRDY & NOT BUSY 

  // Return the error register...
  return ReadBYTE(CMD, 1);
}
*/

⌨️ 快捷键说明

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