📄 ide_lib.c
字号:
/*
Copyright (C) 2003 Bart Bilos <boombox666@yahoo.com>.
Adapted from original work by Paul Stoffregen.
http://www.pjrc.com/tech/8051/ide/
This program 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.
This program 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 this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "IDE_LIB.h" // IDE constants and defines
#include "timertick.h" // timing subsystem
#include <stdio.h> // standard IO
// 8255 to IDE to Xdata mapping registers
xdata char ide_8255_lsb _at_ 0xE000;
xdata char ide_8255_msb _at_ 0xE001;
xdata char ide_8255_ctl _at_ 0xE002;
xdata char cfg_8255 _at_ 0xE003;
#define IDE_RETRIES 4
/* spin the HDD drive up */
void spinup(void) {
char i=0,status;
do {
// spinup command
ide_wr(ide_command,ide_cmd_spinup);
// wait until ide is ready
status = ide_busy(1);
// retry
i++;
} while((i < 5 ) & (status == 0xFF));
// more than 5 retries
if(i > 5)
printf("# spinup timed out #\n");
}
/* spin the HDD drive down */
void spindown(void) {
char i=0,status;
do {
// spindown command
ide_wr(ide_command,ide_cmd_spindown);
// wait until IDE is rdy
status = ide_busy(1);
i++;
} while((i < 5 ) & (status == 0xFF));
if(i > 5)
printf("# spindown timed out #\n");
}
/* check if the IDE is busy */
char ide_busy(unsigned int a_timeout){
//wait for RDY bit to be set
unsigned int inittime = (unsigned int) getseconds();
int status;
do {
status = ide_rd(ide_status);
// if it takes too long bail out
if(((unsigned int) getseconds() - inittime) > a_timeout)
return 0xFF;
} while((status & 0x80) == 0x80);
return status;
}
// wait for IDE to be ready for data transfer
int ide_drq(unsigned int a_timeout) {
unsigned int inittime = (unsigned int) getseconds();
int status;
do {
status = ide_rd(ide_status);
// if it takes too long bail out
if(((unsigned int) getseconds() - inittime) > a_timeout)
return 0xFF;
}while(((status & 0x80) == 0x80) && ((status & 0x08) == 0x00));
return status;
}
/* reinitilise the HDD */
void ide_init (void) {
int status;
//select the master device
ide_wr(ide_head,0xA0);
do {
status = ide_rd(ide_status);
} while( ((status & 0x80) == 0x80) && ((status & 0x40) == 0x00) );
// write heads
ide_wr(ide_head,16);
// write sects per track
ide_wr(ide_sec_cnt,63);
// initialise
ide_wr(ide_command,ide_cmd_init);
// wait until finished
ide_busy(1);
// recalibrate
ide_wr(ide_command,ide_cmd_recal);
// wait until finished
ide_busy(1);
}
/* gets IDE error code */
char get_err(void){
// get error code
char errorcode = ide_rd(ide_err);
if(errorcode == 0)
return 255;
else
return errorcode;
}
/*
reads a sector in a_buffer, at adress a_lba
*/
char read_sector(unsigned long int a_lba,char * a_buffer) {
int status;
// if the return value is FF time out has occured
if(ide_busy(1) == 0xFF) {
printf("# read_sector timeout #\n");
// return error condition
return 0xFF;
}
// write adress
wr_lba(a_lba);
// read data from IDE command
ide_wr(ide_command,ide_cmd_read);
// data ready
status = ide_drq(1);
// error bit not set
if((status & 0x01) == 0x00) {
read_data(a_buffer);
return 0;
}
else {
// timed out?
if(status==0xFF){
printf("# read_sector timeout #\n");
return 0xFF;
}
else {
// nope other generic error
return get_err();
}
}
}
/*
Writes an sector to the drive
*/
char write_sector(unsigned long int a_lba,char * a_buffer) {
int status;
// wait until ready
ide_busy(1);
// write dest adress to IDE
wr_lba(a_lba);
// write data to sect
ide_wr(ide_command,ide_cmd_write);
// get IDE status
status = ide_drq(1);
// if status bit is not set all is set for go
if((status & 0x01) == 0x00){
// write data
write_data(a_buffer);
// return sucess
return 0;
}
else
// something happened
return get_err();
}
/*
write LBA adress
*/
void wr_lba(unsigned long int a_lba) {
// write 28 bits
ide_wr(ide_head,(a_lba>>24 & 0x0F) | 0xE0);
ide_wr(ide_cyl_msb,a_lba>>16);
ide_wr(ide_cyl_lsb,a_lba>>8);
ide_wr(ide_sector,a_lba);
ide_wr(ide_sec_cnt,0x01);
}
/* get drive data and put it in the buffer */
void drive_id(char * a_buffer){
// drive ready?
ide_busy(1);
// request IDE data
ide_wr(ide_command,ide_cmd_id);
// wait until data ready
ide_drq(1);
// put in the buffer
read_data(a_buffer);
}
/*
reads sector from the IDE
*/
void read_data(char * a_buffer) {
int readdata;
int i=0xFF;
do{
// read byte from IDE
readdata = ide_rd(ide_data);
// put lsb in the buffer
*a_buffer = readdata;
a_buffer++;
// put second byte in buffer
*a_buffer = (readdata>>8);
a_buffer++;
i--;
} while(i != 0);
}
/*
writes sector to the IDE
*/
void write_data(char * a_buffer) {
int readdata;
char i=0xFF;
do{
// put LSB in the int
readdata = *a_buffer;
// increment buffer pointer
a_buffer++;
// put MSB in the int
readdata = (*a_buffer << 8) | readdata;
// increment buffer pointer
a_buffer++;
// write byte to IDE
ide_wr(ide_data,readdata);
// next word
i--;
} while(i != 0);
}
/*
------------ low level IDE routines ---------------
*/
/*
Read register from IDE and return it
*/
int ide_rd(char a_reg) {
int output = 0;
// config 8255 in read mode
cfg_8255 = rd_ide_8255;
// put adress on the control lines
ide_8255_ctl = a_reg;
// pulse read line
ide_8255_ctl = a_reg | ide_rd_line;
// put MSB in upper part of int
output = ide_8255_msb;
output = output << 8;
// put LSB in lower part of int
output = output | ide_8255_lsb;
// de assert all control pins
ide_8255_ctl = 0;
// return output
return output;
}
/*
Write to a_reg and write a_input to the IDE register
*/
void ide_wr(char a_reg, int a_input){
// config 8255 in write mode
cfg_8255 = wr_ide_8255;
// write LSB to IDE
ide_8255_lsb = a_input & 0x00FF;
// write MSB to IDE
ide_8255_msb = a_input >> 8;
// put reg address in control reg
ide_8255_ctl = a_reg;
// assert write pin
ide_8255_ctl = a_reg | ide_wr_line;
// de assert all control pins
ide_8255_ctl = 0;
// put 8255 in read mode
cfg_8255 = rd_ide_8255;
}
// resets the IDE by flipping the reset line
void ide_hard_reset (void) {
int i;
// put 8255 in write mode
cfg_8255 = wr_ide_8255;
// toggle reset line
ide_8255_ctl = ide_rst_line;
// wait
for(i=0;i<255;i++);
// reset all lines
ide_8255_ctl = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -