?? ide.h
字號:
#ifndef __IDE_H
#define __IDE_H
typedef unsigned char uint8; /* 無符號8位整型變量 */
typedef signed char int8; /* 有符號8位整型變量 */
typedef unsigned short uint16; /* 無符號16位整型變量 */
typedef signed short int16; /* 有符號16位整型變量 */
typedef unsigned int uint32; /* 無符號32位整型變量 */
typedef signed int int32; /* 有符號32位整型變量 */
#define NULL 0
/***************************************************************************************/
/** 驅動參數配置 */
/***************************************************************************************/
#define MAX_DEVICE 2 /*定義設備數,由于只有主和從設備,所以最大值為2*/
#define TIMEOUTSET 0x0005ffff /*超時響應設備(與CPU的速度有關,沒有準確的時間)*/
/****************************************************************************************/
/** 以下為本驅動定義部份,用戶不需改動 */
/****************************************************************************************/
/*ATA 指令代碼*/
#define IDE_CMD_ATAPI_RESET 0x08
#define IDE_CMD_RECALIBRATE 0x10
#define IDE_CMD_READ 0x20 // 讀扇區命令
#define IDE_CMD_WRITE 0x30 // 寫扇區命令
#define IDE_CMD_VERIFY 0x40
#define IDE_CMD_SEEK 0x70
#define IDE_CMD_EXEC_DIAGNOSE 0x90
#define IDE_CMD_SET_DRIVE_PARAMETERS 0x91
#define IDE_CMD_READ_MULTIPLE 0xC4 // 多扇區讀
#define IDE_CMD_WRITE_MULTIPLE 0xC5 // 多扇區寫
#define IDE_CMD_SET_MULTIPLE 0xC6
#define IDE_CMD_READ_DMA 0xC8
#define IDE_CMD_WRITE_DMA 0xCA
#define IDE_CMD_GET_MEDIA_STATUS 0xDA
#define IDE_CMD_STANDBY_IMMEDIATE 0xE0 // Standby Immediate
#define IDE_CMD_IDLE_IMMEDIATE 0xE1 // Idle Immediate
#define IDE_CMD_SETFEATURES 0xEF
#define IDE_CMD_IDENTIFY 0xEC
#define IDE_CMD_MEDIA_EJECT 0xED
/*狀態寄存器相關位*/
#define ATA_STAT_REG_ERR 0x01 /*設備發生了錯誤*/
#define ATA_STAT_REG_IDX 0x02 /**/
#define ATA_STAT_REG_CORR 0x04 /**/
#define ATA_STAT_REG_DRQ 0x08 /*有數據傳輸請求*/
#define ATA_STAT_REG_DSC 0x10 /**/
#define ATA_STAT_REG_DF 0x20 /**/
#define ATA_STAT_REG_DRDY 0x40 /*設備準備好*/
#define ATA_STAT_REG_BSY 0x80 /*設備忙*/
#define ATA_STAT_BSY_DRDY (ATA_STAT_REG_BSY+ATA_STAT_REG_DRDY)
#define ATA_STAT_BSY_DRQ (ATA_STAT_REG_BSY+ATA_STAT_REG_DRQ)
/*設備磁頭寄存器*/
#define ATA_DevReg_DEV0 0x00 /*設備為主盤*/
#define ATA_DevReg_DEV1 0x10 /*設備為從盤*/
#define ATA_DevReg_b5 0x20 /*保留位,為1*/
#define ATA_DevReg_LBA 0x40 /*模式選擇位*/
#define ATA_DevReg_b7 0x80 /*保留位,為1*/
/*控制寄存器*/
#define CF_SOFT_RESET 0x04 /*軟件復位有效*/
#define CF_DISABLE_INT 0x00 /*禁能中斷*/
#define CF_ENABLE_INT 0x02 /*使能中斷*/
/*錯誤代碼由16位二進制數組成,低8位與ATA錯誤寄存器一致,高8位自定義*/
#define ATA_EER_REG_AMNF 0x01 /*一般錯誤*/
#define ATA_EER_REG_ABRT 0x04 /*指令無效出錯*/
#define ATA_EER_REG_IDNF 0x10 /*尋扇區地址出錯*/
#define ATA_EER_REG_UNC 0x40 /*發生了不可糾正的錯誤*/
#define ATA_EER_REG_BBK 0x80 /*發現錯誤塊*/
/*自定義*/
#define CARD_IS_FALSE 0x00ff /*CF卡不存在或無效*/
#define CARD_IS_TRUE 0x0000 /*CF卡有效*/
#define CARD_TIMEOUT 0x0100 /*對CF卡操作超時出錯*/
#define THIS_DEVICE ((ATA_DevReg&0x10)>>4) /*當前操作的設備,ATA_SelectDevice函數選擇了主或從設備,通過該操作可以確定剛才選定的設備*/
/*設備識別信息*/
/*全部設備信息*/
typedef struct _IDENTIFY_DATA {
uint16 GeneralConfiguration; // 00 普通設置-CF卡標志0x848A
uint16 NumberOfCylinders; // 1 默認柱面數
uint16 Reserved1; // 2
uint16 NumberOfHeads; // 3 默認磁頭數
uint16 UnformattedBytesPerTrack; // 4 在2.0規范已不使用
uint16 UnformattedBytesPerSector; // 5 在2.0規范已不使用
uint16 SectorsPerTrack; // 6 每磁道默認扇區數
uint32 SectorsPerCard; // 7-8 每卡扇區數(Word 7=MSW,Word 8=LSW)
uint16 VendorUnique; // 9 在2.0規范已不使用
uint16 SerialNumber[10]; // 10-19 ASCII序列號(靠右填寫)
uint16 BufferType; // 20 在2.0規范已不使用
uint16 BufferSectorSize; // 21 在2.0規范已不使用
uint16 NumberOfEccBytes; // 22 讀/寫長指令時的ECC字節數
uint16 FirmwareRevision[4]; // 23-26 ASCII固件版本。大端格式
uint16 ModelNumber[20]; // 27-46 ASCII模式號(靠左填寫),大端格式
uint16 MaximumBlockTransfer; //*47 多重 讀/寫指令最大扇區號。
uint16 Reserved2; // 48
uint16 Capabilities; // 49
uint16 Reserved3; // 50
uint16 PioCycleTimingMode; //*51 PIO數據傳輸周期時序模式
uint16 DmaCycleTimingMode; // 52 在2.0規范已不使用
uint16 TranslationFieldsValid; // 53 0位為1,Word54 to 58 有效;1位為1,Word64 to 70 有效
uint16 NumberOfCurrentCylinders; // 54 當前柱面數
uint16 NumberOfCurrentHeads; // 55 當前磁頭數
uint16 CurrentSectorsPerTrack; // 56 當前每磁道扇區數
uint32 CurrentSectorCapacity; // 57-58 當前扇區容量(LBAs)(Word 58=MSW,Word 57=LSW)
uint16 CurrentMultiSectorSetting; //*59 位8為1,則位0-7,指出可多扇區讀寫的扇區數
uint32 UserAddressableSectors; // 60-61 LAB模式下可被尋址的扇區數
uint16 Reserved4[2] ; // 62-63
uint16 AdvancedPIOModes ; // 64 在2.0規范使用
uint16 Reserved6[2] ; // 65-66
uint16 MinimumPIOCycleTime; // 67 非流控制下最小PIO傳輸周期時間
uint16 MinimumPIOCycleTimeIORDY; // 68 IORDY流控制小最小PIO傳輸周期時間
uint16 Reserved7[10]; // 69-79
uint32 AtaVersion; // 80-81 CF卡返回0
uint16 FeaturesCommandSupported[3]; // 82-84 特征/命令集技持
uint16 FeaturesCommandEnabled[3]; // 85-87 特征/命令集使能
uint16 Reserved8; // 88
uint16 MajorRevision; // 89 安全單位完全擦除的時間要求
uint16 MinorRevision; // 90 增強型安全單位完全擦除的時間要求
uint16 PowerManagementValue; // 91 當前高級電源管理值 // 91
uint16 Reserved9[36]; // 92-127
uint16 SecurityStatus; // 128 安全狀態
uint16 VendorUniqueBytes[32]; // 129-159
uint16 PowerRequirementDescription; // 160 電源要求描述
uint16 Reserved10; // 161
uint16 KeyManagementSupported; // 162 按鍵管理表
uint16 Reserved11[83]; // 163-255
} CF_IDENTIFY_DATA, *PCF_IDENTIFY_DATA;
/*設備基本信息*/
typedef struct _ATA_IDENTIFY_DATA {
uint16 NumberOfCylinders; // 1 默認柱面數
uint16 NumberOfHeads; // 3 默認磁頭數
uint16 SectorsPerTrack; // 6 每磁道默認扇區數
uint32 CurrentSectorCapacity; // 57-58 當前扇區容量(LBAs)(Word 58=MSW,Word 57=LSW)
} ATA_IDENTIFY_DATA, *PATA_IDENTIFY_DATA;
/*設備信息結構*/
typedef struct {
uint8 ATAIsOK; /*設備正常為1,無效或有錯為0*/
uint16 ERRCODE; /*設備出錯代碼*/
uint32 ERRSECTOR; /*讀寫扇區是出錯的扇區號*/
ATA_IDENTIFY_DATA INFODATA; /*設備的基本信息*/
}ATADEVICE,*PATADEVICE;
extern ATADEVICE ATA[MAX_DEVICE]; /*設備識別信息識別結構*/
/******************************************************************************************/
/*相關函數原型*/
/******************************************************************************************/
//****************************************************************************************
/*用戶使用函數*/
void ATA_INI(void); /*總線設置與初始化,ATA設備初始化*/
void ATA_FindIDEDevice(void); /*硬件復位,查找IDE設備,并初始化*/
uint8 ATA_IdleImmediate(uint8 Device); /*立即進入空閑狀態(硬盤轉動)*/
uint8 ATA_StandbyImmediate(uint8 Device); /*立即進入待機狀態(硬盤停轉)*/
uint8 ATA_ReadSector(uint8 Device,uint16 *Buffer,uint32 Sector,uint8 count); /*讀出扇區數據函數*/
uint8 ATA_WriteSector(uint8 Device,uint16 * Buffer, uint32 Sector ,uint8 count); /*寫入設備扇區數據函數*/
uint8 ATA_IdentifyDrive(uint8 Device,void *PTR); /*獲取設備信息*/
//****************************************************************************************
/*驅動內部使用函數*/
uint8 ATA_IssueIDEIdentify(uint8 Device); /*設備識別,獲取設備基本信息*/
uint8 ATA_SetFeature(uint8 Device); /*設備總線特性設置*/
void ATA_WriteSetting(uint32 Sectors , uint8 Count); /*寫IDE多數扇區數據*/
void ATA_ReadSetting(uint32 Sectors , uint8 Count); /*寫IDE多數扇區數據*/
void ATA_WriteData512B(uint16 * Buffer); /*連續寫512字節數據*/
void ATA_ReadData512B(uint16 * Buffer); /*連續讀512字節數據*/
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -