Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

ata.h

Go to the documentation of this file.
00001 /*! \file ata.h \brief IDE-ATA hard disk interface driver. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'ata.h'
00005 // Title        : IDE-ATA interface driver for hard disks
00006 // Author       : Pascal Stang
00007 // Date         : 11/22/2000
00008 // Revised      : 12/29/2000
00009 // Version      : 0.3
00010 // Target MCU   : ATmega103 (should work for Atmel AVR Series)
00011 // Editor Tabs  : 4
00012 //
00013 /// \ingroup driver_hw
00014 /// \defgroup ata IDE/ATA Interface Driver (ata.c)
00015 /// \code #include "ata.h" \endcode
00016 /// \par Overview
00017 ///     This library provides an interface from AVR processors to IDE/ATA
00018 ///     devices.  Such devices can include hard disks, CF memory cards, and
00019 ///     PCMCIA disks and memory devices.  The library supports automatic drive
00020 ///     identification and sector-level reading and writing.  Some minimal
00021 ///     address decoding hardware is required to use this interface.  For an
00022 ///     example of interface hardware, see the Procyon MP3 Player docs here:
00023 ///     http://www.procyonengineering.com/embedded/procyonmp3/index.html.
00024 ///     Future revisions if this library may include a direct hardware-less
00025 ///     interface option.
00026 ///
00027 /// \note This code is quite old and in some level of disrepair.  Nonetheless,
00028 ///     it works quite well.
00029 //
00030 // This code is distributed under the GNU Public License
00031 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00032 //
00033 //*****************************************************************************
00034 //@{
00035 
00036 #ifndef ATA_H
00037 #define ATA_H
00038 
00039 #include "global.h"
00040 #include "ataconf.h"
00041 
00042 // constants
00043 #define DRIVE0      0
00044 
00045 #define STANDBY     0
00046 #define SLEEP       1
00047 #define IDLE        2
00048 
00049 // ATA status register bits
00050 #define ATA_SR_BSY      0x80
00051 #define ATA_SR_DRDY     0x40
00052 #define ATA_SR_DF       0x20
00053 #define ATA_SR_DSC      0x10
00054 #define ATA_SR_DRQ      0x08
00055 #define ATA_SR_CORR     0x04
00056 #define ATA_SR_IDX      0x02
00057 #define ATA_SR_ERR      0x01
00058 
00059 // ATA error register bits
00060 #define ATA_ER_UNC      0x40
00061 #define ATA_ER_MC       0x20
00062 #define ATA_ER_IDNF     0x10
00063 #define ATA_ER_MCR      0x08
00064 #define ATA_ER_ABRT     0x04
00065 #define ATA_ER_TK0NF    0x02
00066 #define ATA_ER_AMNF     0x01
00067 
00068 // ATA head register bits
00069 #define ATA_HEAD_USE_LBA    0x40
00070 /*
00071 // ATA registers
00072 #define ATA_REG_BASE        0x8000
00073 #define ATA_REG_DATAL       0x00
00074 #define ATA_REG_ERROR       0x01
00075 #define ATA_REG_SECCOUNT    0x02
00076 #define ATA_REG_STARTSEC    0x03
00077 #define ATA_REG_CYLLO       0x04
00078 #define ATA_REG_CYLHI       0x05
00079 #define ATA_REG_HDDEVSEL    0x06
00080 #define ATA_REG_CMDSTATUS1  0x07
00081 #define ATA_REG_CMDSTATUS2  0x08
00082 #define ATA_REG_ACTSTATUS   0x09
00083 
00084 #define ATA_REG_DATAH       0x10
00085 */
00086 // ATA commands
00087 #define ATA_CMD_READ            0x20
00088 #define ATA_CMD_READNR          0x21
00089 #define ATA_CMD_WRITE           0x30
00090 #define ATA_CMD_WRITENR         0x31
00091 #define ATA_CMD_IDENTIFY        0xEC
00092 #define ATA_CMD_RECALIBRATE     0x10
00093 #define ATA_CMD_SPINDOWN        0xE0    // spin down disk immediately
00094 #define ATA_CMD_SPINUP          0xE1    // spin up disk immediately
00095 #define ATA_CMD_STANDBY_5SU     0xE2    // spin down disk and set auto-power-down timer (sectorcount*5sec)
00096 #define ATA_CMD_IDLE_5SU        0xE3    // keep disk spinning and set auto-power-down timer (sectorcount*5sec)
00097 #define ATA_CMD_SLEEP           0xE6    // sleep disk (wakeup only on HW or SW reset)
00098 #define ATA_CMD_STANDBY_01SU    0xF2    // spin down disk and set auto-power-down timer (sectorcount*0.1sec)
00099 #define ATA_CMD_IDLE_01SU       0xF3    // keep disk spinning and set auto-power-down timer (sectorcount*0.1sec)
00100 
00101 
00102 // ATA CHS disk parameters (examples, now we autodetect)
00103 #define ATA_DISKPARM_CLYS       0x03A6  // number of cylinders per platter
00104 #define ATA_DISKPARM_HEADS      0x10    // number of heads (usable plater sides)
00105 #define ATA_DISKPARM_SECTORS    0x11    // number of sectors per head per cylinder
00106 
00107 // ATA Identity fields
00108 // all offsets refer to word offset (2 byte increments)
00109 #define ATA_IDENT_DEVICETYPE    0       // specifies ATA/ATAPI, removable/non-removable
00110 #define ATA_IDENT_CYLINDERS     1       // number of logical cylinders
00111 #define ATA_IDENT_HEADS         3       // number of logical heads
00112 #define ATA_IDENT_SECTORS       6       // number of sectors per track
00113 #define ATA_IDENT_SERIAL        10      // drive model name (20 characters)
00114 #define ATA_IDENT_MODEL         27      // drive model name (40 characters)
00115 #define ATA_IDENT_FIELDVALID    53      // indicates field validity of higher words (bit0: words54-58, bit1: words 64-70)
00116 #define ATA_IDENT_LBASECTORS    60      // number of sectors in LBA translation mode
00117 
00118 // drive mode defines (for ataSetDrivePowerMode() )
00119 #define ATA_DISKMODE_SPINDOWN   0
00120 #define ATA_DISKMODE_SPINUP     1
00121 #define ATA_DISKMODE_SETTIMEOUT 2
00122 #define ATA_DISKMODE_SLEEP      3
00123 
00124 // typedefs
00125 // drive info structure
00126 typedef struct 
00127 {
00128     unsigned int  cylinders;
00129     unsigned char heads;
00130     unsigned char sectors;
00131     unsigned long sizeinsectors;
00132     unsigned char LBAsupport;
00133     char model[41];
00134 } typeDriveInfo;
00135 
00136 
00137 // Prototypes
00138 void ataInit(void);
00139 void ataDriveInit(void);
00140 void ataDriveSelect(u08 DriveNo);
00141 void ataSetDrivePowerMode(u08 DriveNo, u08 mode, u08 timeout);
00142 u08  ataReadByte(u08 reg);
00143 void ataWriteByte(u08 reg, u08 data);
00144 void ataShowRegisters(unsigned char DriveNo);
00145 u08  ataSWReset(void);
00146 void ataDiskErr(void);
00147 void ataPrintSector( u08 *Buffer);
00148 void ataReadDataBuffer(u08 *Buffer, u16 numBytes);
00149 void ataWriteDataBuffer(u08 *Buffer, u16 numBytes);
00150 u08 ataStatusWait(u08 mask, u08 waitStatus);
00151 
00152 // read and write routines for CHS based drives
00153 unsigned char ataReadSectorsCHS(    unsigned char Drive, 
00154                                     unsigned char Head, 
00155                                     unsigned int Track,
00156                                     unsigned char Sector,
00157                                     unsigned int numsectors,
00158                                     unsigned char *Buffer);
00159 
00160 unsigned char ataWriteSectorsCHS(   unsigned char Drive, 
00161                                     unsigned char Head, 
00162                                     unsigned int Track,
00163                                     unsigned char Sector,
00164                                     unsigned int numsectors,
00165                                     unsigned char *Buffer);
00166 
00167 // read and write routines for LBA based drives
00168 unsigned char ataReadSectorsLBA(    unsigned char Drive, 
00169                                     unsigned long lba,
00170                                     unsigned int numsectors,
00171                                     unsigned char *Buffer);
00172 
00173 unsigned char ataWriteSectorsLBA(   unsigned char Drive, 
00174                                     unsigned long lba,
00175                                     unsigned int numsectors,
00176                                     unsigned char *Buffer);
00177 
00178 // generic read and write routines using LBA
00179 //   uses native or translated LBA addressing
00180 //   given autodetected drive type
00181 unsigned char ataReadSectors(   unsigned char Drive, 
00182                                 unsigned long lba,
00183                                 unsigned int numsectors,
00184                                 unsigned char *Buffer);
00185 
00186 unsigned char ataWriteSectors(  unsigned char Drive, 
00187                                 unsigned long lba,
00188                                 unsigned int numsectors,
00189                                 unsigned char *Buffer);
00190 
00191 //unsigned char IdentifyDrive(unsigned char DriveNo,  unsigned char *Buffer, tdefDriveInfo *DriveInfo);
00192 //unsigned char SetMode(unsigned char DriveNo, unsigned char Mode, unsigned char PwrDown);
00193 //unsigned char ATA_Idle(unsigned char Drive);
00194 
00195 #endif
00196 //@}

Generated on Sun Oct 29 03:41:06 2006 for Procyon AVRlib by  doxygen 1.4.2