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

📄 ata_if.h~

📁 avr MP3 的源程序,包含文件系统,适合初学者
💻 H~
字号:
/*---------------------------------------------------------------
FileName     : ata_if.h
Created by   : ZhengYanbo 
Crtated date : 2006.3.31
Version      : v1.0
Last Modified:
Comments     : ata low-level drive
---------------------------------------------------------------*/

#ifndef __ATA_IF_H__
#define __ATA_IF_H__

#include "type.h"

#define TRUE                    1 
#define FALSE                   0 
#define CTRL                    0
#define CMD                     1
#define DRIVE0                  0

//define ata control pins
#define ATA_IORD_OUT()          DDRD.6=1;
#define ATA_IOWR_OUT()          DDRD.7=1;    
#define nIORD                   PORTD.6 //PD6
#define nIOWR                   PORTD.7 //PD7
//address latch(74HC573)
#define LATCH                   PORTB.3
#define LATCH_OUT()             DDRB.3=1;
#define CLR_LATCH()             LATCH=0;
#define SET_LATCH()             LATCH=1;

//define ata data pins
#define ATA_DATA_PORT_L         PORTA
#define ATA_DATA_PORT_H         PORTC
//read pinx, not port!!!
#define ATA_DATA_PIN_L          PINA
#define ATA_DATA_PIN_H          PINC
//define ata data direction 
#define ATA_DATA_DDDR_L         DDRA
#define ATA_DATA_DDDR_H         DDRC 
//define ata status
#define ATA_DATA_PORT_L_IN()    ATA_DATA_DDDR_L=0x00;
#define ATA_DATA_PORT_H_IN()    ATA_DATA_DDDR_H=0x00;
#define ATA_DATA_PORT_L_OUT()   ATA_DATA_DDDR_L=0xFF;
#define ATA_DATA_PORT_H_OUT()   ATA_DATA_DDDR_H=0xFF;       

#define STANDBY	        0
#define SLEEP	        1
#define IDLE	        2           

//ATA Register address, this is : /CS1 	/CS0 	A2 	A1 	A0 (5 bits, treated as byte)
/*
#define	IOReg		0x10		//	1	0	0	0	0  (used for 16 bit data transfer)
#define ErrorReg    0x11		//	1	0	0	0	1  (Has the error info)
#define SecCountReg	0x12		//	1	0	0	1	0  (# of sector to transfer, always 1 for our case)
#define	SecReg		0x13		//	1	0	0	1	1  (Sector that has to be read)
#define LoCylReg	0x14		//	1	0	1	0	0  (Holds the low byte of cylinder #)
#define HiCylReg	0x15		//	1	0	1	0	1  (Only bit 1 and 0 is significat. 
										  				//Cylinder # is 10 bits long)
#define	DrvHeadReg	0x16		//	1	0	1	1	0  (Holds head and drive to be read from)										  
#define CmdStsReg	0x17		//	1	0	1	1	1  (Command and Status Register)
#define Interrupt	0x0E		//	0	1	1	1	0
//#define CF_FeatureReg   0x11    //  1   0   0   0   1  (Write only)
*/

// ATA Registers Address
#define IOReg               (*(volatile unsigned char *)(0x1000))
#define ErrorReg            (*(volatile unsigned char *)(0x1100))
#define SecCountReg         (*(volatile unsigned char *)(0x1200))
#define SecReg              (*(volatile unsigned char *)(0x1300))
#define LoCylReg            (*(volatile unsigned char *)(0x1400))
#define HiCylReg            (*(volatile unsigned char *)(0x1500))

#define DrvHeadReg          (*(volatile unsigned char *)(0x1600))
#define CmdStsReg           (*(volatile unsigned char *)(0x1700))
#define Interrupt           (*(volatile unsigned char *)(0x0E00))
#define CF_FeatureReg       (*(volatile unsigned char *)(0x1100))

// define function
#define RegWrite(reg,data)  reg = data;
#define RegRead(reg)        reg 

// ATA status register bits

#define SR_BSY		0x80
#define SR_DRDY		0x40
#define SR_DF		0x20
#define SR_DSC		0x10
#define SR_DRQ		0x08
#define SR_CORR		0x04
#define SR_IDX		0x02
#define SR_ERR		0x01

// ATA error register bits

#define ER_UNC		0x40
#define ER_MC		0x20
#define ER_IDNF		0x10
#define ER_MCR		0x08
#define ER_ABRT		0x04
#define ER_TK0NF	0x02
#define ER_AMNF		0x01
#define Drive		0
#define LBAMaster	0xE0
#define LBASlave	0xF0

//CF ATA commamd

#define CF_CMD_DIAGNOSTIC		0x90	//诊断
#define CF_CMD_IDENTITY			0xec	//读CF卡信息
#define CF_CMD_READSECTOR		0x20	//读扇区
#define CF_CMD_SETFEATURE		0xef	//设置特性
#define CF_CMD_SLEEP			0x99	//进入Sleep模式(省电)
#define	CF_CMD_STANDBY			0x96	//进入Standby模式
#define CF_CMD_WRITESECTOR		0x30	//写扇区

#define CF_LBA_MASTER			0xe0
#define CF_LBA_SLAVE			0xf0

//---------------------------------------------------------------
// prototypes
//---------------------------------------------------------------
void ATA_Port_Init(void);
void ATA_Init(void);
byte CF_Initialize(void) ;
void ATA_Device_Indentify(void);
byte ATA_start_read_n_sectors(unsigned long lba, unsigned char nSectors);
byte ATA_start_write_n_sectors(unsigned long lba, unsigned char nSectors);
void ATA_Read(word bytesToRead, byte *buffer); 
void ATA_Write(word bytesToWrite, byte *buffer);
byte ATA_Read_Sector(dword lba, byte *buffer);
byte ATA_Write_Sector(unsigned long lba, unsigned char *buffer);
//void ATA_Read_Sector_Bytes(dword lba,word offset,word len,byte *buffer);
void ATA_Read_Sector_Bytes(dword lba, word len, byte *buffer);

void WaitTillNotBusy(void);
byte ATA_get_status(void);


#endif

⌨️ 快捷键说明

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