1 /***************************************************************************
2 * Copyright (C) 2011 by Martin Schmoelzer *
3 * <martin.schmoelzer@student.tuwien.ac.at> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
22 #include "reg_ezusb.h"
27 #define NULL (void *)0;
29 /* High and Low byte of a word (uint16_t) */
30 #define HI8(word) (uint8_t)(((uint16_t)word >> 8) & 0xff)
31 #define LO8(word) (uint8_t)((uint16_t)word & 0xff)
33 /* Convenience functions */
34 #define STALL_EP0() (EP0CS |= EP0STALL)
35 #define CLEAR_IRQ() (EXIF &= ~USBINT)
37 /*********** USB descriptors. See section 9.5 of the USB 1.1 spec **********/
39 /* USB Descriptor Types. See USB 1.1 spec, page 187, table 9-5 */
40 #define DESCRIPTOR_TYPE_DEVICE 0x01
41 #define DESCRIPTOR_TYPE_CONFIGURATION 0x02
42 #define DESCRIPTOR_TYPE_STRING 0x03
43 #define DESCRIPTOR_TYPE_INTERFACE 0x04
44 #define DESCRIPTOR_TYPE_ENDPOINT 0x05
46 #define STR_DESCR(len, ...) { len * 2 + 2, DESCRIPTOR_TYPE_STRING, { __VA_ARGS__ } }
48 /** USB Device Descriptor. See USB 1.1 spec, pp. 196 - 198 */
49 struct usb_device_descriptor {
50 uint8_t bLength; /**< Size of this descriptor in bytes. */
51 uint8_t bDescriptorType; /**< DEVICE Descriptor Type. */
52 uint16_t bcdUSB; /**< USB specification release number (BCD). */
53 uint8_t bDeviceClass; /**< Class code. */
54 uint8_t bDeviceSubClass; /**< Subclass code. */
55 uint8_t bDeviceProtocol; /**< Protocol code. */
56 uint8_t bMaxPacketSize0; /**< Maximum packet size for EP0 (8, 16, 32, 64). */
57 uint16_t idVendor; /**< USB Vendor ID. */
58 uint16_t idProduct; /**< USB Product ID. */
59 uint16_t bcdDevice; /**< Device Release Number (BCD). */
60 uint8_t iManufacturer; /**< Index of manufacturer string descriptor. */
61 uint8_t iProduct; /**< Index of product string descriptor. */
62 uint8_t iSerialNumber; /**< Index of string descriptor containing serial #. */
63 uint8_t bNumConfigurations; /**< Number of possible configurations. */
66 /** USB Configuration Descriptor. See USB 1.1 spec, pp. 199 - 200 */
67 struct usb_config_descriptor {
68 uint8_t bLength; /**< Size of this descriptor in bytes. */
69 uint8_t bDescriptorType; /**< CONFIGURATION descriptor type. */
70 uint16_t wTotalLength; /**< Combined total length of all descriptors. */
71 uint8_t bNumInterfaces; /**< Number of interfaces in this configuration. */
72 uint8_t bConfigurationValue; /**< Value used to select this configuration. */
73 uint8_t iConfiguration; /**< Index of configuration string descriptor. */
74 uint8_t bmAttributes; /**< Configuration characteristics. */
75 uint8_t MaxPower; /**< Maximum power consumption in 2 mA units. */
78 /** USB Interface Descriptor. See USB 1.1 spec, pp. 201 - 203 */
79 struct usb_interface_descriptor {
80 uint8_t bLength; /**< Size of this descriptor in bytes. */
81 uint8_t bDescriptorType; /**< INTERFACE descriptor type. */
82 uint8_t bInterfaceNumber; /**< Interface number. */
83 uint8_t bAlternateSetting; /**< Value used to select alternate setting. */
84 uint8_t bNumEndpoints; /**< Number of endpoints used by this interface. */
85 uint8_t bInterfaceClass; /**< Class code. */
86 uint8_t bInterfaceSubclass; /**< Subclass code. */
87 uint8_t bInterfaceProtocol; /**< Protocol code. */
88 uint8_t iInterface; /**< Index of interface string descriptor. */
91 /** USB Endpoint Descriptor. See USB 1.1 spec, pp. 203 - 204 */
92 struct usb_endpoint_descriptor {
93 uint8_t bLength; /**< Size of this descriptor in bytes. */
94 uint8_t bDescriptorType; /**< ENDPOINT descriptor type. */
95 uint8_t bEndpointAddress; /**< Endpoint Address: USB 1.1 spec, table 9-10. */
96 uint8_t bmAttributes; /**< Endpoint Attributes: USB 1.1 spec, table 9-10. */
97 uint16_t wMaxPacketSize; /**< Maximum packet size for this endpoint. */
98 uint8_t bInterval; /**< Polling interval (in ms) for this endpoint. */
101 /** USB Language Descriptor. See USB 1.1 spec, pp. 204 - 205 */
102 struct usb_language_descriptor {
103 uint8_t bLength; /**< Size of this descriptor in bytes. */
104 uint8_t bDescriptorType; /**< STRING descriptor type. */
105 uint16_t wLANGID[]; /**< LANGID codes. */
108 /** USB String Descriptor. See USB 1.1 spec, pp. 204 - 205 */
109 struct usb_string_descriptor {
110 uint8_t bLength; /**< Size of this descriptor in bytes. */
111 uint8_t bDescriptorType; /**< STRING descriptor type. */
112 uint16_t bString[]; /**< UNICODE encoded string. */
115 /********************** USB Control Endpoint 0 related *********************/
117 /** USB Control Setup Data. See USB 1.1 spec, pp. 183 - 185 */
119 uint8_t bmRequestType; /**< Characteristics of a request. */
120 uint8_t bRequest; /**< Specific request. */
121 uint16_t wValue; /**< Field that varies according to request. */
122 uint16_t wIndex; /**< Field that varies according to request. */
123 uint16_t wLength; /**< Number of bytes to transfer in data stage. */
126 /* External declarations for variables that need to be accessed outside of
128 extern volatile bool EP2_out;
129 extern volatile bool EP2_in;
130 extern volatile __xdata __at 0x7FE8 struct setup_data setup_data;
133 * USB Request Types (bmRequestType): See USB 1.1 spec, page 183, table 9-2
135 * Bit 7: Data transfer direction
143 * Bit 4...0: Recipient
151 #define USB_DIR_OUT 0x00
152 #define USB_DIR_IN 0x80
154 #define USB_REQ_TYPE_STANDARD (0x00 << 5)
155 #define USB_REQ_TYPE_CLASS (0x01 << 5)
156 #define USB_REQ_TYPE_VENDOR (0x02 << 5)
157 #define USB_REQ_TYPE_RESERVED (0x03 << 5)
159 #define USB_RECIP_DEVICE 0x00
160 #define USB_RECIP_INTERFACE 0x01
161 #define USB_RECIP_ENDPOINT 0x02
162 #define USB_RECIP_OTHER 0x03
164 /* bmRequestType for USB Standard Requests */
166 /* Clear Interface Request */
167 #define CF_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
168 #define CF_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
169 #define CF_ENDPOINT (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
171 /* Get Configuration Request */
172 #define GC_DEVICE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
174 /* Get Descriptor Request */
175 #define GD_DEVICE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
177 /* Get Interface Request */
178 #define GI_INTERFACE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
180 /* Get Status Request: See USB 1.1 spec, page 190 */
181 #define GS_DEVICE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
182 #define GS_INTERFACE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
183 #define GS_ENDPOINT (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
185 /* Set Address Request is handled by EZ-USB core */
187 /* Set Configuration Request */
188 #define SC_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
190 /* Set Descriptor Request */
191 #define SD_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
193 /* Set Feature Request */
194 #define SF_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
195 #define SF_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
196 #define SF_ENDPOINT (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
198 /* Set Interface Request */
199 #define SI_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
201 /* Synch Frame Request */
202 #define SY_ENDPOINT (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
204 /* USB Requests (bRequest): See USB 1.1 spec, table 9-4 on page 187 */
206 #define CLEAR_FEATURE 1
207 /* Value '2' is reserved for future use */
208 #define SET_FEATURE 3
209 /* Value '4' is reserved for future use */
210 #define SET_ADDRESS 5
211 #define GET_DESCRIPTOR 6
212 #define SET_DESCRIPTOR 7
213 #define GET_CONFIGURATION 8
214 #define SET_CONFIGURATION 9
215 #define GET_INTERFACE 10
216 #define SET_INTERFACE 11
217 #define SYNCH_FRAME 12
219 /* Standard Feature Selectors: See USB 1.1 spec, table 9-6 on page 188 */
220 #define DEVICE_REMOTE_WAKEUP 1
221 #define ENDPOINT_HALT 0
223 /************************** EZ-USB specific stuff **************************/
225 /** USB Interrupts. See AN2131-TRM, page 9-4 for details */
251 /*************************** Function Prototypes ***************************/
253 __xdata uint8_t *usb_get_endpoint_cs_reg(uint8_t ep);
254 void usb_reset_data_toggle(uint8_t ep);
256 bool usb_handle_get_status(void);
257 bool usb_handle_clear_feature(void);
258 bool usb_handle_set_feature(void);
259 bool usb_handle_get_descriptor(void);
260 void usb_handle_set_interface(void);
262 void usb_handle_setup_data(void);