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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
24 #include "reg_ezusb.h"
29 #define NULL (void *)0;
31 /* High and Low byte of a word (uint16_t) */
32 #define HI8(word) (uint8_t)(((uint16_t)word >> 8) & 0xff)
33 #define LO8(word) (uint8_t)((uint16_t)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 */
51 struct usb_device_descriptor
{
52 uint8_t bLength
; /* /< Size of this descriptor in bytes. */
53 uint8_t bDescriptorType
;/* /< DEVICE Descriptor Type. */
54 uint16_t bcdUSB
; /* /< USB specification release number (BCD). */
55 uint8_t bDeviceClass
; /* /< Class code. */
56 uint8_t bDeviceSubClass
;/* /< Subclass code. */
57 uint8_t bDeviceProtocol
;/* /< Protocol code. */
58 uint8_t bMaxPacketSize0
;/* /< Maximum packet size for EP0 (8, 16, 32, 64). */
59 uint16_t idVendor
; /* /< USB Vendor ID. */
60 uint16_t idProduct
; /* /< USB Product ID. */
61 uint16_t bcdDevice
; /* /< Device Release Number (BCD). */
62 uint8_t iManufacturer
; /* /< Index of manufacturer string descriptor. */
63 uint8_t iProduct
; /* /< Index of product string descriptor. */
64 uint8_t iSerialNumber
; /* /< Index of string descriptor containing serial #. */
65 uint8_t bNumConfigurations
; /* /< Number of possible configurations. */
68 /** USB Configuration Descriptor. See USB 1.1 spec, pp. 199 - 200 */
69 struct usb_config_descriptor
{
70 uint8_t bLength
; /* /< Size of this descriptor in bytes. */
71 uint8_t bDescriptorType
;/* /< CONFIGURATION descriptor type. */
72 uint16_t wTotalLength
; /* /< Combined total length of all descriptors. */
73 uint8_t bNumInterfaces
; /* /< Number of interfaces in this configuration. */
74 uint8_t bConfigurationValue
; /* /< Value used to select this configuration. */
75 uint8_t iConfiguration
; /* /< Index of configuration string descriptor. */
76 uint8_t bmAttributes
; /* /< Configuration characteristics. */
77 uint8_t MaxPower
; /* /< Maximum power consumption in 2 mA units. */
80 /** USB Interface Descriptor. See USB 1.1 spec, pp. 201 - 203 */
81 struct usb_interface_descriptor
{
82 uint8_t bLength
; /* /< Size of this descriptor in bytes. */
83 uint8_t bDescriptorType
;/* /< INTERFACE descriptor type. */
84 uint8_t bInterfaceNumber
; /* /< Interface number. */
85 uint8_t bAlternateSetting
; /* /< Value used to select alternate setting. */
86 uint8_t bNumEndpoints
; /* /< Number of endpoints used by this interface. */
87 uint8_t bInterfaceClass
;/* /< Class code. */
88 uint8_t bInterfaceSubclass
; /* /< Subclass code. */
89 uint8_t bInterfaceProtocol
; /* /< Protocol code. */
90 uint8_t iInterface
; /* /< Index of interface string descriptor. */
93 /** USB Endpoint Descriptor. See USB 1.1 spec, pp. 203 - 204 */
94 struct usb_endpoint_descriptor
{
95 uint8_t bLength
; /* /< Size of this descriptor in bytes. */
96 uint8_t bDescriptorType
;/* /< ENDPOINT descriptor type. */
97 uint8_t bEndpointAddress
; /* /< Endpoint Address: USB 1.1 spec, table 9-10. */
98 uint8_t bmAttributes
; /* /< Endpoint Attributes: USB 1.1 spec, table 9-10. */
99 uint16_t wMaxPacketSize
;/* /< Maximum packet size for this endpoint. */
100 uint8_t bInterval
; /* /< Polling interval (in ms) for this endpoint. */
103 /** USB Language Descriptor. See USB 1.1 spec, pp. 204 - 205 */
104 struct usb_language_descriptor
{
105 uint8_t bLength
; /* /< Size of this descriptor in bytes. */
106 uint8_t bDescriptorType
;/* /< STRING descriptor type. */
107 uint16_t wLANGID
[]; /* /< LANGID codes. */
110 /** USB String Descriptor. See USB 1.1 spec, pp. 204 - 205 */
111 struct usb_string_descriptor
{
112 uint8_t bLength
; /* /< Size of this descriptor in bytes. */
113 uint8_t bDescriptorType
;/* /< STRING descriptor type. */
114 uint16_t bString
[]; /* /< UNICODE encoded string. */
117 /********************** USB Control Endpoint 0 related *********************/
119 /** USB Control Setup Data. See USB 1.1 spec, pp. 183 - 185 */
121 uint8_t bmRequestType
; /* /< Characteristics of a request. */
122 uint8_t bRequest
; /* /< Specific request. */
123 uint16_t wValue
; /* /< Field that varies according to request. */
124 uint16_t wIndex
; /* /< Field that varies according to request. */
125 uint16_t 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 struct setup_data 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
uint8_t *usb_get_endpoint_cs_reg(uint8_t ep
);
256 void usb_reset_data_toggle(uint8_t 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);