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

📄 __usb.c

📁 C语言版的USB单片机固件源代码
💻 C
字号:
//===================================================================//
//     Project Name : ZBoard
//      Module Name : Master Firmware Program
//     Product Type : License
//      OS/Dev Tool : AT89C52, uVision 2
//  Original Author : Ray Yang
//     Organization : YBWork.com
//    Original Date : July, 25, 2001
//             Addr : Room 402, No. 131, Meilong No. 9
//              TEL : 86-21-54630718
//            EMail : rayyang2000@yahoo.com
//          Website : http://www.ybwork.com
//		  Copyright : Copyright (L) YBWork.com, 2001
//         Comments : 
//                    
//					<< History >> 
//					July, 25, 2001		The first release
//===================================================================//

#include "ZBoard.H"
#include "USB.H"
#include "USBD12.H"
#include "CHAP9.H"
#include "VDOR.H"
#include "HAL.H"

extern ZBOARDFLAGS bZBoardFlags;

/*
//*************************************************************************
// USB protocol function pointer arrays
//*************************************************************************
*/
code void (*StandardDeviceRequest[])(void) =
{
	USBClassRequest_GetStatus,			// 0
	USBClassRequest_ClearFeature,		// 1
	reserved,							// 2
	USBClassRequest_SetFeature,			// 3
	reserved,							// 4
	USBClassRequest_SetAddress,			// 5
	USBClassRequest_GetDescriptor,		// 6
	reserved,							// 7
	USBClassRequest_GetConfiguration,	// 8
	USBClassRequest_SetConfiguration,	// 9
	USBClassRequest_GetInterface,		// 10
	USBClassRequest_SetInterface,		// 12
	reserved,
	reserved,
	reserved,
	reserved
};

code void (*VendorDeviceRequest[])(void) =
{
	USBVendorRequest_WriteCode,
	USBVendorRequest_ReadCode,
//	USBVendorRequest_Write1SectorCode,
	reserved,
	USBVendorRequest_GetFlashPhase,
	USBVendorRequest_GetCodeData,
	reserved,
	USBVendorRequest_LEDON,
	USBVendorRequest_LEDOFF,
	USBVendorRequest_LEDFlash,
	reserved,
	reserved,
	reserved,
//	USBVendorRequest_ReadWriteRegister,
	reserved,      // Upper...
	reserved,
	reserved,
	reserved
};

/*
//*************************************************************************
//  Public static data
//*************************************************************************
*/
extern unsigned long data lClockTicks;

CONTROL_XFER data ControlData;

#ifdef _DEBUG
code char * _NAME_USB_REQUEST_DIRECTION[] =
{
"Host_to_device",
"Device_to_host"
};

code char * _NAME_USB_REQUEST_RECIPIENT[] =
{
"Device",
"Interface",
"Endpoint(0)",
"Other"
};

code char * _NAME_USB_REQUEST_TYPE[] =
{
"Standard",
"Class",
"Vendor",
"Reserved"
};

code char * _NAME_USB_STANDARD_REQUEST[] =
{
"GET_STATUS",
"CLEAR_FEATURE",
"RESERVED",
"SET_FEATURE",
"RESERVED",
"SET_ADDRESS",
"GET_DESCRIPTOR",
"SET_DESCRIPTOR",
"GET_CONFIGURATION",
"SET_CONFIGURATION",
"GET_INTERFACE",
"SET_INTERFACE",
"SYNC_FRAME"
};

void USB_Help_DevReq(unsigned char nType, unsigned char nReq)
{
	nType >>= 5;

	if(nType == USB_STANDARD_REQUEST)
    {
#ifdef _DEBUG
		printf("Request Type = %s, Request = %s.\n", _NAME_USB_REQUEST_TYPE[nType],
			_NAME_USB_STANDARD_REQUEST[nReq]);
#endif
	}
	else
    {
		if(bZBoardFlags.bits.bVerbose)
		{
#ifdef _DEBUG
			printf("Request Type = %s, bRequest = 0x%bx.\n", _NAME_USB_REQUEST_TYPE[nType],
				nReq);
#endif
		}
	}
}
#endif

/* Functions */
void USB_Suspend_Change(void)
{
}

void USB_Stall_EP0(void)
{
	USBD12_SetEndpointStatus(0, 1);
	USBD12_SetEndpointStatus(1, 1);
}

void USB_Disconnect(void)
{
	EX0 = 0;
	// Initialize D12 configuration
	USBD12_SetMode(USBD12_NOLAZYCLOCK | USBD12_CLOCKRUNNING,
        USBD12_SETTOONE | USBD12_CLOCK_12M);
}

void USB_Connect(void)
{
	// reset event flags
//	DISABLE_INTERRUPTS;
	bZBoardFlags.nValue = 0;
//	ENABLE_INTERRUPTS;

	USBD12_SetEndpointStatus(4, 0);
	USBD12_SetEndpointStatus(5, 0);
	// V2.1 enable normal+sof interrupt
	USBD12_SetDMA(USBD12_ENDP4INTENABLE | USBD12_ENDP5INTENABLE);

	// Initialize D12 configuration
	USBD12_SetMode(USBD12_NOLAZYCLOCK | USBD12_CLOCKRUNNING | USBD12_SOFTCONNECT, USBD12_SETTOONE | USBD12_CLOCK_12M);
	EX0 = 1;
}


void USB_Reconnect(void)
{
	unsigned long lClockCount;
	
	bZBoardFlags.bits.bIN_ISR = 1;

	USB_ClearAllInts();

#ifdef _DEBUG
	printf("Disconnect...\n");
#endif
	USB_Disconnect();

	Delay(0xff);
	ENABLE_INTERRUPTS;
	lClockCount = lClockTicks;
	while(lClockTicks < lClockCount + 20);
	DISABLE_INTERRUPTS;
	
	USB_Connect();

#ifdef _DEBUG
	printf("Connect...\n");
#endif
	bZBoardFlags.bits.bIN_ISR = 0;

}

void USB_Init_Unconfig(void)
{
	USBD12_SetEndpointEnable(0);	/* Disable all endpoints but EPP0. */
}

void USB_Init_Config(void)
{
	USBD12_SetEndpointEnable(1);	/* Enable  generic/iso endpoints. */
}

void USB_Single_Transmit(unsigned char* pBuf, unsigned char nLen)
{
#ifdef _DEBUG
	int i;
#endif
	if(nLen <= EP0_PACKET_SIZE)
    {
#ifdef _DEBUG
		printf("SingleTransmit {");
		for(i=0; i<nLen; i++)
		{
			printf("%bx ", *(pBuf + i));
		}
		printf("}\n");
#endif
		USBD12_WriteEndpoint(1, pBuf, nLen);
	}
}

void USB_Code_Transmit(unsigned char code* pRomData, unsigned short nLen)
{
#ifdef _DEBUG
	int i;
#endif

	ControlData.wCount = 0;
	if(ControlData.wLength > nLen)
		ControlData.wLength = nLen;

	ControlData.pData = pRomData;
	if(ControlData.wLength >= EP0_PACKET_SIZE)
    {

#ifdef _DEBUG
		printf("CodeTransmit {");
		for(i=0; i<EP0_PACKET_SIZE; i++)
		{
			printf("%bx ", *(ControlData.pData + i));
		}
		printf("}\n");
#endif

		USBD12_WriteEndpoint(1, ControlData.pData, EP0_PACKET_SIZE);
		ControlData.wCount += EP0_PACKET_SIZE;

		DISABLE_INTERRUPTS;
		bZBoardFlags.bits.bControl_State = USB_TRANSMIT;
		ENABLE_INTERRUPTS;
	}
	else
    {
#ifdef _DEBUG
		printf("CodeTransmit {");
		for(i=0; i<ControlData.wLength; i++)
		{
			printf("%bx ", *(pRomData + i ));
		}
		printf("}\n");
#endif
		USBD12_WriteEndpoint(1, pRomData, ControlData.wLength);
		ControlData.wCount += ControlData.wLength;
		DISABLE_INTERRUPTS;
		bZBoardFlags.bits.bControl_State = USB_IDLE;
		ENABLE_INTERRUPTS;
	}
}

void USB_Control_Handler()
{
	unsigned char nType, nReq;

	nType = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
	nReq = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;

#ifdef _DEBUG
    USB_Help_DevReq(nType, nReq); // print out device request
#endif

	if (nType == USB_STANDARD_REQUEST)
	{
		(*StandardDeviceRequest[nReq])();
	}
	else if (nType == USB_VENDOR_REQUEST)
		(*VendorDeviceRequest[nReq])();
	else
		USB_Stall_EP0();
}

void USB_ClearAllInts(void)
{
	USBD12_ReadInterruptRegister();
	USBD12_ReadLastTransactionStatus(0);
	USBD12_ReadLastTransactionStatus(1);
	USBD12_ReadLastTransactionStatus(2);
	USBD12_ReadLastTransactionStatus(3);
	USBD12_ReadLastTransactionStatus(4);
	USBD12_ReadLastTransactionStatus(5);
}

⌨️ 快捷键说明

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