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

📄 usb_protocol.c

📁 embedded usb protocol C program
💻 C
字号:
#include "basetype.h"

#include "uart.h"
#include "usb.h"
#include "usblib.h"
#include "usb_dsc.h"
#include "targetcfg.h"
#include "regio.h"
#include "intc.h"
#include "clk.h"
#include "timer.h"
#include "TIMER_REG.H"

extern void dld_Ep1TxHandler(void);

__align(4) volatile USB_Control_Block	UsbCb;

volatile int 	isUsbdSetConfiguration;

extern BOOL bReply;

/*=============================================================================

                         M A C R O S

=============================================================================*/

#include "usb_samsung_bulk.c"

volatile usb_dev_descriptor_type 				*pUsbDevDesc;
volatile usb_dev_configuration_descriptor_type 	*pUsbConfigDesc;

#define MAXSTRING	3
#define USB_MAX_DESCRIPTOR_STRING_NUM		0x03

volatile usb_dev_string_type						*pUsbDescString[MAXSTRING];

/*=============================================================================

                  G L O B A L     V A R I A B L E S

=============================================================================*/

/*=============================================================================

       E X T E R N A L     F U N C T I O N S  
         
=============================================================================*/


/*=============================================================================

       I N T E R N A L     F U N C T I O N S  

=============================================================================*/
static void dld_MultiPacketTx(U8  * pRomData, unsigned short len);

/*=============================================================================

FUNCTION     dld_UsbDescInit

DESCRIPTION	
  
PARAMETERS
	None

RETURN VALUE
  	None

=============================================================================*/
void dld_UsbDescInit(void)
{
	int i;

#if 1	
	pUsbDevDesc 	= (usb_dev_descriptor_type*)&UsbModemDevDesc;
	pUsbConfigDesc	= (usb_dev_configuration_descriptor_type*)&UsbModemConfigDesc;
#else
	pUsbDevDesc		= (usb_dev_descriptor_type*)&UsbMsDevDesc;
	pUsbConfigDesc	= (usb_dev_configuration_descriptor_type*)&UsbMsConfigDesc;	
#endif	
	for(i=0; i<MAXSTRING; i++)
		pUsbDescString[i] = (usb_dev_string_type*)&UsbModemDevString[i];
}

void CdcSendNotification(void)
{	
	usb_cdc_acm_notification_type NotiMsg;

	NotiMsg.bmRequestType = USBCDC_REQUEST_TYPE_SERIAL_STATE;
	NotiMsg.bNotification = NOTIFICATION_SERIAL_STATE;
	NotiMsg.wValue		  = 0x0000;
	NotiMsg.wIndex		  = 0x0000;
	NotiMsg.wLength 	  = SERIAL_STATE_DATA_LENGTH;
	NotiMsg.wSerialState  = SERIAL_STATE_ALL_OFF;
	NotiMsg.wSerialState |= SERIAL_STATE_DSR_ON;
}
/*=============================================================================

FUNCTION     dld_ClearFeature

DESCRIPTION	
  
PARAMETERS
	None

RETURN VALUE
  	None

=============================================================================*/
static void dld_ClearFeature(void)
{
	switch (UsbCb.sControl.setup.bmRequestType&USB_REQ_RECIPIENT_MASK)
	{
		case USB_REQ_RECIPIENT_ENDPOINT:
			mb_EDnCtlSTLClr(UsbCb.sControl.setup.wIndex&0x000f);	
		
			switch (UsbCb.sControl.setup.wIndex&0x000f)
			{
				case 0:
					break;
					
				case 1:
					CdcSendNotification(); 
					break;
					
				case 2:
					break;
					
				case 3:
					break;
				
				default:
				  break;
			}
			break;
			
		default: 
			break;
	}
}


/*=============================================================================

FUNCTION     dld_GetDescriptor

DESCRIPTION	
  
PARAMETERS
	None

RETURN VALUE
  	None

=============================================================================*/
static void dld_GetDescriptor(void)
{
	U8 desc_str;
	U8 desc_type = UsbCb.sControl.setup.wValue.descriptor.type;
	
	switch (desc_type)
	{
		case USB_DSC_DEVICE:
			dld_MultiPacketTx((U8 *)pUsbDevDesc, pUsbDevDesc->bLength);
			break;
			
		case USB_DSC_CONFIGURATION:
			dld_MultiPacketTx((U8 *)pUsbConfigDesc, pUsbConfigDesc->wTotalLength); 
			break;
		
		case USB_DSC_STRING:
			desc_str = UsbCb.sControl.setup.wValue.descriptor.index;
			if (desc_str < USB_MAX_DESCRIPTOR_STRING_NUM)
				dld_MultiPacketTx((U8*)pUsbDescString[desc_str], pUsbDescString[desc_str]->bLength);
			break;
	    default:
	   		break;
	}
}
/*=============================================================================

FUNCTION     dld_SetConfiguration

DESCRIPTION	
  
PARAMETERS
	None

RETURN VALUE
  	None

=============================================================================*/
static void dld_SetConfiguration(void)
{
	switch( UsbCb.sControl.setup.wValue.value&0x00ff)
	{
		case 0:

			mb_StsCFGClr();
			break;
			
		case 1:
	   		vUsbFIFO_EPxCfg_FS(); 
	   		mb_StsCFGSet();
			isUsbdSetConfiguration = 1;
			//uprintf("configured\n");
			break;
			
		default: 
	   		break;
	}
}

/*=============================================================================

FUNCTION     dld_MultiPacketTx

DESCRIPTION	
  
PARAMETERS
	None

RETURN VALUE
  	None

=============================================================================*/
static void dld_MultiPacketTx(U8  * pRomData, unsigned short len)
{
	UsbCb.sControl.wCount = 0;
	
	if(UsbCb.sControl.wLength > len)
		UsbCb.sControl.wLength = len;
		
	UsbCb.sControl.pData = pRomData;

	dld_Ep1TxHandler();
}

/*=============================================================================

FUNCTION     dld_UsbStandardReq

DESCRIPTION	
  
PARAMETERS
	None

RETURN VALUE
  	None

=============================================================================*/
void dld_UsbStandardReq(void)
{
	//uprintf("%x",UsbCb.sControl.setup.bRequest);
	switch(UsbCb.sControl.setup.bRequest)
	{
 		case USB_CLEAR_FEATURE:
			dld_ClearFeature();
			break;
			  
		case USB_SET_ADDRESS:
			mb_StsDEVADDRSet(UsbCb.sControl.setup.wValue.value);
			break;
			
		case USB_GET_DESCRIPTOR :
			dld_GetDescriptor();
			break;
			
		case USB_SET_CONFIGURATION :
			dld_SetConfiguration();
			break;

		case USB_SYNCH_FRAME :

			break;
			
		default : 

	   		break;			  
	}
}

⌨️ 快捷键说明

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