/*
==================================================================================================
USB Descriptors for HID class
Name : USB.c
Author : LiYong
Date : 2008.09.20
Compiler: Keil C51
Version : 1.0
Resource: USB module
Support MCU:
C8051Fxxxx
Description : This file contains the USB module for C8051Fxxxx serial MCUs
==================================================================================================
*/
#include"USB.h"
#include"USB_HID_Descriptor.h"
#include"USB_Request.h"
#include"F32x_USB_Register.h"
#define DEVICE_REMOTE_WAKEUP 0x00 // Remote wakeup feature(not used)
#define ENDPOINT_HALT 0x00 // Endpoint_Halt feature selector
static xdata BYTE UsedMemorySpace[1024] _at_ 0x400;
/*
==================================================================================================
Status of a endpoint
==================================================================================================
*/
// Define Endpoint States
#define EP_IDLE 0x00 // This signifies Endpoint Idle State
#define EP_TX 0x01 // Endpoint Transmit State
#define EP_RX 0x02 // Endpoint Receive State
#define EP_HALT 0x03 // Endpoint Halt State (return stalls)
// Endpoint Stall (send procedural stall next status phase)
#define EP_STALL 0x04
// Endpoint Address (change FADDR during next status phase)
#define EP_ADDRESS 0x05
/*
==================================================================================================
Status of the USB device
==================================================================================================
*/
// Define device states
#define DEV_ATTACHED 0x00 // Device is in Attached State
#define DEV_POWERED 0x01 // Device is in Powered State
#define DEV_DEFAULT 0x02 // Device is in Default State
#define DEV_ADDRESS 0x03 // Device is in Addressed State
#define DEV_CONFIGURED 0x04 // Device is in Configured State
#define DEV_SUSPENDED 0x05 // Device is in Suspended State
// Holds the status for each endpoint
code BYTE ONES_PACKET[2] = {0x01, 0x00};
code BYTE ZERO_PACKET[2] = {0x00, 0x00};
/*
==================================================================================================
Variables
==================================================================================================
*/
unsigned char Ep_Status[3] = {EP_IDLE, EP_IDLE, EP_IDLE};
BYTE USB_State; // Holds the current USB State
// def. in F32x_USB_Main.h
//将USB上电枚举过程,分成几个阶段,在程序中判断阶段而得知当前请求是否有效。
//并在枚举过程中不断更新状态指示。
//-----------------------------
// Setup Packet Type Definition
//-----------------------------
typedef struct
{
BYTE bmRequestType; // Request recipient, type, direction
BYTE bRequest; // Specific standard request number
DB16 wValue; // varies according to request
DB16 wIndex; // varies according to request
DB16 wLength; // Number of bytes to transfer
}setup_buffer; // End of Setup Packet Type
setup_buffer Setup; //SETUP数据包
unsigned int DataSize; //要返回到主机的字节数
unsigned int DataSent; //已经发出的字节数。
unsigned char* DataPtr;
struct
{
unsigned char OutBuff[EP2_PACKET_SIZE];
unsigned char InBuff[EP1_PACKET_SIZE];
unsigned char DataOK;
unsigned char SendReq;
}USB; //数据指针,可指向FLASH,XDATA,用于记录返回数据的首地址
/*
==================================================================================================
Static internal functions
==================================================================================================
*/
static void Fifo_Write(BYTE addr, unsigned int uNumBytes, BYTE * pData);
static void Fifo_Read(BYTE addr, unsigned int uNumBytes, BYTE * pData) ;
static void Force_Stall(void);
static void Handle_In1( void );
static void Handle_Out2( void );
static void Get_Status(void);
static void USB_Enable( unsigned char sw);
//=======================================================================================
//功能:发送STALL信号,原因是收到了错误的条件或不希望的传输请求。
void Force_Stall(void)
{
POLL_WRITE_BYTE(INDEX, 0);
POLL_WRITE_BYTE(E0CSR, rbSDSTL); // 发送STALL。
Ep_Status[0] = EP_STALL; // 将断点0置为STALL状态。
}
//=======================================================================================
//功能:获取状态,可能有三种请求:设备状态,接口状态,端点状态
void Get_Status(void)
{
//Setup.wLength 应该为2,Setup.wValue应该为0;
if ( Setup.wValue.c[MSB] || Setup.wValue.c[LSB] ||
Setup.wLength.c[MSB] || (Setup.wLength.c[LSB] != 2))
{
Force_Stall();
}
//判断主机是请求哪种状态,设备?接口?端点?
switch(Setup.bmRequestType)
{
case OUT_DEVICE: //0x80,请求设备状态。
if (Setup.wIndex.c[MSB] || Setup.wIndex.c[LSB])
{
Force_Stall();//Setup.wIndex必须为0,否则,发送STALL信号给主机。
}
else
{
//返回两个字节0,说明,设备不是自供电,而是需要VBUS供电,且不支持远程唤醒。
DataPtr = (BYTE*)&ZERO_PACKET;
DataSize = 2;
}
break;
case OUT_INTERFACE: //0x81,请求接口状态
// Only valid if device is configured and non-zero index
//只有当USB被配置后,且wIndex为0时才有效。
if ((USB_State != DEV_CONFIGURED) ||
Setup.wIndex.c[MSB] || Setup.wIndex.c[LSB])
{
// Otherwise send stall to host
Force_Stall(); //否则发送STALL
}
else
{
// Status packet always returns {0x00,0X00}
//请求接口状态,总是返回{0X00,0X00}
DataPtr = (BYTE*)&ZERO_PACKET;
DataSize = 2;
}
break;
// See if recipient was an endpoint
case OUT_ENDPOINT: // 0x82:请求端点状态。
// Make sure device is configured and index msb = 0x00
//必须在配置阶段,INDEX指示端点号,所以其高8位必须为0.
if ((USB_State != DEV_CONFIGURED) || Setup.wIndex.c[MSB])
{
Force_Stall(); // otherwise return stall to host
}
else
{
//每个端口只有一个方向,只响应该方向
if ( Setup.wIndex.c[LSB] == IN_EP1 )//EP1为IN,所以只处理IN_EP1
{
if ( Ep_Status[1] == EP_HALT )
{
// If endpoint is halted, return 0x01,0x00
//如果端口状态为HALT,则返回{0X01,0X00}
DataPtr = (BYTE*)&ONES_PACKET;
DataSize = 2;
}
else
{
// Otherwise return 0x00,0x00 to indicate endpoint active
DataPtr = (BYTE*)&ZERO_PACKET;
DataSize = 2;
}
}
else
{
// If request is directed to endpoint 2, send either
// 0x01,0x00 if endpoint halted or 0x00,0x00 if endpoint is active
if (Setup.wIndex.c[LSB] == OUT_EP2)
{
没有合适的资源?快使用搜索试试~ 我知道了~
USB-mouse.rar_USB Mou
共41个文件
h:10个
lst:4个
obj:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 100 浏览量
2022-09-23
10:53:06
上传
评论
收藏 149KB RAR 举报
温馨提示
在C8051F320平台上实现鼠标功能的例程,适合USB入门。
资源推荐
资源详情
资源评论
收起资源包目录
USB-mouse.rar (41个子文件)
USB mouse
USB_C8051F
F34x_USB_Register.h 5KB
USB.c 40KB
F34x_USB_Descriptor.h 4KB
USB_HID_Descriptor.c 5KB
F32x_USB_Register.h 5KB
USB_Type.H 659B
USB.H 2KB
USB_Request.H 4KB
USB_HID_Descriptor.H 7KB
MyTypeDef.h 2KB
APPs
main.c 5KB
STARTUP.A51 5KB
main.h 699B
c8051F320.h 16KB
Project
USB mouse.Uv2 2KB
Output
USB_HID_Descriptor.__i 272B
USB mouse 68KB
STARTUP._ia 102B
USB mouse.lnp 183B
USB_HID_Descriptor.obj 10KB
USB mouse.plg 5KB
USB.obj 51KB
USB.__i 212B
main.__i 210B
main.obj 12KB
ExtDll.iex 19B
STARTUP.obj 874B
USB mouse.hex 10KB
List
USB_HID_Descriptor.lst 54KB
USB mouse.m51 49KB
main.lst 63KB
USB.lst 230KB
STARTUP.lst 12KB
main.i 15KB
USB_HID_Descriptor.i 15KB
USB.i 32KB
USB mouse_Uv2.Bak 2KB
USB mouse.plg 266B
USB mouse.Opt 2KB
Target 1.BAT 478B
USB mouse_Opt.Bak 2KB
共 41 条
- 1
资源评论
我虽横行却不霸道
- 粉丝: 73
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功