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

📄 d12_chap9.c

📁 利用sunplus16位单片机实现USB接口的全套技术资料
💻 C
字号:
// =========================================
// 文件名称:D12_Chap9.c
// 功能描述:USB协议层
// 维护记录:modified by liuxue v1.0 2007-03-14
// =========================================
#include "D12_Driver.h"

// =========================================
// 函数:void D12_Reserved(void)
// 描述:保留,停止处理端点0,端点1
// 参数:无
// 返回:无
// =========================================
void D12_Reserved(void)
{
	D12_Stall_Ep0();
}

// =========================================
// 函数:void D12_Get_Status(void)
// 描述:获取状态
// 参数:无
// 返回:无
// =========================================
void D12_Get_Status(void)
{
	unsigned int endp, txdat[2];
	unsigned int bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
	unsigned int c;
	if(bRecipient == USB_RECIPIENT_DEVICE)
	{
		if(bEPPflags.bits.remote_wakeup == 1)
			txdat[0] = 3;
		else
			txdat[0] = 1;
		txdat[1]=0;
		D12_Single_Transmit(txdat, 2);
	}
	else if(bRecipient == USB_RECIPIENT_INTERFACE)
	{
		txdat[0]=0;
		txdat[1]=0;
		D12_Single_Transmit(txdat, 2);
	}
	else if(bRecipient == USB_RECIPIENT_ENDPOINT)
	{
		endp = (unsigned int)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
		if(ControlData.DeviceRequest.wIndex & (unsigned int)USB_ENDPOINT_DIRECTION_MASK)
			c = D12_SelectEndpoint(endp * 2 + 1);			// Control-in
		else
			c = D12_SelectEndpoint(endp * 2);				// Control-out
		if(c & D12_STALL)
			txdat[0] = 1;
		else
			txdat[0] = 0;
		txdat[1] = 0;
		D12_Single_Transmit(txdat, 2);
	}
	else
		D12_Stall_Ep0();
}

// =========================================
// 函数:void D12_Clear_Feature(void)
// 描述:清除特性
// 参数:无
// 返回:无
// =========================================
void D12_Clear_Feature(void)
{
	unsigned int endp;
	unsigned int bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
	if(bRecipient == USB_RECIPIENT_DEVICE && ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP)
	{
		bEPPflags.bits.remote_wakeup = 0;
		D12_Single_Transmit(0, 0);
	}
	else if(bRecipient == USB_RECIPIENT_ENDPOINT && ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL)
	{
		endp = (unsigned int)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
		if(ControlData.DeviceRequest.wIndex & (unsigned int)USB_ENDPOINT_DIRECTION_MASK)
			D12_SetEndpointStatus(endp * 2 + 1, 0);	// clear TX stall for IN on EPn.
		else
			D12_SetEndpointStatus(endp * 2, 0);		// clear RX stall for OUT on EPn.
		D12_Single_Transmit(0, 0);
	}
	else
		D12_Stall_Ep0();
}

// =========================================
// 函数:void D12_Set_Feature(void)
// 描述:设置特性
// 参数:无
// 返回:无
// =========================================
void D12_Set_Feature(void)
{
	unsigned int endp;
	unsigned int bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
	if(bRecipient == USB_RECIPIENT_DEVICE && ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP)
	{
		bEPPflags.bits.remote_wakeup = 1;
		D12_Single_Transmit(0, 0);
	}
	else if(bRecipient == USB_RECIPIENT_ENDPOINT && ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL)
	{
		endp = (unsigned int)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
		if(ControlData.DeviceRequest.wIndex & (unsigned int)USB_ENDPOINT_DIRECTION_MASK)
			D12_SetEndpointStatus(endp * 2 + 1, 1);
		else
			D12_SetEndpointStatus(endp * 2, 1);
		D12_Single_Transmit(0, 0);
	}
	else
		D12_Stall_Ep0();
}

// =========================================
// 函数:void D12_Set_Address(void)
// 描述:设置地址
// 参数:无
// 返回:无
// =========================================
void D12_Set_Address(void)
{
	D12_SetAddressEnable((unsigned int)(ControlData.DeviceRequest.wValue & DEVICE_ADDRESS_MASK), 1);
	D12_Single_Transmit(0, 0);
}

// =========================================
// 函数:void D12_Get_Descriptor(void)
// 描述:获取描述符
// 参数:无
// 返回:无
// =========================================
void D12_Get_Descriptor(void)
{
	unsigned int bDescriptor = MSB(ControlData.DeviceRequest.wValue);
	if(bDescriptor == USB_DEVICE_DESCRIPTOR_TYPE)
	{
		D12_Code_Transmit((unsigned int *)&DeviceDescr, sizeof(USB_DEVICE_DESCRIPTOR));
	}
	else if(bDescriptor == USB_CONFIGURATION_DESCRIPTOR_TYPE)
	{
		D12_Code_Transmit((unsigned int *)&ConfigDescr, CONFIG_DESCRIPTOR_LENGTH);
	}
	else
		D12_Stall_Ep0();
}

// =========================================
// 函数:void D12_Get_Configuration(void)
// 描述:获取配置信息
// 参数:无
// 返回:无
// =========================================
void D12_Get_Configuration(void)
{
	unsigned int c[1];
	c[0] = bEPPflags.bits.configuration;
	D12_Single_Transmit(c, 1);
}

// =========================================
// 函数:void D12_Set_Configuration(void)
// 描述:设置配置
// 参数:无
// 返回:无
// =========================================
void D12_Set_Configuration(void)
{
	if(ControlData.DeviceRequest.wValue == 0)
	{
		// put device in unconfigured state
		D12_Single_Transmit(0, 0);
		bEPPflags.bits.configuration = 0;
		D12_Init_Unconfig();
	}
	else if(ControlData.DeviceRequest.wValue == 1)
	{
		// Configure device
		D12_Single_Transmit(0, 0);
		D12_Init_Unconfig();
		D12_Init_Config();
		bEPPflags.bits.configuration = 1;
	}
	else
		D12_Stall_Ep0();
}

// =========================================
// 函数:void D12_Get_Interface(void)
// 描述:获取接口信息
// 参数:无
// 返回:无
// =========================================
void D12_Get_Interface(void)
{
	unsigned int txdat = 0;
	D12_Single_Transmit(&txdat, 1);
}

// =========================================
// 函数:void D12_Set_Interface(void)
// 描述:设置接口
// 参数:无
// 返回:无
// =========================================
void D12_Set_Interface(void)
{
	if(ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
		D12_Single_Transmit(0, 0);
	else
		D12_Stall_Ep0();
}

// =========================================
// 函数:void D12_Stall_Ep0(void)
// 描述:禁止端点0
// 参数:无
// 返回:无
// =========================================
void D12_Stall_Ep0(void)
{
	D12_SetEndpointStatus(0, 1);
	D12_SetEndpointStatus(1, 1);
}

// =========================================
// 函数:void D12_Single_Transmit(unsigned int * buf, unsigned int len)
// 描述:传输一个缓冲区的数据
// 参数:无
// 返回:无
// =========================================
void D12_Single_Transmit(unsigned int * buf, unsigned int len)
{
	if(len <= EP0_PACKET_SIZE)
	{
		D12_WriteEndpoint(1, len, buf);
	}
}

// =========================================
// 函数:void D12_Code_Transmit(unsigned int * pRomData, unsigned long int len)
// 描述:控制传输
// 参数:无
// 返回:无
// =========================================
void D12_Code_Transmit(unsigned int * pRomData, unsigned long int len)
{
	ControlData.wCount = 0;
	if(ControlData.wLength > len)
		ControlData.wLength = len;
	ControlData.pData = pRomData;
	if(ControlData.wLength >= EP0_PACKET_SIZE)
	{
		D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData);
		ControlData.wCount += EP0_PACKET_SIZE;
		bEPPflags.bits.control_state = USB_TRANSMIT;
	}
	else
	{
		D12_WriteEndpoint(1, ControlData.wLength, pRomData);
		ControlData.wCount += ControlData.wLength;
		bEPPflags.bits.control_state = USB_IDLE;
	}
}

// =========================================
// 函数:void D12_Init_Unconfig(void)
// 描述:Disable all endpoints but EPP0.
// 参数:无
// 返回:无
// =========================================
void D12_Init_Unconfig(void)
{
	D12_SetEndpointEnable(0);		/* Disable all endpoints but EPP0. */
}

// =========================================
// 函数:void D12_Init_Config(void)
// 描述:Enable  generic/iso endpoints.
// 参数:无
// 返回:无
// =========================================
void D12_Init_Config(void)
{
	D12_SetEndpointEnable(1);		/* Enable  generic/iso endpoints. */
}

⌨️ 快捷键说明

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