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

📄 ide_access.c

📁 嵌入式系统中的完整FAT16和FAT32源码
💻 C
字号:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//					    FAT32 File IO Library for AVR 
//								  V0.1c
// 	  							Rob Riglar
//							Copyright 2003,2004 
//
//   					  Email: rob@robriglar.com
//
//			    Compiled with Imagecraft C Compiler for the AVR series
//-----------------------------------------------------------------------------
//
// This file is part of FAT32 File IO Library.
//
// FAT32 File IO Library is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// FAT32 File IO Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FAT32 File IO Library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// IDE_SectorByte: Function is used to retrieve a byte from the 'currentsector'
// array. The value passed in must be between 0 - 511.
//
// Parameters: Word location within currentsector
//
// Returns: Byte of data requested from currentsector
//-----------------------------------------------------------------------------
byte IDE_SectorByte(word sublocation)
{
// NOTE: This function is to be used whereever access to currentsector is required
//  	 by all layers that are higher than the IDE Base functions.
	byte data=0;          
    data=IDE_Internal.currentsector[sublocation];	  // Read Data from specified pos
	return (data);
}

//-----------------------------------------------------------------------------
// IDE_SectorWord: Function is used to retrieve a word from the 'currentsector'
// array. The value passed in must be between 0 - 510.
//
// Parameters: Word location within currentsector
//
// Returns: Word of data requested from currentsector
//-----------------------------------------------------------------------------
word IDE_SectorWord(word sublocation) // Return Word at position specified
{
	 word data=0; 
	 word tempword=0;
	   
	 tempword = IDE_SectorByte(sublocation+1); // Get MSB from Buffer
	 tempword<<=8;							 // Make MSB word half into byte
	 data = IDE_SectorByte(sublocation) + tempword; // Combine LSB and MSB

	 return (data);					  			 // Return value
}

//-----------------------------------------------------------------------------
// IDE_SectorUI32: Function is used to retrieve a 32 bit value from the 
// 'currentsector' array. The value passed in must be between 0 - 511.
//
// Parameters: Word offset within current sector
//
// Returns: 32 bit unsigned data as requested
//
// Functions Used: None
//
//-----------------------------------------------------------------------------
UI32 IDE_SectorUI32(word sublocation) // Return UI32 at position specified
{
    UI32 data=0; 
	UI32 A,B,C,D;
	
	A= IDE_SectorByte(sublocation);		// Read the four bytes which make up
	B= IDE_SectorByte(sublocation+1);    // the 32-bit value
	C= IDE_SectorByte(sublocation+2);
	D= IDE_SectorByte(sublocation+3);         

    data=(D<<=24) + (C<<=16) + (B<<=8) + A; //Combine into correct order
	return (data);					  			 // Return value
}

//-----------------------------------------------------------------------------
// IDE_BufferSector: This function recieves the LBA address of a sector and reads into
// an array of 512 bytes (1 sector) called 'currentsector'.
//
// Parameters: 32bit Unsigned Logical Block Address
//
// Returns: An integer error code: 1 means read successfull, 0 means read fail.
//
// Functions Used: WriteReg, CheckforError, ReadErrors, Wait_DRQ, Set_Address, 
// 			 	   DataInputSetup, printf
//
//-----------------------------------------------------------------------------
int IDE_BufferSector(UI32 LBALocation)
{
    UI16    i;
	
	if (LBALocation>=IDE_Internal.maxLBA) 
	   {
	   printf("\r\nIDE_BufferSector: Out of Range Disc Read of 0x%lx", LBALocation);
	   // Return failure
	   return 0;
	   }
	
	if (IDE_Internal.SectorCurrentlyLoaded!=LBALocation) // Dont reload if already loaded
	{              
	IDE_Internal.SectorCurrentlyLoaded=LBALocation; // update current sector loaded
	
    WriteReg(HEAD_DEVICE_PIO, ((LBALocation >> 24) & 0xFF) | 0xE0);
    WriteReg(CYLINDERHIGH, (LBALocation >> 16) & 0xFF);
    WriteReg(CYLINDERLOW, (LBALocation >> 8) & 0xFF);
    WriteReg(START_SECTOR, LBALocation & 0xFF);
    WriteReg(SECTOR_COUNT, 1); 		   	 	   // 1 Sector to be read only
    WriteReg(COMMAND, READ);    			   // Read sector
	
	if (CheckforError()) ReadErrors(); 		   // Were there any Errors ?			   
	
	Wait_DRQ();			 // Wait till Data is available in IDE buffer
						 // Reading Status Register reset INT too

    Set_Address(DATA);       			   	   // Set Address to DATA register
	DataInputSetup();		 				   // Set Data lines to input

    for(i=0; i <= 511; i=i+2)
    {
        CLR_IDE_RD;	  		 				   // RD assert for read cycle
        IDE_Internal.currentsector[i]=DATALOW_INPORT;	   // Read Data low byte
        IDE_Internal.currentsector[i+1]=DATAHIGH_INPORT;	   // Read Data High Byte
        SET_IDE_RD;							   // Deassert RD
    }
	
	if (CheckforError()) ReadErrors(); // Were there any Errors ?
	}
	
	// Return Success
	return 1;
}

//-----------------------------------------------------------------------------
// IDE_PowerOn: Function set port directions, default values of output pins
// and performs a Hard Reset
//-----------------------------------------------------------------------------
void IDE_Reset(void)
{
 	  // Address Pins set to output A0 - A3, CS1, CS0 RD, WR, RST
	  // Address 0xff including deasserting all control signals
   	  Set_Address(ATA_IO_HIZ);
	  delay_ms(10);						// Delay 
  	  CLR_IDE_RST;						// Bring RST low to reset drive
	  delay_ms(10);
	  SET_IDE_RST;						// Deassert RST for normal operation
	  
	  // Set to defualt value
	  IDE_Internal.SectorCurrentlyLoaded = 0xffffffff;
}

//-----------------------------------------------------------------------------
// InitDrive: Drive Initialisation routine, required before you use the drive 
// for reading or writting to the actual media. 
//-----------------------------------------------------------------------------
void IDE_InitDrive(void)
{
 	 WriteReg(SECTOR_COUNT, 0x3F);
	 WriteReg(HEAD_DEVICE_PIO, 0xAF);
	 WriteReg(COMMAND, 0x91);  		 		// This command is essential
	 // Select LBA, Master Drive and Head 0
	 WriteReg(HEAD_DEVICE_PIO, 0xE0); 

	 // Read Max LBA Read address
	 IDE_Internal.maxLBA = Fetch_ID_Max_LBA(1);
}

⌨️ 快捷键说明

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