annotate Output/pjrcUSB/arm/usb_joystick.c @ 449:45feb80a2ad1

Major USB update, fixes most (if not all) known issues USB - General - Refactored descriptors - Enabled/Disable USB endpoints - Added debug flags for special features - Code cleanup - Interface count calculation based off of enabled endpoints - Delayed wTotalLength calculation to simplify descriptor offsets - Re-ordered endpoints and interfaces - Added more debug output - Added usbInitTime to show how long keyboard initialization took (Useful when debugging bad init sequences) - Added function for usb_resume() which takes care of the resume sequence * Resume is now only called if packets are starting to timeout USB - Special Options - Added enableDeviceRestartOnUSBTimeout * A last resort hammer for bad USB Chipsets/OSs, don't use if you can help it * Disabled - Added enableUSBResume * Enables host resume wake-up signalling, required to wake a computer from sleep * Enabled - Added enableUSBLowPowerNegotiation * Enables power negotiation hack * Required to use firmware with an IPad and other hard-limit low-power USB hosts * Hasn't been tested with the recent changes * Disabled - Added enableUSBSuspend * Enables power down events on host USB bus suspend * Enabled USB - Keyboard - Attempted to cleanup HID SET_REPORT * Works much better * Still has an issue under Linux which generates *a lot* of NAKs (initializes quickly regardless) + Not present on other keyboards + SETUP -> OUT -> IN : This sequence is the problem + Specifically during the OUT phase - Enabled USB - CDC Virtual Serial Port - Code cleanup - Added convenience struct USBCDCLineCoding for easier debugging - Attempted to cleanup CDC_SET_LING_CODING * Works much better * Still has an issue under Linux which generates *a lot* of NAKs (initializes quickly regardless) + SETUP -> OUT -> IN : This sequence is the problem + Specifically during the OUT phase + Likely the same issues as HID SET_REPORT - Enabled USB - Mouse - Enabled USB - Joystick - Disabled USB - RawIO - Initial code, API not used yet - Disabled DFU - Updated load script, now faster
author Jacob Alexander <haata@kiibohd.com>
date Tue, 31 May 2016 00:19:45 -0700
parents ce9720634c15
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
369
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1 /* Teensyduino Core Library
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
2 * http://www.pjrc.com/teensy/
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
3 * Copyright (c) 2013 PJRC.COM, LLC.
449
45feb80a2ad1 Major USB update, fixes most (if not all) known issues
Jacob Alexander <haata@kiibohd.com>
parents: 369
diff changeset
4 * Modified by Jacob Alexander (2015-2016)
369
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
5 *
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
6 * Permission is hereby granted, free of charge, to any person obtaining
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
7 * a copy of this software and associated documentation files (the
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
8 * "Software"), to deal in the Software without restriction, including
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
9 * without limitation the rights to use, copy, modify, merge, publish,
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
10 * distribute, sublicense, and/or sell copies of the Software, and to
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
11 * permit persons to whom the Software is furnished to do so, subject to
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
12 * the following conditions:
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
13 *
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
14 * 1. The above copyright notice and this permission notice shall be
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
15 * included in all copies or substantial portions of the Software.
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
16 *
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
17 * 2. If the Software is incorporated into a build system that allows
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
18 * selection among a list of target devices, then similar target
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
19 * devices manufactured by PJRC.COM must be included in the list of
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
20 * target devices and selectable in the same manner.
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
21 *
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
26 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
27 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
29 * SOFTWARE.
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
30 */
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
31
449
45feb80a2ad1 Major USB update, fixes most (if not all) known issues
Jacob Alexander <haata@kiibohd.com>
parents: 369
diff changeset
32 #if enableJoystick_define == 1
45feb80a2ad1 Major USB update, fixes most (if not all) known issues
Jacob Alexander <haata@kiibohd.com>
parents: 369
diff changeset
33
369
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
34 // ----- Includes -----
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
35
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
36 // Compiler Includes
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
37 #include <string.h> // for memcpy()
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
38
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
39 // Project Includes
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
40 #include <Lib/OutputLib.h>
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
41 #include <print.h>
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
42
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
43 // Local Includes
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
44 #include "usb_dev.h"
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
45 #include "usb_joystick.h"
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
46
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
47
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
48
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
49 // ----- Defines -----
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
50
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
51 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
52 #define TX_PACKET_LIMIT 3
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
53
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
54 // When the PC isn't listening, how long do we wait before discarding data?
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
55 #define TX_TIMEOUT_MSEC 30
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
56
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
57 #if F_CPU == 168000000
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
58 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
59 #elif F_CPU == 144000000
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
60 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
61 #elif F_CPU == 120000000
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
62 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
63 #elif F_CPU == 96000000
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
64 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
65 #elif F_CPU == 72000000
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
66 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
67 #elif F_CPU == 48000000
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
68 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
69 #elif F_CPU == 24000000
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
70 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
71 #endif
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
72
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
73
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
74
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
75 // ----- Variables -----
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
76
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
77 uint32_t usb_joystick_data[3];
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
78
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
79
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
80 static uint8_t transmit_previous_timeout = 0;
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
81
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
82
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
83
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
84 // ----- Functions -----
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
85
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
86 int usb_joystick_send()
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
87 {
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
88 uint32_t wait_count=0;
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
89 usb_packet_t *tx_packet;
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
90
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
91 //serial_print("send");
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
92 //serial_print("\n");
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
93 while (1) {
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
94 if (!usb_configuration) {
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
95 //serial_print("error1\n");
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
96 return -1;
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
97 }
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
98 if (usb_tx_packet_count(JOYSTICK_ENDPOINT) < TX_PACKET_LIMIT) {
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
99 tx_packet = usb_malloc();
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
100 if (tx_packet) break;
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
101 }
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
102 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
103 transmit_previous_timeout = 1;
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
104 //serial_print("error2\n");
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
105 return -1;
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
106 }
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
107 yield();
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
108 }
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
109 transmit_previous_timeout = 0;
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
110 memcpy(tx_packet->buf, usb_joystick_data, 12);
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
111 tx_packet->len = 12;
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
112 usb_tx(JOYSTICK_ENDPOINT, tx_packet);
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
113 //serial_print("ok\n");
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
114 return 0;
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
115 }
ce9720634c15 Forgot to add these files in an earlier commit
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
116
449
45feb80a2ad1 Major USB update, fixes most (if not all) known issues
Jacob Alexander <haata@kiibohd.com>
parents: 369
diff changeset
117 #endif
45feb80a2ad1 Major USB update, fixes most (if not all) known issues
Jacob Alexander <haata@kiibohd.com>
parents: 369
diff changeset
118