-
Notifications
You must be signed in to change notification settings - Fork 3
/
usb_execution.cpp
313 lines (288 loc) · 8.08 KB
/
usb_execution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "usb_execution.h"
#include "UsbControllerDevice.h"
#include "UsbKeyboard.h"
#include "UsbGamepad.h"
#include "configuration.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bsp/board.h"
#include "tusb.h"
#include "device/dcd.h"
#include "usb_descriptors.h"
bool usbEnabled = false;
UsbControllerDevice** pAllUsbDevices = nullptr;
uint8_t numUsbDevices = 0;
bool usbDisconnecting = false;
absolute_time_t usbDisconnectTime;
void set_usb_devices(UsbControllerDevice** devices, uint8_t n)
{
pAllUsbDevices = devices;
numUsbDevices = n;
}
bool gIsConnected = false;
const int16_t INVALID_INDEX = -1;
void led_task()
{
static bool ledOn = false;
static uint32_t startMs = 0;
if (usbDisconnecting)
{
// Currently counting down to disconnect; flash LED
static const uint32_t BLINK_TIME_MS = 500;
uint32_t t = board_millis() - startMs;
if (t >= BLINK_TIME_MS)
{
startMs += BLINK_TIME_MS;
ledOn = !ledOn;
}
}
else
{
bool keyPressed = false;
UsbControllerDevice** pdevs = pAllUsbDevices;
for (uint32_t i = numUsbDevices; i > 0; --i, ++pdevs)
{
if ((*pdevs)->isButtonPressed())
{
keyPressed = true;
}
}
if (gIsConnected)
{
// When connected, LED is ON only when no key is pressed
ledOn = !keyPressed;
}
else
{
// When not connected, LED blinks when key is pressed
static const uint32_t BLINK_TIME_MS = 100;
uint32_t t = board_millis() - startMs;
if (t >= BLINK_TIME_MS)
{
startMs += BLINK_TIME_MS;
ledOn = !ledOn;
}
ledOn = ledOn && keyPressed;
}
}
board_led_write(ledOn);
}
void usb_init()
{
tusb_init();
#if (!USB_ALWAYS_CONNECTED)
// Initialize with USB in disconnected state, connect later once controller detected
dcd_disconnect(0);
#endif
}
void start_disconnecting_countdown()
{
double delaySeconds = 0;
if (is_usb_descriptor_player1_only())
{
delaySeconds = SINGLE_PLAYER_DISCONNECT_DELAY_S;
}
else
{
delaySeconds = DISCONNECT_DELAY_S;
}
update_us_since_boot(&usbDisconnectTime, time_us_64() + (1000000UL * delaySeconds));
usbDisconnecting = true;
}
// This function is a bit of a mess, but it works
void usb_connection_state_machine()
{
static bool lastPlayer1Connected = false;
static bool lastPlayer2Connected = false;
bool player1Connected = false;
bool player2Connected = false;
if (numUsbDevices > 0)
{
player1Connected = pAllUsbDevices[0]->isControllerConnected();
}
if (numUsbDevices > 1)
{
player2Connected = pAllUsbDevices[1]->isControllerConnected();
}
// Handle disconnecting state
bool disconnectWaitComplete = false;
if (usbDisconnecting && !disconnectWaitComplete)
{
absolute_time_t currentTime;
update_us_since_boot(¤tTime, time_us_64());
if ((player1Connected && (player2Connected || is_usb_descriptor_player1_only()))
|| to_us_since_boot(currentTime) >= to_us_since_boot(usbDisconnectTime))
{
// Back to normal or time elapsed
disconnectWaitComplete = true;
usbDisconnecting = false;
}
else if ((player1Connected && !lastPlayer1Connected)
|| (player2Connected && !lastPlayer2Connected && !is_usb_descriptor_player1_only()))
{
// Player 1 or 2 reconnected; restart countdown
start_disconnecting_countdown();
}
}
else if (is_usb_descriptor_player1_only())
{
// Wait complete is immediately true if disconnect delay is 0
disconnectWaitComplete = (SINGLE_PLAYER_DISCONNECT_DELAY_S <= 0.0);
}
else
{
// Wait complete is immediately true if disconnect delay is 0
disconnectWaitComplete = (DISCONNECT_DELAY_S <= 0.0);
}
// Handle normal state
if (!usbDisconnecting || disconnectWaitComplete)
{
// Enable USB iff player 1 is connected
if (player1Connected != usbEnabled)
{
if (player1Connected)
{
// Just connected player 1 controller - enable USB
set_usb_descriptor_player1_only(!player2Connected);
dcd_connect(0);
usbEnabled = true;
}
else
{
if (!disconnectWaitComplete)
{
// Just disconnected - wait the necessary amount of time before acting
start_disconnecting_countdown();
}
else
{
// Countdown elapsed
dcd_disconnect(0);
usbEnabled = false;
}
}
}
else
{
if (usbEnabled && is_usb_descriptor_player1_only() == player2Connected)
{
if (!player2Connected && !disconnectWaitComplete)
{
// Player 2 just disconnected - wait the necessary amount of time before acting
start_disconnecting_countdown();
}
else
{
// Player 2 was added or was disconnected with countdown elapsed
// Need to switch on or off player 2 over USB
// Disconnecting then reconnecting is the only way I know how to refresh the USB config
dcd_disconnect(0);
sleep_ms(50);
tud_task(); // tinyusb device task
led_task();
set_usb_descriptor_player1_only(!player2Connected);
sleep_ms(100);
dcd_connect(0);
}
}
}
}
lastPlayer1Connected = player1Connected;
lastPlayer2Connected = player2Connected;
}
void usb_task()
{
#if (!USB_ALWAYS_CONNECTED)
usb_connection_state_machine();
#endif
tud_task(); // tinyusb device task
led_task();
}
//--------------------------------------------------------------------+
// Device callbacks
//--------------------------------------------------------------------+
// Invoked when device is mounted
void tud_mount_cb(void)
{
UsbControllerDevice** pdevs = pAllUsbDevices;
for (uint32_t i = numUsbDevices; i > 0; --i, ++pdevs)
{
if (!is_usb_descriptor_player1_only() || i == numUsbDevices)
{
(*pdevs)->updateUsbConnected(true);
}
}
gIsConnected = true;
}
// Invoked when device is unmounted
void tud_umount_cb(void)
{
UsbControllerDevice** pdevs = pAllUsbDevices;
for (uint32_t i = numUsbDevices; i > 0; --i, ++pdevs)
{
(*pdevs)->updateUsbConnected(false);
}
gIsConnected = false;
}
// Invoked when usb bus is suspended
// remote_wakeup_en : if host allow us to perform remote wakeup
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
void tud_suspend_cb(bool remote_wakeup_en)
{
(void) remote_wakeup_en;
UsbControllerDevice** pdevs = pAllUsbDevices;
for (uint32_t i = numUsbDevices; i > 0; --i, ++pdevs)
{
(*pdevs)->updateUsbConnected(false);
}
gIsConnected = false;
}
// Invoked when usb bus is resumed
void tud_resume_cb(void)
{
UsbControllerDevice** pdevs = pAllUsbDevices;
for (uint32_t i = numUsbDevices; i > 0; --i, ++pdevs)
{
if (!is_usb_descriptor_player1_only() || i == numUsbDevices)
{
(*pdevs)->updateUsbConnected(true);
}
}
gIsConnected = true;
}
//--------------------------------------------------------------------+
// USB HID
//--------------------------------------------------------------------+
// Invoked when received GET_REPORT control request
// Application must fill buffer report's content and return its length.
// Return zero will cause the stack to STALL request
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t *buffer, uint16_t reqlen)
{
(void) report_id;
(void) report_type;
if (instance < numUsbDevices)
{
return 0;
}
else
{
// Build the report for the given report ID
pAllUsbDevices[instance]->getReport(buffer, reqlen);
// Return the size of the report
return sizeof(hid_keyboard_report_t);
}
}
// Invoked when received SET_REPORT control request or
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
void tud_hid_set_report_cb(uint8_t instance,
uint8_t report_id,
hid_report_type_t report_type,
uint8_t const *buffer,
uint16_t bufsize)
{
(void) instance;
(void) report_type;
// echo back anything we received from host
tud_hid_report(report_id, buffer, bufsize);
}