Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

1552 Zeilen
57KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef USB_HOST_TEENSY36_
  24. #define USB_HOST_TEENSY36_
  25. #include <stdint.h>
  26. #if !defined(__MK66FX1M0__)
  27. #error "USBHost_t36 only works with Teensy 3.6. Please select it in Tools > Boards"
  28. #endif
  29. // Dear inquisitive reader, USB is a complex protocol defined with
  30. // very specific terminology. To have any chance of understand this
  31. // source code, you absolutely must have solid knowledge of specific
  32. // USB terms such as host, device, endpoint, pipe, enumeration....
  33. // You really must also have at least a basic knowledge of the
  34. // different USB transfers: control, bulk, interrupt, isochronous.
  35. //
  36. // The USB 2.0 specification explains these in chapter 4 (pages 15
  37. // to 24), and provides more detail in the first part of chapter 5
  38. // (pages 25 to 55). The USB spec is published for free at
  39. // www.usb.org. Here is a convenient link to just the main PDF:
  40. //
  41. // https://www.pjrc.com/teensy/beta/usb20.pdf
  42. //
  43. // This is a huge file, but chapter 4 is short and easy to read.
  44. // If you're not familiar with the USB lingo, please do yourself
  45. // a favor by reading at least chapter 4 to get up to speed on the
  46. // meaning of these important USB concepts and terminology.
  47. //
  48. // If you wish to ask questions (which belong on the forum, not
  49. // github issues) or discuss development of this library, you
  50. // ABSOLUTELY MUST know the basic USB terminology from chapter 4.
  51. // Please repect other people's valuable time & effort by making
  52. // your best effort to read chapter 4 before asking USB questions!
  53. //#define USBHOST_PRINT_DEBUG
  54. /************************************************/
  55. /* Data Types */
  56. /************************************************/
  57. // These 6 types are the key to understanding how this USB Host
  58. // library really works.
  59. // USBHost is a static class controlling the hardware.
  60. // All common USB functionality is implemented here.
  61. class USBHost;
  62. // These 3 structures represent the actual USB entities
  63. // USBHost manipulates. One Device_t is created for
  64. // each active USB device. One Pipe_t is create for
  65. // each endpoint. Transfer_t structures are created
  66. // when any data transfer is added to the EHCI work
  67. // queues, and then returned to the free pool after the
  68. // data transfer completes and the driver has processed
  69. // the results.
  70. typedef struct Device_struct Device_t;
  71. typedef struct Pipe_struct Pipe_t;
  72. typedef struct Transfer_struct Transfer_t;
  73. typedef enum { CLAIM_NO=0, CLAIM_REPORT, CLAIM_INTERFACE} hidclaim_t;
  74. // All USB device drivers inherit use these classes.
  75. // Drivers build user-visible functionality on top
  76. // of these classes, which receive USB events from
  77. // USBHost.
  78. class USBDriver;
  79. class USBDriverTimer;
  80. /************************************************/
  81. /* Added Defines */
  82. /************************************************/
  83. // Keyboard special Keys
  84. #define KEYD_UP 0xDA
  85. #define KEYD_DOWN 0xD9
  86. #define KEYD_LEFT 0xD8
  87. #define KEYD_RIGHT 0xD7
  88. #define KEYD_INSERT 0xD1
  89. #define KEYD_DELETE 0xD4
  90. #define KEYD_PAGE_UP 0xD3
  91. #define KEYD_PAGE_DOWN 0xD6
  92. #define KEYD_HOME 0xD2
  93. #define KEYD_END 0xD5
  94. #define KEYD_F1 0xC2
  95. #define KEYD_F2 0xC3
  96. #define KEYD_F3 0xC4
  97. #define KEYD_F4 0xC5
  98. #define KEYD_F5 0xC6
  99. #define KEYD_F6 0xC7
  100. #define KEYD_F7 0xC8
  101. #define KEYD_F8 0xC9
  102. #define KEYD_F9 0xCA
  103. #define KEYD_F10 0xCB
  104. #define KEYD_F11 0xCC
  105. #define KEYD_F12 0xCD
  106. // USBSerial formats - Lets encode format into bits
  107. // Bits: 0-4 - Number of data bits
  108. // Bits: 5-7 - Parity (0=none, 1=odd, 2 = even)
  109. // bits: 8-9 - Stop bits. 0=1, 1=2
  110. #define USBHOST_SERIAL_7E1 0x047
  111. #define USBHOST_SERIAL_7O1 0x027
  112. #define USBHOST_SERIAL_8N1 0x08
  113. #define USBHOST_SERIAL_8N2 0x108
  114. #define USBHOST_SERIAL_8E1 0x048
  115. #define USBHOST_SERIAL_8O1 0x028
  116. /************************************************/
  117. /* Data Structure Definitions */
  118. /************************************************/
  119. // setup_t holds the 8 byte USB SETUP packet data.
  120. // These unions & structs allow convenient access to
  121. // the setup fields.
  122. typedef union {
  123. struct {
  124. union {
  125. struct {
  126. uint8_t bmRequestType;
  127. uint8_t bRequest;
  128. };
  129. uint16_t wRequestAndType;
  130. };
  131. uint16_t wValue;
  132. uint16_t wIndex;
  133. uint16_t wLength;
  134. };
  135. struct {
  136. uint32_t word1;
  137. uint32_t word2;
  138. };
  139. } setup_t;
  140. typedef struct {
  141. enum {STRING_BUF_SIZE=50};
  142. enum {STR_ID_MAN=0, STR_ID_PROD, STR_ID_SERIAL, STR_ID_CNT};
  143. uint8_t iStrings[STR_ID_CNT]; // Index into array for the three indexes
  144. uint8_t buffer[STRING_BUF_SIZE];
  145. } strbuf_t;
  146. #define DEVICE_STRUCT_STRING_BUF_SIZE 50
  147. // Device_t holds all the information about a USB device
  148. struct Device_struct {
  149. Pipe_t *control_pipe;
  150. Pipe_t *data_pipes;
  151. Device_t *next;
  152. USBDriver *drivers;
  153. strbuf_t *strbuf;
  154. uint8_t speed; // 0=12, 1=1.5, 2=480 Mbit/sec
  155. uint8_t address;
  156. uint8_t hub_address;
  157. uint8_t hub_port;
  158. uint8_t enum_state;
  159. uint8_t bDeviceClass;
  160. uint8_t bDeviceSubClass;
  161. uint8_t bDeviceProtocol;
  162. uint8_t bmAttributes;
  163. uint8_t bMaxPower;
  164. uint16_t idVendor;
  165. uint16_t idProduct;
  166. uint16_t LanguageID;
  167. };
  168. // Pipe_t holes all information about each USB endpoint/pipe
  169. // The first half is an EHCI QH structure for the pipe.
  170. struct Pipe_struct {
  171. // Queue Head (QH), EHCI page 46-50
  172. struct { // must be aligned to 32 byte boundary
  173. volatile uint32_t horizontal_link;
  174. volatile uint32_t capabilities[2];
  175. volatile uint32_t current;
  176. volatile uint32_t next;
  177. volatile uint32_t alt_next;
  178. volatile uint32_t token;
  179. volatile uint32_t buffer[5];
  180. } qh;
  181. Device_t *device;
  182. uint8_t type; // 0=control, 1=isochronous, 2=bulk, 3=interrupt
  183. uint8_t direction; // 0=out, 1=in (changes for control, others fixed)
  184. uint8_t start_mask;
  185. uint8_t complete_mask;
  186. Pipe_t *next;
  187. void (*callback_function)(const Transfer_t *);
  188. uint16_t periodic_interval;
  189. uint16_t periodic_offset;
  190. uint16_t bandwidth_interval;
  191. uint16_t bandwidth_offset;
  192. uint16_t bandwidth_shift;
  193. uint8_t bandwidth_stime;
  194. uint8_t bandwidth_ctime;
  195. uint32_t unused1;
  196. uint32_t unused2;
  197. uint32_t unused3;
  198. uint32_t unused4;
  199. uint32_t unused5;
  200. };
  201. // Transfer_t represents a single transaction on the USB bus.
  202. // The first portion is an EHCI qTD structure. Transfer_t are
  203. // allocated as-needed from a memory pool, loaded with pointers
  204. // to the actual data buffers, linked into a followup list,
  205. // and placed on ECHI Queue Heads. When the ECHI interrupt
  206. // occurs, the followup lists are used to find the Transfer_t
  207. // in memory. Callbacks are made, and then the Transfer_t are
  208. // returned to the memory pool.
  209. struct Transfer_struct {
  210. // Queue Element Transfer Descriptor (qTD), EHCI pg 40-45
  211. struct { // must be aligned to 32 byte boundary
  212. volatile uint32_t next;
  213. volatile uint32_t alt_next;
  214. volatile uint32_t token;
  215. volatile uint32_t buffer[5];
  216. } qtd;
  217. // Linked list of queued, not-yet-completed transfers
  218. Transfer_t *next_followup;
  219. Transfer_t *prev_followup;
  220. Pipe_t *pipe;
  221. // Data to be used by callback function. When a group
  222. // of Transfer_t are created, these fields and the
  223. // interrupt-on-complete bit in the qTD token are only
  224. // set in the last Transfer_t of the list.
  225. void *buffer;
  226. uint32_t length;
  227. setup_t setup;
  228. USBDriver *driver;
  229. };
  230. /************************************************/
  231. /* Main USB EHCI Controller */
  232. /************************************************/
  233. class USBHost {
  234. public:
  235. static void begin();
  236. static void Task();
  237. static void countFree(uint32_t &devices, uint32_t &pipes, uint32_t &trans, uint32_t &strs);
  238. protected:
  239. static Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
  240. uint32_t direction, uint32_t maxlen, uint32_t interval=0);
  241. static bool queue_Control_Transfer(Device_t *dev, setup_t *setup,
  242. void *buf, USBDriver *driver);
  243. static bool queue_Data_Transfer(Pipe_t *pipe, void *buffer,
  244. uint32_t len, USBDriver *driver);
  245. static Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port);
  246. static void disconnect_Device(Device_t *dev);
  247. static void enumeration(const Transfer_t *transfer);
  248. static void driver_ready_for_device(USBDriver *driver);
  249. static volatile bool enumeration_busy;
  250. public: // Maybe others may want/need to contribute memory example HID devices may want to add transfers.
  251. static void contribute_Devices(Device_t *devices, uint32_t num);
  252. static void contribute_Pipes(Pipe_t *pipes, uint32_t num);
  253. static void contribute_Transfers(Transfer_t *transfers, uint32_t num);
  254. static void contribute_String_Buffers(strbuf_t *strbuf, uint32_t num);
  255. private:
  256. static void isr();
  257. static void convertStringDescriptorToASCIIString(uint8_t string_index, Device_t *dev, const Transfer_t *transfer);
  258. static void claim_drivers(Device_t *dev);
  259. static uint32_t assign_address(void);
  260. static bool queue_Transfer(Pipe_t *pipe, Transfer_t *transfer);
  261. static void init_Device_Pipe_Transfer_memory(void);
  262. static Device_t * allocate_Device(void);
  263. static void delete_Pipe(Pipe_t *pipe);
  264. static void free_Device(Device_t *q);
  265. static Pipe_t * allocate_Pipe(void);
  266. static void free_Pipe(Pipe_t *q);
  267. static Transfer_t * allocate_Transfer(void);
  268. static void free_Transfer(Transfer_t *q);
  269. static strbuf_t * allocate_string_buffer(void);
  270. static void free_string_buffer(strbuf_t *strbuf);
  271. static bool allocate_interrupt_pipe_bandwidth(Pipe_t *pipe,
  272. uint32_t maxlen, uint32_t interval);
  273. static void add_qh_to_periodic_schedule(Pipe_t *pipe);
  274. static bool followup_Transfer(Transfer_t *transfer);
  275. static void followup_Error(void);
  276. protected:
  277. #ifdef USBHOST_PRINT_DEBUG
  278. static void print_(const Transfer_t *transfer);
  279. static void print_(const Transfer_t *first, const Transfer_t *last);
  280. static void print_token(uint32_t token);
  281. static void print_(const Pipe_t *pipe);
  282. static void print_driverlist(const char *name, const USBDriver *driver);
  283. static void print_qh_list(const Pipe_t *list);
  284. static void print_device_descriptor(const uint8_t *p);
  285. static void print_config_descriptor(const uint8_t *p, uint32_t maxlen);
  286. static void print_string_descriptor(const char *name, const uint8_t *p);
  287. static void print_hexbytes(const void *ptr, uint32_t len);
  288. static void print_(const char *s) { Serial.print(s); }
  289. static void print_(int n) { Serial.print(n); }
  290. static void print_(unsigned int n) { Serial.print(n); }
  291. static void print_(long n) { Serial.print(n); }
  292. static void print_(unsigned long n) { Serial.print(n); }
  293. static void println_(const char *s) { Serial.println(s); }
  294. static void println_(int n) { Serial.println(n); }
  295. static void println_(unsigned int n) { Serial.println(n); }
  296. static void println_(long n) { Serial.println(n); }
  297. static void println_(unsigned long n) { Serial.println(n); }
  298. static void println_() { Serial.println(); }
  299. static void print_(uint32_t n, uint8_t b) { Serial.print(n, b); }
  300. static void println_(uint32_t n, uint8_t b) { Serial.println(n, b); }
  301. static void print_(const char *s, int n, uint8_t b = DEC) {
  302. Serial.print(s); Serial.print(n, b); }
  303. static void print_(const char *s, unsigned int n, uint8_t b = DEC) {
  304. Serial.print(s); Serial.print(n, b); }
  305. static void print_(const char *s, long n, uint8_t b = DEC) {
  306. Serial.print(s); Serial.print(n, b); }
  307. static void print_(const char *s, unsigned long n, uint8_t b = DEC) {
  308. Serial.print(s); Serial.print(n, b); }
  309. static void println_(const char *s, int n, uint8_t b = DEC) {
  310. Serial.print(s); Serial.println(n, b); }
  311. static void println_(const char *s, unsigned int n, uint8_t b = DEC) {
  312. Serial.print(s); Serial.println(n, b); }
  313. static void println_(const char *s, long n, uint8_t b = DEC) {
  314. Serial.print(s); Serial.println(n, b); }
  315. static void println_(const char *s, unsigned long n, uint8_t b = DEC) {
  316. Serial.print(s); Serial.println(n, b); }
  317. friend class USBDriverTimer; // for access to print & println
  318. #else
  319. static void print_(const Transfer_t *transfer) {}
  320. static void print_(const Transfer_t *first, const Transfer_t *last) {}
  321. static void print_token(uint32_t token) {}
  322. static void print_(const Pipe_t *pipe) {}
  323. static void print_driverlist(const char *name, const USBDriver *driver) {}
  324. static void print_qh_list(const Pipe_t *list) {}
  325. static void print_device_descriptor(const uint8_t *p) {}
  326. static void print_config_descriptor(const uint8_t *p, uint32_t maxlen) {}
  327. static void print_string_descriptor(const char *name, const uint8_t *p) {}
  328. static void print_hexbytes(const void *ptr, uint32_t len) {}
  329. static void print_(const char *s) {}
  330. static void print_(int n) {}
  331. static void print_(unsigned int n) {}
  332. static void print_(long n) {}
  333. static void print_(unsigned long n) {}
  334. static void println_(const char *s) {}
  335. static void println_(int n) {}
  336. static void println_(unsigned int n) {}
  337. static void println_(long n) {}
  338. static void println_(unsigned long n) {}
  339. static void println_() {}
  340. static void print_(uint32_t n, uint8_t b) {}
  341. static void println_(uint32_t n, uint8_t b) {}
  342. static void print_(const char *s, int n, uint8_t b = DEC) {}
  343. static void print_(const char *s, unsigned int n, uint8_t b = DEC) {}
  344. static void print_(const char *s, long n, uint8_t b = DEC) {}
  345. static void print_(const char *s, unsigned long n, uint8_t b = DEC) {}
  346. static void println_(const char *s, int n, uint8_t b = DEC) {}
  347. static void println_(const char *s, unsigned int n, uint8_t b = DEC) {}
  348. static void println_(const char *s, long n, uint8_t b = DEC) {}
  349. static void println_(const char *s, unsigned long n, uint8_t b = DEC) {}
  350. #endif
  351. static void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
  352. uint32_t wValue, uint32_t wIndex, uint32_t wLength) {
  353. s.word1 = bmRequestType | (bRequest << 8) | (wValue << 16);
  354. s.word2 = wIndex | (wLength << 16);
  355. }
  356. };
  357. /************************************************/
  358. /* USB Device Driver Common Base Class */
  359. /************************************************/
  360. // All USB device drivers inherit from this base class.
  361. class USBDriver : public USBHost {
  362. public:
  363. operator bool() {
  364. Device_t *dev = *(Device_t * volatile *)&device;
  365. return dev != nullptr;
  366. }
  367. uint16_t idVendor() {
  368. Device_t *dev = *(Device_t * volatile *)&device;
  369. return (dev != nullptr) ? dev->idVendor : 0;
  370. }
  371. uint16_t idProduct() {
  372. Device_t *dev = *(Device_t * volatile *)&device;
  373. return (dev != nullptr) ? dev->idProduct : 0;
  374. }
  375. const uint8_t *manufacturer() {
  376. Device_t *dev = *(Device_t * volatile *)&device;
  377. if (dev == nullptr || dev->strbuf == nullptr) return nullptr;
  378. return &dev->strbuf->buffer[dev->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  379. }
  380. const uint8_t *product() {
  381. Device_t *dev = *(Device_t * volatile *)&device;
  382. if (dev == nullptr || dev->strbuf == nullptr) return nullptr;
  383. return &dev->strbuf->buffer[dev->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  384. }
  385. const uint8_t *serialNumber() {
  386. Device_t *dev = *(Device_t * volatile *)&device;
  387. if (dev == nullptr || dev->strbuf == nullptr) return nullptr;
  388. return &dev->strbuf->buffer[dev->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  389. }
  390. protected:
  391. USBDriver() : next(NULL), device(NULL) {}
  392. // Check if a driver wishes to claim a device or interface or group
  393. // of interfaces within a device. When this function returns true,
  394. // the driver is considered bound or loaded for that device. When
  395. // new devices are detected, enumeration.cpp calls this function on
  396. // all unbound driver objects, to give them an opportunity to bind
  397. // to the new device.
  398. // device has its vid&pid, class/subclass fields initialized
  399. // type is 0 for device level, 1 for interface level, 2 for IAD
  400. // descriptors points to the specific descriptor data
  401. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  402. // When an unknown (not chapter 9) control transfer completes, this
  403. // function is called for all drivers bound to the device. Return
  404. // true means this driver originated this control transfer, so no
  405. // more drivers need to be offered an opportunity to process it.
  406. // This function is optional, only needed if the driver uses control
  407. // transfers and wishes to be notified when they complete.
  408. virtual void control(const Transfer_t *transfer) { }
  409. // When any of the USBDriverTimer objects a driver creates generates
  410. // a timer event, this function is called.
  411. virtual void timer_event(USBDriverTimer *whichTimer) { }
  412. // When the user calls USBHost::Task, this Task function for all
  413. // active drivers is called, so they may update state and/or call
  414. // any attached user callback functions.
  415. virtual void Task() { }
  416. // When a device disconnects from the USB, this function is called.
  417. // The driver must free all resources it allocated and update any
  418. // internal state necessary to deal with the possibility of user
  419. // code continuing to call its API. However, pipes and transfers
  420. // are the handled by lower layers, so device drivers do not free
  421. // pipes they created or cancel transfers they had in progress.
  422. virtual void disconnect();
  423. // Drivers are managed by this single-linked list. All inactive
  424. // (not bound to any device) drivers are linked from
  425. // available_drivers in enumeration.cpp. When bound to a device,
  426. // drivers are linked from that Device_t drivers list.
  427. USBDriver *next;
  428. // The device this object instance is bound to. In words, this
  429. // is the specific device this driver is using. When not bound
  430. // to any device, this must be NULL. Drivers may set this to
  431. // any non-NULL value if they are in a state where they do not
  432. // wish to claim any device or interface (eg, if getting data
  433. // from the HID parser).
  434. Device_t *device;
  435. friend class USBHost;
  436. };
  437. // Device drivers may create these timer objects to schedule a timer call
  438. class USBDriverTimer {
  439. public:
  440. USBDriverTimer() { }
  441. USBDriverTimer(USBDriver *d) : driver(d) { }
  442. void init(USBDriver *d) { driver = d; };
  443. void start(uint32_t microseconds);
  444. void stop();
  445. void *pointer;
  446. uint32_t integer;
  447. uint32_t started_micros; // testing only
  448. private:
  449. USBDriver *driver;
  450. uint32_t usec;
  451. USBDriverTimer *next;
  452. USBDriverTimer *prev;
  453. friend class USBHost;
  454. };
  455. // Device drivers may inherit from this base class, if they wish to receive
  456. // HID input data fully decoded by the USBHIDParser driver
  457. class USBHIDParser;
  458. class USBHIDInput {
  459. public:
  460. operator bool() { return (mydevice != nullptr); }
  461. uint16_t idVendor() { return (mydevice != nullptr) ? mydevice->idVendor : 0; }
  462. uint16_t idProduct() { return (mydevice != nullptr) ? mydevice->idProduct : 0; }
  463. const uint8_t *manufacturer()
  464. { return ((mydevice == nullptr) || (mydevice->strbuf == nullptr)) ? nullptr : &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]]; }
  465. const uint8_t *product()
  466. { return ((mydevice == nullptr) || (mydevice->strbuf == nullptr)) ? nullptr : &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_PROD]]; }
  467. const uint8_t *serialNumber()
  468. { return ((mydevice == nullptr) || (mydevice->strbuf == nullptr)) ? nullptr : &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]]; }
  469. private:
  470. virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
  471. virtual bool hid_process_in_data(const Transfer_t *transfer) {return false;}
  472. virtual bool hid_process_out_data(const Transfer_t *transfer) {return false;}
  473. virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
  474. virtual void hid_input_data(uint32_t usage, int32_t value);
  475. virtual void hid_input_end();
  476. virtual void disconnect_collection(Device_t *dev);
  477. void add_to_list();
  478. USBHIDInput *next;
  479. friend class USBHIDParser;
  480. protected:
  481. Device_t *mydevice = NULL;
  482. };
  483. /************************************************/
  484. /* USB Device Drivers */
  485. /************************************************/
  486. class USBHub : public USBDriver {
  487. public:
  488. USBHub(USBHost &host) : debouncetimer(this), resettimer(this) { init(); }
  489. USBHub(USBHost *host) : debouncetimer(this), resettimer(this) { init(); }
  490. // Hubs with more more than 7 ports are built from two tiers of hubs
  491. // using 4 or 7 port hub chips. While the USB spec seems to allow
  492. // hubs to have up to 255 ports, in practice all hub chips on the
  493. // market are only 2, 3, 4 or 7 ports.
  494. enum { MAXPORTS = 7 };
  495. typedef uint8_t portbitmask_t;
  496. enum {
  497. PORT_OFF = 0,
  498. PORT_DISCONNECT = 1,
  499. PORT_DEBOUNCE1 = 2,
  500. PORT_DEBOUNCE2 = 3,
  501. PORT_DEBOUNCE3 = 4,
  502. PORT_DEBOUNCE4 = 5,
  503. PORT_DEBOUNCE5 = 6,
  504. PORT_RESET = 7,
  505. PORT_RECOVERY = 8,
  506. PORT_ACTIVE = 9
  507. };
  508. protected:
  509. virtual bool claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len);
  510. virtual void control(const Transfer_t *transfer);
  511. virtual void timer_event(USBDriverTimer *whichTimer);
  512. virtual void disconnect();
  513. void init();
  514. bool can_send_control_now();
  515. void send_poweron(uint32_t port);
  516. void send_getstatus(uint32_t port);
  517. void send_clearstatus_connect(uint32_t port);
  518. void send_clearstatus_enable(uint32_t port);
  519. void send_clearstatus_suspend(uint32_t port);
  520. void send_clearstatus_overcurrent(uint32_t port);
  521. void send_clearstatus_reset(uint32_t port);
  522. void send_setreset(uint32_t port);
  523. void send_setinterface();
  524. static void callback(const Transfer_t *transfer);
  525. void status_change(const Transfer_t *transfer);
  526. void new_port_status(uint32_t port, uint32_t status);
  527. void start_debounce_timer(uint32_t port);
  528. void stop_debounce_timer(uint32_t port);
  529. private:
  530. Device_t mydevices[MAXPORTS];
  531. Pipe_t mypipes[2] __attribute__ ((aligned(32)));
  532. Transfer_t mytransfers[4] __attribute__ ((aligned(32)));
  533. strbuf_t mystring_bufs[1];
  534. USBDriverTimer debouncetimer;
  535. USBDriverTimer resettimer;
  536. setup_t setup;
  537. Pipe_t *changepipe;
  538. Device_t *devicelist[MAXPORTS];
  539. uint32_t changebits;
  540. uint32_t statusbits;
  541. uint8_t hub_desc[16];
  542. uint8_t interface_count;
  543. uint8_t interface_number;
  544. uint8_t altsetting;
  545. uint8_t protocol;
  546. uint8_t endpoint;
  547. uint8_t interval;
  548. uint8_t numports;
  549. uint8_t characteristics;
  550. uint8_t powertime;
  551. uint8_t sending_control_transfer;
  552. uint8_t port_doing_reset;
  553. uint8_t port_doing_reset_speed;
  554. uint8_t portstate[MAXPORTS];
  555. portbitmask_t send_pending_poweron;
  556. portbitmask_t send_pending_getstatus;
  557. portbitmask_t send_pending_clearstatus_connect;
  558. portbitmask_t send_pending_clearstatus_enable;
  559. portbitmask_t send_pending_clearstatus_suspend;
  560. portbitmask_t send_pending_clearstatus_overcurrent;
  561. portbitmask_t send_pending_clearstatus_reset;
  562. portbitmask_t send_pending_setreset;
  563. portbitmask_t debounce_in_use;
  564. static volatile bool reset_busy;
  565. };
  566. //--------------------------------------------------------------------------
  567. class USBHIDParser : public USBDriver {
  568. public:
  569. USBHIDParser(USBHost &host) { init(); }
  570. static void driver_ready_for_hid_collection(USBHIDInput *driver);
  571. bool sendPacket(const uint8_t *buffer, int cb=-1);
  572. void setTXBuffers(uint8_t *buffer1, uint8_t *buffer2, uint8_t cb);
  573. bool sendControlPacket(uint32_t bmRequestType, uint32_t bRequest,
  574. uint32_t wValue, uint32_t wIndex, uint32_t wLength, void *buf);
  575. protected:
  576. enum { TOPUSAGE_LIST_LEN = 4 };
  577. enum { USAGE_LIST_LEN = 24 };
  578. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  579. virtual void control(const Transfer_t *transfer);
  580. virtual void disconnect();
  581. static void in_callback(const Transfer_t *transfer);
  582. static void out_callback(const Transfer_t *transfer);
  583. void in_data(const Transfer_t *transfer);
  584. void out_data(const Transfer_t *transfer);
  585. bool check_if_using_report_id();
  586. void parse();
  587. USBHIDInput * find_driver(uint32_t topusage);
  588. void parse(uint16_t type_and_report_id, const uint8_t *data, uint32_t len);
  589. void init();
  590. // Atempt for RAWhid to take over processing of data
  591. //
  592. uint16_t inSize(void) {return in_size;}
  593. uint16_t outSize(void) {return out_size;}
  594. uint8_t activeSendMask(void) {return txstate;}
  595. private:
  596. Pipe_t *in_pipe;
  597. Pipe_t *out_pipe;
  598. static USBHIDInput *available_hid_drivers_list;
  599. //uint32_t topusage_list[TOPUSAGE_LIST_LEN];
  600. USBHIDInput *topusage_drivers[TOPUSAGE_LIST_LEN];
  601. uint16_t in_size;
  602. uint16_t out_size;
  603. setup_t setup;
  604. uint8_t descriptor[512];
  605. uint8_t report[64];
  606. uint16_t descsize;
  607. bool use_report_id;
  608. Pipe_t mypipes[3] __attribute__ ((aligned(32)));
  609. Transfer_t mytransfers[4] __attribute__ ((aligned(32)));
  610. strbuf_t mystring_bufs[1];
  611. uint8_t txstate = 0;
  612. uint8_t *tx1 = nullptr;
  613. uint8_t *tx2 = nullptr;
  614. bool hid_driver_claimed_control_ = false;
  615. };
  616. //--------------------------------------------------------------------------
  617. class KeyboardController : public USBDriver , public USBHIDInput {
  618. public:
  619. typedef union {
  620. struct {
  621. uint8_t numLock : 1;
  622. uint8_t capsLock : 1;
  623. uint8_t scrollLock : 1;
  624. uint8_t compose : 1;
  625. uint8_t kana : 1;
  626. uint8_t reserved : 3;
  627. };
  628. uint8_t byte;
  629. } KBDLeds_t;
  630. public:
  631. KeyboardController(USBHost &host) { init(); }
  632. KeyboardController(USBHost *host) { init(); }
  633. // Some methods are in both public classes so we need to figure out which one to use
  634. operator bool() { return (device != nullptr); }
  635. // Main boot keyboard functions.
  636. uint16_t getKey() { return keyCode; }
  637. uint8_t getModifiers() { return modifiers; }
  638. uint8_t getOemKey() { return keyOEM; }
  639. void attachPress(void (*f)(int unicode)) { keyPressedFunction = f; }
  640. void attachRelease(void (*f)(int unicode)) { keyReleasedFunction = f; }
  641. void LEDS(uint8_t leds);
  642. uint8_t LEDS() {return leds_.byte;}
  643. void updateLEDS(void);
  644. bool numLock() {return leds_.numLock;}
  645. bool capsLock() {return leds_.capsLock;}
  646. bool scrollLock() {return leds_.scrollLock;}
  647. void numLock(bool f);
  648. void capsLock(bool f);
  649. void scrollLock(bool f);
  650. // Added for extras information.
  651. void attachExtrasPress(void (*f)(uint32_t top, uint16_t code)) { extrasKeyPressedFunction = f; }
  652. void attachExtrasRelease(void (*f)(uint32_t top, uint16_t code)) { extrasKeyReleasedFunction = f; }
  653. enum {MAX_KEYS_DOWN=4};
  654. protected:
  655. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  656. virtual void control(const Transfer_t *transfer);
  657. virtual void disconnect();
  658. static void callback(const Transfer_t *transfer);
  659. void new_data(const Transfer_t *transfer);
  660. void init();
  661. protected: // HID functions for extra keyboard data.
  662. virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
  663. virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
  664. virtual void hid_input_data(uint32_t usage, int32_t value);
  665. virtual void hid_input_end();
  666. virtual void disconnect_collection(Device_t *dev);
  667. private:
  668. void update();
  669. uint16_t convert_to_unicode(uint32_t mod, uint32_t key);
  670. void key_press(uint32_t mod, uint32_t key);
  671. void key_release(uint32_t mod, uint32_t key);
  672. void (*keyPressedFunction)(int unicode);
  673. void (*keyReleasedFunction)(int unicode);
  674. Pipe_t *datapipe;
  675. setup_t setup;
  676. uint8_t report[8];
  677. uint16_t keyCode;
  678. uint8_t modifiers;
  679. uint8_t keyOEM;
  680. uint8_t prev_report[8];
  681. KBDLeds_t leds_ = {0};
  682. Pipe_t mypipes[2] __attribute__ ((aligned(32)));
  683. Transfer_t mytransfers[4] __attribute__ ((aligned(32)));
  684. strbuf_t mystring_bufs[1];
  685. // Added to process secondary HID data.
  686. void (*extrasKeyPressedFunction)(uint32_t top, uint16_t code);
  687. void (*extrasKeyReleasedFunction)(uint32_t top, uint16_t code);
  688. uint32_t topusage_ = 0; // What top report am I processing?
  689. uint8_t collections_claimed_ = 0;
  690. volatile bool hid_input_begin_ = false;
  691. volatile bool hid_input_data_ = false; // did we receive any valid data with report?
  692. uint8_t count_keys_down_ = 0;
  693. uint16_t keys_down[MAX_KEYS_DOWN];
  694. };
  695. class MouseController : public USBHIDInput {
  696. public:
  697. MouseController(USBHost &host) { USBHIDParser::driver_ready_for_hid_collection(this); }
  698. bool available() { return mouseEvent; }
  699. void mouseDataClear();
  700. uint8_t getButtons() { return buttons; }
  701. int getMouseX() { return mouseX; }
  702. int getMouseY() { return mouseY; }
  703. int getWheel() { return wheel; }
  704. int getWheelH() { return wheelH; }
  705. protected:
  706. virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
  707. virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
  708. virtual void hid_input_data(uint32_t usage, int32_t value);
  709. virtual void hid_input_end();
  710. virtual void disconnect_collection(Device_t *dev);
  711. private:
  712. uint8_t collections_claimed = 0;
  713. volatile bool mouseEvent = false;
  714. volatile bool hid_input_begin_ = false;
  715. uint8_t buttons = 0;
  716. int mouseX = 0;
  717. int mouseY = 0;
  718. int wheel = 0;
  719. int wheelH = 0;
  720. };
  721. //--------------------------------------------------------------------------
  722. class JoystickController : public USBDriver, public USBHIDInput {
  723. public:
  724. JoystickController(USBHost &host) { init(); }
  725. uint16_t idVendor();
  726. uint16_t idProduct();
  727. const uint8_t *manufacturer();
  728. const uint8_t *product();
  729. const uint8_t *serialNumber();
  730. operator bool() { return (((device != nullptr) || (mydevice != nullptr)) && connected_); } // override as in both USBDriver and in USBHIDInput
  731. bool available() { return joystickEvent; }
  732. void joystickDataClear();
  733. uint32_t getButtons() { return buttons; }
  734. int getAxis(uint32_t index) { return (index < (sizeof(axis)/sizeof(axis[0]))) ? axis[index] : 0; }
  735. uint64_t axisMask() {return axis_mask_;}
  736. uint64_t axisChangedMask() { return axis_changed_mask_;}
  737. uint64_t axisChangeNotifyMask() {return axis_change_notify_mask_;}
  738. void axisChangeNotifyMask(uint64_t notify_mask) {axis_change_notify_mask_ = notify_mask;}
  739. // set functions functionality depends on underlying joystick.
  740. bool setRumble(uint8_t lValue, uint8_t rValue, uint8_t timeout=0xff);
  741. // setLEDs on PS4(RGB), PS3 simple LED setting (only uses lr)
  742. bool setLEDs(uint8_t lr, uint8_t lg=0, uint8_t lb=0); // sets Leds,
  743. enum { STANDARD_AXIS_COUNT = 10, ADDITIONAL_AXIS_COUNT = 54, TOTAL_AXIS_COUNT = (STANDARD_AXIS_COUNT+ADDITIONAL_AXIS_COUNT) };
  744. typedef enum { UNKNOWN=0, PS3, PS4, XBOXONE, XBOX360} joytype_t;
  745. joytype_t joystickType = UNKNOWN;
  746. protected:
  747. // From USBDriver
  748. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  749. virtual void control(const Transfer_t *transfer);
  750. virtual void disconnect();
  751. // From USBHIDInput
  752. virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
  753. virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
  754. virtual void hid_input_data(uint32_t usage, int32_t value);
  755. virtual void hid_input_end();
  756. virtual void disconnect_collection(Device_t *dev);
  757. virtual bool hid_process_out_data(const Transfer_t *transfer);
  758. private:
  759. // Class specific
  760. void init();
  761. USBHIDParser *driver_ = nullptr;
  762. joytype_t mapVIDPIDtoJoystickType(uint16_t idVendor, uint16_t idProduct, bool exclude_hid_devices);
  763. bool transmitPS4UserFeedbackMsg();
  764. bool transmitPS3UserFeedbackMsg();
  765. bool anychange = false;
  766. volatile bool joystickEvent = false;
  767. uint32_t buttons = 0;
  768. int axis[TOTAL_AXIS_COUNT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  769. uint64_t axis_mask_ = 0; // which axis have valid data
  770. uint64_t axis_changed_mask_ = 0;
  771. uint64_t axis_change_notify_mask_ = 0x3ff; // assume the low 10 values only.
  772. uint16_t additional_axis_usage_page_ = 0;
  773. uint16_t additional_axis_usage_start_ = 0;
  774. uint16_t additional_axis_usage_count_ = 0;
  775. // State values to output to Joystick.
  776. uint8_t rumble_lValue_ = 0;
  777. uint8_t rumble_rValue_ = 0;
  778. uint8_t rumble_timeout_ = 0;
  779. uint8_t leds_[3] = {0,0,0};
  780. uint8_t connected_ = 0; // what type of device if any is connected xbox 360...
  781. // Used by HID code
  782. uint8_t collections_claimed = 0;
  783. // Used by USBDriver code
  784. static void rx_callback(const Transfer_t *transfer);
  785. static void tx_callback(const Transfer_t *transfer);
  786. void rx_data(const Transfer_t *transfer);
  787. void tx_data(const Transfer_t *transfer);
  788. Pipe_t mypipes[3] __attribute__ ((aligned(32)));
  789. Transfer_t mytransfers[7] __attribute__ ((aligned(32)));
  790. strbuf_t mystring_bufs[1];
  791. uint8_t rx_ep_ = 0; // remember which end point this object is...
  792. uint16_t rx_size_ = 0;
  793. uint16_t tx_size_ = 0;
  794. Pipe_t *rxpipe_;
  795. Pipe_t *txpipe_;
  796. uint8_t rxbuf_[64]; // receive circular buffer
  797. uint8_t txbuf_[64]; // buffer to use to send commands to joystick
  798. // Mapping table to say which devices we handle
  799. typedef struct {
  800. uint16_t idVendor;
  801. uint16_t idProduct;
  802. joytype_t joyType;
  803. bool hidDevice;
  804. } product_vendor_mapping_t;
  805. static product_vendor_mapping_t pid_vid_mapping[];
  806. };
  807. //--------------------------------------------------------------------------
  808. class MIDIDevice : public USBDriver {
  809. public:
  810. enum { SYSEX_MAX_LEN = 290 };
  811. // Message type names for compatibility with Arduino MIDI library 4.3.1
  812. enum MidiType {
  813. InvalidType = 0x00, // For notifying errors
  814. NoteOff = 0x80, // Note Off
  815. NoteOn = 0x90, // Note On
  816. AfterTouchPoly = 0xA0, // Polyphonic AfterTouch
  817. ControlChange = 0xB0, // Control Change / Channel Mode
  818. ProgramChange = 0xC0, // Program Change
  819. AfterTouchChannel = 0xD0, // Channel (monophonic) AfterTouch
  820. PitchBend = 0xE0, // Pitch Bend
  821. SystemExclusive = 0xF0, // System Exclusive
  822. TimeCodeQuarterFrame = 0xF1, // System Common - MIDI Time Code Quarter Frame
  823. SongPosition = 0xF2, // System Common - Song Position Pointer
  824. SongSelect = 0xF3, // System Common - Song Select
  825. TuneRequest = 0xF6, // System Common - Tune Request
  826. Clock = 0xF8, // System Real Time - Timing Clock
  827. Start = 0xFA, // System Real Time - Start
  828. Continue = 0xFB, // System Real Time - Continue
  829. Stop = 0xFC, // System Real Time - Stop
  830. ActiveSensing = 0xFE, // System Real Time - Active Sensing
  831. SystemReset = 0xFF, // System Real Time - System Reset
  832. };
  833. MIDIDevice(USBHost &host) { init(); }
  834. MIDIDevice(USBHost *host) { init(); }
  835. void sendNoteOff(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) {
  836. send(0x80, note, velocity, channel, cable);
  837. }
  838. void sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel, uint8_t cable=0) {
  839. send(0x90, note, velocity, channel, cable);
  840. }
  841. void sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) {
  842. send(0xA0, note, pressure, channel, cable);
  843. }
  844. void sendAfterTouchPoly(uint8_t note, uint8_t pressure, uint8_t channel, uint8_t cable=0) {
  845. send(0xA0, note, pressure, channel, cable);
  846. }
  847. void sendControlChange(uint8_t control, uint8_t value, uint8_t channel, uint8_t cable=0) {
  848. send(0xB0, control, value, channel, cable);
  849. }
  850. void sendProgramChange(uint8_t program, uint8_t channel, uint8_t cable=0) {
  851. send(0xC0, program, 0, channel, cable);
  852. }
  853. void sendAfterTouch(uint8_t pressure, uint8_t channel, uint8_t cable=0) {
  854. send(0xD0, pressure, 0, channel, cable);
  855. }
  856. void sendPitchBend(int value, uint8_t channel, uint8_t cable=0) {
  857. if (value < -8192) {
  858. value = -8192;
  859. } else if (value > 8191) {
  860. value = 8191;
  861. }
  862. value += 8192;
  863. send(0xE0, value, value >> 7, channel, cable);
  864. }
  865. void sendSysEx(uint32_t length, const uint8_t *data, bool hasTerm=false, uint8_t cable=0) {
  866. //if (cable >= MIDI_NUM_CABLES) return;
  867. if (hasTerm) {
  868. send_sysex_buffer_has_term(data, length, cable);
  869. } else {
  870. send_sysex_add_term_bytes(data, length, cable);
  871. }
  872. }
  873. void sendRealTime(uint8_t type, uint8_t cable=0) {
  874. switch (type) {
  875. case 0xF8: // Clock
  876. case 0xFA: // Start
  877. case 0xFB: // Continue
  878. case 0xFC: // Stop
  879. case 0xFE: // ActiveSensing
  880. case 0xFF: // SystemReset
  881. send(type, 0, 0, 0, cable);
  882. break;
  883. default: // Invalid Real Time marker
  884. break;
  885. }
  886. }
  887. void sendTimeCodeQuarterFrame(uint8_t type, uint8_t value, uint8_t cable=0) {
  888. send(0xF1, ((type & 0x07) << 4) | (value & 0x0F), 0, 0, cable);
  889. }
  890. void sendSongPosition(uint16_t beats, uint8_t cable=0) {
  891. send(0xF2, beats, beats >> 7, 0, cable);
  892. }
  893. void sendSongSelect(uint8_t song, uint8_t cable=0) {
  894. send(0xF3, song, 0, 0, cable);
  895. }
  896. void sendTuneRequest(uint8_t cable=0) {
  897. send(0xF6, 0, 0, 0, cable);
  898. }
  899. void beginRpn(uint16_t number, uint8_t channel, uint8_t cable=0) {
  900. sendControlChange(101, number >> 7, channel, cable);
  901. sendControlChange(100, number, channel, cable);
  902. }
  903. void sendRpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) {
  904. sendControlChange(6, value >> 7, channel, cable);
  905. sendControlChange(38, value, channel, cable);
  906. }
  907. void sendRpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) {
  908. sendControlChange(96, amount, channel, cable);
  909. }
  910. void sendRpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) {
  911. sendControlChange(97, amount, channel, cable);
  912. }
  913. void endRpn(uint8_t channel, uint8_t cable=0) {
  914. sendControlChange(101, 0x7F, channel, cable);
  915. sendControlChange(100, 0x7F, channel, cable);
  916. }
  917. void beginNrpn(uint16_t number, uint8_t channel, uint8_t cable=0) {
  918. sendControlChange(99, number >> 7, channel, cable);
  919. sendControlChange(98, number, channel, cable);
  920. }
  921. void sendNrpnValue(uint16_t value, uint8_t channel, uint8_t cable=0) {
  922. sendControlChange(6, value >> 7, channel, cable);
  923. sendControlChange(38, value, channel, cable);
  924. }
  925. void sendNrpnIncrement(uint8_t amount, uint8_t channel, uint8_t cable=0) {
  926. sendControlChange(96, amount, channel, cable);
  927. }
  928. void sendNrpnDecrement(uint8_t amount, uint8_t channel, uint8_t cable=0) {
  929. sendControlChange(97, amount, channel, cable);
  930. }
  931. void endNrpn(uint8_t channel, uint8_t cable=0) {
  932. sendControlChange(99, 0x7F, channel, cable);
  933. sendControlChange(98, 0x7F, channel, cable);
  934. }
  935. void send(uint8_t type, uint8_t data1, uint8_t data2, uint8_t channel, uint8_t cable=0) {
  936. //if (cable >= MIDI_NUM_CABLES) return;
  937. if (type < 0xF0) {
  938. if (type < 0x80) return;
  939. type &= 0xF0;
  940. write_packed((type << 8) | (type >> 4) | ((cable & 0x0F) << 4)
  941. | (((channel - 1) & 0x0F) << 8) | ((data1 & 0x7F) << 16)
  942. | ((data2 & 0x7F) << 24));
  943. } else if (type >= 0xF8 || type == 0xF6) {
  944. write_packed((type << 8) | 0x0F | ((cable & 0x0F) << 4));
  945. } else if (type == 0xF1 || type == 0xF3) {
  946. write_packed((type << 8) | 0x02 | ((cable & 0x0F) << 4)
  947. | ((data1 & 0x7F) << 16));
  948. } else if (type == 0xF2) {
  949. write_packed((type << 8) | 0x03 | ((cable & 0x0F) << 4)
  950. | ((data1 & 0x7F) << 16) | ((data2 & 0x7F) << 24));
  951. }
  952. }
  953. void send_now(void) __attribute__((always_inline)) {
  954. }
  955. bool read(uint8_t channel=0);
  956. uint8_t getType(void) {
  957. return msg_type;
  958. };
  959. uint8_t getCable(void) {
  960. return msg_cable;
  961. }
  962. uint8_t getChannel(void) {
  963. return msg_channel;
  964. };
  965. uint8_t getData1(void) {
  966. return msg_data1;
  967. };
  968. uint8_t getData2(void) {
  969. return msg_data2;
  970. };
  971. uint8_t * getSysExArray(void) {
  972. return msg_sysex;
  973. }
  974. uint16_t getSysExArrayLength(void) {
  975. return msg_data2 << 8 | msg_data1;
  976. }
  977. void setHandleNoteOff(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  978. // type: 0x80 NoteOff
  979. handleNoteOff = fptr;
  980. }
  981. void setHandleNoteOn(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  982. // type: 0x90 NoteOn
  983. handleNoteOn = fptr;
  984. }
  985. void setHandleVelocityChange(void (*fptr)(uint8_t channel, uint8_t note, uint8_t velocity)) {
  986. // type: 0xA0 AfterTouchPoly
  987. handleVelocityChange = fptr;
  988. }
  989. void setHandleAfterTouchPoly(void (*fptr)(uint8_t channel, uint8_t note, uint8_t pressure)) {
  990. // type: 0xA0 AfterTouchPoly
  991. handleVelocityChange = fptr;
  992. }
  993. void setHandleControlChange(void (*fptr)(uint8_t channel, uint8_t control, uint8_t value)) {
  994. // type: 0xB0 ControlChange
  995. handleControlChange = fptr;
  996. }
  997. void setHandleProgramChange(void (*fptr)(uint8_t channel, uint8_t program)) {
  998. // type: 0xC0 ProgramChange
  999. handleProgramChange = fptr;
  1000. }
  1001. void setHandleAfterTouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  1002. // type: 0xD0 AfterTouchChannel
  1003. handleAfterTouch = fptr;
  1004. }
  1005. void setHandleAfterTouchChannel(void (*fptr)(uint8_t channel, uint8_t pressure)) {
  1006. // type: 0xD0 AfterTouchChannel
  1007. handleAfterTouch = fptr;
  1008. }
  1009. void setHandlePitchChange(void (*fptr)(uint8_t channel, int pitch)) {
  1010. // type: 0xE0 PitchBend
  1011. handlePitchChange = fptr;
  1012. }
  1013. void setHandleSysEx(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  1014. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  1015. handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  1016. }
  1017. void setHandleSystemExclusive(void (*fptr)(const uint8_t *data, uint16_t length, bool complete)) {
  1018. // type: 0xF0 SystemExclusive - multiple calls for message bigger than buffer
  1019. handleSysExPartial = (void (*)(const uint8_t *, uint16_t, uint8_t))fptr;
  1020. }
  1021. void setHandleSystemExclusive(void (*fptr)(uint8_t *data, unsigned int size)) {
  1022. // type: 0xF0 SystemExclusive - single call, message larger than buffer is truncated
  1023. handleSysExComplete = fptr;
  1024. }
  1025. void setHandleTimeCodeQuarterFrame(void (*fptr)(uint8_t data)) {
  1026. // type: 0xF1 TimeCodeQuarterFrame
  1027. handleTimeCodeQuarterFrame = fptr;
  1028. }
  1029. void setHandleSongPosition(void (*fptr)(uint16_t beats)) {
  1030. // type: 0xF2 SongPosition
  1031. handleSongPosition = fptr;
  1032. }
  1033. void setHandleSongSelect(void (*fptr)(uint8_t songnumber)) {
  1034. // type: 0xF3 SongSelect
  1035. handleSongSelect = fptr;
  1036. }
  1037. void setHandleTuneRequest(void (*fptr)(void)) {
  1038. // type: 0xF6 TuneRequest
  1039. handleTuneRequest = fptr;
  1040. }
  1041. void setHandleClock(void (*fptr)(void)) {
  1042. // type: 0xF8 Clock
  1043. handleClock = fptr;
  1044. }
  1045. void setHandleStart(void (*fptr)(void)) {
  1046. // type: 0xFA Start
  1047. handleStart = fptr;
  1048. }
  1049. void setHandleContinue(void (*fptr)(void)) {
  1050. // type: 0xFB Continue
  1051. handleContinue = fptr;
  1052. }
  1053. void setHandleStop(void (*fptr)(void)) {
  1054. // type: 0xFC Stop
  1055. handleStop = fptr;
  1056. }
  1057. void setHandleActiveSensing(void (*fptr)(void)) {
  1058. // type: 0xFE ActiveSensing
  1059. handleActiveSensing = fptr;
  1060. }
  1061. void setHandleSystemReset(void (*fptr)(void)) {
  1062. // type: 0xFF SystemReset
  1063. handleSystemReset = fptr;
  1064. }
  1065. void setHandleRealTimeSystem(void (*fptr)(uint8_t realtimebyte)) {
  1066. // type: 0xF8-0xFF - if more specific handler not configured
  1067. handleRealTimeSystem = fptr;
  1068. }
  1069. protected:
  1070. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  1071. virtual void disconnect();
  1072. static void rx_callback(const Transfer_t *transfer);
  1073. static void tx_callback(const Transfer_t *transfer);
  1074. void rx_data(const Transfer_t *transfer);
  1075. void tx_data(const Transfer_t *transfer);
  1076. void init();
  1077. void write_packed(uint32_t data);
  1078. void send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable);
  1079. void send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable);
  1080. void sysex_byte(uint8_t b);
  1081. private:
  1082. Pipe_t *rxpipe;
  1083. Pipe_t *txpipe;
  1084. enum { MAX_PACKET_SIZE = 64 };
  1085. enum { RX_QUEUE_SIZE = 80 }; // must be more than MAX_PACKET_SIZE/4
  1086. uint32_t rx_buffer[MAX_PACKET_SIZE/4];
  1087. uint32_t tx_buffer1[MAX_PACKET_SIZE/4];
  1088. uint32_t tx_buffer2[MAX_PACKET_SIZE/4];
  1089. uint16_t rx_size;
  1090. uint16_t tx_size;
  1091. uint32_t rx_queue[RX_QUEUE_SIZE];
  1092. bool rx_packet_queued;
  1093. uint16_t rx_head;
  1094. uint16_t rx_tail;
  1095. volatile uint8_t tx1_count;
  1096. volatile uint8_t tx2_count;
  1097. uint8_t rx_ep;
  1098. uint8_t tx_ep;
  1099. uint8_t rx_ep_type;
  1100. uint8_t tx_ep_type;
  1101. uint8_t msg_cable;
  1102. uint8_t msg_channel;
  1103. uint8_t msg_type;
  1104. uint8_t msg_data1;
  1105. uint8_t msg_data2;
  1106. uint8_t msg_sysex[SYSEX_MAX_LEN];
  1107. uint16_t msg_sysex_len;
  1108. void (*handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel);
  1109. void (*handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel);
  1110. void (*handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel);
  1111. void (*handleControlChange)(uint8_t ch, uint8_t control, uint8_t value);
  1112. void (*handleProgramChange)(uint8_t ch, uint8_t program);
  1113. void (*handleAfterTouch)(uint8_t ch, uint8_t pressure);
  1114. void (*handlePitchChange)(uint8_t ch, int pitch);
  1115. void (*handleSysExPartial)(const uint8_t *data, uint16_t length, uint8_t complete);
  1116. void (*handleSysExComplete)(uint8_t *data, unsigned int size);
  1117. void (*handleTimeCodeQuarterFrame)(uint8_t data);
  1118. void (*handleSongPosition)(uint16_t beats);
  1119. void (*handleSongSelect)(uint8_t songnumber);
  1120. void (*handleTuneRequest)(void);
  1121. void (*handleClock)(void);
  1122. void (*handleStart)(void);
  1123. void (*handleContinue)(void);
  1124. void (*handleStop)(void);
  1125. void (*handleActiveSensing)(void);
  1126. void (*handleSystemReset)(void);
  1127. void (*handleRealTimeSystem)(uint8_t rtb);
  1128. Pipe_t mypipes[3] __attribute__ ((aligned(32)));
  1129. Transfer_t mytransfers[7] __attribute__ ((aligned(32)));
  1130. strbuf_t mystring_bufs[1];
  1131. };
  1132. //--------------------------------------------------------------------------
  1133. class USBSerial: public USBDriver, public Stream {
  1134. public:
  1135. // FIXME: need different USBSerial, with bigger buffers for 480 Mbit & faster speed
  1136. enum { BUFFER_SIZE = 648 }; // must hold at least 6 max size packets, plus 2 extra bytes
  1137. enum { DEFAULT_WRITE_TIMEOUT = 3500};
  1138. USBSerial(USBHost &host) : txtimer(this) { init(); }
  1139. void begin(uint32_t baud, uint32_t format=USBHOST_SERIAL_8N1);
  1140. void end(void);
  1141. uint32_t writeTimeout() {return write_timeout_;}
  1142. void writeTimeOut(uint32_t write_timeout) {write_timeout_ = write_timeout;} // Will not impact current ones.
  1143. virtual int available(void);
  1144. virtual int peek(void);
  1145. virtual int read(void);
  1146. virtual int availableForWrite();
  1147. virtual size_t write(uint8_t c);
  1148. virtual void flush(void);
  1149. using Print::write;
  1150. protected:
  1151. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  1152. virtual void control(const Transfer_t *transfer);
  1153. virtual void disconnect();
  1154. virtual void timer_event(USBDriverTimer *whichTimer);
  1155. private:
  1156. static void rx_callback(const Transfer_t *transfer);
  1157. static void tx_callback(const Transfer_t *transfer);
  1158. void rx_data(const Transfer_t *transfer);
  1159. void tx_data(const Transfer_t *transfer);
  1160. void rx_queue_packets(uint32_t head, uint32_t tail);
  1161. void init();
  1162. static bool check_rxtx_ep(uint32_t &rxep, uint32_t &txep);
  1163. bool init_buffers(uint32_t rsize, uint32_t tsize);
  1164. void ch341_setBaud(uint8_t byte_index);
  1165. private:
  1166. Pipe_t mypipes[3] __attribute__ ((aligned(32)));
  1167. Transfer_t mytransfers[7] __attribute__ ((aligned(32)));
  1168. strbuf_t mystring_bufs[1];
  1169. USBDriverTimer txtimer;
  1170. uint32_t bigbuffer[(BUFFER_SIZE+3)/4];
  1171. setup_t setup;
  1172. uint8_t setupdata[16]; //
  1173. uint32_t baudrate;
  1174. uint32_t format_;
  1175. uint32_t write_timeout_ = DEFAULT_WRITE_TIMEOUT;
  1176. Pipe_t *rxpipe;
  1177. Pipe_t *txpipe;
  1178. uint8_t *rx1; // location for first incoming packet
  1179. uint8_t *rx2; // location for second incoming packet
  1180. uint8_t *rxbuf; // receive circular buffer
  1181. uint8_t *tx1; // location for first outgoing packet
  1182. uint8_t *tx2; // location for second outgoing packet
  1183. uint8_t *txbuf;
  1184. volatile uint16_t rxhead;// receive head
  1185. volatile uint16_t rxtail;// receive tail
  1186. volatile uint16_t txhead;
  1187. volatile uint16_t txtail;
  1188. uint16_t rxsize;// size of receive circular buffer
  1189. uint16_t txsize;// size of transmit circular buffer
  1190. volatile uint8_t rxstate;// bitmask: which receive packets are queued
  1191. volatile uint8_t txstate;
  1192. uint8_t pending_control;
  1193. uint8_t setup_state; // PL2303 - has several steps... Could use pending control?
  1194. uint8_t pl2303_v1; // Which version do we have
  1195. uint8_t pl2303_v2;
  1196. uint8_t interface;
  1197. bool control_queued;
  1198. typedef enum { UNKNOWN=0, CDCACM, FTDI, PL2303, CH341, CP210X } sertype_t;
  1199. sertype_t sertype;
  1200. typedef struct {
  1201. uint16_t idVendor;
  1202. uint16_t idProduct;
  1203. sertype_t sertype;
  1204. } product_vendor_mapping_t;
  1205. static product_vendor_mapping_t pid_vid_mapping[];
  1206. };
  1207. //--------------------------------------------------------------------------
  1208. class AntPlus: public USBDriver {
  1209. // Please post any AntPlus feedback or contributions on this forum thread:
  1210. // https://forum.pjrc.com/threads/43110-Ant-libarary-and-USB-driver-for-Teensy-3-5-6
  1211. public:
  1212. AntPlus(USBHost &host) : /* txtimer(this),*/ updatetimer(this) { init(); }
  1213. void begin(const uint8_t key=0);
  1214. void onStatusChange(void (*function)(int channel, int status)) {
  1215. user_onStatusChange = function;
  1216. }
  1217. void onDeviceID(void (*function)(int channel, int devId, int devType, int transType)) {
  1218. user_onDeviceID = function;
  1219. }
  1220. void onHeartRateMonitor(void (*f)(int bpm, int msec, int seqNum), uint32_t devid=0) {
  1221. profileSetup_HRM(&ant.dcfg[PROFILE_HRM], devid);
  1222. memset(&hrm, 0, sizeof(hrm));
  1223. user_onHeartRateMonitor = f;
  1224. }
  1225. void onSpeedCadence(void (*f)(float speed, float distance, float rpm), uint32_t devid=0) {
  1226. profileSetup_SPDCAD(&ant.dcfg[PROFILE_SPDCAD], devid);
  1227. memset(&spdcad, 0, sizeof(spdcad));
  1228. user_onSpeedCadence = f;
  1229. }
  1230. void onSpeed(void (*f)(float speed, float distance), uint32_t devid=0) {
  1231. profileSetup_SPEED(&ant.dcfg[PROFILE_SPEED], devid);
  1232. memset(&spd, 0, sizeof(spd));
  1233. user_onSpeed = f;
  1234. }
  1235. void onCadence(void (*f)(float rpm), uint32_t devid=0) {
  1236. profileSetup_CADENCE(&ant.dcfg[PROFILE_CADENCE], devid);
  1237. memset(&cad, 0, sizeof(cad));
  1238. user_onCadence = f;
  1239. }
  1240. void setWheelCircumference(float meters) {
  1241. wheelCircumference = meters * 1000.0f;
  1242. }
  1243. protected:
  1244. virtual void Task();
  1245. virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
  1246. virtual void disconnect();
  1247. virtual void timer_event(USBDriverTimer *whichTimer);
  1248. private:
  1249. static void rx_callback(const Transfer_t *transfer);
  1250. static void tx_callback(const Transfer_t *transfer);
  1251. void rx_data(const Transfer_t *transfer);
  1252. void tx_data(const Transfer_t *transfer);
  1253. void init();
  1254. size_t write(const void *data, const size_t size);
  1255. int read(void *data, const size_t size);
  1256. void transmit();
  1257. private:
  1258. Pipe_t mypipes[2] __attribute__ ((aligned(32)));
  1259. Transfer_t mytransfers[3] __attribute__ ((aligned(32)));
  1260. strbuf_t mystring_bufs[1];
  1261. //USBDriverTimer txtimer;
  1262. USBDriverTimer updatetimer;
  1263. Pipe_t *rxpipe;
  1264. Pipe_t *txpipe;
  1265. bool first_update;
  1266. uint8_t txbuffer[240];
  1267. uint8_t rxpacket[64];
  1268. volatile uint16_t txhead;
  1269. volatile uint16_t txtail;
  1270. volatile bool txready;
  1271. volatile uint8_t rxlen;
  1272. volatile bool do_polling;
  1273. private:
  1274. enum _eventi {
  1275. EVENTI_MESSAGE = 0,
  1276. EVENTI_CHANNEL,
  1277. EVENTI_TOTAL
  1278. };
  1279. enum _profiles {
  1280. PROFILE_HRM = 0,
  1281. PROFILE_SPDCAD,
  1282. PROFILE_POWER,
  1283. PROFILE_STRIDE,
  1284. PROFILE_SPEED,
  1285. PROFILE_CADENCE,
  1286. PROFILE_TOTAL
  1287. };
  1288. typedef struct {
  1289. uint8_t channel;
  1290. uint8_t RFFreq;
  1291. uint8_t networkNumber;
  1292. uint8_t stub;
  1293. uint8_t searchTimeout;
  1294. uint8_t channelType;
  1295. uint8_t deviceType;
  1296. uint8_t transType;
  1297. uint16_t channelPeriod;
  1298. uint16_t searchWaveform;
  1299. uint32_t deviceNumber; // deviceId
  1300. struct {
  1301. uint8_t chanIdOnce;
  1302. uint8_t keyAccepted;
  1303. uint8_t profileValid;
  1304. uint8_t channelStatus;
  1305. uint8_t channelStatusOld;
  1306. } flags;
  1307. } TDCONFIG;
  1308. struct {
  1309. uint8_t initOnce;
  1310. uint8_t key; // key index
  1311. int iDevice; // index to the antplus we're interested in, if > one found
  1312. TDCONFIG dcfg[PROFILE_TOTAL]; // channel config, we're using one channel per device
  1313. } ant;
  1314. void (*user_onStatusChange)(int channel, int status);
  1315. void (*user_onDeviceID)(int channel, int devId, int devType, int transType);
  1316. void (*user_onHeartRateMonitor)(int beatsPerMinute, int milliseconds, int sequenceNumber);
  1317. void (*user_onSpeedCadence)(float speed, float distance, float cadence);
  1318. void (*user_onSpeed)(float speed, float distance);
  1319. void (*user_onCadence)(float cadence);
  1320. void dispatchPayload(TDCONFIG *cfg, const uint8_t *payload, const int len);
  1321. static const uint8_t *getAntKey(const uint8_t keyIdx);
  1322. static uint8_t calcMsgChecksum (const uint8_t *buffer, const uint8_t len);
  1323. static uint8_t * findStreamSync(uint8_t *stream, const size_t rlen, int *pos);
  1324. static int msgCheckIntegrity(uint8_t *stream, const int len);
  1325. static int msgGetLength(uint8_t *stream);
  1326. int handleMessages(uint8_t *buffer, int tBytes);
  1327. void sendMessageChannelStatus(TDCONFIG *cfg, const uint32_t channelStatus);
  1328. void message_channel(const int chan, const int eventId,
  1329. const uint8_t *payload, const size_t dataLength);
  1330. void message_response(const int chan, const int msgId,
  1331. const uint8_t *payload, const size_t dataLength);
  1332. void message_event(const int channel, const int msgId,
  1333. const uint8_t *payload, const size_t dataLength);
  1334. int ResetSystem();
  1335. int RequestMessage(const int channel, const int message);
  1336. int SetNetworkKey(const int netNumber, const uint8_t *key);
  1337. int SetChannelSearchTimeout(const int channel, const int searchTimeout);
  1338. int SetChannelPeriod(const int channel, const int period);
  1339. int SetChannelRFFreq(const int channel, const int freq);
  1340. int SetSearchWaveform(const int channel, const int wave);
  1341. int OpenChannel(const int channel);
  1342. int CloseChannel(const int channel);
  1343. int AssignChannel(const int channel, const int channelType, const int network);
  1344. int SetChannelId(const int channel, const int deviceNum, const int deviceType,
  1345. const int transmissionType);
  1346. int SendBurstTransferPacket(const int channelSeq, const uint8_t *data);
  1347. int SendBurstTransfer(const int channel, const uint8_t *data, const int nunPackets);
  1348. int SendBroadcastData(const int channel, const uint8_t *data);
  1349. int SendAcknowledgedData(const int channel, const uint8_t *data);
  1350. int SendExtAcknowledgedData(const int channel, const int devNum, const int devType,
  1351. const int TranType, const uint8_t *data);
  1352. int SendExtBroadcastData(const int channel, const int devNum, const int devType,
  1353. const int TranType, const uint8_t *data);
  1354. int SendExtBurstTransferPacket(const int chanSeq, const int devNum,
  1355. const int devType, const int TranType, const uint8_t *data);
  1356. int SendExtBurstTransfer(const int channel, const int devNum, const int devType,
  1357. const int tranType, const uint8_t *data, const int nunPackets);
  1358. static void profileSetup_HRM(TDCONFIG *cfg, const uint32_t deviceId);
  1359. static void profileSetup_SPDCAD(TDCONFIG *cfg, const uint32_t deviceId);
  1360. static void profileSetup_POWER(TDCONFIG *cfg, const uint32_t deviceId);
  1361. static void profileSetup_STRIDE(TDCONFIG *cfg, const uint32_t deviceId);
  1362. static void profileSetup_SPEED(TDCONFIG *cfg, const uint32_t deviceId);
  1363. static void profileSetup_CADENCE(TDCONFIG *cfg, const uint32_t deviceId);
  1364. struct {
  1365. struct {
  1366. uint8_t bpm;
  1367. uint8_t sequence;
  1368. uint16_t time;
  1369. } previous;
  1370. } hrm;
  1371. void payload_HRM(TDCONFIG *cfg, const uint8_t *data, const size_t dataLength);
  1372. struct {
  1373. struct {
  1374. uint16_t cadenceTime;
  1375. uint16_t cadenceCt;
  1376. uint16_t speedTime;
  1377. uint16_t speedCt;
  1378. } previous;
  1379. float distance;
  1380. } spdcad;
  1381. void payload_SPDCAD(TDCONFIG *cfg, const uint8_t *data, const size_t dataLength);
  1382. /* struct {
  1383. struct {
  1384. uint8_t sequence;
  1385. uint16_t pedalPowerContribution;
  1386. uint8_t pedalPower;
  1387. uint8_t instantCadence;
  1388. uint16_t sumPower;
  1389. uint16_t instantPower;
  1390. } current;
  1391. struct {
  1392. uint16_t stub;
  1393. } previous;
  1394. } pwr; */
  1395. void payload_POWER(TDCONFIG *cfg, const uint8_t *data, const size_t dataLength);
  1396. /* struct {
  1397. struct {
  1398. uint16_t speed;
  1399. uint16_t cadence;
  1400. uint8_t strides;
  1401. } current;
  1402. struct {
  1403. uint8_t strides;
  1404. uint16_t speed;
  1405. uint16_t cadence;
  1406. } previous;
  1407. } stride; */
  1408. void payload_STRIDE(TDCONFIG *cfg, const uint8_t *data, const size_t dataLength);
  1409. struct {
  1410. struct {
  1411. uint16_t speedTime;
  1412. uint16_t speedCt;
  1413. } previous;
  1414. float distance;
  1415. } spd;
  1416. void payload_SPEED(TDCONFIG *cfg, const uint8_t *data, const size_t dataLength);
  1417. struct {
  1418. struct {
  1419. uint16_t cadenceTime;
  1420. uint16_t cadenceCt;
  1421. } previous;
  1422. } cad;
  1423. void payload_CADENCE(TDCONFIG *cfg, const uint8_t *data, const size_t dataLength);
  1424. uint16_t wheelCircumference; // default is WHEEL_CIRCUMFERENCE (2122cm)
  1425. };
  1426. //--------------------------------------------------------------------------
  1427. class RawHIDController : public USBHIDInput {
  1428. public:
  1429. RawHIDController(USBHost &host, uint32_t usage = 0) : fixed_usage_(usage) { init(); }
  1430. uint32_t usage(void) {return usage_;}
  1431. void attachReceive(bool (*f)(uint32_t usage, const uint8_t *data, uint32_t len)) {receiveCB = f;}
  1432. bool sendPacket(const uint8_t *buffer);
  1433. protected:
  1434. virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
  1435. virtual bool hid_process_in_data(const Transfer_t *transfer);
  1436. virtual bool hid_process_out_data(const Transfer_t *transfer);
  1437. virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
  1438. virtual void hid_input_data(uint32_t usage, int32_t value);
  1439. virtual void hid_input_end();
  1440. virtual void disconnect_collection(Device_t *dev);
  1441. private:
  1442. void init();
  1443. USBHIDParser *driver_;
  1444. enum { MAX_PACKET_SIZE = 64 };
  1445. bool (*receiveCB)(uint32_t usage, const uint8_t *data, uint32_t len) = nullptr;
  1446. uint8_t collections_claimed = 0;
  1447. //volatile bool hid_input_begin_ = false;
  1448. uint32_t fixed_usage_;
  1449. uint32_t usage_ = 0;
  1450. // See if we can contribute transfers
  1451. Transfer_t mytransfers[2] __attribute__ ((aligned(32)));
  1452. };
  1453. #endif