📄 usbmouse.lst
字号:
C51 COMPILER V6.12 USBMOUSE 09/21/2007 12:44:30 PAGE 1
C51 COMPILER V6.12, COMPILATION OF MODULE USBMOUSE
OBJECT MODULE PLACED IN .\UsbMouse.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE .\UsbMouse.c BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /******************************************************************
2 本程序只供学习使用,未经作者许可,不得用于其它任何用途
3 我的邮箱:computer-lov@tom.com
4 欢迎访问我的blog: http://computer00.21ic.org
5
6 USBMouse.C file
7
8 作者:Computer-lov
9 建立日期: 2007.03.20
10 修改日期: 2007.03.23
11 版本:V1.1
12 版权所有,盗版必究。
13 Copyright(C) Computer-lov 2007-2017
14 All rights reserved
15 *******************************************************************/
16
17 #include <AT89x52.H>
18 #include "USB.H"
19 #include "PDIUSBD12.H"
20 #include "My_type.h"
21 #include "UART.H"
22 #include "key.h"
23 #include "Mouse.h"
24
25 #define USB_COMMAND_ADD 1
26 #define USB_DATA_ADD 0
27
28 //USB芯片连接引脚
29 #define USB_DATA P0
30 #define USB_A0 P3_5
31 #define USB_WR P3_6
32 #define USB_RD P3_7
33 #define USB_INT P3_3
34 #define USB_CS P2_4
35
36
37 #define ENDPOINT_NUMBER 2
38 //#define MAX_CONTROL_DATA_SIZE 16
39
40
41 //控制传输时保存数据
42 CONTROL_DATA_BUFF Control_Data;
43
44 //缓冲
45 uint8 idata buffer[64];
46
47 //交换数据时用的指针
48 uint8 *pData;
49
50 uint8 idle; //空闲状态
51 uint8 protocol; //当前协议
52
53 //高低字节交换
54
55 #define SWAP16(x) ((((uint16)(x))<<8)|(((uint16)(x))>>8))
C51 COMPILER V6.12 USBMOUSE 09/21/2007 12:44:30 PAGE 2
56
57 //取一个整数的低字节
58 #define LSB(x) ((uint8)(x))
59
60 //取一个整数的高字节
61 #define MSB(x) ((uint8)(((uint16)(x))>>8))
62
63 //字符串描述符所用的语言种类
64 code uint8 LANGUAGE_ID[4]={0x04,0x03,0x09,0x04};
65
66 //设备序列号
67 code uint8 device_serial_number[]=
68 {22,STRING_DESCRIPTOR,'2',0,'0',0,'0',0,'7',0,'-',0,'0',0,'3',0,'-',0,'2',0,'3',0};
69
70 //厂商字符串
71 code uint8 ManufacturerString[80]=
72 {80,STRING_DESCRIPTOR,0x35,0x75,0x11,0x81,0x08,0x57,0x08,0x57,0x84,0x76,0xB6,0x5B,
73 0x53,0x5F,'-',0x00,'-',0x00,'-',0x00,'-',0x00,'@',0,0x20,0x00,'H',0,'t',0,'t',0,
74 'p',0,':',0,'/',0,'/',0,'C',0,'o',0,'m',0,'p',0,'u',0,'t',0,'e',0,'r',0,'0',0,
75 '0',0,'.',0,'2',0,'1',0,'i',0,'c',0,'.',0,'o',0,'r',0,'g',0};
76
77 //产品字符串
78 code uint8 ProducterString[80]=
79 {80,STRING_DESCRIPTOR,0x35,0x75,0x11,0x81,0x08,0x57,0x08,0x57,0x5A,0x50,0x84,0x76,
80 'U',0,'S',0,'B',0,0x20,0x9F,0x07,0x68,'@',0,0x20,0x00,'H',0,'t',0,'t',0,
81 'p',0,':',0,'/',0,'/',0,'C',0,'o',0,'m',0,'p',0,'u',0,'t',0,'e',0,'r',0,'0',0,
82 '0',0,'.',0,'2',0,'1',0,'i',0,'c',0,'.',0,'o',0,'r',0,'g',0};
83
84
85 code DEVICE_DESCRIPTOR_STRUCT device_descriptor= //设备描述符
86 {
87 sizeof(DEVICE_DESCRIPTOR_STRUCT), //设备描述符的字节数大小
88 DEVICE_DESCRIPTOR, //设备描述符类型编号
89 0x1001, //USB版本号
90 0x00, //USB分配的设备类代码
91 0x00, //USB分配的子类代码
92 0x00, //USB分配的设备协议代码
93 0x10, //端点0的最大包大小
94 0x3412, //厂商编号VID,这里只是作为测试用,请使用自己的VID
95 0x7956, //产品编号PID,这里只是作为测试用,请使用自己的PID
96 0x0100, //设备出厂编号
97 0x01, //设备厂商字符串的索引
98 0x02, //描述产品字符串的索引
99 0x03, //描述设备序列号字符串的索引
100 0x01 //可能的配置数量
101 };
102
103 //定义配置,接口,端点等描述符结构体,因为它们是随配置描述符一起返回的
104 typedef struct _CON_INT_ENDP_DESCRIPTOR_STRUCT
105 {
106 CONFIGURATION_DESCRIPTOR_STRUCT configuration_descriptor;
107 INTERFACE_DESCRIPTOR_STRUCT interface_descriptor;
108 HID_DESCRIPTOR_STRUCT hid_descriptor;
109 ENDPOINT_DESCRIPTOR_STRUCT endpoint_descriptor[ENDPOINT_NUMBER];
110 }CON_INT_ENDP_DESCRIPTOR_STRUCT;
111
112 code CON_INT_ENDP_DESCRIPTOR_STRUCT con_int_endp_descriptor=
113 {
114 //configuration_descriptor //配置描述符
115 {
116 sizeof(CONFIGURATION_DESCRIPTOR_STRUCT), //配置描述符的字节数大小
117 CONFIGURATION_DESCRIPTOR, //配置描述符类型编号
C51 COMPILER V6.12 USBMOUSE 09/21/2007 12:44:30 PAGE 3
118 SWAP16(sizeof(CON_INT_ENDP_DESCRIPTOR_STRUCT)),
119 0x01, //只包含一个接口
120 0x01, //该配置的编号
121 0x00, //iConfiguration字段
122 0xA0, //采用总线供电,支持远程唤醒
123 0xC8 //从总线获取最大电流400mA
124 },
125 //interface_descritor //接口描述符
126 {
127 sizeof(INTERFACE_DESCRIPTOR_STRUCT), //接口描述符的字节数大小
128 INTERFACE_DESCRIPTOR, //接口描述符类型编号
129 0x00, //接口编号为0
130 0x00, //该接口描述符的编号
131 ENDPOINT_NUMBER, //非0端点数量为2,只使用端点主端点输入和
-输出
132 0x03, //人机接口设备(HID)类
133 0x01, //使用的子类:支持BIOS根启动Boot的子类
134 0x02, //使用的协议:鼠标
135 0x00 //接口描述符字符串索引
136 },
137 //hid_descriptor
138 {
139 sizeof(HID_DESCRIPTOR_STRUCT), //HID描述符的字节数大小
140 HID_DESCRIPTOR, //HID描述符类型编号
141 0x1001, //HID类协议版本号,为1.1
142 0x21, //固件的国家地区代号,0x21为美国
143 NUM_SUB_DESCRIPTORS, //下级描述符的数量
144 {
145 {
146 REPORT_DESCRIPTOR, //下级描述符为报告描述符
147 SWAP16(sizeof(MouseReportDescriptor)) //下级描述符的长度
148 }
149 },
150 },
151 //endpoint_descriptor[]
152 {
153 { //主端点输入描述
154 sizeof(ENDPOINT_DESCRIPTOR_STRUCT), //端点描述符的字节数大小
155 ENDPOINT_DESCRIPTOR, //端点描述符类型编号
156 MAIN_POINT_IN, //端点号,主输入端点
157 ENDPOINT_TYPE_INTERRUPT, //使用的传输类型:中断传输
158 0x4000, //该端点支持的最大包尺寸,64字节
159 0x0A //中断扫描时间:设置为10ms
160 },
161
162 { //主端点输出描述
163 sizeof(ENDPOINT_DESCRIPTOR_STRUCT), //端点描述符的字节数大小
164 ENDPOINT_DESCRIPTOR, //端点描述符类型编号
165 MAIN_POINT_OUT, //端点号,主输出端点
166 ENDPOINT_TYPE_INTERRUPT, //使用的传输类型:中断传输
167 0x4000, //该端点支持的最大包尺寸,64字节
168 0x0A //中断扫描时间:设置为10ms
169 }
170 }
171 };
172
173 union //程序标志位
174 {
175 uint16 Register;
176 struct
177 {
178 uint8 bus_reset :1;
C51 COMPILER V6.12 USBMOUSE 09/21/2007 12:44:30 PAGE 4
179 uint8 suspend :1;
180 uint8 remote_wakeup :1;
181 uint8 int_isr :1;
182 uint8 not_end :1;
183 uint8 usb_idle :1;
184 uint8 usb_busy :1;
185 uint8 setup_packet_in :1;
186 uint8 setup_packet_out :1;
187 uint8 set_addr :1;
188 uint8 usb_endp0_in :1;
189 uint8 usb_endp2_in :1;
190 uint8 usb_endp2_out :1;
191 }flags;
192 }usb_flags;
193
194 union //中断寄存器
195 {
196 uint8 Register[2];
197 struct
198 {
199 uint8 control_out_port :1;
200 uint8 control_in_port :1;
201 uint8 port_out_1 :1;
202 uint8 port_in_1 :1;
203 uint8 main_out_port :1;
204 uint8 main_in_port :1;
205 uint8 bus_reset :1;
206 uint8 suspend_change :1;
207 uint8 DMA_EOT :1;
208 uint8 not_use :7;
209 }Interrupt;
210 }Interrupt_Register;
211
212
213 union //端点最后处理状态
214 {
215 uint8 Register;
216 struct
217 {
218 uint8 successful :1;
219 uint8 error_code :4;
220 uint8 setup_packet :1;
221 uint8 data_1 :1;
222 uint8 prestatus_not_read :1;
223 }Status;
224 }Last_Status;
225
226
227 /***************** 延时x毫秒函数 ***********/
228 void delay(uint16 x)
229 {
230 1 uint16 i;
231 1 uint16 j;
232 1 for(i=0;i<x;i++)
233 1 for(j=0;j<230;j++);
234 1 }
235 /********************************************/
236
237 /*******************************写USB命令******************************************/
238 void write_usb_command(uint8 usb_command)
239 {
240 1 USB_A0=USB_COMMAND_ADD;
C51 COMPILER V6.12 USBMOUSE 09/21/2007 12:44:30 PAGE 5
241 1 USB_DATA=usb_command;
242 1 USB_WR=0;
243 1 USB_WR=1;
244 1 USB_DATA=0xFF;
245 1 }
246 /******************************************************************************/
247
248 /*********************写一字节USB数据*******************************************/
249 void write_a_usb_data(uint8 usb_data)
250 {
251 1 USB_A0=USB_DATA_ADD;
252 1 USB_DATA=usb_data;
253 1 USB_WR=0;
254 1 USB_WR=1;
255 1 USB_DATA=0XFF;
256 1 }
257 /******************************************************************************/
258
259 /****************************读一字节USB数据************************************/
260 uint8 read_a_usb_data(void)
261 {
262 1 uint8 temp;
263 1 USB_A0=USB_DATA_ADD;
264 1 USB_RD=0;
265 1 temp=USB_DATA;
266 1 USB_RD=1;
267 1 return temp;
268 1 }
269 /******************************************************************************/
270
271 /************************读USB中断寄存器**************************************/
272 void read_interrupt_register(void)
273 {
274 1 write_usb_command(Read_Interrupt_Register);
275 1 Interrupt_Register.Register[0]=read_a_usb_data();
276 1 Interrupt_Register.Register[1]=read_a_usb_data();
277 1 }
278 /******************************************************************************/
279
280 /************************设置USB地址*******************************************/
281 void set_usb_addr(uint8 addr)
282 {
283 1
284 1 write_usb_command(Set_Address);
285 1 write_a_usb_data(0x80|addr);
286 1 Prints(" Seting Address.....................",1);
287 1 Prints(" ADDR is : ",0);
288 1 PrintLongInt(addr);
289 1 Prints("",1);
290 1 }
291 /******************************************************************************/
292
293 /*************************端点使能******************************************/
294 void set_endpoint_enable(void)
295 {
296 1 write_usb_command(Set_Endpoint_Enable);
297 1 write_a_usb_data(0x01);
298 1 }
299 /******************************************************************************/
300
301 /****************************选择终端点*************************************/
302 uint8 select_endpoint(uint8 endp)
C51 COMPILER V6.12 USBMOUSE 09/21/2007 12:44:30 PAGE 6
303 {
304 1 write_usb_command(Select_EndPoint+endp);
305 1 return read_a_usb_data();
306 1 }
307 /******************************************************************************/
308
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -