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

📄 hdd.c

📁 at91的典型HDD代码,经典的HDD代码范例!at91rm9200也是经典的芯片!
💻 C
字号:
//*----------------------------------------------------------------------------
//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : HDD.c
//* Object              : main application written in C
//* Creation            : GGi   12/11/2002
//*
//*----------------------------------------------------------------------------
#include "hdd.h"

volatile char *HDD_BASE	= (char *)0x50c00000;
volatile short *HDD_BASE_16	= (short *)0x50c00000;

/* local procedures */

/*delay fct*/
void delay(int nb){
	while(nb--);
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_HDDGetStatus
//* \brief wait the RDY status
//*----------------------------------------------------------------------------
 char AT91F_HDDGetStatus (void){

			return ( *(HDD_BASE + ATA_STATUS_OFFSET));
		
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_HDD_StatusLow
//* \brief wait the corresponding bit of status falls
//*----------------------------------------------------------------------------
 int AT91F_HDD_StatusLow (int mask){
    struct _AT91S_SMC2 *PS_ebi = (struct _AT91S_SMC2 *) (AT91C_BASE_SMC2) ;

    char status = 0;
    int cpt = 0;
   
	/*swap to 8bits data bus width*/
	PS_ebi->SMC2_CSR[4] &= 	~(AT91C_SMC2_DBW_16|AT91C_SMC2_BAT);
	PS_ebi->SMC2_CSR[4] |= 	AT91C_SMC2_DBW_8;
   
    while (status && cpt++ < 100000){
			status = (AT91F_HDDGetStatus()) & mask;
	}

	/*back to 16*/
	PS_ebi->SMC2_CSR[4] &= 	~AT91C_SMC2_DBW_8;
	PS_ebi->SMC2_CSR[4] |= 	(AT91C_SMC2_DBW_16|AT91C_SMC2_BAT);
	
	if (cpt == 100000) return -1;
	return 0;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_HDD_StatusHigh
//* \brief wait the corresponding bit of status raise
//*----------------------------------------------------------------------------
 int AT91F_HDD_StatusHigh (int mask){
    struct _AT91S_SMC2 *PS_ebi = (struct _AT91S_SMC2 *) (AT91C_BASE_SMC2) ;

    char status = 0;
    int cpt = 0;
   
	/*swap to 8bits data bus width*/
	PS_ebi->SMC2_CSR[4] &= 	~(AT91C_SMC2_DBW_16|AT91C_SMC2_BAT);
	PS_ebi->SMC2_CSR[4] |= 	AT91C_SMC2_DBW_8;
   
    while (!status && cpt++ < 100000){
			status = (AT91F_HDDGetStatus()) & mask;
	}

	/*back to 16*/
	PS_ebi->SMC2_CSR[4] &= 	~AT91C_SMC2_DBW_8;
	PS_ebi->SMC2_CSR[4] |= 	(AT91C_SMC2_DBW_16|AT91C_SMC2_BAT);
	
	if (cpt == 100000) return -1;
	return 0;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_HDDWriteCommand
//* \brief Write a command to the HDD
//*----------------------------------------------------------------------------
int AT91F_HDDWriteCommand(char * buffer, int size, int offset){
	struct _AT91S_SMC2 *PS_ebi = (struct _AT91S_SMC2 *) (AT91C_BASE_SMC2) ;
	int i;
	
	if (AT91F_HDD_StatusLow (ATA_ST_BUSY) < 0) return -1;

	/*swap to 8bits data bus width*/
	PS_ebi->SMC2_CSR[4] &= 	~(AT91C_SMC2_DBW_16|AT91C_SMC2_BAT);
	PS_ebi->SMC2_CSR[4] |= 	AT91C_SMC2_DBW_8;
	
	for (i=0;i<size;i++){
			*(HDD_BASE + i + offset) = *buffer++;
			delay(10000);
			}

	/*back to 16*/
	PS_ebi->SMC2_CSR[4] &= 	~AT91C_SMC2_DBW_8;
	PS_ebi->SMC2_CSR[4] |= 	(AT91C_SMC2_DBW_16|AT91C_SMC2_BAT);
	
	return 0;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_HDD_Read16
//* \brief Read from HDD in 16 bitwidth databus mode
//*----------------------------------------------------------------------------
int AT91F_HDD_Read16(unsigned short *buffer,int size){
	int cpt = 0;
	if (AT91F_HDD_StatusHigh (ATA_ST_DRQ) < 0) return -1;

	while ((cpt < size) && (cpt < BUF_SIZE)){
		*buffer++ =*(HDD_BASE_16);
		cpt ++;
	}
	return cpt;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_HDD_Write16
//* \brief Write to HDD
//*----------------------------------------------------------------------------
int AT91F_HDD_Write16(unsigned short *buffer,int size){
	int cpt = 0;
	if (AT91F_HDD_StatusHigh (ATA_ST_DRQ) < 0) return -1;


	while ((cpt < size) && (cpt < BUF_SIZE)){
		*(HDD_BASE_16 ) = *buffer++;
		cpt++;
	}

	return cpt;
}

/***************************** driver interface ******************************/

//*----------------------------------------------------------------------------
//* \fn    AT91F_HDDOpen
//* \brief Opens HDD device
//*----------------------------------------------------------------------------
void AT91F_HDDOpen(void){
struct _AT91S_EBI *PS_ebi = (struct _AT91S_EBI *) (AT91C_BASE_EBI) ;
struct _AT91S_SMC2 *PS_smc2 = (struct _AT91S_SMC2 *) (AT91C_BASE_SMC2) ;

	//CS configuration
	
    PS_smc2->SMC2_CSR[4]=(HDD_IO_RWH|HDD_IO_RWS|AT91C_SMC2_ACSS_STANDARD|AT91C_SMC2_BAT|AT91C_SMC2_DBW_16|\
    						HDD_IO_TDF|AT91C_SMC2_WSEN|HDD_IO_NWS);

	//PIO configuration

	// (EBI) Chip Select 4 is assigned to the Static Memory Controller and NCS4,NCS5 and NCS6
	// behave as defined by the SMC2.
	
	AT91F_PIO_CfgPeriph(AT91C_BASE_PIOC, AT91C_PC6_NWAIT, 0);
	AT91F_PIO_CfgPeriph(AT91C_BASE_PIOC, AT91C_PC7_A23 , 0);
	AT91F_PIO_CfgPeriph(AT91C_BASE_PIOC, AT91C_PC9_A25_CFRNW, 0);
	AT91F_PIO_CfgPeriph(AT91C_BASE_PIOC, AT91C_PC10_NCS4_CFCS, 0);

	//IDE mode
	AT91F_PIO_CfgOutput(AT91C_BASE_PIOC, AT91C_PC11_NCS5_CFCE1);
	AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PC11_NCS5_CFCE1);
	AT91F_PIO_CfgOutput(AT91C_BASE_PIOC, AT91C_PC12_NCS6_CFCE2);
	AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PC12_NCS6_CFCE2);

	//resets the HDD Card
	AT91F_PIO_CfgOutput(AT91C_BASE_PIOC, AT91C_PIO_PC5);

	AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, AT91C_PIO_PC5);
	delay(100000);
	AT91F_PIO_SetOutput(AT91C_BASE_PIOC, AT91C_PIO_PC5);

	PS_ebi->EBI_CSA |= AT91C_EBI_CS4A_SMC_CompactFlash;

	delay(0x400000);
	

}

//*----------------------------------------------------------------------------
//* \fn    AT91F_HDDRead_ID
//* \brief Read the CF ID in 16bits databus width
//*----------------------------------------------------------------------------
 int AT91F_HDDRead_ID (unsigned short * buf){
 	char cmd[2] = {0,DRIVE_ID};

    if (AT91F_HDDWriteCommand(cmd,2,6) < 0) return -1;
    if (AT91F_HDD_Read16(buf,BUF_SIZE/2) != BUF_SIZE/2) return -1;
   	if (AT91F_HDD_StatusLow (ATA_ST_ERR) < 0) return (-1);

   	return 0;

}

//*----------------------------------------------------------------------------
//* \fn    AT91F_HDDErase
//* \brief erase a sector
//*----------------------------------------------------------------------------
int AT91F_HDDErase (int add){
 	char cmd[6];

 	/*CC*/cmd[5] = ERASE_SECTOR;
 	/*DR*/cmd[4] = (1 << 7) | (1 << 6) | (1 << 5) | ((add & 0xf000000) >> 24); //LBA = 1
 	/*CY*/cmd[3] = (add & 0xff0000) >> 16;
 	/*HD*/cmd[2] = (add & 0xff00) >> 8;
 	/*SN*/cmd[1] = (add & 0xff);
 	/*SC*/cmd[0] = 1;
    if(AT91F_HDDWriteCommand(cmd,6,2) < 0) return -1;
	if (AT91F_HDD_StatusLow (ATA_ST_ERR) < 0) return -1;

   	return 0;

}


//*----------------------------------------------------------------------------
//* \fn    HDDWrite
//* \brief write to the HDD in LBA mode
//*----------------------------------------------------------------------------
int AT91F_HDDWrite (unsigned short * buf , int add){
char cmd[6];
 	/*CC*/cmd[5] = WRITE_SECTOR;
 	/*DR*/cmd[4] = (1 << 7) | (1 << 6) | (1 << 5) | ((add & 0xf000000) >> 24); //LBA = 1
 	/*CY*/cmd[3] = (add & 0xff0000) >> 16;
 	/*HD*/cmd[2] = (add & 0xff00) >> 8;
 	/*SN*/cmd[1] = (add & 0xff);
 	/*SC*/cmd[0] = 1;
    if (AT91F_HDDWriteCommand(cmd,6,2) < 0) return -1;
    if (AT91F_HDD_Write16(buf,BUF_SIZE/2) < BUF_SIZE/2) return -1;
   	if (AT91F_HDD_StatusLow (ATA_ST_ERR) < 0) return (-1);

   	return 0;

}

//*----------------------------------------------------------------------------
//* \fn    HDDRead
//* \brief read from the HDD in LBA mode
//*----------------------------------------------------------------------------
int AT91F_HDDRead (unsigned short * buf , int add){
char cmd[6];
 	/*CC*/cmd[5] = READ_SECTOR;
 	/*DR*/cmd[4] = (1 << 7) | (1 << 6) | (1 << 5) | ((add & 0xf000000) >> 24); //LBA = 1
 	/*CY*/cmd[3] = (add & 0xff0000) >> 16;
 	/*HD*/cmd[2] = (add & 0xff00) >> 8;
 	/*SN*/cmd[1] = (add & 0xff);
 	/*SC*/cmd[0] = 1;

    if (AT91F_HDDWriteCommand(cmd,6,2) < 0) return -1;
    if (AT91F_HDD_Read16(buf,BUF_SIZE/2) < BUF_SIZE/2) return -1;
   	if (AT91F_HDD_StatusLow (ATA_ST_ERR) < 0) return (-1);

   	return 0;

}


//*----------------------------------------------------------------------------
//* \fn    HDDSleep
//* \brief put the HDD into sleep mode
//*----------------------------------------------------------------------------
int AT91F_HDDSleep (void){
 	char sleep[3];

 	sleep[0] = STAND_BY; 
 	sleep[1] = STAND_BY_IMM;
 	sleep[2] = SET_SLEEP_MODE;

    if (AT91F_HDDWriteCommand(&sleep[0],1,7) < 0) return -1;
    if (AT91F_HDDWriteCommand(&sleep[1],1,7) < 0) return -1;
    if (AT91F_HDDWriteCommand(&sleep[2],1,7) < 0) return -1;
   	if (AT91F_HDD_StatusLow (ATA_ST_ERR) < 0) return (-1);

   	return 0;

}


/******************************************************************************/

⌨️ 快捷键说明

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