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

📄 ata.c

📁 avr之IDE驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
	ataStatusWait(ATA_SR_DRQ, 0);

	// read data from drive
	ataReadDataBuffer(Buffer, 512*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, 512*numsectors);
	
	// Wait for drive to finish write
	temp = ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);

	// check for errors
	if (temp & ATA_SR_ERR)
	{
		rprintfProgStrM("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
	rprintfProgStrM("ATA LBA read ");
	rprintfu32(lba); rprintfProgStrM(" ");
	rprintfu16(numsectors); rprintfProgStrM(" ");
	rprintfu16((unsigned int)Buffer); 
	rprintfCRLF();
#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
	rprintfProgStrM("ATA LBA write ");
	rprintfu32(lba); rprintfProgStrM(" ");
	rprintfu16(numsectors); rprintfProgStrM(" ");
	rprintfu16((unsigned int)Buffer); 
	rprintfCRLF();
#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
			rprintfProgStrM("ATA LBA for CHS read: ");
			rprintfProgStrM("LBA="); rprintfu32(lba); rprintfProgStrM(" ");
		#endif

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

		#ifdef DEBUG_ATA
			rprintfProgStrM("C:H:S=");
			rprintfu16(cyl); rprintfProgStrM(":");
			rprintfu08(head); rprintfProgStrM(":");
			rprintfu08(sect); rprintfCRLF();
		#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
			rprintfProgStrM("ATA LBA for CHS write: ");
			rprintfProgStrM("LBA="); rprintfu32(lba); rprintfProgStrM(" ");
		#endif

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

		#ifdef DEBUG_ATA
			rprintfProgStrM("C:H:S=");
			rprintfu16(cyl); rprintfProgStrM(":");
			rprintfu08(head); rprintfProgStrM(":");
			rprintfu08(sect); rprintfCRLF();
		#endif

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

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

void ataDriveSelect(u08 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);
}

*/

u08 ataReadByte(u08 reg)
{
	register u08 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(u08 reg, u08 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
	
	rprintfProgStrM("R0: DATALOW  = 0x");	rprintfu08(ataReadByte(ATA_REG_DATAL	));		rprintfProgStrM(" \r\n");
	rprintfProgStrM("R1: ERROR    = 0x");	rprintfu08(ataReadByte(ATA_REG_ERROR	));		rprintfProgStrM(" \r\n");
	rprintfProgStrM("R2: SECT CNT = 0x");	rprintfu08(ataReadByte(ATA_REG_SECCOUNT));		rprintfProgStrM(" \r\n");
	rprintfProgStrM("R3: SECT NUM = 0x");	rprintfu08(ataReadByte(ATA_REG_STARTSEC));		rprintfProgStrM(" \r\n");
	rprintfProgStrM("R4: CYL LOW  = 0x");	rprintfu08(ataReadByte(ATA_REG_CYLLO	));		rprintfProgStrM(" \r\n");
	rprintfProgStrM("R5: CYL HIGH = 0x");	rprintfu08(ataReadByte(ATA_REG_CYLHI	));		rprintfProgStrM(" \r\n");
	rprintfProgStrM("R6: HEAD/DEV = 0x");	rprintfu08(ataReadByte(ATA_REG_HDDEVSEL));		rprintfProgStrM(" \r\n");
	rprintfProgStrM("R7: CMD/STA  = 0x");	rprintfu08(ataReadByte(ATA_REG_CMDSTATUS1));	rprintfProgStrM("\r\n");
} 

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 + -