📄 usb.lst
字号:
C51 COMPILER V6.12 USB 06/28/2005 22:56:52 PAGE 1
C51 COMPILER V6.12, COMPILATION OF MODULE USB
OBJECT MODULE PLACED IN USB.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE USB.C LARGE ROM(COMPACT) BROWSE DEBUG OBJECTEXTEND
stmt level source
1
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "REGSND1.H"
7 #include "USBDEF.H"
8 #include "UART.h"
9
10 //时钟参数宏
11 #define Rdiv 11
12 //USB寄存器设置相关的宏定义
13 #define PLLRES (0x1<<3) //PLLCON
14 #define PLLEN (0x1<<1)
15 #define USBE (0x1<<7) //USBCON
16 #define EEOFINT (0x1<<4)
17 #define FEN (0x1<<7)
18 #define FADDEN (0x1)
19 #define CONFG (0x1<<1)
20 #define DIR (0x1<<7) //UEPSTAX
21 #define RXOUTB1 (0x1<<6)
22 #define STALLRQ (0x1<<5)
23 #define TXRDY (0x1<<4)
24 #define STLCRC (0x1<<3)
25 #define RXSETUP (0x1<<2)
26 #define RXOUTB0 (0x1<<1)
27 #define TXCMP (0x1<<0)
28
29 //USB 请求的bRequert的含意宏
30 #define get_status 0x00 //取得状态
31 #define clear_feature 0x01 //清除特性
32 #define reserved 0x02 //保留
33 #define set_feature 0x03 //设置特性
34
35 #define set_address 0x05 //设置地址
36 #define get_descriptor 0x06 //取得描述符
37
38 #define get_configuration 0x08 //取得配置
39 #define set_configuration 0x09 //设置配置
40 #define get_interface 0x0a //取得接口
41 #define set_interface 0x0b //设置接口
42
43 /**---------------------------------------------------------------------------------
44
45 ----------------------------------------------------------------------------------**/
46
47 void AtmelUSBInit() //用于USB的PLL时钟20M晶振时如下设置
48 {
49 1 int data i;
50 1 PLLNDIV = 0x04; //设置PLL时钟
51 1 PLLCON |= (0x3&Rdiv)<<6;
52 1 PLLRDIV = (0x3ff&Rdiv)>>2;
53 1 USBCLK=0;
54 1 PLLCON&=(~PLLRES); //使能PLL
55 1 PLLCON|=PLLEN;
C51 COMPILER V6.12 USB 06/28/2005 22:56:52 PAGE 2
56 1 USBCON&=(~USBE); //关闭USB并复位USB
57 1 for(i=0;i<3000;i++); //等待PLL工作稳定
58 1 USBCON|=USBE; //开启USB控制器
59 1
60 1 }
61
62 void EpEnable(void) //初始化USB端点
63 {
64 1 UEPNUM=0x00; UEPCONX=0x80;
65 1 UEPNUM=0x01; UEPCONX=0x86;
66 1 UEPNUM=0x02; UEPCONX=0x82;
67 1 UEPRST=0x07; UEPRST= 0x00;
68 1 UEPIEN=0x07; USBIEN|=EEOFINT;
69 1 USBADDR=FEN;
70 1 }
71
72 unsigned char ReadEp(unsigned char EpNum,unsigned char *Data) //读取端口数据
73 {
74 1 unsigned char data i=0;
75 1 UEPNUM=EpNum; //指端口号
76 1 while(i<UBYCTLX) //读数据到缓冲//UBYCTX得到数据长度
77 1 {
78 2 Data[i++]=UEPDATX;
79 2 }
80 1 UEPSTAX&=~(RXOUTB0|RXOUTB1|RXSETUP); //清除相关位
81 1 return(i);
82 1 }
83
84 void WriteEp(unsigned char EpNum,unsigned char nLength,unsigned char *Data) //写端口数据
85 {
86 1 unsigned char data i=0;
87 1 UEPNUM=EpNum; //选择端口
88 1 UEPSTAX|=DIR; //控制方向
89 1 while(nLength--) UEPDATX=Data[i++]; //写入FIFO
90 1 UEPSTAX|=TXRDY; //开始发送
91 1 while(!(UEPSTAX&TXCMP)) ;
92 1 UEPSTAX&=(~(TXCMP)); //清除TXCMP
93 1 }
94
95 //-------------以下用于对端点进行处理
96
97 void Set_Address(unsigned char EpNum) //设置USB地址1-127
98 {
99 1 WriteEp(0,0,0); //在Status阶段过后才能改变设备地址
100 1 USBADDR|=EpNum;
101 1 USBADDR|=FEN; //设置地址
102 1 USBCON|=FADDEN; //地址使能
103 1 }
104
105 //设备描述符
106 code unsigned char Device_Descriptor[18] = {
107 0x12, //0x12
108 0x01, //DEVICE descriptor
109 0x10, 0x01, //spec rev level (BCD) 1.0
110 0x0, //device class
111 0x0, //device subclass
112 0x0, //device protocol
113 0x20, //max packet size
114 0x05, 0x82, //National's vendor ID
115 0x00, 0x11, //National's product ID
116 0x00, 0x00, //National's revision ID
117 0, //index of manuf. string
C51 COMPILER V6.12 USB 06/28/2005 22:56:52 PAGE 3
118 0, //index of prod. string
119 0, //index of ser. # string
120 0x01 //number of configs.
121 };
122 //配置描述符
123 code unsigned char Configuration_Descriptor_All[32] = {
124
125 9, //Size of Descriptor in Bytes
126 2, //Configuration Descriptor (0x02)
127 0x20, //Total length in bytes of data returned LSB
128 0x00, //MSB
129 1, //Number of Interfaces
130 1, //Value to use as an argument to select this configuration
131 0, //Index of String Descriptor describing this configuration
132 0x80,
133 0xfa, //Maximum Power Consumption in 2mA units
134
135 9,
136 4,
137 0, //the index of the interface descriptor Number of Interface
138 0, //Value used to select alternative setting
139 2, //EndPoint Number Used in this Descriptor
140 8, //Class Code (Assigned by USB Org)
141 6, //interface subclass1=RBC,2=SFF,3=QIC,4=UFI,5=SFF,6=SCSI
142 0x50, //bulk 0nly Transport
143 0, //Index of String Descriptor Describing this interface
144
145 //Bulk-in Endpoint
146 0x07, //length of this desc.
147 0x05, //ENDPOINT descriptor TYPE
148 0x81, //address (IN) Endpoint 4 84
149 0x02, //attributes (BULK)
150 0x40, 0x00, //max packet size (64)
151 0x0, //Does not apply to Bulk endpoints
152
153 //Bulk-out Endpoint
154 0x07, //length of this desc.
155 0x05, //ENDPOINT descriptor TYPE
156 0x02, //address (OUT) Endpoint 5 05
157 0x02, //attributes (BULK)
158 0x40, 0x00, //max packet size (64)
159 0x0 //Does not apply to Bulk endpoints
160 };
161
162 void Get_Descriptor(unsigned char DesType,unsigned char nLength) //得到描述符
163 {
164 1 if(DesType==0x01) //取设备描述符
165 1 WriteEp(0,18,Device_Descriptor);
166 1
167 1 if((DesType==0x02)&&(nLength==0x09)) //取端口描述符
168 1 WriteEp(0,9,Configuration_Descriptor_All);
169 1
170 1 if((DesType==0x02)&&(nLength==0xff)) //取端点描述符
171 1 {
172 2 WriteEp(0,32,Configuration_Descriptor_All);
173 2 WriteEp(0,2,&Device_Descriptor[4]);
174 2 }
175 1
176 1 if((DesType==0x02)&&(nLength==0x20))
177 1 WriteEp(0,32,Configuration_Descriptor_All); //取配置描述符
178 1 }
179
C51 COMPILER V6.12 USB 06/28/2005 22:56:52 PAGE 4
180 void Set_Configuration(unsigned char wValue) //设置配置
181 {
182 1 if(wValue == 0)
183 1 {
184 2 UEPNUM=0x00; UEPCONX=0x80;
185 2 UEPNUM=0x01; UEPCONX=0x86;
186 2 UEPNUM=0x02; UEPCONX=0x82;
187 2 USBCON&=(~CONFG);
188 2 WriteEp(0,0,0); //状态传送阶段之前完成指定操作
189 2 }
190 1 else if(wValue == 1)
191 1 {
192 2 UEPNUM=0x00; UEPCONX=0x80;
193 2 UEPNUM=0x01; UEPCONX=0x86;
194 2 UEPNUM=0x02; UEPCONX=0x82;
195 2 USBCON|=CONFG;
196 2 WriteEp(0,0,0); //状态传送阶段之前完成指定操作
197 2 }
198 1 }
199
200 void Ep0() //---端点0处理主函数
201 {
202 1 unsigned char data DT[32]={0,};
203 1 unsigned char data i;
204 1 i = ReadEp(0,DT);
205 1 if (((DT[0] & 0x60)==0) && i)
206 1 {
207 2 switch (DT[1])
208 2 {
209 3 case set_address :Set_Address(DT[2]); printuf("set_address", 0);break; //设置地址
210 3 case get_descriptor :Get_Descriptor(DT[3],DT[6]); printuf("get_descriptor", 0);break; //取描述符
211 3 case set_configuration :Set_Configuration(DT[2]); printuf("set_configuration", 0);break; //设
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -