embkernel
 All Classes Functions Variables Typedefs Groups Pages
UsbDefs.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #ifndef USB_DEFS_HPP_
7 #define USB_DEFS_HPP_
8 
9 #include <stdint.h>
10 #include <stddef.h>
11 
12 #define LE_WORD(x) (x&0xFF), (x>>8)
13 
14 class UsbDefs {
15 public:
16 
17  typedef enum
18  :uint8_t {
19  REQ_CODE_GET_STATUS = 0, //
20  REQ_CODE_CLEAR_FEATURE = 1, //
21  REQ_CODE_SET_FEATURE = 3, //
22  REQ_CODE_SET_ADDR = 5, //
23  REQ_CODE_GET_DESC = 6, //
24  REQ_CODE_SET_DESC = 7, //
25  REQ_CODE_GET_CONFIG = 8, //
26  REQ_CODE_SET_CONFIG = 9, //
27  REQ_CODE_GET_INTERFACE = 10, //
28  REQ_CODE_SET_INTERFACE = 11, //
29  REQ_CODE_SYNCH_FRAME = 12, //
30  } SETUP_REQ_CODE;
31 
32  typedef enum {
33  RECIPIENT_DEVICE = 0,
34  RECIPIENT_INTERFACE = 1,
35  RECIPIENT_ENDPOINT = 2,
36  RECIPIENT_OTHER = 3,
37  } SETUP_RECIPIENT;
38 
39  typedef enum {
40  TYPE_STANDARD = 0,
41  TYPE_CLASS = 1,
42  TYPE_VENDOR = 2,
43  TYPE_RESERVED = 3,
44  } SETUP_TYPE;
45 
46  typedef enum {
47  DIR_HOST_TO_DEVICE = 0,
48  DIR_DEVICE_TO_HOST = 1,
49  } DIRECTION;
50 
51  typedef struct {
52  union {
53  struct {
54  SETUP_RECIPIENT recipient :5;
55  SETUP_TYPE type :2;
56  DIRECTION dir :1;
57  } bits;
58  uint8_t value;
59  } bmRequestType;
60  SETUP_REQ_CODE bRequest;
61  uint16_t wValue;
62  uint16_t wIndex;
63  uint16_t wLength;
64  }__attribute__((__packed__)) USB_SETUP_PACKET;
65 
66  typedef enum
67  :uint8_t {
68  DESC_TYPE_DEVICE = 1, //
69  DESC_TYPE_CONFIG = 2, //
70  DESC_TYPE_STRING = 3, //
71  DESC_TYPE_INTERFACE = 4, //
72  DESC_TYPE_ENDPOINT = 5, //
73  DESC_TYPE_DEVICE_QUALIFIER = 6, //
74  DESC_TYPE_OTHER_SPEED_CONF = 7, //
75  DESC_TYPE_INTERFACE_POWER = 8, //
76  } DESC_TYPE;
77 
78  typedef union {
79  struct {
80  uint8_t epn :7;
81  DIRECTION dir :1;
82  } bits;
83  uint8_t value;
84  } ENDPOINT;
85 
86  typedef union {
87  struct {
88  uint16_t zero :14;
89  uint16_t remoteWakeup :1;
90  uint16_t selfPowered :1;
91  } bits;
92  uint16_t value;
93  } USB_DEVICE_STATUS;
94 
95  typedef struct {
96  uint8_t bLength;
97  uint8_t bDescriptorType;
98  uint16_t bcdUSB;
99  uint8_t bDeviceClass;
100  uint8_t bDeviceSubClass;
101  uint8_t bDeviceProtocol;
102  uint8_t bMaxPacketSize0;
103  uint16_t idVendor;
104  uint16_t idProduct;
105  uint16_t bcdDevice;
106  uint8_t iManufacturer;
107  uint8_t iProduct;
108  uint8_t iSerialNumber;
109  uint8_t bNumConfigurations;
110  }__attribute__((__packed__)) USB_DESC_DEVICE;
111 
112  typedef struct {
113  uint8_t bLength;
114  uint8_t bDescriptorType;
115  uint16_t wTotalLength;
116  uint8_t bNumInterfaces;
117  uint8_t bConfigurationValue;
118  uint8_t iConfiguration;
119  uint8_t bmAttributes;
120  uint8_t MaxPower;
121  }__attribute__((__packed__)) USB_DESC_CONFIG;
122 
123  const static uint8_t DESC_CONFIG_ATTR_RESERVED = 0x80; //Must always be set
124  const static uint8_t DESC_CONFIG_ATTR_SELF_POWERED = 0x40;
125  const static uint8_t DESC_CONFIG_ATTR_REMOTE_WAKEUP = 0x20;
126 
127  typedef enum
128  :uint8_t {
129  CLASS_DEVICE = 0x00, //
130  CLASS_AUDIO = 0x01, //
131  CLASS_CDC = 0x02, //
132  CLASS_HID = 0x03, //
133  CLASS_PHYSICAL = 0x05, //
134  CLASS_IMAGE = 0x06, //
135  CLASS_PRINTER = 0x07, //
136  CLASS_MASS_STORAGE = 0x08, //
137  CLASS_HUB = 0x09, //
138  CLASS_CDC_DATA = 0x0a, //
139  CLASS_SMART_CARD = 0x0b, //
140  CLASS_SECURITY = 0x0d, //
141  CLASS_VIDEO = 0x0e, //
142  CLASS_HEALTHCARE = 0x0f, //
143  CLASS_DIAG_DEVICE = 0xdc, //
144  CLASS_WIRELESS = 0xe0, //
145  CLASS_MISC = 0xef, //
146  CLASS_APP_SPECIFIC = 0xfe, //
147  CLASS_VEND_SPECIFIC = 0xff //
148  } CLASS;
149 
150  typedef enum
151  :uint8_t {
152  SUB_CLASS_LOG_OUT = 0x65, //
153  SUB_CLASS_COM = 0x66, //
154  SUB_CLASS_TRACE = 0x67
155  } EMBKERNEL_SUB_CLASS;
156 
157  typedef struct {
158  uint8_t bLength;
159  uint8_t bDescriptorType;
160  uint8_t bInterfaceNumber;
161  uint8_t bAlternateSetting;
162  uint8_t bNumEndpoints;
163  CLASS bInterfaceClass;
164  uint8_t bInterfaceSubClass;
165  uint8_t bInterfaceProtocol;
166  uint8_t iInterface;
167  }__attribute__((__packed__)) USB_DESC_INTERFACE;
168 
169  typedef enum {
170  EP_TRANSF_TYPE_CTRL = 0x00,
171  EP_TRANSF_TYPE_ISO = 0x01,
172  EP_TRANSF_TYPE_BULK = 0x02,
173  EP_TRANSF_TYPE_INT = 0x03,
174  } EP_TRANSF_TYPE;
175 
176  typedef struct {
177  uint8_t bLength;
178  uint8_t bDescriptorType;
179  struct {
180  uint8_t epn :4;
181  uint8_t zero :3;
182  DIRECTION dir :1;
183  } bEndpointAddress;
184  struct {
185  EP_TRANSF_TYPE transferType :2;
186  uint8_t sync :2;
187  uint8_t usage :2;
188  uint8_t unused :2;
189  } bmAttributes;
190  uint16_t wMaxPacketSize;
191  uint8_t bInterval;
192  }__attribute__((__packed__)) USB_DESC_EP;
193 
194  typedef struct {
195  uint8_t bLength;
196  uint8_t bDescriptorType;
197  }__attribute__((__packed__)) USB_DESC_COMMON;
198 
199  const static size_t MAX_PACKET_SIZE = 64;
200 
201  typedef struct {
202  uint16_t len;
203  uint8_t epn;
204  uint8_t isSetup;
205  } PKT_HEADER;
206 
207  typedef struct {
208  PKT_HEADER header;
209  union {
210  uint8_t bytes[MAX_PACKET_SIZE];
211  } data;
212  }__attribute__((__packed__)) PKT;
213 
214 // const static uint8_t FEAT_SEL_ENDPOINT_HALT = 0;
215 // const static uint8_t FEAT_SEL_DEV_REMOTE_WAKEUP = 1;
216 // const static uint8_t FEAT_SEL_TEST_MODE = 2;
217 //
218 // const static uint8_t DEVICE_CLASS_RESERVED = 0x00;
219 // const static uint8_t DEVICE_CLASS_AUDIO = 0x01;
220 // const static uint8_t DEVICE_CLASS_COMMUNICATIONS = 0x02;
221 // const static uint8_t DEVICE_CLASS_HUMAN_INTERFACE = 0x03;
222 // const static uint8_t DEVICE_CLASS_MONITOR = 0x04;
223 // const static uint8_t DEVICE_CLASS_PHYSICAL_INTERFACE = 0x05;
224 // const static uint8_t DEVICE_CLASS_POWER = 0x06;
225 // const static uint8_t DEVICE_CLASS_PRINTER = 0x07;
226 // const static uint8_t DEVICE_CLASS_STORAGE = 0x08;
227 // const static uint8_t DEVICE_CLASS_HUB = 0x09;
228 // const static uint8_t DEVICE_CLASS_MISCELLANEOUS = 0xEF;
229 // const static uint8_t DEVICE_CLASS_VENDOR_SPECIFIC = 0xFF;
230 //
231 // const static uint8_t DESC_EP_ATTR_SYNC_NO = 0x00;
232 // const static uint8_t DESC_EP_ATTR_SYNC_ASYNC = 0x04;
233 // const static uint8_t DESC_EP_ATTR_SYNC_ADAPTATIVE = 0x08;
234 // const static uint8_t DESC_EP_ATTR_TYPE_SYNC = 0x0C;
235 //
236 // const static uint8_t DESC_EP_ATTR_USAGE_DATA = 0x00;
237 // const static uint8_t DESC_EP_ATTR_USAGE_FEEDBACK = 0x40;
238 // const static uint8_t DESC_EP_ATTR_USAGE_IMP_FEED = 0x80;
239 // const static uint8_t DESC_EP_ATTR_USAGE_RESERVER = 0xC0;
240 //
241 // const static uint8_t ENDPOINT_TYPE_MASK = 0x03;
242 // const static uint8_t ENDPOINT_TYPE_CONTROL = 0x00;
243 // const static uint8_t ENDPOINT_TYPE_ISOCHRONOUS = 0x01;
244 // const static uint8_t ENDPOINT_TYPE_BULK = 0x02;
245 // const static uint8_t ENDPOINT_TYPE_INTERRUPT = 0x03;
246 // const static uint8_t ENDPOINT_SYNC_MASK = 0x0C;
247 // const static uint8_t ENDPOINT_SYNC_NO_SYNCHRONIZATION = 0x00;
248 // const static uint8_t ENDPOINT_SYNC_ASYNCHRONOUS = 0x04;
249 // const static uint8_t ENDPOINT_SYNC_ADAPTIVE = 0x08;
250 // const static uint8_t ENDPOINT_SYNC_SYNCHRONOUS = 0x0C;
251 // const static uint8_t ENDPOINT_USAGE_MASK = 0x30;
252 // const static uint8_t ENDPOINT_USAGE_DATA = 0x00;
253 // const static uint8_t ENDPOINT_USAGE_FEEDBACK = 0x10;
254 // const static uint8_t ENDPOINT_USAGE_IMPLICIT_FEEDBACK = 0x20;
255 // const static uint8_t ENDPOINT_USAGE_RESERVED = 0x30;
256 //
257 // const static uint8_t CONF_ATTR_SELF_PWR = 0xC0;
258 // const static uint8_t CONF_ATTR_BUS_PWR = 0x80;
259 // const static uint8_t CONF_ATTR_RWAKE = 0xA0;
260 
261 };
262 
263 #endif /* USB_DEFS_HPP_ */