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, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "shorttypes.h"
25 #include "reg_ezusb.h"
29 #define NULL (void*)0;
31 /* High and Low byte of a word (u16) */
32 #define HI8(word) (u8)(((u16)word >> 8) & 0xff)
33 #define LO8(word) (u8)((u16)word & 0xff)
35 /* Convenience functions */
36 #define STALL_EP0() EP0CS |= EP0STALL
37 #define CLEAR_IRQ() EXIF &= ~USBINT
39 /*********** USB descriptors. See section 9.5 of the USB 1.1 spec **********/
41 /* USB Descriptor Types. See USB 1.1 spec, page 187, table 9-5 */
42 #define DESCRIPTOR_TYPE_DEVICE 0x01
43 #define DESCRIPTOR_TYPE_CONFIGURATION 0x02
44 #define DESCRIPTOR_TYPE_STRING 0x03
45 #define DESCRIPTOR_TYPE_INTERFACE 0x04
46 #define DESCRIPTOR_TYPE_ENDPOINT 0x05
48 #define STR_DESCR(len,...) { len*2+2, DESCRIPTOR_TYPE_STRING, { __VA_ARGS__ } }
50 /** USB Device Descriptor. See USB 1.1 spec, pp. 196 - 198 */
52 u8 bLength; ///< Size of this descriptor in bytes.
53 u8 bDescriptorType; ///< DEVICE Descriptor Type.
54 u16 bcdUSB; ///< USB specification release number (BCD).
55 u8 bDeviceClass; ///< Class code.
56 u8 bDeviceSubClass; ///< Subclass code.
57 u8 bDeviceProtocol; ///< Protocol code.
58 u8 bMaxPacketSize0; ///< Maximum packet size for EP0 (8, 16, 32, 64).
59 u16 idVendor; ///< USB Vendor ID.
60 u16 idProduct; ///< USB Product ID.
61 u16 bcdDevice; ///< Device Release Number (BCD).
62 u8 iManufacturer; ///< Index of manufacturer string descriptor.
63 u8 iProduct; ///< Index of product string descriptor.
64 u8 iSerialNumber; ///< Index of string descriptor containing serial #.
65 u8 bNumConfigurations; ///< Number of possible configurations.
66 } usb_device_descriptor_t;
68 /** USB Configuration Descriptor. See USB 1.1 spec, pp. 199 - 200 */
70 u8 bLength; ///< Size of this descriptor in bytes.
71 u8 bDescriptorType; ///< CONFIGURATION descriptor type.
72 u16 wTotalLength; ///< Combined total length of all descriptors.
73 u8 bNumInterfaces; ///< Number of interfaces in this configuration.
74 u8 bConfigurationValue; ///< Value used to select this configuration.
75 u8 iConfiguration; ///< Index of configuration string descriptor.
76 u8 bmAttributes; ///< Configuration characteristics.
77 u8 MaxPower; ///< Maximum power consumption in 2 mA units.
78 } usb_config_descriptor_t;
80 /** USB Interface Descriptor. See USB 1.1 spec, pp. 201 - 203 */
82 u8 bLength; ///< Size of this descriptor in bytes.
83 u8 bDescriptorType; ///< INTERFACE descriptor type.
84 u8 bInterfaceNumber; ///< Interface number.
85 u8 bAlternateSetting; ///< Value used to select alternate setting.
86 u8 bNumEndpoints; ///< Number of endpoints used by this interface.
87 u8 bInterfaceClass; ///< Class code.
88 u8 bInterfaceSubclass; ///< Subclass code.
89 u8 bInterfaceProtocol; ///< Protocol code.
90 u8 iInterface; ///< Index of interface string descriptor.
91 } usb_interface_descriptor_t;
93 /** USB Endpoint Descriptor. See USB 1.1 spec, pp. 203 - 204 */
95 u8 bLength; ///< Size of this descriptor in bytes.
96 u8 bDescriptorType; ///< ENDPOINT descriptor type.
97 u8 bEndpointAddress; ///< Endpoint Address: USB 1.1 spec, table 9-10.
98 u8 bmAttributes; ///< Endpoint Attributes: USB 1.1 spec, table 9-10.
99 u16 wMaxPacketSize; ///< Maximum packet size for this endpoint.
100 u8 bInterval; ///< Polling interval (in ms) for this endpoint.
101 } usb_endpoint_descriptor_t;
103 /** USB Language Descriptor. See USB 1.1 spec, pp. 204 - 205 */
105 u8 bLength; ///< Size of this descriptor in bytes.
106 u8 bDescriptorType; ///< STRING descriptor type.
107 u16 wLANGID[]; ///< LANGID codes.
108 } usb_language_descriptor_t;
110 /** USB String Descriptor. See USB 1.1 spec, pp. 204 - 205 */
112 u8 bLength; ///< Size of this descriptor in bytes.
113 u8 bDescriptorType; ///< STRING descriptor type.
114 u16 bString[]; ///< UNICODE encoded string.
115 } usb_string_descriptor_t;
117 /********************** USB Control Endpoint 0 related *********************/
119 /** USB Control Setup Data. See USB 1.1 spec, pp. 183 - 185 */
121 u8 bmRequestType; ///< Characteristics of a request.
122 u8 bRequest; ///< Specific request.
123 u16 wValue; ///< Field that varies according to request.
124 u16 wIndex; ///< Field that varies according to request.
125 u16 wLength; ///< Number of bytes to transfer in data stage.
128 /* External declarations for variables that need to be accessed outside of
130 extern volatile bool EP2_out;
131 extern volatile bool EP2_in;
132 extern volatile __xdata __at 0x7FE8 setup_data_t setup_data;
135 * USB Request Types (bmRequestType): See USB 1.1 spec, page 183, table 9-2
137 * Bit 7: Data transfer direction
145 * Bit 4...0: Recipient
153 #define USB_DIR_OUT 0x00
154 #define USB_DIR_IN 0x80
156 #define USB_REQ_TYPE_STANDARD (0x00 << 5)
157 #define USB_REQ_TYPE_CLASS (0x01 << 5)
158 #define USB_REQ_TYPE_VENDOR (0x02 << 5)
159 #define USB_REQ_TYPE_RESERVED (0x03 << 5)
161 #define USB_RECIP_DEVICE 0x00
162 #define USB_RECIP_INTERFACE 0x01
163 #define USB_RECIP_ENDPOINT 0x02
164 #define USB_RECIP_OTHER 0x03
166 /* bmRequestType for USB Standard Requests */
168 /* Clear Interface Request */
169 #define CF_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
170 #define CF_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
171 #define CF_ENDPOINT (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
173 /* Get Configuration Request */
174 #define GC_DEVICE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
176 /* Get Descriptor Request */
177 #define GD_DEVICE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
179 /* Get Interface Request */
180 #define GI_INTERFACE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
182 /* Get Status Request: See USB 1.1 spec, page 190 */
183 #define GS_DEVICE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
184 #define GS_INTERFACE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
185 #define GS_ENDPOINT (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
187 /* Set Address Request is handled by EZ-USB core */
189 /* Set Configuration Request */
190 #define SC_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
192 /* Set Descriptor Request */
193 #define SD_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
195 /* Set Feature Request */
196 #define SF_DEVICE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
197 #define SF_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
198 #define SF_ENDPOINT (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
200 /* Set Interface Request */
201 #define SI_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
203 /* Synch Frame Request */
204 #define SY_ENDPOINT (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
206 /* USB Requests (bRequest): See USB 1.1 spec, table 9-4 on page 187 */
208 #define CLEAR_FEATURE 1
209 // Value '2' is reserved for future use
210 #define SET_FEATURE 3
211 // Value '4' is reserved for future use
212 #define SET_ADDRESS 5
213 #define GET_DESCRIPTOR 6
214 #define SET_DESCRIPTOR 7
215 #define GET_CONFIGURATION 8
216 #define SET_CONFIGURATION 9
217 #define GET_INTERFACE 10
218 #define SET_INTERFACE 11
219 #define SYNCH_FRAME 12
221 /* Standard Feature Selectors: See USB 1.1 spec, table 9-6 on page 188 */
222 #define DEVICE_REMOTE_WAKEUP 1
223 #define ENDPOINT_HALT 0
225 /************************** EZ-USB specific stuff **************************/
227 /** USB Interrupts. See AN2131-TRM, page 9-4 for details */
253 /*************************** Function Prototypes ***************************/
255 __xdata u8* usb_get_endpoint_cs_reg(u8 ep);
256 void usb_reset_data_toggle(u8 ep);
258 bool usb_handle_get_status(void);
259 bool usb_handle_clear_feature(void);
260 bool usb_handle_set_feature(void);
261 bool usb_handle_get_descriptor(void);
262 void usb_handle_set_interface(void);
264 void usb_handle_setup_data(void);