|
darius@23
|
1 |
/*
|
|
darius@23
|
2 |
* Copyright (c) 2003 Bernd Walter
|
|
darius@23
|
3 |
* All rights reserved.
|
|
darius@23
|
4 |
*/
|
|
darius@23
|
5 |
|
|
darius@23
|
6 |
/* Example Source Code for USB Enumeration using a PDIUSBD11 connected to a PIC16F87x
|
|
darius@23
|
7 |
* Copyright 2001 Craig Peacock, Craig.Peacock@beyondlogic.org
|
|
darius@23
|
8 |
* 31th December 2001 http://www.beyondlogic.org
|
|
darius@23
|
9 |
*/
|
|
darius@23
|
10 |
|
|
darius@23
|
11 |
typedef struct {
|
|
darius@23
|
12 |
uint8_t bmRequestType;
|
|
darius@23
|
13 |
uint8_t bRequest;
|
|
darius@23
|
14 |
uint16_t wValue;
|
|
darius@23
|
15 |
uint16_t wIndex;
|
|
darius@23
|
16 |
uint16_t wLength;
|
|
darius@31
|
17 |
} __attribute__ ((packed)) USB_SETUP_REQUEST;
|
|
darius@23
|
18 |
|
|
darius@23
|
19 |
typedef struct {
|
|
darius@23
|
20 |
uint8_t bLength;
|
|
darius@23
|
21 |
uint8_t bDescriptorType;
|
|
darius@23
|
22 |
uint16_t bcdUSB;
|
|
darius@23
|
23 |
uint8_t bDeviceClass;
|
|
darius@23
|
24 |
uint8_t bDeviceSubClass;
|
|
darius@23
|
25 |
uint8_t bDeviceProtocol;
|
|
darius@23
|
26 |
uint8_t bMaxPacketSize0;
|
|
darius@23
|
27 |
uint16_t idVendor;
|
|
darius@23
|
28 |
uint16_t idProduct;
|
|
darius@23
|
29 |
uint16_t bcdDevice;
|
|
darius@23
|
30 |
uint8_t iManufacturer;
|
|
darius@23
|
31 |
uint8_t iProduct;
|
|
darius@23
|
32 |
uint8_t iSerialNumber;
|
|
darius@23
|
33 |
uint8_t bNumConfigurations;
|
|
darius@31
|
34 |
} __attribute__ ((packed)) USB_DEVICE_DESCRIPTOR;
|
|
darius@23
|
35 |
|
|
darius@23
|
36 |
typedef struct {
|
|
darius@23
|
37 |
uint8_t bLength;
|
|
darius@23
|
38 |
uint8_t bDescriptorType;
|
|
darius@23
|
39 |
uint8_t bEndpointAddress;
|
|
darius@23
|
40 |
uint8_t bmAttributes;
|
|
darius@23
|
41 |
uint16_t wMaxPacketSize;
|
|
darius@23
|
42 |
uint8_t bInterval;
|
|
darius@31
|
43 |
} __attribute__ ((packed)) USB_ENDPOINT_DESCRIPTOR;
|
|
darius@23
|
44 |
|
|
darius@23
|
45 |
typedef struct {
|
|
darius@23
|
46 |
uint8_t bLength;
|
|
darius@23
|
47 |
uint8_t bDescriptorType;
|
|
darius@23
|
48 |
uint16_t wTotalLength;
|
|
darius@23
|
49 |
uint8_t bNumInterfaces;
|
|
darius@23
|
50 |
uint8_t bConfigurationValue;
|
|
darius@23
|
51 |
uint8_t iConfiguration;
|
|
darius@23
|
52 |
uint8_t bmAttributes;
|
|
darius@23
|
53 |
uint8_t MaxPower;
|
|
darius@31
|
54 |
} __attribute__ ((packed)) USB_CONFIGURATION_DESCRIPTOR;
|
|
darius@23
|
55 |
|
|
darius@23
|
56 |
typedef struct {
|
|
darius@23
|
57 |
uint8_t bLength;
|
|
darius@23
|
58 |
uint8_t bDescriptorType;
|
|
darius@23
|
59 |
uint8_t bInterfaceNumber;
|
|
darius@23
|
60 |
uint8_t bAlternateSetting;
|
|
darius@23
|
61 |
uint8_t bNumEndpoints;
|
|
darius@23
|
62 |
uint8_t bInterfaceClass;
|
|
darius@23
|
63 |
uint8_t bInterfaceSubClass;
|
|
darius@23
|
64 |
uint8_t bInterfaceProtocol;
|
|
darius@23
|
65 |
uint8_t iInterface;
|
|
darius@31
|
66 |
} __attribute__ ((packed)) USB_INTERFACE_DESCRIPTOR;
|
|
darius@23
|
67 |
|
|
darius@23
|
68 |
typedef struct {
|
|
darius@23
|
69 |
uint8_t bLength;
|
|
darius@23
|
70 |
uint8_t bDescriptorType;
|
|
darius@23
|
71 |
uint16_t wHIDClassSpecComp;
|
|
darius@23
|
72 |
uint8_t bCountry;
|
|
darius@23
|
73 |
uint8_t bNumDescriptors;
|
|
darius@23
|
74 |
uint8_t b1stDescType;
|
|
darius@23
|
75 |
uint16_t w1stDescLength;
|
|
darius@31
|
76 |
} __attribute__ ((packed)) USB_HID_DESCRIPTOR;
|
|
darius@23
|
77 |
|
|
darius@23
|
78 |
typedef struct {
|
|
darius@23
|
79 |
USB_CONFIGURATION_DESCRIPTOR ConfigDescriptor;
|
|
darius@23
|
80 |
USB_INTERFACE_DESCRIPTOR InterfaceDescriptor0;
|
|
darius@23
|
81 |
USB_ENDPOINT_DESCRIPTOR EndpointDescriptor00;
|
|
darius@23
|
82 |
USB_ENDPOINT_DESCRIPTOR EndpointDescriptor01;
|
|
darius@23
|
83 |
USB_INTERFACE_DESCRIPTOR InterfaceDescriptor1;
|
|
darius@23
|
84 |
USB_ENDPOINT_DESCRIPTOR EndpointDescriptor10;
|
|
darius@23
|
85 |
USB_ENDPOINT_DESCRIPTOR EndpointDescriptor11;
|
|
darius@31
|
86 |
} __attribute__ ((packed)) USB_CONFIG_DATA;
|
|
darius@23
|
87 |
|
|
darius@23
|
88 |
typedef struct {
|
|
darius@23
|
89 |
uint8_t bLength;
|
|
darius@23
|
90 |
uint8_t bDescriptorType;
|
|
darius@23
|
91 |
char bString[];
|
|
darius@31
|
92 |
} __attribute__ ((packed)) STRING_DESCRIPTOR;
|
|
darius@23
|
93 |
|
|
darius@23
|
94 |
typedef struct {
|
|
darius@23
|
95 |
uint8_t bLength;
|
|
darius@23
|
96 |
uint8_t bDescriptorType;
|
|
darius@23
|
97 |
uint16_t wLANGID0;
|
|
darius@31
|
98 |
} __attribute__ ((packed)) LANGID_DESCRIPTOR;
|
|
darius@23
|
99 |
|
|
darius@35
|
100 |
#define D12_SET_ADDRESS_ENABLE 0xd0
|
|
darius@35
|
101 |
#define D12_SET_ENDPOINT_ENABLE 0xd8
|
|
darius@35
|
102 |
#define D12_SET_MODE 0xf3
|
|
darius@35
|
103 |
#define D12_SET_DMA 0xfb
|
|
darius@35
|
104 |
#define D12_READ_INTERRUPT_REGISTER 0xf4
|
|
darius@35
|
105 |
#define D12_READ_BUFFER 0xf0
|
|
darius@35
|
106 |
#define D12_WRITE_BUFFER 0xf0
|
|
darius@35
|
107 |
#define D12_ACK_SETUP 0xf2
|
|
darius@35
|
108 |
#define D12_CLEAR_BUFFER 0xf2
|
|
darius@35
|
109 |
#define D12_VALIDATE_BUFFER 0xfa
|
|
darius@23
|
110 |
#define D12_READ_LAST_TRANSACTION 0x40
|
|
darius@23
|
111 |
#define D12_SET_ENDPOINT_STATUS 0x40
|
|
darius@23
|
112 |
#define D12_READ_ENDPOINT_STATUS 0x80
|
|
darius@35
|
113 |
#define D12_READ_CHIP_ID 0xfd
|
|
darius@23
|
114 |
|
|
darius@23
|
115 |
#define D12_ENDPOINT_EP0_OUT 0x00
|
|
darius@23
|
116 |
#define D12_ENDPOINT_EP0_IN 0x01
|
|
darius@23
|
117 |
#define D12_ENDPOINT_EP1_OUT 0x02
|
|
darius@23
|
118 |
#define D12_ENDPOINT_EP1_IN 0x03
|
|
darius@23
|
119 |
#define D12_ENDPOINT_EP2_OUT 0x04
|
|
darius@23
|
120 |
#define D12_ENDPOINT_EP2_IN 0x05
|
|
darius@23
|
121 |
|
|
darius@23
|
122 |
#define D12_INT_EP0_OUT 0x01
|
|
darius@23
|
123 |
#define D12_INT_EP0_IN 0x02
|
|
darius@23
|
124 |
#define D12_INT_EP1_OUT 0x04
|
|
darius@23
|
125 |
#define D12_INT_EP1_IN 0x08
|
|
darius@23
|
126 |
#define D12_INT_EP2_OUT 0x10
|
|
darius@23
|
127 |
#define D12_INT_EP2_IN 0x20
|
|
darius@23
|
128 |
#define D12_INT_BUS_RESET 0x40
|
|
darius@23
|
129 |
#define D12_INT_SUSPEND 0x80
|
|
darius@23
|
130 |
|
|
darius@31
|
131 |
#define D12_LAST_TRAN_OK 0x01
|
|
darius@31
|
132 |
#define D12_LAST_TRAN_ERRMSK 0x1e
|
|
darius@23
|
133 |
#define D12_LAST_TRAN_SETUP 0x20
|
|
darius@31
|
134 |
#define D12_LAST_TRAN_DATA 0x40
|
|
darius@31
|
135 |
#define D12_LAST_TRAN_OFLOW 0x80
|
|
darius@23
|
136 |
|
|
darius@23
|
137 |
#define STANDARD_DEVICE_REQUEST 0x00
|
|
darius@23
|
138 |
#define STANDARD_INTERFACE_REQUEST 0x01
|
|
darius@23
|
139 |
#define STANDARD_ENDPOINT_REQUEST 0x02
|
|
darius@23
|
140 |
#define VENDOR_DEVICE_REQUEST 0x40
|
|
darius@23
|
141 |
#define VENDOR_INTERFACE_REQUEST 0x41
|
|
darius@23
|
142 |
#define VENDOR_ENDPOINT_REQUEST 0x42
|
|
darius@23
|
143 |
|
|
darius@23
|
144 |
#define GET_STATUS 0
|
|
darius@23
|
145 |
#define CLEAR_FEATURE 1
|
|
darius@23
|
146 |
#define SET_FEATURE 3
|
|
darius@23
|
147 |
#define SET_ADDRESS 5
|
|
darius@23
|
148 |
#define GET_DESCRIPTOR 6
|
|
darius@23
|
149 |
#define SET_DESCRIPTOR 7
|
|
darius@23
|
150 |
#define GET_CONFIGURATION 8
|
|
darius@23
|
151 |
#define SET_CONFIGURATION 9
|
|
darius@23
|
152 |
#define GET_INTERFACE 10
|
|
darius@23
|
153 |
#define SET_INTERFACE 11
|
|
darius@23
|
154 |
#define SYNCH_FRAME 12
|
|
darius@23
|
155 |
|
|
darius@31
|
156 |
#define VENDOR_DOTEMP 125
|
|
darius@23
|
157 |
#define VENDOR_UPDATE 126
|
|
darius@23
|
158 |
#define VENDOR_RESET 127
|
|
darius@23
|
159 |
|
|
darius@23
|
160 |
#define ENDPOINT_HALT 0
|
|
darius@23
|
161 |
|
|
darius@23
|
162 |
#define TYPE_DEVICE_DESCRIPTOR 1
|
|
darius@23
|
163 |
#define TYPE_CONFIGURATION_DESCRIPTOR 2
|
|
darius@23
|
164 |
#define TYPE_STRING_DESCRIPTOR 3
|
|
darius@23
|
165 |
#define TYPE_INTERFACE_DESCRIPTOR 4
|
|
darius@23
|
166 |
#define TYPE_ENDPOINT_DESCRIPTOR 5
|
|
darius@23
|
167 |
#define TYPE_HID_DESCRIPTOR 0x21
|
|
darius@23
|
168 |
|
|
darius@23
|
169 |
#define USB_ENDPOINT_TYPE_CONTROL 0x00
|
|
darius@23
|
170 |
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
|
|
darius@23
|
171 |
#define USB_ENDPOINT_TYPE_BULK 0x02
|
|
darius@23
|
172 |
#define USB_ENDPOINT_TYPE_INTERRUPT 0x03
|
|
darius@23
|
173 |
|
|
darius@23
|
174 |
/* Function prototypes */
|
|
darius@23
|
175 |
uint8_t d12_get_data(void);
|
|
darius@23
|
176 |
void d12_set_data(uint8_t data);
|
|
darius@23
|
177 |
void d12_set_cmd(uint8_t cmd);
|
|
darius@23
|
178 |
void d12_write_cmd(uint8_t command, const uint8_t *buffer, uint8_t count);
|
|
darius@23
|
179 |
void d12_read_cmd(uint8_t command, uint8_t *buffer, uint8_t count);
|
|
darius@23
|
180 |
uint8_t d12_read_endpt(uint8_t endpt, uint8_t *buffer);
|
|
darius@23
|
181 |
|
|
darius@23
|
182 |
void d12_ep0_irq(void);
|
|
darius@31
|
183 |
void d12_handle_setup(void);
|
|
darius@31
|
184 |
void d12_getdescriptor(USB_SETUP_REQUEST *setuppkt);
|
|
darius@31
|
185 |
void d12_stallendpt(uint8_t ep);
|
|
darius@23
|
186 |
uint8_t d12_read_endpt(uint8_t endpt, uint8_t *buffer);
|
|
darius@23
|
187 |
void d12_write_endpt(uint8_t endpt, const uint8_t *buffer, uint8_t bytes);
|
|
darius@31
|
188 |
void d12_send_data_ep0(void);
|
|
darius@31
|
189 |
void d12_receive_data_ep1(void);
|
|
darius@23
|
190 |
void d12_send_data_ep2(void);
|
|
darius@23
|
191 |
void d12_receive_data_ep2(void);
|
|
darius@23
|
192 |
|
|
darius@35
|
193 |
void reset(void);
|
|
darius@23
|
194 |
|
|
darius@23
|
195 |
void usb_init(void);
|
|
darius@23
|
196 |
void usb_intr(void);
|
|
darius@23
|
197 |
void usb_gendata(void);
|
|
darius@35
|
198 |
|
|
darius@35
|
199 |
/*
|
|
darius@35
|
200 |
* The PDIUSBD12 is wired up like so
|
|
darius@35
|
201 |
*
|
|
darius@35
|
202 |
* PDI AVR
|
|
darius@35
|
203 |
* ======================
|
|
darius@35
|
204 |
* D7:0 <=> PA7:0
|
|
darius@35
|
205 |
* RD_N <= PB0
|
|
darius@35
|
206 |
* INT_N => PB1
|
|
darius@35
|
207 |
* WR_N <= PB2
|
|
darius@35
|
208 |
* A0 <= PB3 (0 = data, 1 = cmd)
|
|
darius@35
|
209 |
* SUSPEND <=> PB4
|
|
darius@35
|
210 |
*/
|
|
darius@35
|
211 |
|
|
darius@35
|
212 |
#define PDIPORT PORTA
|
|
darius@35
|
213 |
#define PDIDDR PORTA
|
|
darius@35
|
214 |
#define PDICTL PORTB
|
|
darius@35
|
215 |
#define PDIRD PB0
|
|
darius@35
|
216 |
#define PDIINT PB1
|
|
darius@35
|
217 |
#define PDIWR PB2
|
|
darius@35
|
218 |
#define PDIA0 PB3
|
|
darius@35
|
219 |
#define PDISUSP PB4
|
|
darius@35
|
220 |
|