23
|
1 /*
|
|
2 * Copyright (c) 2003 Bernd Walter
|
|
3 * All rights reserved.
|
|
4 */
|
|
5
|
|
6 /* Example Source Code for USB Enumeration using a PDIUSBD11 connected to a PIC16F87x
|
|
7 * Copyright 2001 Craig Peacock, Craig.Peacock@beyondlogic.org
|
|
8 * 31th December 2001 http://www.beyondlogic.org
|
|
9 */
|
|
10
|
|
11 typedef struct {
|
|
12 uint8_t bmRequestType;
|
|
13 uint8_t bRequest;
|
|
14 uint16_t wValue;
|
|
15 uint16_t wIndex;
|
|
16 uint16_t wLength;
|
|
17 } USB_SETUP_REQUEST;
|
|
18
|
|
19 typedef struct {
|
|
20 uint8_t bLength;
|
|
21 uint8_t bDescriptorType;
|
|
22 uint16_t bcdUSB;
|
|
23 uint8_t bDeviceClass;
|
|
24 uint8_t bDeviceSubClass;
|
|
25 uint8_t bDeviceProtocol;
|
|
26 uint8_t bMaxPacketSize0;
|
|
27 uint16_t idVendor;
|
|
28 uint16_t idProduct;
|
|
29 uint16_t bcdDevice;
|
|
30 uint8_t iManufacturer;
|
|
31 uint8_t iProduct;
|
|
32 uint8_t iSerialNumber;
|
|
33 uint8_t bNumConfigurations;
|
|
34 } USB_DEVICE_DESCRIPTOR;
|
|
35
|
|
36 typedef struct {
|
|
37 uint8_t bLength;
|
|
38 uint8_t bDescriptorType;
|
|
39 uint8_t bEndpointAddress;
|
|
40 uint8_t bmAttributes;
|
|
41 uint16_t wMaxPacketSize;
|
|
42 uint8_t bInterval;
|
|
43 } USB_ENDPOINT_DESCRIPTOR;
|
|
44
|
|
45 typedef struct {
|
|
46 uint8_t bLength;
|
|
47 uint8_t bDescriptorType;
|
|
48 uint16_t wTotalLength;
|
|
49 uint8_t bNumInterfaces;
|
|
50 uint8_t bConfigurationValue;
|
|
51 uint8_t iConfiguration;
|
|
52 uint8_t bmAttributes;
|
|
53 uint8_t MaxPower;
|
|
54 } USB_CONFIGURATION_DESCRIPTOR;
|
|
55
|
|
56 typedef struct {
|
|
57 uint8_t bLength;
|
|
58 uint8_t bDescriptorType;
|
|
59 uint8_t bInterfaceNumber;
|
|
60 uint8_t bAlternateSetting;
|
|
61 uint8_t bNumEndpoints;
|
|
62 uint8_t bInterfaceClass;
|
|
63 uint8_t bInterfaceSubClass;
|
|
64 uint8_t bInterfaceProtocol;
|
|
65 uint8_t iInterface;
|
|
66 } USB_INTERFACE_DESCRIPTOR;
|
|
67
|
|
68 typedef struct {
|
|
69 uint8_t bLength;
|
|
70 uint8_t bDescriptorType;
|
|
71 uint16_t wHIDClassSpecComp;
|
|
72 uint8_t bCountry;
|
|
73 uint8_t bNumDescriptors;
|
|
74 uint8_t b1stDescType;
|
|
75 uint16_t w1stDescLength;
|
|
76 } USB_HID_DESCRIPTOR;
|
|
77
|
|
78 typedef struct {
|
|
79 USB_CONFIGURATION_DESCRIPTOR ConfigDescriptor;
|
|
80 USB_INTERFACE_DESCRIPTOR InterfaceDescriptor0;
|
|
81 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor00;
|
|
82 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor01;
|
|
83 USB_INTERFACE_DESCRIPTOR InterfaceDescriptor1;
|
|
84 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor10;
|
|
85 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor11;
|
|
86 } USB_CONFIG_DATA;
|
|
87
|
|
88 typedef struct {
|
|
89 uint8_t bLength;
|
|
90 uint8_t bDescriptorType;
|
|
91 char bString[];
|
|
92 } STRING_DESCRIPTOR;
|
|
93
|
|
94 typedef struct {
|
|
95 uint8_t bLength;
|
|
96 uint8_t bDescriptorType;
|
|
97 uint16_t wLANGID0;
|
|
98 } LANGID_DESCRIPTOR;
|
|
99
|
|
100 #define D12_SET_ADDRESS_ENABLE 0xD0
|
|
101 #define D12_SET_ENDPOINT_ENABLE 0xD8
|
|
102 #define D12_SET_MODE 0xF3
|
|
103 #define D12_SET_DMA 0xFB
|
|
104 #define D12_READ_INTERRUPT_REGISTER 0xF4
|
|
105 #define D12_READ_BUFFER 0xF0
|
|
106 #define D12_WRITE_BUFFER 0xF0
|
|
107 #define D12_ACK_SETUP 0xF1
|
|
108 #define D12_CLEAR_BUFFER 0xF2
|
|
109 #define D12_VALIDATE_BUFFER 0xFA
|
|
110 #define D12_READ_LAST_TRANSACTION 0x40
|
|
111 #define D12_SET_ENDPOINT_STATUS 0x40
|
|
112 #define D12_READ_ENDPOINT_STATUS 0x80
|
|
113
|
|
114 #define D12_ENDPOINT_EP0_OUT 0x00
|
|
115 #define D12_ENDPOINT_EP0_IN 0x01
|
|
116 #define D12_ENDPOINT_EP1_OUT 0x02
|
|
117 #define D12_ENDPOINT_EP1_IN 0x03
|
|
118 #define D12_ENDPOINT_EP2_OUT 0x04
|
|
119 #define D12_ENDPOINT_EP2_IN 0x05
|
|
120
|
|
121 #define D12_INT_EP0_OUT 0x01
|
|
122 #define D12_INT_EP0_IN 0x02
|
|
123 #define D12_INT_EP1_OUT 0x04
|
|
124 #define D12_INT_EP1_IN 0x08
|
|
125 #define D12_INT_EP2_OUT 0x10
|
|
126 #define D12_INT_EP2_IN 0x20
|
|
127 #define D12_INT_BUS_RESET 0x40
|
|
128 #define D12_INT_SUSPEND 0x80
|
|
129
|
|
130 #define D12_LAST_TRAN_SETUP 0x20
|
|
131
|
|
132 #define STANDARD_DEVICE_REQUEST 0x00
|
|
133 #define STANDARD_INTERFACE_REQUEST 0x01
|
|
134 #define STANDARD_ENDPOINT_REQUEST 0x02
|
|
135 #define VENDOR_DEVICE_REQUEST 0x40
|
|
136 #define VENDOR_INTERFACE_REQUEST 0x41
|
|
137 #define VENDOR_ENDPOINT_REQUEST 0x42
|
|
138
|
|
139 #define GET_STATUS 0
|
|
140 #define CLEAR_FEATURE 1
|
|
141 #define SET_FEATURE 3
|
|
142 #define SET_ADDRESS 5
|
|
143 #define GET_DESCRIPTOR 6
|
|
144 #define SET_DESCRIPTOR 7
|
|
145 #define GET_CONFIGURATION 8
|
|
146 #define SET_CONFIGURATION 9
|
|
147 #define GET_INTERFACE 10
|
|
148 #define SET_INTERFACE 11
|
|
149 #define SYNCH_FRAME 12
|
|
150
|
|
151 #define VENDOR_UPDATE 126
|
|
152 #define VENDOR_RESET 127
|
|
153
|
|
154 #define ENDPOINT_HALT 0
|
|
155
|
|
156 #define TYPE_DEVICE_DESCRIPTOR 1
|
|
157 #define TYPE_CONFIGURATION_DESCRIPTOR 2
|
|
158 #define TYPE_STRING_DESCRIPTOR 3
|
|
159 #define TYPE_INTERFACE_DESCRIPTOR 4
|
|
160 #define TYPE_ENDPOINT_DESCRIPTOR 5
|
|
161 #define TYPE_HID_DESCRIPTOR 0x21
|
|
162
|
|
163 #define USB_ENDPOINT_TYPE_CONTROL 0x00
|
|
164 #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
|
|
165 #define USB_ENDPOINT_TYPE_BULK 0x02
|
|
166 #define USB_ENDPOINT_TYPE_INTERRUPT 0x03
|
|
167
|
|
168 /* Function prototypes */
|
|
169 uint8_t d12_get_data(void);
|
|
170 void d12_set_data(uint8_t data);
|
|
171 void d12_set_cmd(uint8_t cmd);
|
|
172 void d12_write_cmd(uint8_t command, const uint8_t *buffer, uint8_t count);
|
|
173 void d12_read_cmd(uint8_t command, uint8_t *buffer, uint8_t count);
|
|
174 uint8_t d12_read_endpt(uint8_t endpt, uint8_t *buffer);
|
|
175
|
|
176 void d12_ep0_irq(void);
|
|
177 void d12_getdescriptor(USB_SETUP_REQUEST *SetupPacket);
|
|
178 void d12_stallctrlendpt(void);
|
|
179 uint8_t d12_read_endpt(uint8_t endpt, uint8_t *buffer);
|
|
180 void d12_write_endpt(uint8_t endpt, const uint8_t *buffer, uint8_t bytes);
|
|
181 void d12_write_buffer_ep0(void);
|
|
182 void d12_send_data_ep2(void);
|
|
183 void d12_receive_data_ep2(void);
|
|
184
|
|
185 static void reset(void);
|
|
186
|
|
187 void usb_init(void);
|
|
188 void usb_intr(void);
|
|
189 void usb_gendata(void);
|