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

📄 cf_driver.c

📁 文件包含了开发motorola mc9s12uf32单片机一些源码。  1、USB device 实现  2、CF卡读取  3、SD卡读取  4、ATA硬盘读取
💻 C
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************
 
  (c) copyright Motorola Semiconductors (HK) Ltd. 2002
  ALL RIGHTS RESERVED
 
 *********************************************************************
 
                   CF Driver Module for S12 MCUs 
 
 *********************************************************************

  File:			cf_driver.c
 
  Description:  Major part of CF Driver module. (APIs & Functions)
 
  Date:         Apr. 2003
  Author:		Derek Lau
  
 ********************************************************************/


#include	"FreescaleDef.h"			// Get my definitions (Constants & Macros)
#include	"uf32reg.h"		// Get the DP256 registers.
#include	"mk_extern.h"		// Get Mini-Kernel global prototypes
#include	"cf_includes.h"	// Get ATA Driver Configuration
#define _CF_EXTRN_G_DECL_
#include	"cf_extern.h"
#include	"usbms_extern.h"
#include	"utl_includes.h"

// code is placed in the main code area.
#pragma CODE_SEG	DEFAULT



CFErrorCode CF_Init(void)
{
	#ifdef	_CF_SECOND_SLOT_
		MODRR = (1<<MODRR_CFSLOT2);
	#endif

	CF_SCR1 = (1<<CFE) + (1<<CFOE) + (1<<DISINC);	// enable CF host
	CF_SCR2 = (1<<CA10E)+(1<<CA9E);

	mSetBit(CFResetPin,CFResetPort);		// set resetpin to high
	mSetBit(CFResetPin,CFResetDDR);	// set resetpin to output
	UTL_Delay(10);						// delay
	mClearBit(CFResetPin,CFResetPort);		// set resetpin to low

	UTL_Delay(10);						// delay	


//	gCFSenseKey = 0x03;
/*	
	CF_Write8Reg(CF_R_SOCKET_COPY,0x00);	// drive #0
	CF_Read8Reg(CF_R_SOCKET_COPY);
	CF_Write8Reg(CF_R_CONFIG,0x01);	// I/O Mode
	CF_Read8Reg(CF_R_CONFIG);
*/
//	CF_IQTest();

	return CF_GetSetDeviceInfo();
}

// ==================================================================
//   CF_Wait_Card_Busy() -
//
//     Wait for CF not busy.
//
//	Input -		=	nil
//				
//	Output - 	=	nil
//
//	Function returns: nil
//
// ==================================================================

CFErrorCode CF_WaitCardBusy(void)
{
	muint16 BegTime, CurTime;
	muint8 status;

	BegTime = MK_GetCurrentTime();				// begin time

	do
	{
//		status = CF_Read8Mem(CF_M_ALT_STATUS);	// get status
			status = CF_Read8Mem(CF_M_STATUS);	// get status

	    if (!mCheckBit(bitCFBUSY,status))
	    	return(kCSWPass);					// exit if not busy
	    
	    if (mCheckBit(bitCFDWF,status)||mCheckBit(bitCFERR,status))	
	 	 	return(kCSWFailed);					// write fault

		CurTime = MK_GetCurrentTime();			// current time
	}
	while ( ((CurTime-BegTime) < kCFTimeout) || ((BegTime - CurTime) < kCFTimeout));

	gCFError = kSCSISKHardwareError;
	return(kCSWPhaseError);						// timeout => phase error
}
CFErrorCode CF_WaitCardDRQ(void)
{
	muint16 BegTime, CurTime;
	muint8 status;

	BegTime = MK_GetCurrentTime();				// begin time

	do
	{
//		status = (muint8) CF_Read8Mem(CF_M_ALT_STATUS);
			status = (muint8) CF_Read8Mem(CF_M_STATUS);	// get status

	    if (mCheckBit(bitCFDRQ,status) && !mCheckBit(bitCFBUSY,status))
	    	return(kCSWPass);					// exit if data request is set
	    
	    if (mCheckBit(bitCFDWF,status)||mCheckBit(bitCFERR,status))	
	 	 	return(kCSWFailed);					// write fault
	 	 	
		CurTime = MK_GetCurrentTime();			// current time
	}
	while ( ((CurTime-BegTime) < kCFTimeout) || ((BegTime - CurTime) < kCFTimeout));

	gCFError = kSCSISKHardwareError;
	return(kCSWPhaseError);						// timeout => phase error
}



void CF_Wait8CardBusy(void)
{
	while (CF_Read8Mem(CF_M_STATUS) & (1<<bitCFBUSY));
}

CFErrorCode CF_WaitCardBusyIO(void)
{
	muint16 BegTime, CurTime;

	BegTime = MK_GetCurrentTime();		// begin time

	do
	{
		
	    if (!(CF_ReadIO(CF_M_HEAD_STATUS) & (1<<bitCFBUSY)))
	    	return(kCSWPass);			// exit 
	    	
		CurTime = MK_GetCurrentTime();	// current time
	}
	while ( ((CurTime-BegTime) < kCFTimeout) || ((BegTime - CurTime) < kCFTimeout));

	gCFError = kSCSISKHardwareError;
	return(kCSWPhaseError);				// timeout => phase error
}

// ==================================================================
//   CF_ReadReg() -
//
//     To read CF register.
//
//	Input -		=	Register
//				
//	Output - 	=	nil
//
//	Function return: data
//
// ==================================================================
muint16 CF_ReadReg(muint16 Reg)
{
	muint16 	dummy;
	CF_CCR_L	= 0;				// card in 16 bit reg mode
	CF_BBAR		= Reg;				// set reg address
	
	while (CF_CR_L & (1<<BSY));		// loop while busy
	CF_CR_L		= ((1<<INVOKE) | (1<<RWB));	// read CF
	while (CF_CR_L & (1<<BSY));		// loop while busy
	while ((CF_IFR & (1<<OOIF)) == 0);
	dummy = CF_ISR;
	return(CF_DR);
}

muint8 CF_Read8Reg(muint16 Reg)
{
	muint16 	dummy;
	CF_CCR_L	= 0;				// card in 16 bit reg mode
	CF_BBAR		= Reg;				// set reg address
	
	while (CF_CR_L & (1<<BSY));		// loop while busy
	CF_CR_L		= ((1<<INVOKE) | (1<<RWB));	// read CF
	while (CF_CR_L & (1<<BSY));		// loop while busy
	while ((CF_IFR & (1<<OOIF)) == 0);
	dummy = CF_ISR;
	return(CF_DR_H);
}

// ==================================================================
//   CF_WriteReg() -
//
//     To write CF register.
//
//	Input -		=	Register, data
//				
//	Output - 	=	nil
//
//	Function return 
//
// ==================================================================
void CF_WriteReg(muint16 Reg, muint16 Data)
{
	CF_CCR_L	= 0;		// card in 16 bit reg mode
	CF_BBAR	= Reg;			// set reg address 

	while ((CF_IFR_L & (1<<CFTEIF)) == 0);
	CF_DR	= Data;

	while (CF_CR_L & (1<<BSY));	// loop while busy
	CF_CR_L		= (1<<INVOKE);	// write CF
	while (CF_CR_L & (1<<BSY));	// loop while busy
	while ((CF_IFR & (1<<OOIF)) == 0);
}

void CF_Write8Reg(muint16 Reg, muint8 Data)
{
//	CF_CCR_L	= (1<<CE2B);	// card in 8 bit reg mode
	CF_CCR_L	= 0;
	CF_BBAR	= Reg;				// set reg address 

	while ((CF_IFR_L & (1<<CFTEIF)) == 0);
	CF_DR_H	= Data;
//	CF_DR_L	= Data;

	while (CF_CR_L & (1<<BSY));	// loop while busy
	CF_CR_L		= (1<<INVOKE);	// write CF
	while (CF_CR_L & (1<<BSY));	// loop while busy
	while ((CF_IFR & (1<<OOIF)) == 0);
}


// ==================================================================
//   CF_ReadMem() -
//
//     To read CF in 16 bit memory mode
//
//	Input -		=	Memory location
//				
//	Output - 	=	nil
//
//	Function return: data
//
// ==================================================================
muint16 CF_ReadMem(muint16 Mem)
{
	CF_CCR_L	= (1<<REGB);		// card in 16 bit memory mode

	CF_BBAR		= Mem;				// set reg address

	while (CF_CR_L & (1<<BSY));		// loop while busy
	CF_CR_L		= ((1<<INVOKE)+(1<<RWB));
	while (CF_CR_L & (1<<BSY));		// loop while busy
	while ((CF_IFR & (1<<OOIF)) == 0);

	return(CF_DR);
}

muint8 CF_Read8Mem(muint16 Mem)
{
	CF_CR_L		= 0x00;
	CF_CCR_L	= ((1<<CE2B)+(1<<REGB));	// card in 8 bit memory mode
	CF_BBAR		= Mem;				// set reg address

	while (CF_CR_L & (1<<BSY));		// loop while busy
	CF_CR_L		= ((1<<INVOKE) | (1<<RWB));
	while (CF_CR_L & (1<<BSY));		// loop while busy
	while ((CF_IFR & (1<<OOIF)) == 0);

	return(CF_DR_H);
}

muint16 CF_ReadIO(muint16 Mem)
{
	CF_CR_L		= 0x00;
	CF_CCR_L	= (1<<COM);			// card in 16 bit I/O mode
	CF_BBAR		= Mem;				// set reg address

	while (CF_CR_L & (1<<BSY));		// loop while busy
	CF_CR_L		= ((1<<INVOKE) | (1<<RWB));
	while (CF_CR_L & (1<<BSY));		// loop while busy
	while ((CF_IFR & (1<<OOIF)) == 0);

	return(CF_DR);
}
// ==================================================================
//   CF_BufferReadMem() -
//
//     Buffer Read data from CF device
//
//	Input -		= Buffer pointer, Lenght of Transfer
//				
//	Output - 	= Data return in buffer pointer	
//
//	Function returns error code
//
// ==================================================================
CFErrorCode CF_BufferReadMem(muint16 *pBuffer, muint16 XferLen)
{
	int i;

	CF_CCR_L	= (1<<REGB);		// card in 16 bit memory mode
	CF_BBAR		= CF_M_DATA;				// set reg address
	
	for (i=0; i<XferLen; i++)			// get data
	{		
		while (CF_CR_L & (1<<BSY));		// loop while busy
		CF_CR_L		= ((1<<INVOKE)+(1<<RWB));
		while (CF_CR_L & (1<<BSY));		// loop while busy
		while ((CF_IFR & (1<<OOIF)) == 0);

		*pBuffer++ = CF_DR;
	}
	return (kCFPass);	
}



// ==================================================================
//   CF_IQRead_Mem() -
//
//     To read CF in 16 bit memory mode through 
//
//	Input -		=	Memory location
//				
//	Output - 	=	data in IQUE RAM
//
//	Function return 
//
// ==================================================================
CFErrorCode CF_IQReadMem(void)
{
	muint16 dummy;

	CF_CCR_L	= (1<<REGB);		// card in 16 bit memory mode
	CF_BBAR		= CF_M_DATA;		// set reg address

	while (CF_CR_L & (1<<BSY));		// loop while busy
	CF_CR_L		= ((1<<HIS) | (1<<INVOKE) | (1<<RWB));
	while (CF_CR_L & (1<<BSY));		// loop while busy
	while ((CF_IFR & (1<<OOIF)) == 0);
	dummy = CF_CR;
	CF_CR_L = 0;
	return(0);
}

CFErrorCode CF_IQReadMemOneBlock(muint8 pIQUEBuffer)
{
	muint16 dummy;

	IQUECR = (1<<CH34DBE)+(1<<IQUEEN)+(1<<IQUERST);	// Enable IQUECR
	QC34DTR = (1<<SBTE)+(1<<DTHE);
					// enable force handshake
	QC3CR = (1<<QnEN)+(1<<QnTHRU)+(1<<Qn16EN)+(1<<QnPRST);					// enable QC3

	mSetBit(DBRST,QC34DCR);				// reset QC34 double buffer
	mSetBit(QnPRST,QC3CR);
	QC3REQ = kQCREQCFRx;
	QC3SZB = 0x10 | pIQUEBuffer;
	CF_BSR = 0x1FE;	

	CF_CCR_L	= (1<<REGB);		// card in 16 bit memory mode
	CF_BBAR		= CF_M_DATA;		// set reg address

	while (CF_CR_L & (1<<BSY));		// loop while busy
	CF_CR_L		= ((1<<HIS) | (1<<INVOKE) | (1<<RWB));
	while (CF_CR_L & (1<<BSY));		// loop while busy
	while ((CF_IFR & (1<<OOIF)) == 0);
	dummy = CF_CR;
	CF_CR_L = 0;
	while ((QC34DSR & 0x03) != 0x01);
	(void) CF_WaitCardBusy();

	mClearBit(QnEN,QC3CR);
	QC3REQ = kQCREQNone;
	mSet2Bit(DRHE,DTHE,QC34DTR);					// enable force handshake
	return(0);
}

CFErrorCode CF_IQReadMemTwoBlock(muint8 pIQUEBuffer)
{
	muint16 dummy;

	IQUECR = (1<<CH34DBE)+(1<<IQUEEN)+(1<<IQUERST);	// Enable IQUECR
	QC34DTR = (1<<SBTE)+(1<<DTHE);
	QC3CR = (1<<QnEN)+(1<<QnTHRU)+(1<<Qn16EN)+(1<<QnPRST);					// enable QC3
	QC1REQ = kQCREQNone;
	QC2REQ = kQCREQNone;
	mClearBit(QnEN,QC1CR);
	mSetBit(DBRST,QC34DCR);				// reset QC34 double buffer
	QC3REQ = kQCREQCFRx;
//	QC4REQ = kQCREQCFRx;
	QC3SZB = 0x10 | pIQUEBuffer;
	CF_BSR = 0x1FE;	
	CF_CCR_L	= (1<<REGB);		// card in 16 bit memory mode
	CF_BBAR		= CF_M_DATA;		// set reg address

	while (CF_CR_L & (1<<BSY));		// loop while busy
	CF_CR_L		= ((1<<HIS) | (1<<INVOKE) | (1<<RWB));
	while (CF_CR_L & (1<<BSY));		// loop while busy
	while ((CF_IFR & (1<<OOIF)) == 0);
	dummy = CF_CR;
	CF_CR_L = 0;

//	while ((QC34DSR & 0x03) != 0x01);
	(void) CF_WaitCardBusy();
	while (CF_CR_L & (1<<BSY));		// loop while busy

	CF_BSR = 0x1FE;	
	CF_CCR_L	= (1<<REGB);		// card in 16 bit memory mode
	CF_BBAR		= CF_M_DATA;		// set reg address

⌨️ 快捷键说明

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