USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Usb.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16  */
17 /* USB functions */
18 #ifndef _usb_h_
19 #define _usb_h_
20 
21 //#define BOARD_BLACK_WIDDOW
22 
23 #define USB_METHODS_INLINE
24 
25 #include <inttypes.h>
26 
27 #include <assert.h>
28 
29 #include "avrpins.h"
30 #include "max3421e.h"
31 #include "usbhost.h"
32 #include "usb_ch9.h"
33 #include "address.h"
34 
35 #if defined(ARDUINO) && ARDUINO >=100
36 #include "Arduino.h"
37 #else
38 #include <WProgram.h>
39 #endif
40 #include "message.h"
41 
42 /* shield pins. First parameter - SS pin, second parameter - INT pin */
43 
44 #if defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
45 #define BOARD_TEENSY_PLUS_PLUS
46 #endif
47 
48 #ifdef BOARD_BLACK_WIDDOW
49 typedef MAX3421e<P6, P3> MAX3421E; // Black Widow
50 #elif defined(BOARD_TEENSY_PLUS_PLUS)
51 typedef MAX3421e<P9, P8> MAX3421E; // Teensy++ 2.0 & 1.0
52 #elif defined(BOARD_MEGA_ADK)
53 typedef MAX3421e<P53, P54> MAX3421E; // Arduino Mega ADK
54 #elif defined(BOARD_BALANDUINO)
55 typedef MAX3421e<P20, P19> MAX3421E; // Balanduino
56 #else
57 typedef MAX3421e<P10, P9> MAX3421E; // Official Arduinos (UNO, Duemilanove, Mega, 2560, Leonardo etc.)
58 #endif
59 
60 //Debug macros. In 1.0 it is possible to move strings to PROGMEM by defining USBTRACE (Serial.print(F(s)))
61 #define USBTRACE(s) (Notify(PSTR(s), 0x80))
62 #define USBTRACE2(s,r) (Notify(PSTR(s), 0x80), PrintHex((r), 0x80), Notify(PSTR("\r\n"), 0x80))
63 
64 
65 
66 /* Common setup data constant combinations */
67 #define bmREQ_GET_DESCR USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_DEVICE //get descriptor request type
68 #define bmREQ_SET USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_DEVICE //set request type for all but 'set feature' and 'set interface'
69 #define bmREQ_CL_GET_INTF USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE //get interface request type
70 
71 // D7 data transfer direction (0 - host-to-device, 1 - device-to-host)
72 // D6-5 Type (0- standard, 1 - class, 2 - vendor, 3 - reserved)
73 // D4-0 Recipient (0 - device, 1 - interface, 2 - endpoint, 3 - other, 4..31 - reserved)
74 
75 // USB Device Classes
76 #define USB_CLASS_USE_CLASS_INFO 0x00 // Use Class Info in the Interface Descriptors
77 #define USB_CLASS_AUDIO 0x01 // Audio
78 #define USB_CLASS_COM_AND_CDC_CTRL 0x02 // Communications and CDC Control
79 #define USB_CLASS_HID 0x03 // HID
80 #define USB_CLASS_PHYSICAL 0x05 // Physical
81 #define USB_CLASS_IMAGE 0x06 // Image
82 #define USB_CLASS_PRINTER 0x07 // Printer
83 #define USB_CLASS_MASS_STORAGE 0x08 // Mass Storage
84 #define USB_CLASS_HUB 0x09 // Hub
85 #define USB_CLASS_CDC_DATA 0x0a // CDC-Data
86 #define USB_CLASS_SMART_CARD 0x0b // Smart-Card
87 #define USB_CLASS_CONTENT_SECURITY 0x0d // Content Security
88 #define USB_CLASS_VIDEO 0x0e // Video
89 #define USB_CLASS_PERSONAL_HEALTH 0x0f // Personal Healthcare
90 #define USB_CLASS_DIAGNOSTIC_DEVICE 0xdc // Diagnostic Device
91 #define USB_CLASS_WIRELESS_CTRL 0xe0 // Wireless Controller
92 #define USB_CLASS_MISC 0xef // Miscellaneous
93 #define USB_CLASS_APP_SPECIFIC 0xfe // Application Specific
94 #define USB_CLASS_VENDOR_SPECIFIC 0xff // Vendor Specific
95 
96 // Additional Error Codes
97 #define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED 0xD1
98 #define USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE 0xD2
99 #define USB_ERROR_UNABLE_TO_REGISTER_DEVICE_CLASS 0xD3
100 #define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL 0xD4
101 #define USB_ERROR_HUB_ADDRESS_OVERFLOW 0xD5
102 #define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL 0xD6
103 #define USB_ERROR_EPINFO_IS_NULL 0xD7
104 #define USB_ERROR_INVALID_ARGUMENT 0xD8
105 #define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE 0xD9
106 #define USB_ERROR_INVALID_MAX_PKT_SIZE 0xDA
107 #define USB_ERROR_EP_NOT_FOUND_IN_TBL 0xDB
108 #define USB_ERROR_TRANSFER_TIMEOUT 0xFF
109 
111 public:
112  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed) = 0;
113  virtual uint8_t Release() = 0;
114  virtual uint8_t Poll() = 0;
115  virtual uint8_t GetAddress() = 0;
116 };
117 
118 #define USB_XFER_TIMEOUT 5000 //USB transfer timeout in milliseconds, per section 9.2.6.1 of USB 2.0 spec
119 //#define USB_NAK_LIMIT 32000 //NAK limit for a transfer. 0 means NAKs are not counted
120 #define USB_RETRY_LIMIT 3 //retry limit for a transfer
121 #define USB_SETTLE_DELAY 200 //settle delay in milliseconds
122 
123 #define USB_NUMDEVICES 16 //number of USB devices
124 //#define HUB_MAX_HUBS 7 // maximum number of hubs that can be attached to the host controller
125 #define HUB_PORT_RESET_DELAY 20 // hub port reset delay 10 ms recomended, can be up to 20 ms
126 
127 /* USB state machine states */
128 #define USB_STATE_MASK 0xf0
129 
130 #define USB_STATE_DETACHED 0x10
131 #define USB_DETACHED_SUBSTATE_INITIALIZE 0x11
132 #define USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE 0x12
133 #define USB_DETACHED_SUBSTATE_ILLEGAL 0x13
134 #define USB_ATTACHED_SUBSTATE_SETTLE 0x20
135 #define USB_ATTACHED_SUBSTATE_RESET_DEVICE 0x30
136 #define USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE 0x40
137 #define USB_ATTACHED_SUBSTATE_WAIT_SOF 0x50
138 #define USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE 0x60
139 #define USB_STATE_ADDRESSING 0x70
140 #define USB_STATE_CONFIGURING 0x80
141 #define USB_STATE_RUNNING 0x90
142 #define USB_STATE_ERROR 0xa0
143 
144 /* USB Setup Packet Structure */
145 typedef struct {
146 
147  union { // offset description
148  uint8_t bmRequestType; // 0 Bit-map of request type
149 
150  struct {
151  uint8_t recipient : 5; // Recipient of the request
152  uint8_t type : 2; // Type of request
153  uint8_t direction : 1; // Direction of data X-fer
154  } __attribute__((packed));
155  } ReqType_u;
156  uint8_t bRequest; // 1 Request
157 
158  union {
159  uint16_t wValue; // 2 Depends on bRequest
160 
161  struct {
162  uint8_t wValueLo;
163  uint8_t wValueHi;
164  } __attribute__((packed));
165  } wVal_u;
166  uint16_t wIndex; // 4 Depends on bRequest
167  uint16_t wLength; // 6 Depends on bRequest
168 } SETUP_PKT, *PSETUP_PKT __attribute__((packed));
169 
170 
171 
172 // Base class for incomming data parser
173 
175 public:
176  virtual void Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset) = 0;
177 };
178 
179 class USB : public MAX3421E {
181  USBDeviceConfig* devConfig[USB_NUMDEVICES];
182  uint8_t devConfigIndex;
183  uint8_t bmHubPre;
184 
185 public:
186  USB(void);
187 
188  void SetHubPreMask() {
189  bmHubPre |= bmHUBPRE;
190  };
191 
193  bmHubPre &= (~bmHUBPRE);
194  };
195 
197  return(AddressPool&) addrPool;
198  };
199 
201  for(uint8_t i = 0; i < USB_NUMDEVICES; i++) {
202  if(!devConfig[i]) {
203  devConfig[i] = pdev;
204  return 0;
205  }
206  }
208  };
209 
211  addrPool.ForEachUsbDevice(pfunc);
212  };
213  uint8_t getUsbTaskState(void);
214  void setUsbTaskState(uint8_t state);
215 
216  EpInfo* getEpInfoEntry(uint8_t addr, uint8_t ep);
217  uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo* eprecord_ptr);
218 
219  //uint8_t ctrlReq( uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi, uint16_t wInd, uint16_t nbytes, uint8_t* dataptr);
220 
221  /* Control requests */
222  uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr);
223  uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr);
224 
225  uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser *p);
226 
227  uint8_t getStrDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t index, uint16_t langid, uint8_t* dataptr);
228  uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr);
229  uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value);
230 
231  uint8_t ctrlData(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr, boolean direction);
232  uint8_t ctrlStatus(uint8_t ep, boolean direction, uint16_t nak_limit);
233  uint8_t inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data);
234  uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* data);
235  uint8_t dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit);
236 
237  void Task(void);
238 
239  uint8_t DefaultAddressing(uint8_t parent, uint8_t port, bool lowspeed);
240  uint8_t Configuring(uint8_t parent, uint8_t port, bool lowspeed);
241  uint8_t ReleaseDevice(uint8_t addr);
242 
243  uint8_t ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi,
244  uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t* dataptr, USBReadParser *p);
245 
246 private:
247  void init();
248  uint8_t SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t &nak_limit);
249  uint8_t OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data);
250  uint8_t InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, uint8_t *data);
251 };
252 
253 #if 0 //defined(USB_METHODS_INLINE)
254 //get device descriptor
255 
256 inline uint8_t USB::getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr) {
257  return( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, dataptr));
258 }
259 //get configuration descriptor
260 
261 inline uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) {
262  return( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, dataptr));
263 }
264 //get string descriptor
265 
266 inline uint8_t USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t nuint8_ts, uint8_t index, uint16_t langid, uint8_t* dataptr) {
267  return( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, nuint8_ts, dataptr));
268 }
269 //set address
270 
271 inline uint8_t USB::setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr) {
272  return( ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, NULL));
273 }
274 //set configuration
275 
276 inline uint8_t USB::setConf(uint8_t addr, uint8_t ep, uint8_t conf_value) {
277  return( ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, NULL));
278 }
279 
280 #endif // defined(USB_METHODS_INLINE)
281 
282 #endif //_usb_h_