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

📄 msd-b2.c

📁 PIC18F4550 SD CARD USB
💻 C
📖 第 1 页 / 共 2 页
字号:
	MSD_BD_IN.ADR=(byte*)&msd_csw;
	MSD_BD_IN.Cnt=0x0;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	return;
	*/
	
	msd_buffer[0]=0x01;		// No of LBAs 246,016 from SD Card manual Page 3-24
	msd_buffer[1]=0x02;
	msd_buffer[2]=0x03;
	msd_buffer[3]=0x04;
	msd_buffer[4]=0x05;		// Block size 512 bytes = 200h
	msd_buffer[5]=0x06;
	msd_buffer[6]=0x07;
	msd_buffer[7]=0x08;
	msd_buffer[8]=0x09;		// Block size 512 bytes = 200h
	msd_buffer[9]=0x10;
	msd_buffer[10]=0x11;
	msd_buffer[11]=0x12;
	while(mMSDTxIsBusy());
    MSD_BD_IN.ADR=(byte*)&msd_buffer[0];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	while(mMSDTxIsBusy());
	MSD_BD_IN.ADR=(byte*)&msd_buffer[64];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	while(mMSDTxIsBusy());
	MSD_BD_IN.ADR=(byte*)&msd_buffer[128];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	while(mMSDTxIsBusy());
	MSD_BD_IN.ADR=(byte*)&msd_buffer[192];
	MSD_BD_IN.Cnt=0x3c;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	MSDSuccessCSW(0x00);
}

void MSDInquiryHandler(void)
{
	byte i;
	byte *buffer;
	memcpypgm2ram((byte *)&msd_buffer[0],(byte *)&inq_resp,sizeof(InquiryResponse));
	MSD_Data=sizeof(InquiryResponse);
	
	while(mMSDTxIsBusy());
	MSD_BD_IN.ADR=(byte*)&msd_buffer[0];
	MSD_BD_IN.Cnt=0x24;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	MSDSuccessCSW(msd_cbw.dCBWDataTransferLength-0x24);

}

void MSDReadCapacityHandler()
{
	dword one=0x1, C_size=0x0, C_mult=0x0, Mult=0x0,  C_size_U=0x0, C_size_H=0x0, C_size_L=0x0, C_mult_H=0x0, C_mult_L=0x0;
	dword C_Read_Bl_Len;
	DWORD NumBLKS=0x00,BLKLen=0x00;
	// CSD *csd;
	// SDC_Error status;
	// byte C_mult;
	//status=CSDread((byte*)&msd_buffer);
	//csd=(byte*)&gblCSDReg;
	C_Read_Bl_Len=gblCSDReg._byte[5]&0x0f;
	BLKLen._dword=one<<C_Read_Bl_Len;
	
	C_size_U=gblCSDReg._byte[6]&0x03;			// 2 LSB bits
	C_size_H=gblCSDReg._byte[7];		
	C_size_L=(gblCSDReg._byte[8]&0xC0)>>6;			// 2 MSB, right shift by 6places to get in LSB
	C_size=(C_size_U<<10)|(C_size_H<<2)|(C_size_L);
	C_mult_H=gblCSDReg._byte[9]&0x03;
	C_mult_L=(gblCSDReg._byte[10]&0x80)>>7;
	C_mult=(C_mult_H<<1)|C_mult_L;
	Mult = one<<(C_mult+2);
	NumBLKS._dword=Mult*(C_size+1);

	msd_buffer[0]=NumBLKS.v[3];
	msd_buffer[1]=NumBLKS.v[2];
	msd_buffer[2]=NumBLKS.v[1];
	msd_buffer[3]=NumBLKS.v[0];
	msd_buffer[4]=BLKLen.v[3];
	msd_buffer[5]=BLKLen.v[2];
	msd_buffer[6]=BLKLen.v[1];
	msd_buffer[7]=BLKLen.v[0];
	
//	/*
//	SDC_Error status;
//	PT_FAT Pt;
//	dword LastLBA, TotSec;
//	byte *p;
////	dword TotSec, BlkSize;
//	status=RMediaInitialize(&gblFlag);
//	//status =SECTORread(0L, (byte*)&msd_buffer[0]);
//	if(status==sdcValid) {								// If we are not even able to read from SDCard then terminate
//		Pt=(PT_FAT)&msd_buffer[0];
//		if((Pt->Signature0!=0x55)||(Pt->Signature1!=0xAA))
//			msd_csw.bCSWStatus=0x01;
//		TotSec=Pt->Partition0.PTE_NumSect;				// even in case of error i.e. the data read is not partition table
//														// some bytes are taken as TotSec, we will take care of this in data stage
//														// in data stage since status !=0x0, we will send zeros to Host
//		LastLBA=TotSec+Pt->Partition0.PTE_FrstSect-1;   // see the example in circuit cellar, june 2002, p73
//		p=(byte*)&LastLBA;
//		msd_buffer[0]=*p++;
//		msd_buffer[1]=*p++;
//		msd_buffer[2]=*p++;
//		msd_buffer[3]=*p++;
//		msd_buffer[4]=0x00;
//		msd_buffer[5]=0x00;
//		msd_buffer[6]=0x02;
//		msd_buffer[7]=0x00;
//		//memcpy((byte*)&msd_buffer[0],&TotSec,4);	
//		//BlkSize=BLOCKLEN_512;
//		//memcpy((byte*)&msd_buffer[4],&BlkSize,4);
//	}
//	
//	msd_buffer[0]=0x00;
//	msd_buffer[1]=0x03;
//	msd_buffer[2]=0xc0;
//	msd_buffer[3]=0xff;
//	msd_buffer[4]=0x00;
//	msd_buffer[5]=0x00;
//	msd_buffer[6]=0x02;
//	msd_buffer[7]=0x00;
//	*/
	
	while(mMSDTxIsBusy());
	MSD_BD_IN.ADR=(byte*)&msd_buffer[0];
	MSD_BD_IN.Cnt=0x08;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	MSDSuccessCSW(0x00);
		while(1) {	mLED_3_On();}
}

void MSDReadHandler()
{
	word i;
	SDC_Error status;
	//status=RMediaInitialize(&gblFlag);						// media initialize reads the first sector in msd_buffer
	//if (status) {mLED_1_On();mLED_2_On();mLED_3_On();mLED_4_On(); return;}	
	status =SECTORread(0L, (byte*)&msd_buffer[0]);
	//if (status) {SECTORread(0L, (byte*)&msd_buffer[0]);}
	//if(status)
	//	while(1){mLED_1_On();mLED_2_On();mLED_3_On();mLED_4_On();}
    while(mMSDTxIsBusy());
    MSD_BD_IN.ADR=(byte*)&msd_buffer[0];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	while(mMSDTxIsBusy());
	MSD_BD_IN.ADR=(byte*)&msd_buffer[64];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	while(mMSDTxIsBusy());
	MSD_BD_IN.ADR=(byte*)&msd_buffer[128];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	while(mMSDTxIsBusy());
	MSD_BD_IN.ADR=(byte*)&msd_buffer[192];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	while(mMSDTxIsBusy());
    MSD_BD_IN.ADR=(byte*)&msd_buffer[256];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	while(mMSDTxIsBusy());
    MSD_BD_IN.ADR=(byte*)&msd_buffer[320];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	while(mMSDTxIsBusy());
    MSD_BD_IN.ADR=(byte*)&msd_buffer[384];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	while(mMSDTxIsBusy());
    MSD_BD_IN.ADR=(byte*)&msd_buffer[448];
	MSD_BD_IN.Cnt=0x40;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
	MSDSuccessCSW(0x00);
	mLED_4_On();
}
void MSDSuccessCSW(dword residue)
{	
	//mLED_Both_On();
	//msd_cbw_csw.dCSWTag=msd_cbw_csw.dCBWTag;
	//msd_cbw_csw.dCSWSignature._dword=0x53425355;
	//msd_cbw_csw.dCSWDataResidue._dword = 0x0;
	//msd_cbw_csw.bCSWStatus._byte=0x0;
	msd_csw.dCSWTag=msd_cbw.dCBWTag;
	msd_csw.dCSWSignature=0x53425355;
	msd_csw.dCSWDataResidue = residue;
	msd_csw.bCSWStatus=0x0;
	while(mMSDTxIsBusy());
	MSD_BD_IN.ADR=(byte*)&msd_csw;
	MSD_BD_IN.Cnt=0x0d;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
}

void MSDErrorCSW(void)
{	
	//mLED_1_On();
	msd_csw.dCSWTag=msd_cbw.dCBWTag;
	msd_csw.dCSWSignature=0x53425355;
	msd_csw.dCSWDataResidue=msd_cbw.dCBWDataTransferLength;
	msd_csw.bCSWStatus=0x1;
	while(mMSDTxIsBusy());
	MSD_BD_IN.ADR=(byte*)&msd_csw;
	MSD_BD_IN.Cnt=0x0d;
	mUSBBufferReady(MSD_BD_IN);
	USBDriverService();
}
	
void MSDInHandler(void)
{
	return;	
}

void MSDTxPkt(char *buffer, byte len)
{
	byte i;
    /*
     * Value of len should be equal to or smaller than MSD_INT_IN_EP_SIZE.
     * This check forces the value of len to meet the precondition.
     */
	if(len > MSD_IN_EP_SIZE)
	    len = MSD_IN_EP_SIZE;

   /*
    * Copy data from user's buffer to dual-ram buffer
    */
    for (i = 0; i < len; i++)
    	msd_buffer[i] = buffer[i];
	MSD_BD_IN.ADR=(byte*)&msd_buffer;
    MSD_BD_IN.Cnt = len;
    mUSBBufferReady(MSD_BD_IN);

}//end MSDTxPkt

void MSDInitEP(void)
{   
	mInitAllLEDs();
    MSD_UEP = EP_OUT_IN|HSHK_EN;                // Enable 2 data pipes
    MSD_BD_OUT.Cnt=sizeof(msd_cbw);
    MSD_BD_OUT.ADR=(byte*)&msd_cbw;
    MSD_BD_OUT.Stat._byte = _USIE|_DAT0|_DTSEN;	//usbmmap.h owner SIE, DAT0 expected next, data toggle sunc enable
   
    /*
     * Do not have to init Cnt of IN pipes here.
     * Reason:  Number of bytes to send to the host
     *          varies from one transaction to
     *          another. Cnt should equal the exact
     *          number of bytes to transmit for
     *          a given IN transaction.
     *          This number of bytes will only
     *          be known right before the data is
     *          sent.
     */
    MSD_BD_IN.ADR = (byte*)&msd_buffer[0];      // Set buffer address    
    MSD_BD_IN.Stat._byte = _UCPU|_DAT1;   // Set status CPU owns Data1 expected next
    MSD_State=MSD_WAIT;
    
}//end MSDInitEP

⌨️ 快捷键说明

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