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

📄 main.c

📁 基于U(375)盘及SD(SPI模式)卡集成文件系统(包括底层驱动)
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *  Copyright 2003 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  
 */
/* "@(#) DSP/BIOS 4.90.270 06-11-03 (barracuda-m10)" */
/*
 *  Copyright 2001 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  U.S. Patent Nos. 5,283,900  5,392,448
 */
/* "@(#) DSP/BIOS 4.51.0 05-23-01 (barracuda-i10)" */
/******************************************************************************\
*           Copyright (C) 2000 Texas Instruments Incorporated.
*                           All Rights Reserved
*------------------------------------------------------------------------------
* FILENAME...... main.c
* DATE CREATED.. 01/11/2000
* LAST MODIFIED. 09/27/2000
\******************************************************************************/
#include <std.h>
#include <stdio.h>
#include <log.h>
#include <swi.h>

#include <csl.h>
#include <csl_mcbsp.h>
#include <csl_timer.h>
#define  FAT_INI_VAR  

#include "TYPE.H"
#include "FAT.H"


#define CMD0                    0x40
#define CMD1                    (0x40 + 1)
#define CMD8                    (0x40 + 8)
#define CMD9                    (0x40 + 9)
#define CMD10                   (0x40 + 10)
#define CMD12					(0x40 + 12)
#define CMD16                   (0x40 + 16)
#define CMD17					(0x40 + 17)
#define CMD18					(0x40 + 18)
#define CMD24 					(0x40 + 24)
#define CMD32                   (0x40 + 32)  //设置擦除开始块地址
#define CMD33                   (0x40 + 33)  //设置擦除最后块地址
#define CMD38                   (0x40 + 38)  //擦除所选择的块
#define CMD25					(0x40 + 25)
#define CMD55                   (0x40 + 55)
#define CMD58                   (0x40 + 58)

#define ACMD13					(0xC0 + 13)
#define ACMD23					(0xC0 + 23)
#define ACMD41					(0xC0 + 41)

typedef enum{ STA_INITED, STA_NOINIT, STA_NODISK,STA_PROTECT} DSTATUS;

static volatile DSTATUS Stat = STA_NOINIT;

static volatile unsigned char CardType; // b0:MMC, b1:SDv1, b2:SDv2, b3:Block addressing //
unsigned char buff_w[10512];
unsigned char buff_r[10512];
static volatile unsigned int Timer1, Timer2;
TIMER_Handle htimer1;
unsigned int start, end, overhead;
unsigned int writetime, readtime;
unsigned int transfertime1, transfertime2,transfertime3, transfertime4;
unsigned int testcount1, testcount2,testcount3;
unsigned char temp1[128];
unsigned char temp2[128];

volatile Uint8 SD_U;
/*----------------------------------------------------------------------------*/
extern far LOG_Obj LogMain;
extern far SWI_Obj SwiMain;
MCBSP_Handle myMcbsp;
/*----------------------------------------------------------------------------*/


static MCBSP_Config myInitialConfig = {
  MCBSP_SPCR_RMK(
    MCBSP_SPCR_FREE_DEFAULT	,
    MCBSP_SPCR_SOFT_DEFAULT,
    MCBSP_SPCR_FRST_DEFAULT,
    MCBSP_SPCR_GRST_DEFAULT,
    MCBSP_SPCR_XINTM_DEFAULT,
    MCBSP_SPCR_XSYNCERR_DEFAULT,
    MCBSP_SPCR_XRST_DEFAULT,
    MCBSP_SPCR_DLB_DEFAULT,
    MCBSP_SPCR_RJUST_DEFAULT,
    MCBSP_SPCR_CLKSTP_NODELAY,
    MCBSP_SPCR_DXENA_DEFAULT,
    MCBSP_SPCR_RINTM_RRDY,
    MCBSP_SPCR_RSYNCERR_DEFAULT,
    MCBSP_SPCR_RRST_DEFAULT
  ),
  MCBSP_RCR_RMK(
    MCBSP_RCR_RPHASE_SINGLE,
    MCBSP_RCR_RFRLEN2_DEFAULT,
    MCBSP_RCR_RWDLEN2_DEFAULT,
    MCBSP_RCR_RCOMPAND_DEFAULT,
    MCBSP_RCR_RFIG_NO,
    MCBSP_RCR_RDATDLY_1BIT,
    MCBSP_RCR_RFRLEN1_DEFAULT, 
    MCBSP_RCR_RWDLEN1_8BIT,
    MCBSP_RCR_RWDREVRS_DISABLE
  ),
  MCBSP_XCR_RMK(
    MCBSP_XCR_XPHASE_SINGLE,
    MCBSP_XCR_XFRLEN2_DEFAULT,
    MCBSP_XCR_XWDLEN2_DEFAULT,
    MCBSP_XCR_XCOMPAND_DEFAULT,
    MCBSP_XCR_XFIG_NO,
    MCBSP_XCR_XDATDLY_1BIT,
    MCBSP_XCR_XFRLEN1_DEFAULT, 
    MCBSP_XCR_XWDLEN1_8BIT,
    MCBSP_XCR_XWDREVRS_DISABLE
  ),
  MCBSP_SRGR_RMK(
    MCBSP_SRGR_GSYNC_FREE,
    MCBSP_SRGR_CLKSP_RISING,
    MCBSP_SRGR_CLKSM_INTERNAL,
    MCBSP_SRGR_FSGM_DXR2XSR,
    MCBSP_SRGR_FPER_DEFAULT,
    MCBSP_SRGR_FWID_DEFAULT,
    MCBSP_SRGR_CLKGDV_OF(4)
  ),
  MCBSP_MCR_DEFAULT,
  MCBSP_RCERE0_DEFAULT,
  MCBSP_RCERE1_DEFAULT,
  MCBSP_RCERE2_DEFAULT,
  MCBSP_RCERE3_DEFAULT,
  MCBSP_XCERE0_DEFAULT,
  MCBSP_XCERE1_DEFAULT,
  MCBSP_XCERE2_DEFAULT,
  MCBSP_XCERE3_DEFAULT,
  MCBSP_PCR_RMK(
    MCBSP_PCR_XIOEN_SP,
    MCBSP_PCR_RIOEN_SP,
    MCBSP_PCR_FSXM_INTERNAL,
    MCBSP_PCR_FSRM_EXTERNAL,
    MCBSP_PCR_CLKXM_OUTPUT,
    MCBSP_PCR_CLKRM_INPUT,
    MCBSP_PCR_CLKSSTAT_0,
    MCBSP_PCR_DXSTAT_0,
    MCBSP_PCR_FSXP_ACTIVELOW,
    MCBSP_PCR_FSRP_ACTIVELOW,
    MCBSP_PCR_CLKXP_FALLING,
    MCBSP_PCR_CLKRP_DEFAULT
  )
}; 

UINT8 SPI_TransferByte(UINT8 value)       //数据传入后等待返回的数据
{ 
	UINT32  incoming;	
	while (!MCBSP_xrdy(myMcbsp));                      
    MCBSP_write(myMcbsp,value);                      
	
	while(!MCBSP_rrdy(myMcbsp));
	incoming = MCBSP_read(myMcbsp);   //得到数据
	return(incoming);
}



static Bool rcvr_datablock(
	unsigned char* buff,
	unsigned int btr
)
{
	UINT8 token;
	UINT32 token_wait = 10000;
	do{
		token = SPI_TransferByte(0xff);
	}while((token !=0xFE)&&token_wait--);
	if(!token_wait) return FALSE;
	
	do{
		*buff++=SPI_TransferByte(0xff);
	}while(--btr);
	SPI_TransferByte(0xff);  //Discard CRC
	SPI_TransferByte(0xff);  //
	return TRUE;
}

static Bool xmit_datablock(
	UINT8* buff,
	UINT8 token
)
{
	UINT8 resp;
	UINT16 wc;
	UINT32 resp_wait=1000000;
	wc = 512;
	SPI_TransferByte(token);
	if(token!=0xFD)
	{
		do{
			SPI_TransferByte(*buff++);
		}while(--wc);
		SPI_TransferByte(0xff);//CRC
		SPI_TransferByte(0xff);
		do{
			resp = SPI_TransferByte(0xff);
		}while((--resp_wait)&&((resp&0x1F)!=0x05));
		if(!resp_wait)
			return FALSE;
	}
	else
	{
		SPI_TransferByte(0xff);
		SPI_TransferByte(0xff);
	}
	return TRUE;
}




UINT8 Write_Command_MMC(UINT8 cmd, UINT32 address)
{
	UINT32 retry=100;
	UINT8 tmp;
	UINT8 crcc = 0xFF;
	if(cmd&0x80)
	{
		cmd &=0x7F;
		tmp = Write_Command_MMC(CMD55,0);
		if(tmp>1)
			return tmp;
	}
	SPI_TransferByte(0xff);
	
	tmp = SPI_TransferByte(cmd);
	tmp = SPI_TransferByte(address>>24);
	tmp = SPI_TransferByte(address>>16);
	tmp = SPI_TransferByte(address>>8);
	tmp = SPI_TransferByte(address);
	if(cmd == CMD0) crcc = 0x95;
	if(cmd == CMD8) crcc = 0x87;
	tmp = SPI_TransferByte(crcc);   
	
	if(cmd == CMD12) SPI_TransferByte(0xff);
	
	do{
		tmp = SPI_TransferByte(0xFF);
		--retry;
	}while((tmp&0x80)&&retry);   //  等待返回或者计时时间到达
	if(!retry)
		printf("Error: CMD%d, \n",(cmd-64));
	if(cmd == CMD17)
		crcc = 0xff;
	if((cmd == CMD25)||(cmd == CMD24))
		crcc = 0xff;
	return(tmp);
}




UINT8 MMC_Init()
{

    UINT8 i;
    UINT8 ty;
    UINT8 ocr[4];
    UINT8 cmd;
    if(Stat & STA_NODISK) return Stat;
    for (i=0;i<20;i++)
    {
    	SPI_TransferByte(0xff); //send 74 clock at least!!!
    }
    //复位为idle 
    if(Write_Command_MMC(CMD0,0)==0x01)
    {
    	Timer1 = 100;
    	if(Write_Command_MMC(CMD8,0x1AA) ==0x01)
    	{
    		for(i=0;i<4;i++)
    			ocr[i] = SPI_TransferByte(0xff);
    		if(ocr[2] == 0x01 && ocr[3] == 0xAA){
    			while(Timer1 && Write_Command_MMC(ACMD41 , 1UL <<30));
    			if(Timer1 && Write_Command_MMC(CMD58, 0) ==0){
    				for(i=0;i<4;i++) ocr[i] = SPI_TransferByte(0xff);
    				ty = (ocr[0] & 0x40)? 12: 4;
    			}
    		}
    	}
    	else
    	{
    		if(Write_Command_MMC(ACMD41, 0)<=1)
    		{
    			ty = 2; cmd = ACMD41;
    		}
    		else
    		{
    			ty = 1;
    			cmd = CMD1;
    		}
    		while(Timer1 && Write_Command_MMC(cmd,0));
    		if(!Timer1 || Write_Command_MMC(CMD16, 512) !=0)
    			ty = 0;
    	}
    }
    CardType = ty;
    if(ty)
    {
    	Stat = STA_INITED;
    }
    else
    	Stat = STA_NOINIT;
    return Stat;
    	
}



UINT8   RBC_Read( UINT32 iLbaStart, UINT8 iSectorCount,UINT8 xdata *mBufferPoint )
{
	if(!iSectorCount) 
	{
		printf("Error: RBC_Read() Parameter error \n");
		return CH_ERROR;
	}
	if(Stat == STA_NOINIT) 
	{
		printf("Error: RBC_Read() Chip is not inited \n");
		return CH_ERROR;
	}
	if(!(CardType&8)) iLbaStart = iLbaStart*512;
	SPI_TransferByte(0xff);
	if(iSectorCount ==1)
	{
		if((Write_Command_MMC(CMD17,iLbaStart)<=1)&&rcvr_datablock(mBufferPoint,512))
			iSectorCount = 0;
	}
	else
	{
		if(Write_Command_MMC(CMD18,iLbaStart)==0)
		{
			do{
				if(!rcvr_datablock(mBufferPoint, 512)) 
					break;
				mBufferPoint =mBufferPoint + 512;
			}while(--iSectorCount);
			Write_Command_MMC(CMD12,0);
		}
	}
	SPI_TransferByte(0xff);
	if(iSectorCount)
	{
		printf("Error: RBC_Read() quit, write is not finished \n");
		return CH_ERROR;
	}
	else
	{
//		printf("Success: RBC_Read() success \n");
		return CH_OK;
	}
}


UINT8 RBC_Write(UINT32 iLbaStart, UINT8 iSectorCount,UINT8 xdata *mBufferPoint)
{
	UINT8 temp;
	UINT16 write_wait = 65535;
	if(!iSectorCount) 
	{
		printf("Error: RBC_Write() Parameter error \n");
		return CH_ERROR;
	}
	if(Stat == STA_NOINIT) 
	{
		printf("Error: RBC_Write() Chip is not inited \n");
		return CH_ERROR;
	}
	if(Stat == STA_PROTECT) 
	{
		printf("Error: RBC_Write() Chip is protected \n");
		return CH_ERROR;
	}
	
	if(!(CardType & 8)) iLbaStart = iLbaStart * 512;
	SPI_TransferByte(0xff);
	SPI_TransferByte(0xff);  //wait at least 8 clock
	if(iSectorCount == 1)
	{
		if(Write_Command_MMC(CMD24, iLbaStart)==0x00)
		{
			if(xmit_datablock(mBufferPoint,0xFE))
				iSectorCount = 0;
			write_wait = 65535;
			do{
				temp = SPI_TransferByte(0xff);
				write_wait--;
			}while((temp!=0xff)&&write_wait);    //wait for write complete
			if(!write_wait)	
			{
				printf("Error: RBC_Write(), single sector write time out \n");
				return CH_ERROR;
			}
		}
				
	}
	else
	{
		if(CardType == 6) 
			Write_Command_MMC(ACMD23, iSectorCount);
		if(Write_Command_MMC(CMD25,iLbaStart)==0x00)
		{
			do{
				if(!xmit_datablock(mBufferPoint,0xFC)) break;
					mBufferPoint = mBufferPoint +512;
					write_wait = 65535;
					do{
						temp = SPI_TransferByte(0xff);
						write_wait--;
					}while((temp!=0xff)&&write_wait);
					if(!write_wait)	
					{
						printf("Error: RBC_Write(), multi sectors write time out \n");
						return CH_ERROR;
					}
			}while(--iSectorCount);
			xmit_datablock(0,0xFD);
			write_wait = 65535;
			do{
				temp = SPI_TransferByte(0xff);
				write_wait--;
			}while((temp!=0xff)&&write_wait);
			if(!write_wait)	
			{
				printf("Error: RBC_Write(), write return to ilde time out \n");
				return CH_ERROR;
			}
		}
	}
	if(iSectorCount)
	{
		printf("Error: RBC_Write() quit, write is not finished \n");
		return CH_ERROR;
	}
	else
	{
//		printf("Success: RBC_Write() success \n");
		return CH_OK;
	}
}


void ini_file_buf(void)
{

	UINT16 j;

    for(j=0;j!=FAT_FILE_BUF_LEN;j++)
        fat_file_buffer[j]=j;

}/*向FAT文件缓冲区里填入数据*/


void test_enum_file()
{
	Uint32 temp32;
	Uint8 i;
	for( temp32=0; temp32<2000; temp32++ )
    {
        strcpy( fat_cmd.open.buffer, "\\*" );
        /*fat_file_length为枚举序号*/
        fat_file_length = temp32;
        i = fat_fopen( );
        if( i!=F_OK )
        {
            printf("Error: fat_fopen() \n");
            break;
        }
        printf( "\n" );
    }
    printf("success: enum file \n");
}

void test_copy_file(char* src_file, char* dst_file )
{
	Bool    fin;
	UINT8   i;
	UINT32  rs;
	UINT32  temp32;
	UINT32  file_len;

    rs = 0;
    fin = TRUE;
    while( 1 )
    {
        strcpy( fat_cmd.open.buffer, src_file );
        i = fat_fopen( );
        if( i==F_OK )
        {
            if( fin == TRUE )
            {
                fin = FALSE;
                file_len = fat_file_length; 

⌨️ 快捷键说明

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