Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Circuits At Home, LTD
  11. Web : http://www.circuitsathome.com
  12. e-mail : [email protected]
  13. */
  14. #if !defined(_usb_h_) || defined(USBCORE_H)
  15. #error "Never include UsbCore.h directly; include Usb.h instead"
  16. #else
  17. #define USBCORE_H
  18. // Not used anymore? If anyone uses this, please let us know so that this may be
  19. // moved to the proper place, settings.h.
  20. //#define USB_METHODS_INLINE
  21. /* shield pins. First parameter - SS pin, second parameter - INT pin */
  22. #ifdef BOARD_BLACK_WIDDOW
  23. typedef MAX3421e<P6, P3> MAX3421E; // Black Widow
  24. #elif defined(CORE_TEENSY) && (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__))
  25. #if EXT_RAM
  26. typedef MAX3421e<P20, P7> MAX3421E; // Teensy++ 2.0 with XMEM2
  27. #else
  28. typedef MAX3421e<P9, P8> MAX3421E; // Teensy++ 1.0 and 2.0
  29. #endif
  30. #elif defined(BOARD_MEGA_ADK)
  31. typedef MAX3421e<P53, P54> MAX3421E; // Arduino Mega ADK
  32. #elif defined(ARDUINO_AVR_BALANDUINO)
  33. typedef MAX3421e<P20, P19> MAX3421E; // Balanduino
  34. #elif defined(__ARDUINO_X86__) && PLATFORM_ID == 0x06
  35. typedef MAX3421e<P3, P2> MAX3421E; // The Intel Galileo supports much faster read and write speed at pin 2 and 3
  36. #else
  37. typedef MAX3421e<P10, P9> MAX3421E; // Official Arduinos (UNO, Duemilanove, Mega, 2560, Leonardo, Due etc.), Intel Edison, Intel Galileo 2 or Teensy 2.0 and 3.0
  38. #endif
  39. /* Common setup data constant combinations */
  40. #define bmREQ_GET_DESCR USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_DEVICE //get descriptor request type
  41. #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'
  42. #define bmREQ_CL_GET_INTF USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE //get interface request type
  43. // D7 data transfer direction (0 - host-to-device, 1 - device-to-host)
  44. // D6-5 Type (0- standard, 1 - class, 2 - vendor, 3 - reserved)
  45. // D4-0 Recipient (0 - device, 1 - interface, 2 - endpoint, 3 - other, 4..31 - reserved)
  46. // USB Device Classes
  47. #define USB_CLASS_USE_CLASS_INFO 0x00 // Use Class Info in the Interface Descriptors
  48. #define USB_CLASS_AUDIO 0x01 // Audio
  49. #define USB_CLASS_COM_AND_CDC_CTRL 0x02 // Communications and CDC Control
  50. #define USB_CLASS_HID 0x03 // HID
  51. #define USB_CLASS_PHYSICAL 0x05 // Physical
  52. #define USB_CLASS_IMAGE 0x06 // Image
  53. #define USB_CLASS_PRINTER 0x07 // Printer
  54. #define USB_CLASS_MASS_STORAGE 0x08 // Mass Storage
  55. #define USB_CLASS_HUB 0x09 // Hub
  56. #define USB_CLASS_CDC_DATA 0x0a // CDC-Data
  57. #define USB_CLASS_SMART_CARD 0x0b // Smart-Card
  58. #define USB_CLASS_CONTENT_SECURITY 0x0d // Content Security
  59. #define USB_CLASS_VIDEO 0x0e // Video
  60. #define USB_CLASS_PERSONAL_HEALTH 0x0f // Personal Healthcare
  61. #define USB_CLASS_DIAGNOSTIC_DEVICE 0xdc // Diagnostic Device
  62. #define USB_CLASS_WIRELESS_CTRL 0xe0 // Wireless Controller
  63. #define USB_CLASS_MISC 0xef // Miscellaneous
  64. #define USB_CLASS_APP_SPECIFIC 0xfe // Application Specific
  65. #define USB_CLASS_VENDOR_SPECIFIC 0xff // Vendor Specific
  66. // Additional Error Codes
  67. #define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED 0xD1
  68. #define USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE 0xD2
  69. #define USB_ERROR_UNABLE_TO_REGISTER_DEVICE_CLASS 0xD3
  70. #define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL 0xD4
  71. #define USB_ERROR_HUB_ADDRESS_OVERFLOW 0xD5
  72. #define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL 0xD6
  73. #define USB_ERROR_EPINFO_IS_NULL 0xD7
  74. #define USB_ERROR_INVALID_ARGUMENT 0xD8
  75. #define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE 0xD9
  76. #define USB_ERROR_INVALID_MAX_PKT_SIZE 0xDA
  77. #define USB_ERROR_EP_NOT_FOUND_IN_TBL 0xDB
  78. #define USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET 0xE0
  79. #define USB_ERROR_FailGetDevDescr 0xE1
  80. #define USB_ERROR_FailSetDevTblEntry 0xE2
  81. #define USB_ERROR_FailGetConfDescr 0xE3
  82. #define USB_ERROR_TRANSFER_TIMEOUT 0xFF
  83. #define USB_XFER_TIMEOUT 5000 // (5000) USB transfer timeout in milliseconds, per section 9.2.6.1 of USB 2.0 spec
  84. //#define USB_NAK_LIMIT 32000 // NAK limit for a transfer. 0 means NAKs are not counted
  85. #define USB_RETRY_LIMIT 3 // 3 retry limit for a transfer
  86. #define USB_SETTLE_DELAY 200 // settle delay in milliseconds
  87. #define USB_NUMDEVICES 16 //number of USB devices
  88. //#define HUB_MAX_HUBS 7 // maximum number of hubs that can be attached to the host controller
  89. #define HUB_PORT_RESET_DELAY 20 // hub port reset delay 10 ms recomended, can be up to 20 ms
  90. /* USB state machine states */
  91. #define USB_STATE_MASK 0xf0
  92. #define USB_STATE_DETACHED 0x10
  93. #define USB_DETACHED_SUBSTATE_INITIALIZE 0x11
  94. #define USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE 0x12
  95. #define USB_DETACHED_SUBSTATE_ILLEGAL 0x13
  96. #define USB_ATTACHED_SUBSTATE_SETTLE 0x20
  97. #define USB_ATTACHED_SUBSTATE_RESET_DEVICE 0x30
  98. #define USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE 0x40
  99. #define USB_ATTACHED_SUBSTATE_WAIT_SOF 0x50
  100. #define USB_ATTACHED_SUBSTATE_WAIT_RESET 0x51
  101. #define USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE 0x60
  102. #define USB_STATE_ADDRESSING 0x70
  103. #define USB_STATE_CONFIGURING 0x80
  104. #define USB_STATE_RUNNING 0x90
  105. #define USB_STATE_ERROR 0xa0
  106. class USBDeviceConfig {
  107. public:
  108. virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed) {
  109. return 0;
  110. }
  111. virtual uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed) {
  112. return 0;
  113. }
  114. virtual uint8_t Release() {
  115. return 0;
  116. }
  117. virtual uint8_t Poll() {
  118. return 0;
  119. }
  120. virtual uint8_t GetAddress() {
  121. return 0;
  122. }
  123. virtual void ResetHubPort(uint8_t port) {
  124. return;
  125. } // Note used for hubs only!
  126. virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
  127. return false;
  128. }
  129. virtual bool DEVCLASSOK(uint8_t klass) {
  130. return false;
  131. }
  132. virtual bool DEVSUBCLASSOK(uint8_t subklass) {
  133. return true;
  134. }
  135. };
  136. /* USB Setup Packet Structure */
  137. typedef struct {
  138. union { // offset description
  139. uint8_t bmRequestType; // 0 Bit-map of request type
  140. struct {
  141. uint8_t recipient : 5; // Recipient of the request
  142. uint8_t type : 2; // Type of request
  143. uint8_t direction : 1; // Direction of data X-fer
  144. } __attribute__((packed));
  145. } ReqType_u;
  146. uint8_t bRequest; // 1 Request
  147. union {
  148. uint16_t wValue; // 2 Depends on bRequest
  149. struct {
  150. uint8_t wValueLo;
  151. uint8_t wValueHi;
  152. } __attribute__((packed));
  153. } wVal_u;
  154. uint16_t wIndex; // 4 Depends on bRequest
  155. uint16_t wLength; // 6 Depends on bRequest
  156. } __attribute__((packed)) SETUP_PKT, *PSETUP_PKT;
  157. // Base class for incoming data parser
  158. class USBReadParser {
  159. public:
  160. virtual void Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset) = 0;
  161. };
  162. class USB : public MAX3421E {
  163. AddressPoolImpl<USB_NUMDEVICES> addrPool;
  164. USBDeviceConfig* devConfig[USB_NUMDEVICES];
  165. uint8_t bmHubPre;
  166. public:
  167. USB(void);
  168. void SetHubPreMask() {
  169. bmHubPre |= bmHUBPRE;
  170. };
  171. void ResetHubPreMask() {
  172. bmHubPre &= (~bmHUBPRE);
  173. };
  174. AddressPool& GetAddressPool() {
  175. return (AddressPool&)addrPool;
  176. };
  177. uint8_t RegisterDeviceClass(USBDeviceConfig *pdev) {
  178. for(uint8_t i = 0; i < USB_NUMDEVICES; i++) {
  179. if(!devConfig[i]) {
  180. devConfig[i] = pdev;
  181. return 0;
  182. }
  183. }
  184. return USB_ERROR_UNABLE_TO_REGISTER_DEVICE_CLASS;
  185. };
  186. void ForEachUsbDevice(UsbDeviceHandleFunc pfunc) {
  187. addrPool.ForEachUsbDevice(pfunc);
  188. };
  189. uint8_t getUsbTaskState(void);
  190. void setUsbTaskState(uint8_t state);
  191. EpInfo* getEpInfoEntry(uint8_t addr, uint8_t ep);
  192. uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo* eprecord_ptr);
  193. /* Control requests */
  194. uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr);
  195. uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr);
  196. uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser *p);
  197. uint8_t getStrDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t index, uint16_t langid, uint8_t* dataptr);
  198. uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr);
  199. uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value);
  200. /**/
  201. uint8_t ctrlData(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr, bool direction);
  202. uint8_t ctrlStatus(uint8_t ep, bool direction, uint16_t nak_limit);
  203. uint8_t inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data);
  204. uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* data);
  205. uint8_t dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit);
  206. void Task(void);
  207. uint8_t DefaultAddressing(uint8_t parent, uint8_t port, bool lowspeed);
  208. uint8_t Configuring(uint8_t parent, uint8_t port, bool lowspeed);
  209. uint8_t ReleaseDevice(uint8_t addr);
  210. uint8_t ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi,
  211. uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t* dataptr, USBReadParser *p);
  212. private:
  213. void init();
  214. uint8_t SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t *nak_limit);
  215. uint8_t OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data);
  216. uint8_t InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, uint8_t *data);
  217. uint8_t AttemptConfig(uint8_t driver, uint8_t parent, uint8_t port, bool lowspeed);
  218. };
  219. #if 0 //defined(USB_METHODS_INLINE)
  220. //get device descriptor
  221. inline uint8_t USB::getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr) {
  222. return ( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, dataptr));
  223. }
  224. //get configuration descriptor
  225. inline uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) {
  226. return ( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, dataptr));
  227. }
  228. //get string descriptor
  229. inline uint8_t USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t nuint8_ts, uint8_t index, uint16_t langid, uint8_t* dataptr) {
  230. return ( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, nuint8_ts, dataptr));
  231. }
  232. //set address
  233. inline uint8_t USB::setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr) {
  234. return ( ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, NULL));
  235. }
  236. //set configuration
  237. inline uint8_t USB::setConf(uint8_t addr, uint8_t ep, uint8_t conf_value) {
  238. return ( ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, NULL));
  239. }
  240. #endif // defined(USB_METHODS_INLINE)
  241. #endif /* USBCORE_H */