Skip to content

Commit 8877294

Browse files
committed
update tinyusb to fix various bugs
add optional to debug tinyusb lib stack with Serial1 with samd
1 parent 02c478e commit 8877294

38 files changed

+367
-283
lines changed

src/arduino/Adafruit_TinyUSB_API.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void TinyUSB_Device_FlushCDC(void) {
5555
tud_cdc_n_write_flush(instance);
5656
}
5757
}
58-
}
58+
59+
} // extern C
5960

6061
#endif

src/arduino/ports/Adafruit_TinyUSB_samd.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ void USB_Handler(void) { tud_int_handler(0); }
5454

5555
} // extern C
5656

57-
//--------------------------------------------------------------------+
58-
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
59-
//--------------------------------------------------------------------+
60-
57+
// Debug log with Serial1
6158
#if CFG_TUSB_DEBUG
6259
extern "C" int serial1_printf(const char *__restrict format, ...) {
63-
char buf[PRINTF_BUF];
60+
char buf[256];
61+
int len;
6462
va_list ap;
6563
va_start(ap, format);
66-
vsnprintf(buf, sizeof(buf), format, ap);
64+
len = vsnprintf(buf, sizeof(buf), format, ap);
6765
Serial1.write(buf);
6866
va_end(ap);
67+
return len;
6968
}
7069
#endif
7170

71+
7272
//--------------------------------------------------------------------+
7373
// Porting API
7474
//--------------------------------------------------------------------+
@@ -127,6 +127,10 @@ void TinyUSB_Port_InitDevice(uint8_t rhport) {
127127
NVIC_SetPriority((IRQn_Type)USB_IRQn, 0UL);
128128
#endif
129129

130+
#if CFG_TUSB_DEBUG
131+
Serial1.begin(115200);
132+
#endif
133+
130134
tusb_init();
131135
}
132136

src/class/audio/audio_device.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@
5555
//--------------------------------------------------------------------+
5656
// INCLUDE
5757
//--------------------------------------------------------------------+
58+
#include "device/usbd.h"
5859
#include "device/usbd_pvt.h"
60+
5961
#include "audio_device.h"
60-
//#include "common/tusb_fifo.h"
6162

6263
//--------------------------------------------------------------------+
6364
// MACRO CONSTANT TYPEDEF
@@ -548,7 +549,7 @@ static bool audiod_rx_done_cb(uint8_t rhport, audiod_function_t* audio, uint16_t
548549
TU_VERIFY(usbd_edpt_xfer(rhport, audio->ep_out, audio->lin_buf_out, audio->ep_out_sz), false);
549550
#else
550551
// Data is already placed in EP FIFO, schedule for next receive
551-
TU_VERIFY(usbd_edpt_iso_xfer(rhport, audio->ep_out, &audio->ep_out_ff, audio->ep_out_sz), false);
552+
TU_VERIFY(usbd_edpt_xfer_fifo(rhport, audio->ep_out, &audio->ep_out_ff, audio->ep_out_sz), false);
552553
#endif
553554

554555
#endif
@@ -852,7 +853,7 @@ static bool audiod_tx_done_cb(uint8_t rhport, audiod_function_t * audio)
852853
TU_VERIFY(usbd_edpt_xfer(rhport, audio->ep_in, audio->lin_buf_in, n_bytes_tx));
853854
#else
854855
// Send everything in ISO EP FIFO
855-
TU_VERIFY(usbd_edpt_iso_xfer(rhport, audio->ep_in, &audio->ep_in_ff, n_bytes_tx));
856+
TU_VERIFY(usbd_edpt_xfer_fifo(rhport, audio->ep_in, &audio->ep_in_ff, n_bytes_tx));
856857
#endif
857858

858859
#endif
@@ -1611,7 +1612,7 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
16111612
#if USE_LINEAR_BUFFER_RX
16121613
TU_VERIFY(usbd_edpt_xfer(rhport, audio->ep_out, audio->lin_buf_out, audio->ep_out_sz), false);
16131614
#else
1614-
TU_VERIFY(usbd_edpt_iso_xfer(rhport, audio->ep_out, &audio->ep_out_ff, audio->ep_out_sz), false);
1615+
TU_VERIFY(usbd_edpt_xfer_fifo(rhport, audio->ep_out, &audio->ep_out_ff, audio->ep_out_sz), false);
16151616
#endif
16161617
}
16171618

src/class/audio/audio_device.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
#ifndef _TUSB_AUDIO_DEVICE_H_
2929
#define _TUSB_AUDIO_DEVICE_H_
3030

31-
#include "assert.h"
32-
#include "common/tusb_common.h"
33-
#include "device/usbd.h"
34-
3531
#include "audio.h"
3632

3733
//--------------------------------------------------------------------+

src/class/bth/bth_device.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
// INCLUDE
3333
//--------------------------------------------------------------------+
3434
#include "bth_device.h"
35-
#include <common/tusb_types.h>
3635
#include <device/usbd_pvt.h>
3736

3837
//--------------------------------------------------------------------+

src/class/cdc/cdc_device.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

2929
#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_CDC)
3030

31-
#include "cdc_device.h"
31+
#include "device/usbd.h"
3232
#include "device/usbd_pvt.h"
3333

34+
#include "cdc_device.h"
35+
3436
//--------------------------------------------------------------------+
3537
// MACRO CONSTANT TYPEDEF
3638
//--------------------------------------------------------------------+

src/class/cdc/cdc_device.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#define _TUSB_CDC_DEVICE_H_
2929

3030
#include "common/tusb_common.h"
31-
#include "device/usbd.h"
3231
#include "cdc.h"
3332

3433
//--------------------------------------------------------------------+

src/class/dfu/dfu_rt_device.c

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,18 @@
2828

2929
#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_DFU_RUNTIME)
3030

31-
#include "dfu_rt_device.h"
31+
#include "device/usbd.h"
3232
#include "device/usbd_pvt.h"
3333

34+
#include "dfu_rt_device.h"
35+
3436
//--------------------------------------------------------------------+
3537
// MACRO CONSTANT TYPEDEF
3638
//--------------------------------------------------------------------+
37-
typedef enum {
38-
DFU_REQUEST_DETACH = 0,
39-
DFU_REQUEST_DNLOAD = 1,
40-
DFU_REQUEST_UPLOAD = 2,
41-
DFU_REQUEST_GETSTATUS = 3,
42-
DFU_REQUEST_CLRSTATUS = 4,
43-
DFU_REQUEST_GETSTATE = 5,
44-
DFU_REQUEST_ABORT = 6,
45-
} dfu_requests_t;
46-
47-
typedef struct TU_ATTR_PACKED
48-
{
49-
uint8_t status;
50-
uint8_t poll_timeout[3];
51-
uint8_t state;
52-
uint8_t istring;
53-
} dfu_status_t;
39+
40+
//--------------------------------------------------------------------+
41+
// INTERNAL OBJECT & FUNCTION DECLARATION
42+
//--------------------------------------------------------------------+
5443

5544
//--------------------------------------------------------------------+
5645
// USBD Driver API
@@ -61,7 +50,7 @@ void dfu_rtd_init(void)
6150

6251
void dfu_rtd_reset(uint8_t rhport)
6352
{
64-
(void) rhport;
53+
(void) rhport;
6554
}
6655

6756
uint16_t dfu_rtd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
@@ -70,8 +59,8 @@ uint16_t dfu_rtd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, ui
7059
(void) max_len;
7160

7261
// Ensure this is DFU Runtime
73-
TU_VERIFY(itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS &&
74-
itf_desc->bInterfaceProtocol == DFU_PROTOCOL_RT, 0);
62+
TU_VERIFY((itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS) &&
63+
(itf_desc->bInterfaceProtocol == DFU_PROTOCOL_RT), 0);
7564

7665
uint8_t const * p_desc = tu_desc_next( itf_desc );
7766
uint16_t drv_len = sizeof(tusb_desc_interface_t);
@@ -90,7 +79,7 @@ uint16_t dfu_rtd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, ui
9079
// return false to stall control endpoint (e.g unsupported request)
9180
bool dfu_rtd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
9281
{
93-
// nothing to do with DATA and ACK stage
82+
// nothing to do with DATA or ACK stage
9483
if ( stage != CONTROL_STAGE_SETUP ) return true;
9584

9685
TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);
@@ -106,34 +95,34 @@ bool dfu_rtd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request
10695
// Handle class request only from here
10796
TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
10897

109-
switch ( request->bRequest )
98+
switch (request->bRequest)
11099
{
111100
case DFU_REQUEST_DETACH:
101+
{
102+
TU_LOG2(" DFU RT Request: DETACH\r\n");
112103
tud_control_status(rhport, request);
113104
tud_dfu_runtime_reboot_to_dfu_cb();
105+
}
114106
break;
115107

116108
case DFU_REQUEST_GETSTATUS:
117109
{
118-
// status = OK, poll timeout = 0, state = app idle, istring = 0
119-
uint8_t status_response[6] = { 0, 0, 0, 0, 0, 0 };
120-
tud_control_xfer(rhport, request, status_response, sizeof(status_response));
110+
TU_LOG2(" DFU RT Request: GETSTATUS\r\n");
111+
dfu_status_req_payload_t resp;
112+
// Status = OK, Poll timeout is ignored during RT, State = APP_IDLE, IString = 0
113+
memset(&resp, 0x00, sizeof(dfu_status_req_payload_t));
114+
tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_req_payload_t));
121115
}
122116
break;
123117

124-
default: return false; // stall unsupported request
118+
default:
119+
{
120+
TU_LOG2(" DFU RT Unexpected Request: %d\r\n", request->bRequest);
121+
return false; // stall unsupported request
122+
}
125123
}
126124

127125
return true;
128126
}
129127

130-
bool dfu_rtd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
131-
{
132-
(void) rhport;
133-
(void) ep_addr;
134-
(void) result;
135-
(void) xferred_bytes;
136-
return true;
137-
}
138-
139128
#endif

src/class/dfu/dfu_rt_device.h

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,17 @@
2727
#ifndef _TUSB_DFU_RT_DEVICE_H_
2828
#define _TUSB_DFU_RT_DEVICE_H_
2929

30-
#include "common/tusb_common.h"
31-
#include "device/usbd.h"
30+
#include "dfu.h"
3231

3332
#ifdef __cplusplus
3433
extern "C" {
3534
#endif
3635

37-
38-
//--------------------------------------------------------------------+
39-
// Common Definitions
40-
//--------------------------------------------------------------------+
41-
42-
// DFU Protocol
43-
typedef enum
44-
{
45-
DFU_PROTOCOL_RT = 1,
46-
DFU_PROTOCOL_DFU = 2,
47-
} dfu_protocol_type_t;
48-
49-
// DFU Descriptor Type
50-
typedef enum
51-
{
52-
DFU_DESC_FUNCTIONAL = 0x21,
53-
} dfu_descriptor_type_t;
54-
55-
5636
//--------------------------------------------------------------------+
5737
// Application Callback API (weak is optional)
5838
//--------------------------------------------------------------------+
59-
60-
// Invoked when received new data
61-
TU_ATTR_WEAK void tud_dfu_runtime_reboot_to_dfu_cb(void);
39+
// Invoked when a DFU_DETACH request is received and bitWillDetach is set
40+
void tud_dfu_runtime_reboot_to_dfu_cb(void);
6241

6342
//--------------------------------------------------------------------+
6443
// Internal Class Driver API
@@ -67,7 +46,6 @@ void dfu_rtd_init(void);
6746
void dfu_rtd_reset(uint8_t rhport);
6847
uint16_t dfu_rtd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
6948
bool dfu_rtd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
70-
bool dfu_rtd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
7149

7250
#ifdef __cplusplus
7351
}

src/class/hid/hid.h

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,30 +201,73 @@ typedef struct TU_ATTR_PACKED
201201
int8_t rx; ///< Delta Rx movement of analog left trigger
202202
int8_t ry; ///< Delta Ry movement of analog right trigger
203203
uint8_t hat; ///< Buttons mask for currently pressed buttons in the DPad/hat
204-
uint16_t buttons; ///< Buttons mask for currently pressed buttons
204+
uint32_t buttons; ///< Buttons mask for currently pressed buttons
205205
}hid_gamepad_report_t;
206206

207-
/// Standard Gamepad Buttons Bitmap (from Linux input event codes)
207+
/// Standard Gamepad Buttons Bitmap
208208
typedef enum
209209
{
210-
GAMEPAD_BUTTON_A = TU_BIT(0), ///< A/South button
211-
GAMEPAD_BUTTON_B = TU_BIT(1), ///< B/East button
212-
GAMEPAD_BUTTON_C = TU_BIT(2), ///< C button
213-
GAMEPAD_BUTTON_X = TU_BIT(3), ///< X/North button
214-
GAMEPAD_BUTTON_Y = TU_BIT(4), ///< Y/West button
215-
GAMEPAD_BUTTON_Z = TU_BIT(5), ///< Z button
216-
GAMEPAD_BUTTON_TL = TU_BIT(6), ///< L1 button
217-
GAMEPAD_BUTTON_TR = TU_BIT(7), ///< R1 button
218-
GAMEPAD_BUTTON_TL2 = TU_BIT(8), ///< L2 button
219-
GAMEPAD_BUTTON_TR2 = TU_BIT(9), ///< R2 button
220-
GAMEPAD_BUTTON_SELECT = TU_BIT(10), ///< Select button
221-
GAMEPAD_BUTTON_START = TU_BIT(11), ///< Start button
222-
GAMEPAD_BUTTON_MODE = TU_BIT(12), ///< Mode button
223-
GAMEPAD_BUTTON_THUMBL = TU_BIT(13), ///< L3 button
224-
GAMEPAD_BUTTON_THUMBR = TU_BIT(14), ///< R3 button
225-
//GAMEPAD_BUTTON_ = TU_BIT(15), ///< Undefined button
210+
GAMEPAD_BUTTON_0 = TU_BIT(0),
211+
GAMEPAD_BUTTON_1 = TU_BIT(1),
212+
GAMEPAD_BUTTON_2 = TU_BIT(2),
213+
GAMEPAD_BUTTON_3 = TU_BIT(3),
214+
GAMEPAD_BUTTON_4 = TU_BIT(4),
215+
GAMEPAD_BUTTON_5 = TU_BIT(5),
216+
GAMEPAD_BUTTON_6 = TU_BIT(6),
217+
GAMEPAD_BUTTON_7 = TU_BIT(7),
218+
GAMEPAD_BUTTON_8 = TU_BIT(8),
219+
GAMEPAD_BUTTON_9 = TU_BIT(9),
220+
GAMEPAD_BUTTON_10 = TU_BIT(10),
221+
GAMEPAD_BUTTON_11 = TU_BIT(11),
222+
GAMEPAD_BUTTON_12 = TU_BIT(12),
223+
GAMEPAD_BUTTON_13 = TU_BIT(13),
224+
GAMEPAD_BUTTON_14 = TU_BIT(14),
225+
GAMEPAD_BUTTON_15 = TU_BIT(15),
226+
GAMEPAD_BUTTON_16 = TU_BIT(16),
227+
GAMEPAD_BUTTON_17 = TU_BIT(17),
228+
GAMEPAD_BUTTON_18 = TU_BIT(18),
229+
GAMEPAD_BUTTON_19 = TU_BIT(19),
230+
GAMEPAD_BUTTON_20 = TU_BIT(20),
231+
GAMEPAD_BUTTON_21 = TU_BIT(21),
232+
GAMEPAD_BUTTON_22 = TU_BIT(22),
233+
GAMEPAD_BUTTON_23 = TU_BIT(23),
234+
GAMEPAD_BUTTON_24 = TU_BIT(24),
235+
GAMEPAD_BUTTON_25 = TU_BIT(25),
236+
GAMEPAD_BUTTON_26 = TU_BIT(26),
237+
GAMEPAD_BUTTON_27 = TU_BIT(27),
238+
GAMEPAD_BUTTON_28 = TU_BIT(28),
239+
GAMEPAD_BUTTON_29 = TU_BIT(29),
240+
GAMEPAD_BUTTON_30 = TU_BIT(30),
241+
GAMEPAD_BUTTON_31 = TU_BIT(31),
226242
}hid_gamepad_button_bm_t;
227243

244+
/// Standard Gamepad Buttons Naming from Linux input event codes
245+
/// https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h
246+
#define GAMEPAD_BUTTON_A GAMEPAD_BUTTON_0
247+
#define GAMEPAD_BUTTON_SOUTH GAMEPAD_BUTTON_0
248+
249+
#define GAMEPAD_BUTTON_B GAMEPAD_BUTTON_1
250+
#define GAMEPAD_BUTTON_EAST GAMEPAD_BUTTON_1
251+
252+
#define GAMEPAD_BUTTON_C GAMEPAD_BUTTON_2
253+
254+
#define GAMEPAD_BUTTON_X GAMEPAD_BUTTON_3
255+
#define GAMEPAD_BUTTON_NORTH GAMEPAD_BUTTON_3
256+
257+
#define GAMEPAD_BUTTON_Y GAMEPAD_BUTTON_4
258+
#define GAMEPAD_BUTTON_WEST GAMEPAD_BUTTON_4
259+
260+
#define GAMEPAD_BUTTON_Z GAMEPAD_BUTTON_5
261+
#define GAMEPAD_BUTTON_TL GAMEPAD_BUTTON_6
262+
#define GAMEPAD_BUTTON_TR GAMEPAD_BUTTON_7
263+
#define GAMEPAD_BUTTON_TL2 GAMEPAD_BUTTON_8
264+
#define GAMEPAD_BUTTON_TR2 GAMEPAD_BUTTON_9
265+
#define GAMEPAD_BUTTON_SELECT GAMEPAD_BUTTON_10
266+
#define GAMEPAD_BUTTON_START GAMEPAD_BUTTON_11
267+
#define GAMEPAD_BUTTON_MODE GAMEPAD_BUTTON_12
268+
#define GAMEPAD_BUTTON_THUMBL GAMEPAD_BUTTON_13
269+
#define GAMEPAD_BUTTON_THUMBR GAMEPAD_BUTTON_14
270+
228271
/// Standard Gamepad HAT/DPAD Buttons (from Linux input event codes)
229272
typedef enum
230273
{

0 commit comments

Comments
 (0)