Wixel SDK
usb.h
Go to the documentation of this file.
1 
21 #ifndef _USB_H
22 #define _USB_H
23 
24 #include <cc2511_types.h>
25 
30 #define USB_VENDOR_ID_POLOLU 0x1FFB
31 
34 #define USB_EP0_PACKET_SIZE 32
35 
36 /* USB CONSTANTS **************************************************************/
37 
41 {
42  USB_STATE_DETACHED = 0,
43  USB_STATE_ATTACHED = 1,
44  USB_STATE_POWERED = 2,
45  USB_STATE_DEFAULT = 4,
46  USB_STATE_ADDRESS = 8,
47  USB_STATE_CONFIGURED = 16
48 };
49 
50 // SETUP_PACKET.bRequest values from USB Spec Table 9-4
51 #define USB_REQUEST_GET_STATUS 0u
52 #define USB_REQUEST_CLEAR_FEATURE 1u
53 #define USB_REQUEST_SET_FEATURE 3u
54 #define USB_REQUEST_SET_ADDRESS 5u
55 #define USB_REQUEST_GET_DESCRIPTOR 6u
56 #define USB_REQUEST_SET_DESCRIPTOR 7u
57 #define USB_REQUEST_GET_CONFIGURATION 8u
58 #define USB_REQUEST_SET_CONFIGURATION 9u
59 #define USB_REQUEST_GET_INTERFACE 10u
60 #define USB_REQUEST_SET_INTERFACE 11u
61 #define USB_REQUEST_SYNCH_FRAME 12u
62 
63 // SETUP_PACKET.request_type values from USB Spec Table 9-2
64 #define USB_REQUEST_TYPE_STANDARD 0u
65 #define USB_REQUEST_TYPE_CLASS 1u
66 #define USB_REQUEST_TYPE_VENDOR 2u
67 
68 // SETUP_PACKET.recipient values from USB Spec Table 9-2
69 #define USB_RECIPIENT_DEVICE 0u
70 #define USB_RECIPIENT_INTERFACE 1u
71 #define USB_RECIPIENT_ENDPOINT 2u
72 #define USB_RECIPIENT_OTHER 3u
73 
74 // Descriptor types from USB Spec Table 9-5
75 #define USB_DESCRIPTOR_TYPE_DEVICE 1u
76 #define USB_DESCRIPTOR_TYPE_CONFIGURATION 2u
77 #define USB_DESCRIPTOR_TYPE_STRING 3u
78 #define USB_DESCRIPTOR_TYPE_INTERFACE 4u
79 #define USB_DESCRIPTOR_TYPE_ENDPOINT 5u
80 #define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 6u
81 #define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION 7u
82 #define USB_DESCRIPTOR_TYPE_INTERFACE_POWER 8u
83 #define USB_DESCRIPTOR_TYPE_ENDPOINT_CLASS_SPECIFIC 0x25u
84 #define USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION 0x0Bu
85 
86 // setup_packet feature selectors, USB Spec Table 9-6, Standard Feature Selectors
87 #define USB_FEATURE_DEVICE_REMOTE_WAKEUP 1u
88 #define USB_FEATURE_ENDPOINT_HALT 0u
89 #define USB_FEATURE_TEST_MODE 2u
90 
91 // Configuration Decriptor bmAttributes bitmasks (USB Spec Table 9-10)
92 #define USB_CONFIG_ATTR_DEFAULT (1<<7) //Default Value (bit 7 must be set to 1)
93 #define USB_CONFIG_ATTR_SELF_POWERED (1<<6) //Self-powered
94 #define USB_CONFIG_ATTR_REMOTE_WAKEUP (1<<5) //Remote Wakeup
95 
96 // Values for usbSetupPacket.direction bit.
97 #define USB_OUT 0u
98 #define USB_IN 1u
99 
100 // USB Endpoint addresses, to be used in descriptors
101 // To get the address of an IN endpoint, do: USB_ENDPOINT_ADDRESS_IN | 1
102 #define USB_ENDPOINT_ADDRESS_IN 0x80
103 #define USB_ENDPOINT_ADDRESS_OUT 0x00
104 
105 // Endpoint transfer types, used in endpoint descriptors (USB Spec Table 9-13)
106 #define USB_TRANSFER_TYPE_CONTROL 0
107 #define USB_TRANSFER_TYPE_ISOCHRONOUS 1
108 #define USB_TRANSFER_TYPE_BULK 2
109 #define USB_TRANSFER_TYPE_INTERRUPT 3
110 
113 #define USB_LANGUAGE_EN_US 0x0409
114 
115 /* USB STRUCTS ****************************************************************/
116 
117 typedef union
118 {
120  struct
121  {
122  uint8 bmRequestType;
123  uint8 bRequest;
124  uint16 wValue;
125  uint16 wIndex;
126  uint16 wLength;
127  };
128 
130  struct
131  {
132  unsigned recipient:5; //see REQUEST_RECIPIENT_*
133  unsigned requestType:2; //see REQUEST_TYPE_*
134  unsigned direction:1; //0=USB_OUT,1=USB_IN
135  };
137 
139 typedef struct USB_DESCRIPTOR_DEVICE
140 {
156 
159 {
169 
174 {
184 
187 {
198 
201 {
209 
210 /* CC2511 USB CONSTANTS *******************************************************/
211 
212 // USBCSOL register bit values
213 #define USBCSOL_OUTPKT_RDY 0x01
214 
215 // USBCSIL register bit values
216 #define USBCSIL_INPKT_RDY 0x01
217 #define USBCSIL_PKT_PRESENT 0x02
218 
219 /* HELPERS ********************************************************************/
220 
230 #define DEFINE_STRING_DESCRIPTOR(name,char_count,...) static uint16 CODE name[] = { (2*(char_count+1)) | (USB_DESCRIPTOR_TYPE_STRING<<8), __VA_ARGS__ };
231 
232 /* PROTOTYPES DEFINED BY LIBUSB ***********************************************/
233 
241 
248 
251 void usbInit(void);
252 
259 void usbPoll(void);
260 
270 void usbControlRead(uint16 bytesCount, uint8 XDATA * source);
271 
282 void usbControlWrite(uint16 bytesCount, uint8 XDATA * source);
283 
291 void usbControlAcknowledge(void);
292 
304 void usbControlStall(void);
305 
310 void usbInitEndpointIn(uint8 endpointNumber, uint8 maxPacketSize);
311 
316 void usbInitEndpointOut(uint8 endpointNumber, uint8 maxPacketSize);
317 
323 void usbWriteFifo(uint8 endpointNumber, uint8 count, const uint8 XDATA * buffer);
324 
330 void usbReadFifo(uint8 endpointNumber, uint8 count, uint8 XDATA * buffer);
331 
337 BIT usbSuspended(void);
338 
354 void usbSleep(void);
355 
382 extern volatile BIT usbSuspendMode;
383 
404 void usbShowStatusWithGreenLed(void);
405 
414 extern volatile BIT usbActivityFlag;
415 
416 /* HIGH-LEVEL CALLBACKS AND DATA STRUCTURES REQUIRED BY usb.c *****************/
417 // usb.c requires these high-level callbacks and data structures:
418 
424 
430 
438 
450 void usbCallbackSetupHandler(void);
451 
467 
474 void usbCallbackInitEndpoints(void);
475 
484 
485 #endif
BIT usbSuspended(void)
Definition: usb.c:537
struct USB_DESCRIPTOR_DEVICE USB_DESCRIPTOR_DEVICE
void usbCallbackInitEndpoints(void)
Definition: usb_cdc_acm.c:234
uint8 bDeviceSubClass
Definition: usb.h:145
uint8 CODE usbStringDescriptorCount
Definition: usb_cdc_acm.c:224
uint16 bcdDevice
Definition: usb.h:150
uint16 idVendor
Definition: usb.h:148
void usbInitEndpointOut(uint8 endpointNumber, uint8 maxPacketSize)
Definition: usb.c:642
volatile BIT usbActivityFlag
Definition: usb.c:27
#define XDATA
Definition: cc2511_types.h:65
uint8 bDescriptorType
Definition: usb.h:142
enum USB_DEVICE_STATES XDATA usbDeviceState
Definition: usb.c:18
void usbControlWrite(uint16 bytesCount, uint8 XDATA *source)
Definition: usb.c:617
uint8 bDeviceClass
Definition: usb.h:144
USB_DESCRIPTOR_DEVICE CODE usbDeviceDescriptor
Definition: usb_cdc_acm.c:109
uint16 idProduct
Definition: usb.h:149
struct USB_DESCRIPTOR_CONFIGURATION USB_DESCRIPTOR_CONFIGURATION
USB_DEVICE_STATES
Definition: usb.h:40
struct USB_DESCRIPTOR_INTERFACE USB_DESCRIPTOR_INTERFACE
struct USB_DESCRIPTOR_ENDPOINT USB_DESCRIPTOR_ENDPOINT
uint16 wMaxPacketSize
Definition: usb.h:206
USB_SETUP_PACKET XDATA usbSetupPacket
Definition: usb.c:17
void usbShowStatusWithGreenLed(void)
Definition: green_led.c:13
void usbWriteFifo(uint8 endpointNumber, uint8 count, const uint8 XDATA *buffer)
Definition: usb.c:46
void usbInitEndpointIn(uint8 endpointNumber, uint8 maxPacketSize)
Definition: usb.c:635
void usbReadFifo(uint8 endpointNumber, uint8 count, uint8 XDATA *buffer)
Definition: usb.c:34
__bit BIT
Definition: cc2511_types.h:32
void usbPoll(void)
Definition: usb.c:73
uint8 iSerialNumber
Definition: usb.h:153
unsigned short uint16
Definition: cc2511_types.h:15
uint8 bInterfaceNumber
Definition: usb.h:190
uint8 bDeviceProtocol
Definition: usb.h:146
uint8 bAlternateSetting
Definition: usb.h:191
void usbSleep(void)
Definition: usb.c:556
void usbCallbackSetupHandler(void)
Definition: usb_cdc_acm.c:246
void usbControlStall(void)
Definition: usb.c:630
uint8 bDescriptorType
Definition: usb.h:203
uint8 bNumConfigurations
Definition: usb.h:154
unsigned char uint8
Definition: cc2511_types.h:9
void usbControlAcknowledge(void)
Definition: usb.c:624
void usbInit(void)
Definition: usb.c:29
uint8 bMaxPacketSize0
Definition: usb.h:147
void usbCallbackControlWriteHandler(void)
Definition: usb_cdc_acm.c:281
uint8 bEndpointAddress
Definition: usb.h:204
void usbControlRead(uint16 bytesCount, uint8 XDATA *source)
Definition: usb.c:610
void usbCallbackClassDescriptorHandler(void)
Definition: usb_cdc_acm.c:271
uint8 iManufacturer
Definition: usb.h:151
volatile BIT usbSuspendMode
Definition: usb.c:24
uint16 CODE *CODE usbStringDescriptors[]
Definition: usb_cdc_acm.c:228
uint8 bInterfaceProtocol
Definition: usb.h:195
struct USB_DESCRIPTOR_INTERFACE_ASSOCIATION USB_DESCRIPTOR_INTERFACE_ASSOCIATION
#define CODE
Definition: cc2511_types.h:39
uint8 bInterfaceSubClass
Definition: usb.h:194