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

📄 sd_processor.c

📁 文件包含了开发motorola mc9s12uf32单片机一些源码。  1、USB device 实现  2、CF卡读取  3、SD卡读取  4、ATA硬盘读取
💻 C
字号:
/******************************************************************************
*                                                  
*  (c) copyright Freescale Semiconductor Hong Kong Ltd 2004
*  ALL RIGHTS RESERVED
*                                                                       
*******************************************************************************  
** THIS  CODE IS ONLY INTENDED AS AN EXAMPLE FOR DEMONSTRATING THE FREESCALE **
** MICROCONTROLLERS.  IT  HAS ONLY BEEN GIVEN A MIMIMUM LEVEL OF TEST. IT IS **
** PROVIDED  'AS  SEEN'  WITH  NO  GUARANTEES  AND  NO  PROMISE  OF SUPPORT. **
*******************************************************************************  
*                                                                           
* FILE:        sd_processor.c	 REVISION 0.1
*  
* DESCRIPTION: This module handles SD state machine & command processor 
*              applicaton tasks for the system
*
* NOTES:       All modules remain at their reset addresses
*                                                  
* UPDATED HISTORY:
*
* REV   YYYY.MM.DD  AUTHOR        DESCRIPTION OF CHANGE
* ---   ----------  ------        --------------------- 
* 0.0   2004.04.01  Taine Chang   Initial version
* 0.1   2004.04.12  Kenny Lam     Demo version
*
******************************************************************************/                                                                        
/* Freescale  is  not  obligated  to  provide  any  support, upgrades or new */
/* releases  of  the Software. Freescale may make changes to the Software at */
/* any time, without any obligation to notify or provide updated versions of */
/* the  Software  to you. Freescale expressly disclaims any warranty for the */
/* Software.  The  Software is provided as is, without warranty of any kind, */
/* either  express  or  implied,  including, without limitation, the implied */
/* warranties  of  merchantability,  fitness  for  a  particular purpose, or */
/* non-infringement.  You  assume  the entire risk arising out of the use or */
/* performance of the Software, or any systems you design using the software */
/* (if  any).  Nothing  may  be construed as a warranty or representation by */
/* Freescale  that  the  Software  or  any derivative work developed with or */
/* incorporating  the  Software  will  be  free  from  infringement  of  the */
/* intellectual property rights of third parties. In no event will Freescale */
/* be  liable,  whether in contract, tort, or otherwise, for any incidental, */
/* special,  indirect, consequential or punitive damages, including, but not */
/* limited  to,  damages  for  any loss of use, loss of time, inconvenience, */
/* commercial loss, or lost profits, savings, or revenues to the full extent */
/* such  may be disclaimed by law. The Software is not fault tolerant and is */
/* not  designed,  manufactured  or  intended by Freescale for incorporation */
/* into  products intended for use or resale in on-line control equipment in */
/* hazardous, dangerous to life or potentially life-threatening environments */
/* requiring  fail-safe  performance,  such  as  in the operation of nuclear */
/* facilities,  aircraft  navigation  or  communication systems, air traffic */
/* control,  direct  life  support machines or weapons systems, in which the */
/* failure  of  products  could  lead  directly to death, personal injury or */
/* severe  physical  or  environmental  damage  (High  Risk Activities). You */
/* specifically  represent and warrant that you will not use the Software or */
/* any  derivative  work of the Software for High Risk Activities.           */
/* Freescale  and the Freescale logos are registered trademarks of Freescale */
/* Semiconductor Inc.                                                        */ 
/*****************************************************************************/

#include	"FreescaleDef.h"			// Get my definitions (Constants & Macros)
#include	"sd_includes.h"	// Get ATA Driver Configuration
#include	"sd_extern.h"
#include	"usbms_extern.h"
//#include 	"sd_def.h"
#include	"UF32reg.h"

// code is placed in the main code area.
//#pragma DATA_SEG DEFAULT_RAM 

#pragma CODE_SEG	DEFAULT

//muint32 kSD_loop_counter=0;
//muint8 kSD_WriteProtect;

//-----------------------
// additional SD codes
// ==================================================================
//   SD_LBASDCommand() -
//
//     To get all parameters for SD
//
//	Input -		=	7 command parameters
//				
//	Output - 	=	
//
//	Function returns error code
//
// ==================================================================
mErrorCode SD_LBASDCommand(muint8 Block)
{
    	
//    volatile muint32	lblock=0;
    
    gSD_LBA = ((muint32) *(gpbCBWPacket+kSCSI10LBAByte3))<<24;
    gSD_LBA |= ((muint32) *(gpbCBWPacket+kSCSI10LBAByte2))<<16;
    gSD_LBA |= ((muint32) *(gpbCBWPacket+kSCSI10LBAByte1))<<8;
    gSD_LBA |= ((muint32) *(gpbCBWPacket+kSCSI10LBAByte0));
//    gMS_LBA += gMS_PhyPageOffSet;
//t  	gSD_LBA = gSD_LBA>>5;	// testing
    
    gSD_DSCR_B = Block;
//	gMS_PhyPage = ((muint8)(gMS_LBA))&0x1f;
//	gMS_PhyBlock = (muint16)(gMS_LBA>>5);
//	lblock=(muint32) gMS_PhyBlock+(muint32)gMSBootBlock;	
//	MS_ReadPage(lblock, gMS_PhyPage);
  			
	return (kSDNoError);
		
}

// ==================================================================
//   SD_SCSIReadCapacity() -
//
//   Test the device is ready or not
//
//	Input -		nil
//
//	Output - 	
//  ErrorCode - 0x00: Ready
//				0x01: Not ready
//				0x02: Timeout
//
// ==================================================================
mErrorCode	SD_SCSIReadCapacity(void)
{		
	volatile muint32 lpblock=0;

	muint16 i, kSD_Temp, kSD_READ_BL_LEN, kSD_C_SIZE, kSD_C_SIZE_MULTI;
	muint32 gSD_MaxBlockNumber;
	
	
	// Capacity calculation
	kSD_Temp = (gSD_CSD[2] & 0x0F);
	kSD_READ_BL_LEN = 1;
	for(i=0; i<kSD_Temp; i++)
		kSD_READ_BL_LEN *= 2;	
	kSD_C_SIZE = ( ((gSD_CSD[3] & 0x03FF) << 2) | ((gSD_CSD[4] & 0xC000) >> 14) );
	kSD_C_SIZE += 1;
	kSD_Temp = ((gSD_CSD[4] & 0x0003) <<1)| ( (gSD_CSD[5] & 0x8000) >> 15 );
	kSD_C_SIZE_MULTI = 1;
	for(i=0; i<kSD_Temp+2; i++)
		kSD_C_SIZE_MULTI *= 2;
	gSD_MaxBlockNumber =  ((muint32)kSD_C_SIZE * (muint32)kSD_C_SIZE_MULTI)-1;
	QC3DR = (gSD_MaxBlockNumber & 0xFFFF0000) >> 16;	// byte 0-3 logical block address LBA
	QC3DR = gSD_MaxBlockNumber & 0xFFFF;			// byte 0-3 logical block address LBA
	QC3DR = 0;									// byte 4-7 block length in byte
	QC3DR = kSD_READ_BL_LEN;						// byte 4-7 block length in byte
	return (kSDNoError);	


//------------------------
// original codes
/*	lpblock=(muint32)gMSBootBlock;
	cptr=(muint8*)ReadBlockAddr;
	if(MS_ReadPage(lpblock, 1)==kMSNoError)		// read out the data of block 5, page 2
    {
//	  *pBuffer++ = *(cptr+kMSTotSec32+3);
//	  *pBuffer++ = *(cptr+kMSTotSec32+2);		//0x3;
//	  *pBuffer++ = *(cptr+kMSTotSec32+1);		//0xdd;
//	  *pBuffer++ = *(cptr+kMSTotSec32+0);		//0xdf;	
	  *pBuffer++ = 0;
	  *pBuffer++ = 0x3;		//0x3;
	  *pBuffer++ = 0xde;		//0xdd;
	  *pBuffer++ = 0xff;		//0xdf;	
    }
    else
    {
	  *pBuffer++ = 0;
	  *pBuffer++ = 0;		//0x3;
	  *pBuffer++ = 0;		//0xdd;
	  *pBuffer++ = 0;		//0xdf;
    
    }
    *pBuffer++ = 0;							
    *pBuffer++ = 0;							
	*pBuffer++ = 0x02;						// 512 bytes per block
	*pBuffer++ = 0x00;
	return (kMSNoError);	
*/	
}



/*
// temp disable
// ok
// ==================================================================
//   SD_SCSIVerify() -
//
//   Test the device is ready or not
//
//	Input -		BytCHong Kong: 0= check if device data can be read
//						1= compare sent data and device data
//				SD_BlockAddress: byte address. e.g. 512, 1024...
//
//	Output - 	
//  Response - 0x00: Passed (if BytCHong Kong=1, means no different between device data and sent data)
//			   0x01: Failed (if BytCHong Kong=1, means the device data is different from the sent data)
//			   0x02: Timeout
//			   0x03: data is different
//
// ==================================================================
mErrorCode	SD_SCSIVerify(muint8 BytCHong Kong, muint32 SD_BlockAddress)
{		
	muint16 i, j, k=0, kSD_Temp, kSD_READ_BL_LEN, kSD_C_SIZE, kSD_C_SIZE_MULTI;
	muint16 SD_BlockAdrs_H, SD_BlockAdrs_L, kSD_ComparisonDifCounter=0;
	muint16 *kSD_Que, kSD_TempQueData;
	muint16 SD_RxDATA[32];

	for(i=0; i<32; i++)
		SD_RxDATA[i] = 0;
		
	if(BytCHong Kong == 0)					// check if device data can be read
	{
		i = SD_QUE_ReadMultipleBlock(SD_BlockAddress, 1);	
		return(i);					// returns the result		
	}
	else							// compares sent data and device data
	{			
		SD_SDCLKCON = 0x000f;				// set SD clk 
		for(i=0; i<SD_TimeOut; i++)
		{
			kSD_Temp = SD_CheckStatus();
			if(kSD_Temp == 1)				// device is ready
				break;
		}
		SD_BlockAdrs_H = (muint16)((SD_BlockAddress & 0xFFFF0000) >16);
		SD_BlockAdrs_L = (muint16)(SD_BlockAddress & 0x0000FFFF);
//	   	SD_SendCommand(0x0011, SD_BlockAdrs_H, SD_BlockAdrs_L, 0x0005);	//CMD17 READ_SINGLE_BLOCK, data adrs	
	 	if(gSD_SDcard)		// Plugged flash card is SD
		   	SD_SendCommand(0x0012, SD_BlockAdrs_H, SD_BlockAdrs_L, 0x0625);	//CMD18 READ_MULTUPLE_BLOCK, data adrs, WBUS for SD card	
		else
		   	SD_SendCommand(0x0012, SD_BlockAdrs_H, SD_BlockAdrs_L, 0x0405);	//CMD18 READ_MULTIPLE_BLOCK, data adrs, 1-bit mode for MMC	
  
		while(!(gSD_SDSATAT&0x0400)) 			// wait for cmd end
		{
			k++;
			if( k > SD_TimeOut)
				return(1);					// command failed
		}		
		//----------------
		// Method 1, proposed by K.W.
		// SD_FIFO_empty_counter++;
//		SD_SDCLKCON = 0x000f;		// set SD clk to 1/3 module clk
//        IQUECR=0x03;            // Enable and reset IQUE module
//        QC1CR=0x15;				// Q1SML = 0, large buffer 16-bit transfer
//        QC1SZB=0x14;			// 512 bytes block, starting from $0400
//	    QC1REQ=0x08; 			// SD Rx channel		
//	    SD_QIEN=1;	
	
		kSD_Que = (muint16 *)0x400;
//		for(i=0; i<32; i++)
//		    	QC1DR = 1;
		for(i=0; i<256; i++)								 // due to ram size limitation, only 32 word array is used
		{	 
	  		while(SD_FEMPTY);                        // Loop if FEMPTY = 1
			j= i%32;
			SD_RxDATA[j] = SD_SDATA;
	    	kSD_TempQueData = *kSD_Que++;
	    	if(SD_RxDATA[j] != kSD_TempQueData)
		    	kSD_ComparisonDifCounter++;
	    	if(i==256)
	    		break;
		}


         
//		for(i=0; i<8; i++)
//		{
//			for(k=0; k<32; k++)								 // due to ram size limitation, only 32 word array is used//
//			{	 
///			    while(SD_FEMPTY);                        // Loop if FEMPTY = 1
//		    	SD_RxDATA[k] = SD_SDATA;
//			    kSD_Temp = QC1DR;
//			    if(SD_RxDATA[k] != kSD_Temp)
//					kSD_ComparisonDifCounter++;
//			    	return(3);					// data is different
//			}
//		}
	
	
	    k=0;      
//	    while(!(gSD_SDSATAT&0x0100))            // wait for DTDN=1
//		{
//			k++;
//			if( k > SD_TimeOut)
//				return(1);					// command failed
//		}
		SD_SendCommand(0x000C, 0, 0, 0x0061);	//CMD12, STOP_TRANSMISSION,
		while(!(gSD_SDSATAT&0x0400)) 			// wait for cmd end  
		{
			k++;
			if( k > SD_TimeOut)
				return(1);					// command failed
		}	
		if(kSD_ComparisonDifCounter == 0)		  
			return(0);								// data is the same
		else
			return(1);
	}
	
}
*/


/* temp disable
// ok
// ==================================================================
//   SD_SCSIReadCapacity() -
//
//   Test the device is ready or not
//
//	Input -		nil
//
//	Output - 	
//  ErrorCode - 0x00: Ready
//				0x01: Not ready
//				0x02: Timeout
//
// ==================================================================
mErrorCode	SD_SCSIReadCapacity(muint16 *pBuffer)
{		
	muint16 i, kSD_Temp, kSD_READ_BL_LEN, kSD_C_SIZE, kSD_C_SIZE_MULTI;
	muint32 gSD_MaxBlockNumber;
	
	// Capacity calculation
	kSD_Temp = (gSD_CSD[2] & 0x0F);
	kSD_READ_BL_LEN = 1;
	for(i=0; i<kSD_Temp; i++)
		kSD_READ_BL_LEN *= 2;	
	kSD_C_SIZE = ( ((gSD_CSD[3] & 0x03FF) << 2) | ((gSD_CSD[4] & 0xC000) >> 14) );
	kSD_C_SIZE += 1;
	kSD_Temp = ((gSD_CSD[4] & 0x0003) <<1)| ( (gSD_CSD[5] & 0x8000) >> 15 );
	kSD_C_SIZE_MULTI = 1;
	for(i=0; i<kSD_Temp+2; i++)
		kSD_C_SIZE_MULTI *= 2;
//	gSD_Capacity = ((muint32)kSD_C_SIZE * (muint32)kSD_C_SIZE_MULTI * (muint32)(kSD_READ_BL_LEN)) / 0x100000;
	gSD_MaxBlockNumber =  ((muint32)kSD_C_SIZE * (muint32)kSD_C_SIZE_MULTI)-1;
	*pBuffer = (gSD_MaxBlockNumber & 0xFFFF0000) >> 16;	// byte 0-3 logical block address LBA
	*(pBuffer+1) = gSD_MaxBlockNumber & 0xFFFF;			// byte 0-3 logical block address LBA
	*(pBuffer+2) = 0;									// byte 4-7 block length in byte
	*(pBuffer+3) = kSD_READ_BL_LEN;						// byte 4-7 block length in byte

	return (kSDNoError);	

}
*/


/* temp disable
// OK
// ==================================================================
//   SD_SCSITestUnitReady() -
//
//   Test the device is ready or not
//
//	Input -		nil
//
//	Output - 	
//  ErrorCode - 0x00: Ready
//				0x01: Not ready
//				0x02: Timeout
//
// ==================================================================
mErrorCode	SD_SCSITestUnitReady(void)
{
	muint16 i;
	muint8	kOperationResult=0;

	
	for(i=0; i<SD_TimeOut; i++)
	{
		kOperationResult = SD_CheckStatus();
		if(kOperationResult == 1)						// the card is ready
			return(0);
	}
	
	return(2);			// timeout
}
*/

//
// The end of file sd_processor.c 
// *********************************************************************************

⌨️ 快捷键说明

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