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

📄 main.c

📁 HardDiskUSBconnectsourcecode
💻 C
字号:
/**************************************************************************
 *                         H E A D E R   F I L E S
 *************************************************************************/
#define  GLOBAL_H
#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
#include "AT89C51SND.h"
#include "typedef.h"
#include "config.h"
#include "constant.h"
#include "global.h"
#include "gpio.h"
#include "macro.h"
#include "external.h"
/**************************************************************************
 *                           C O N S T A N T S
 *************************************************************************/

/**************************************************************************
 *                              M A C R O S
 *************************************************************************/

/**************************************************************************
 *                          D A T A    T Y P E S
 *************************************************************************/

/**************************************************************************
 *                         G L O B A L    D A T A
 *************************************************************************/


UINT8 COUNT=0;
/**************************************************************************
 *  Function Name:
 *
 *  Descriptions:
 *
 *  Arguments:
 *
 *  Returns: None
 **************************************************************************/
void main(void){

	UINT8 status;
	P1_0=1;
	MCU_Init();
	usb_init();
	EA=1;
	printf(" program start\n");
	stInit(1);
	printf("hd_initial_ok\n");
	while(1)
	{
	
		usb_task();
		if(GetCBW()) continue;
		status=ufiCommandDecoder();
		SendCSW(status);
		gState=0;

	}//while

}

/**************************************************************************
 *  Function Name:
 *
 *  Descriptions:
 *
 *  Arguments:
 *
 *  Returns: None
 **************************************************************************/
UINT8 GetCBW(void)
{
	UINT8 i;
	union {
		UINT32 u32value;
		UINT8  u8byte[4];
	} s;

	Usb_sel_ep(2);
	if(Usb_receive_out_data())
	{

		//dCBWSignature 43425355
		s.u8byte[3]=Usb_read_byte();
		s.u8byte[2]=Usb_read_byte();
		s.u8byte[1]=Usb_read_byte();
		s.u8byte[0]=Usb_read_byte();
			if ( (s.u8byte[3]!=0x55)||(s.u8byte[2]!=0x53)||(s.u8byte[1]!=0x42)||(s.u8byte[0]!=0x43) )
		{

			return 1;
		}

		//dCBWTag
		s.u8byte[3]=Usb_read_byte();
		s.u8byte[2]=Usb_read_byte();
		s.u8byte[1]=Usb_read_byte();
		s.u8byte[0]=Usb_read_byte();
		gCBWTag=s.u32value;

		//dCBWDataTransferLength
		s.u8byte[3]=Usb_read_byte();
		s.u8byte[2]=Usb_read_byte();
		s.u8byte[1]=Usb_read_byte();
		s.u8byte[0]=Usb_read_byte();

		gCBWDataLen=s.u32value;
		gOrgCBWDataLen=gCBWDataLen;

		//bCBWFlag(ignore)
		gCBWFlags=(Usb_read_byte())&0x80;

		//bCBWLUN
		gCmdBlock.CB[1]=Usb_read_byte()& 0x0F;      //(J)get LUN from CBW

		//bCBWCBLength(ignore)
		i=Usb_read_byte();
		//CBWCB
			for (i=0;i<12;i++)
			gCmdBlock.CB[i]=Usb_read_byte();
		//printf("cbw:%#2x:%#2x:%#2x:%#2x:%#2x:%#2x:%#2x\n",gCmdBlock.CB[0],gCmdBlock.CB[1],gCmdBlock.CB[2],gCmdBlock.CB[3],gCmdBlock.CB[4],gCmdBlock.CB[5],gCmdBlock.CB[6]);
		Usb_clear_RXOUT_PP();                       // usb read acknowledgement
		gActiveCard=gCmdBlock.CB1.LUN;          //(J)give active LUN
		return 0;
	}
	else    return 1;

}

/**************************************************************************
 *  Function Name:
 *
 *  Descriptions:
 *
 *  Arguments:
 *
 *  Returns: None
 **************************************************************************/

void SendCSW(UINT8 u8Status)
{
	union {
		UINT32 u32value;
		UINT8  u8byte[4];
	} s;

	Usb_sel_ep(1);
		//dCSWSignature
	Usb_write_byte(0x55);
	Usb_write_byte(0x53);
	Usb_write_byte(0x42);
	Usb_write_byte(0x53);

	//dCSWTag
	s.u32value=gCBWTag;
	UEPDATX=s.u8byte[3];
	UEPDATX=s.u8byte[2];
	UEPDATX=s.u8byte[1];
	UEPDATX=s.u8byte[0];
	//dCSWDataResidue
	s.u32value=gCBWDataLen;
	UEPDATX=s.u8byte[3];
	UEPDATX=s.u8byte[2];
	UEPDATX=s.u8byte[1];
	UEPDATX=s.u8byte[0];
	//bCSWStatus
	UEPDATX=u8Status;
		Usb_set_TXRDY_bit();
	while (!(Usb_tx_complete_bit()));
	Usb_clear_TXCMPL_bit();
	//printf("u8Status:%#x\n",u8Status);

}

				

⌨️ 快捷键说明

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