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

📄 d12_init.c

📁 LPC2294 测试程序 YL_LPC229X_Test_Data的目录说明
💻 C
字号:
#include "def.h"
#include "config.h"
#include "board.h"
#include "utils.h"
#include "D12Def.h" 

#define	STATUS_ERR	1
#define	STATUS_OK	0

static U8 usb_remote_wake;
static U8 usb_config;
static U8 UsbSendDescF;

static int send_remain;
static U32 send_ptr;

U8 UsbInit()
{
	UsbSendDescF = 0;

	if ( D12RdChipId() != 0x1210 )
		return STATUS_ERR;

	D12SetDma( MyD12DmaCfg );
	if ( D12GetDma() != MyD12DmaCfg )
		return STATUS_ERR;

	D12SetMode( MyD12EpCfgOff , D12Pll24M );
	Delay( 100 );
	D12SetMode( MyD12EpCfgOn , D12Pll24M );

	return STATUS_OK;
}

///////////////////////////////

#define	UsbDevDescType	1
#define	UsbCfgDescType	2
#define	UsbStrDescType	3
#define	UsbItfDescType	4
#define	UsbEndpDescType	5
#define	UsbPwrDescType	6

#define	UsbDevDescSize	18
#define	UsbCfgDescSize	9
#define	UsbItfDescSize	9
#define	UsbEndpDescSize	7

#define	UsbEndpControl		0
#define	UsbEndpIsochronous	1
#define	UsbEndpBulk			2
#define	UsbEndpInterrupt	3	

////////////////////////////////////////

#define	UsbClassMassStorage	8

#define	UsbSubClassRbc		1
#define	UsbSubClassSff8020i	2
#define	UsbSubClassQic157	3
#define	UsbSubClassUfi		4
#define	UsbSubClassSff8070i	5
#define	UsbSubClassScsi		6

#define	UsbProtocolCbi0	0
#define	UsbProtocolCbi1	1
#define	UsbProtocolBulk	0x50
////////////////////////////////////////

#define	Ep0PacketSize	16
#define	Ep2PacketSize	64
#define	EndpCnt	2
#define	UsbTotalCfgDescSize	UsbCfgDescSize+UsbItfDescSize+EndpCnt*UsbEndpDescSize
static __packed struct
{
	UsbDevDesc DevDesc;
	U8 CfgDesc[9];
	U8 ItfDesc[9];
	U8 Endp4Desc[7];
	U8 Endp5Desc[7];
}ThisDevDesc =
{
	{
	UsbDevDescSize,
	UsbDevDescType,
	0x110,
	0, 0, 0,
	Ep0PacketSize,
	0x471,
	0x222,
	0x113,
	0, 0, 0,
	1
	}, {
	UsbCfgDescSize,
	UsbCfgDescType,
	UsbTotalCfgDescSize, 0,
	1,
	1,
	0,
	0x80,
	0x32
	}, {
	UsbItfDescSize,
	UsbItfDescType,
	0,
	0,
	EndpCnt,
	UsbClassMassStorage,
	UsbSubClassUfi,
	UsbProtocolBulk,
	0
	}, {
	UsbEndpDescSize,
	UsbEndpDescType,
	0x82,
	UsbEndpBulk,
	Ep2PacketSize,
	0,
	0
	}, {
	UsbEndpDescSize,
	UsbEndpDescType,
	0x2,
	UsbEndpBulk,
	Ep2PacketSize,
	0,
	0
	}
};

/****************** standard device request ****************/
U8 UsbGetStatus( U8* SetupPkt )
{
	if ( SetupPkt[2] | SetupPkt[3] | SetupPkt[5] | SetupPkt[7] )
		return 1;
	if ( SetupPkt[6] != 2 )
		return 1;
	if ( SetupPkt[0] == 0x80 )
	{
		if ( SetupPkt[4] )
			return 1;
		SetupPkt[0] = usb_remote_wake ? 1 : 0;
		SetupPkt[1] = 0;
	}
	else if ( SetupPkt[0] == 0x81 )
	{
		if ( SetupPkt[4] )
			return 1;
		SetupPkt[0] = 0;
		SetupPkt[1] = 0;
	}
	else if ( SetupPkt[0] == 0x82 )
	{
		D12SelEp( ( SetupPkt[4] * 2 ) | ( ( SetupPkt[4] & 0x80 ) ? 1 : 0 ) );
		SetupPkt[1] = ( D12RdDat() & 2 ) ? 1 : 0;
		SetupPkt[0] = 0;
	}
	else
		return 1;

	D12WrEp( 1 , SetupPkt , 2 );
	return 0;
}

U8 UsbClrFeature( U8* SetupPkt )
{
	if ( SetupPkt[0] & 0x80 )
		return STATUS_ERR;
	if ( SetupPkt[3] | SetupPkt[5] | SetupPkt[6] | SetupPkt[7] )
		return STATUS_ERR;

	if ( SetupPkt[0] & 0x1f )
	{
		if ( ( SetupPkt[0] & 0x1f ) != 2 )
			return STATUS_ERR;
		D12SetEpStat( ( SetupPkt[4] * 2 ) |
			( ( SetupPkt[4] & 0x80 ) ? 1 : 0 ) ,
			0 );
	}
	else
	{
		if ( SetupPkt[4] )
			return STATUS_ERR;
		usb_remote_wake = 0;
	}

	D12WrEp( 1 , SetupPkt , 0 );
	return STATUS_OK;
}

U8 UsbSetFeature( U8* SetupPkt )
{
	if ( !( SetupPkt[0] & 0x80 ) )
		return STATUS_ERR;
	if ( SetupPkt[3] | SetupPkt[5] | SetupPkt[6] | SetupPkt[7] )
		return STATUS_ERR;

	if ( SetupPkt[0] & 0x1f )
	{
		if ( ( SetupPkt[0] & 0x1f ) != 2 )
			return STATUS_ERR;
		D12SetEpStat( ( SetupPkt[4] * 2 ) |
			( ( SetupPkt[4] & 0x80 ) ? 1 : 0 ) ,
			1 );
	}
	else
	{
		if ( SetupPkt[4] )
			return STATUS_ERR;
		usb_remote_wake = 1;
	}

	D12WrEp( 1 , SetupPkt , 0 );
	return STATUS_OK;
}

U8 UsbSetAddress( U8* SetupPkt )
{
	if ( SetupPkt[0] |
		SetupPkt[3] |
		SetupPkt[4] |
		SetupPkt[5] |
		SetupPkt[6] |
		SetupPkt[7] )
		return STATUS_ERR;

	D12SetAddr( SetupPkt[2] | 0x80 );
	D12WrEp( 1 , SetupPkt , 0 );
	return STATUS_OK;
}

void UsbSendDesc()
{
	//	U8 i;

	UsbSendDescF = send_remain >= Ep0PacketSize;	
	D12WrEp( 1 , ( U8 * ) send_ptr ,
		( send_remain > Ep0PacketSize ) ? Ep0PacketSize : send_remain );

	//	putch('@');
	//	for(i=0; i<((send_remain>Ep0PacketSize)?Ep0PacketSize:send_remain); i++)
	//		printf("%x,", *(U8 *)(send_ptr+i));	

	send_remain -= Ep0PacketSize;
	send_ptr += Ep0PacketSize;
}

U8 UsbGetDescriptor( U8* SetupPkt )
{
	if ( SetupPkt[0] != 0x80 )
		return STATUS_ERR;

	switch ( SetupPkt[3] )
	{
		case UsbDevDescType:
			send_ptr = ( U32 ) & ThisDevDesc;
			send_remain = UsbDevDescSize;
			break;
		case UsbCfgDescType:
			send_ptr = ( U32 ) & ThisDevDesc.CfgDesc;
			send_remain = UsbTotalCfgDescSize;
			break;
		case UsbStrDescType:
			return STATUS_ERR;	
		default:
			return STATUS_ERR;
	}

	if ( !SetupPkt[7] && ( SetupPkt[6] < send_remain ) )
		send_remain = SetupPkt[6];

	UsbSendDesc();	
	return STATUS_OK;
}

U8 UsbGetConfiguration( U8* SetupPkt )
{
	if ( SetupPkt[0] != 0x80 )
		return STATUS_ERR;
	if ( SetupPkt[2] | SetupPkt[3] | SetupPkt[4] | SetupPkt[5] | SetupPkt[7] )
		return STATUS_ERR;
	if ( SetupPkt[6] != 1 )
		return STATUS_ERR;

	SetupPkt[0] = usb_config ? 1 : 0;
	D12WrEp( 1 , SetupPkt , 1 );	
	return STATUS_OK;
}

U8 UsbSetConfiguration( U8* SetupPkt )
{
	if ( SetupPkt[0] |
		SetupPkt[3] |
		SetupPkt[4] |
		SetupPkt[5] |
		SetupPkt[6] |
		SetupPkt[7] )
		return STATUS_ERR;
	if ( SetupPkt[2] & 0xfe )
		return STATUS_ERR;

	usb_config = SetupPkt[2] & 1;
	D12SetEp( usb_config );
	D12SetEpStat( 4 , usb_config ^ 1 );
	D12SetEpStat( 5 , usb_config ^ 1 );
	D12WrEp( 1 , SetupPkt , 0 );
	printf( "Device attached!\n" );
	return STATUS_OK;
}

U8 UsbGetInterface( U8* SetupPkt )
{
	if ( SetupPkt[0] != 0x81 )
		return STATUS_ERR;
	if ( SetupPkt[2] | SetupPkt[3] | SetupPkt[4] | SetupPkt[5] | SetupPkt[7] )
		return STATUS_ERR;
	if ( SetupPkt[6] != 1 )
		return STATUS_ERR;

	SetupPkt[0] = 0;
	D12WrEp( 1 , SetupPkt , 1 );
	return STATUS_OK;
}

U8 UsbSetInterface( U8* SetupPkt )
{
	return STATUS_ERR;
}

U8 UsbReserved( U8* SetupPkt )
{
	return STATUS_ERR;
}

U8 ( *StdDevReq[] )( U8* SetupPkt ) = {
UsbGetStatus,
UsbClrFeature,
UsbReserved,
UsbSetFeature,
UsbReserved,
UsbSetAddress,
UsbGetDescriptor,
UsbReserved,
UsbGetConfiguration,
UsbSetConfiguration,
UsbGetInterface,
UsbSetInterface,
UsbReserved,
UsbReserved,
UsbReserved,
UsbReserved
}; 

/********************** D12 interrupt process ******************/
void D12Ep0IntProc()
{
	U8 SetupPkt[8];

	UsbSendDescF = 0;

	D12RdLastTrStat( 0 );
	if ( D12RdCmd() & 0x20 )
		if ( D12RdEp( 0 , SetupPkt , 8 ) == 8 )
		{
			D12AckEp( 0 );
			D12AckEp( 1 );

			/*{
						int i;
				
						putch('\n');
						for(i=0; i<8; i++)
							printf("%x,", SetupPkt[i]);
					}*/

			if ( !StdDevReq[SetupPkt[1] & 0xf]( SetupPkt ) )
				;
			return;
		}

	D12SetEpStat( 0 , D12EpStall );
	D12SetEpStat( 1 , D12EpStall );
}

void D12Ep1IntProc()
{
	//U8 i;

	//i = D12RdLastTrStat(1);
	D12RdLastTrStat( 1 );
	if ( UsbSendDescF )
		UsbSendDesc();
}

void D12Ep2IntProc()
{
	D12SetEpStat( 2 , D12EpStall );
}

void D12Ep3IntProc()
{
	D12SetEpStat( 3 , D12EpStall );
}

void D12Ep4IntProc()
{
	U8 size;
	U8 Ep2OutBuf[Ep2PacketSize];

	D12RdLastTrStat( 4 );	
	size = D12RdEp( 4 , Ep2OutBuf , Ep2PacketSize );
	if ( ( size == 6 ) && ( *( U32 * ) Ep2OutBuf == 0x12345678 ) )
	{
		
		//printf("%x\n", Ep2OutBuf[4]+(Ep2OutBuf[5]<<8));
		//printf("%x\n", Ep2OutBuf[4]);
		//LedSet( Ep2OutBuf[4] & 0x1 );
		//LedSet( Ep2OutBuf[4] & 0x2 );
		
		LedSet( Ep2OutBuf[4]);
		//LedSet( Ep2OutBuf[4] & 0x8 );
	}
}

void D12Ep5IntProc()
{
}

void D12BusRstProc()
{
}

void D12SuspChgProc()
{
}

⌨️ 快捷键说明

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