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

📄 chap_9.c

📁 LPC2148 USB固件程序,是学习固件不可多的的范例.
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************Copyright (c)**************************************************
**                               Guangzou ZLG-MCU Development Co.,LTD.
**                                      graduate school
**                                 http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name:			Chap_9.c
** Last modified Date:	2005-8-6
** Last Version:		V1.0
** Descriptions:		chap_9.c, 实现USB1.1协议
**						chap_9.c, realize USB1.1 protocol
**------------------------------------------------------------------------------------------------------
** Created by:			郑明远 		MingYuan Zheng
** Created date:		2005-8-6
** Version:				V1.0
** Descriptions:		初始版本	The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:			
** Modified date:
** Version:				
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by: 
** Modified date:
** Version:	
** Descriptions: 
**
********************************************************************************************************/

#include "config.h"
#include "USBConfig.h"

#include "USBCI.h"
#include "USBDriver.h"
#include "Chap_9.h"
#include "Descriptor.h"

/* define control transfer structure variable */
CONTROL_XFER ControlData;								/* 定义传输控制结构变量 */

/*************************************************************************
** 函数名称:  USB 标准设备请求入口地址指针表
** Function:  Entry address of USB standard device request Function 
**************************************************************************/
void (*StandardDeviceRequest[])(void) =
{
	get_status,
	clear_feature,
	reserved,
	set_feature,
	reserved,
	set_address,
	get_descriptor,
	reserved,
	get_configuration,
	set_configuration,
	get_interface,
	set_interface,
	reserved,
	reserved,
	reserved,
	reserved
};

   //*************************************************************************
   //		 		USB 协议层函数       USB Protocal Function 
   //*************************************************************************

/*************************************************************************************************
** 函数名称: stall_ep0()							Name:  	  stall_ep0()	
** 功能描述: 使控制端点处于停止状态					Function: stall logical endpoint 0
*************************************************************************************************/   
void stall_ep0(void)
{
	USB_SetEndpointStatus(0, 1);
	USB_SetEndpointStatus(1, 1);
}
   
/*************************************************************************************************
** 函数名称: reserved()								Name:  	  reserved()
** 功能描述: 保留子程序                        		Function: reserved function
*************************************************************************************************/
void reserved(void)
{
	stall_ep0();
}

/*************************************************************************************************
** 函数名称: init_unconfig()						Name:  	  init_unconfig()
** 功能描述: 进入地址状态,禁止 0 除外的所有端点		Function: disable all endpoints except for control endpoint 0
*************************************************************************************************/
void init_unconfig(void)
{
	USB_SetEndpointEnable(0);
}

/*************************************************************************************************
** 函数名称: init_config()							Name:  	 init_config()
** 功能描述: 配置处理,允许端点收发					Function: enable all endpoints		
*************************************************************************************************/
void init_config(void)
{
	USB_SetEndpointEnable(1);
}

/*************************************************************************************************
** 函数名称: single_transmit()						Name:  	  single_transmit()
** 功能描述: 通过物理端点 1 发送数据				Function: send data by physical endpoint 1
** 输	 入: INT8U * buf:	发送数据指针			Input:	  INT8U * buf:  send buffer		
			 INT8U len:		发送数据长度					  INT8U len:	send data length
** 输	 出: 无										Output:	  NULL
*************************************************************************************************/
void single_transmit(INT8U * buf, INT8U len)
{
	/* the len must below the maxpacket size of physical endpoint 1 */	
	if( len <= EP0_PACKET_SIZE)
	{												/* len 必须小于端点的最大包信息长度 */
		USB_WriteEndpoint(1, len, buf);
	}
}

/*************************************************************************************************
** 函数名称: code_transmit()						Name:  	   code_transmit()
** 功能描述: 通过物理端点 1 发送数据				Function:  send data by physical endpoint 1
** 输	 入: INT8U  *pRomData:	发送数据指针		Inptut:	   INT8U * buf:  send buffer		
			 INT16U len:		发送数据长度		INT8U len: send data length(the value can above 
			 												   EP0_PACKET_SIZE) 
** 输	 出: 无										Output:    NULL
*************************************************************************************************/
void code_transmit(INT8U * pRomData, INT16U len)
{
	ControlData.wCount = 0;
	
	if(ControlData.wLength > len)
		ControlData.wLength = len;									/* 长度要小于len 		the wLength can't above len */

	ControlData.pData = pRomData;
	if( ControlData.wLength >= EP0_PACKET_SIZE)
	{	/* wLength above MaxPacketSize of physical endpoint 1 */
		USB_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData);	
		ControlData.wCount += EP0_PACKET_SIZE;						/* 计数器累加			count number have been sent */

		DISABLE();
		bEPPflags.bits.control_state = USB_TRANSMIT;				/* 标识发送状态			flag transmit state */		
		ENABLE();
	}
	else 
	{
		USB_WriteEndpoint(1, ControlData.wLength, pRomData);		/* 将全部数据写入端点 	write all data into endpoint */
		ControlData.wCount += ControlData.wLength;					/* 计数器累加			count number have been sent */
		DISABLE();
		bEPPflags.bits.control_state = USB_IDLE;					/* 标识空闲状态			flag IDLE state */
		ENABLE();
	}
}



   //*************************************************************************
   // USB 标准设备请求服务程序    USB standard device request service program
   //*************************************************************************


/***************************************************************************************************************
** 函数名称: get_status()							Name:  	  get_status()
** 功能描述: 主机要求获取状态,设备返回16位的		Function: the USB host request get the status of USB device,
			 状态描述符给主机								  USB device will response 16-bit descriptor 
****************************************************************************************************************/
void get_status(void)
{
	INT8U endp, txdat[2], c;
	INT8U bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;	
																/* 获取请求类型			get the Request type */	
	if (bRecipient == USB_RECIPIENT_DEVICE) 
	{	/* request device */ 
		if(bEPPflags.bits.remote_wakeup == 1)			
			txdat[0] = 3;										/* 支持远程唤醒和自供电	support reamote wakeup and power itself */ 
		else
			txdat[0] = 1;										/* 不支持以上两个功能	not support two function above */
		txdat[1]=0;												/* 高 8 位清 0          upper 8-bit clear */
		single_transmit(txdat, 2);								/* 发送16ibt到USB主机	transmit 16-bit to USB host */
	} 
	
	else if (bRecipient == USB_RECIPIENT_INTERFACE) 
	{	/* request interface */
		txdat[0]=0;
		txdat[1]=0;
		single_transmit(txdat, 2);								/* 发送16ibt到USB主机	transmit 16-bit to USB host */
	}
	
	else if (bRecipient == USB_RECIPIENT_ENDPOINT) 
	{	/* request endpoint */
		endp = (INT8U)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
		if (ControlData.DeviceRequest.wIndex & (INT8U)USB_ENDPOINT_DIRECTION_MASK)
			c = USB_SelectEndpoint(endp * 2 + 1);				/* 读 IN 端点状态		read status of the IN endpoint */
		else
			c = USB_SelectEndpoint(endp * 2);					/* 读 OUT 端点状态		read status of the OUT endpoint */
		if(c & USB_STALL)
			txdat[0] = 1;										/* 端点已被禁止			the endpoint is stalled */
		else
			txdat[0] = 0;										/* 端点已解禁			the endpoint is unstalled */
		txdat[1] = 0;
		single_transmit(txdat, 2);								/* 发送16ibt到USB主机	transmit 16-bit to USB host */

	}
	else
		stall_ep0();											/* 非标准请求,禁止逻辑端点0 */
																/* not standard request, stall logical endpoint 0 */	
}																


/***************************************************************************************************************
** 函数名称: clear_feature()						Name:  	  clear_feature()
** 功能描述: 清除特性								Function: clear feature
****************************************************************************************************************/
void clear_feature(void)
{
	INT8U endp;
	INT8U bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
																/* 获取请求类型	 		 get request type */
	if (bRecipient == USB_RECIPIENT_DEVICE					
		&& ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP)
    {	/* request device */
		DISABLE();								
		bEPPflags.bits.remote_wakeup = 0;						/* 清除远程唤醒特性		clear reamote wakeup feature */	
		ENABLE();
		single_transmit(0, 0);									/* 返回一个空包			return an empty packet */
	}
	
	else if (bRecipient == USB_RECIPIENT_ENDPOINT
		&& ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL)
	{
		/* request endpoint */
		endp = (INT8U)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
		if (ControlData.DeviceRequest.wIndex & (INT8U)USB_ENDPOINT_DIRECTION_MASK)
			USB_SetEndpointStatus(endp * 2 + 1, 0);				/* 解禁 IN 端点			the IN endpoint is unstalled */
		else
			USB_SetEndpointStatus(endp * 2, 0);					/* 解禁 OUT 端点		the OUT endpoint is unstalled */
		single_transmit(0, 0);									/* 发送空包表示执行成功 return an empty packet indicate perform sucessfully */
	} else
		stall_ep0();											/* 不成功,禁止逻辑端点0 not the request, stall logical endpoint 0 */							
}


/***************************************************************************************************************
** 函数名称: set_feature()				   			Name:  	 set_feature() 
** 功能描述: 设置特性							    Function: set feature
****************************************************************************************************************/
void set_feature(void)
{
	INT8U endp;											
	INT8U bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
																/* 获取请求类型			get request type */
	if (bRecipient == USB_RECIPIENT_DEVICE						/* 请求设备				request device */
		&& ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP) 
	{	
		DISABLE();
		bEPPflags.bits.remote_wakeup = 1;						/* 设置远程唤醒标志		set reamote wakeup flag */

⌨️ 快捷键说明

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