Skip to content

Commit a34e2f2

Browse files
committed
update tinyusb to 0.16.0
1 parent 221ef2c commit a34e2f2

File tree

14 files changed

+144
-137
lines changed

14 files changed

+144
-137
lines changed

src/class/bth/bth_device.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ bool btd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t c
210210
request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_DEVICE)
211211
{
212212
// HCI command packet addressing for single function Primary Controllers
213-
TU_VERIFY(request->bRequest == 0 && request->wValue == 0 && request->wIndex == 0);
213+
// also compatible with historical mode if enabled
214+
TU_VERIFY((request->bRequest == 0 && request->wValue == 0 && request->wIndex == 0) ||
215+
(CFG_TUD_BTH_HISTORICAL_COMPATIBLE && request->bRequest == 0xe0));
214216
}
215217
else if (request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE)
216218
{

src/class/bth/bth_device.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@
3636
#ifndef CFG_TUD_BTH_EVENT_EPSIZE
3737
#define CFG_TUD_BTH_EVENT_EPSIZE 16
3838
#endif
39+
3940
#ifndef CFG_TUD_BTH_DATA_EPSIZE
4041
#define CFG_TUD_BTH_DATA_EPSIZE 64
4142
#endif
4243

44+
// Allow BTH class to work in historically compatibility mode where the bRequest is always 0xe0.
45+
// See Bluetooth Core v5.3, Vol. 4, Part B, Section 2.2
46+
#ifndef CFG_TUD_BTH_HISTORICAL_COMPATIBLE
47+
#define CFG_TUD_BTH_HISTORICAL_COMPATIBLE 0
48+
#endif
49+
4350
typedef struct TU_ATTR_PACKED
4451
{
4552
uint16_t op_code;

src/class/cdc/cdc.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,23 @@ typedef enum
182182
CDC_REQUEST_MDLM_SEMANTIC_MODEL = 0x60,
183183
}cdc_management_request_t;
184184

185-
enum
186-
{
185+
enum {
187186
CDC_CONTROL_LINE_STATE_DTR = 0x01,
188187
CDC_CONTROL_LINE_STATE_RTS = 0x02,
189188
};
190189

191-
enum
192-
{
193-
CDC_LINE_CONDING_STOP_BITS_1 = 0, // 1 bit
194-
CDC_LINE_CONDING_STOP_BITS_1_5 = 1, // 1.5 bits
195-
CDC_LINE_CONDING_STOP_BITS_2 = 2, // 2 bits
190+
enum {
191+
CDC_LINE_CODING_STOP_BITS_1 = 0, // 1 bit
192+
CDC_LINE_CODING_STOP_BITS_1_5 = 1, // 1.5 bits
193+
CDC_LINE_CODING_STOP_BITS_2 = 2, // 2 bits
196194
};
197195

198-
enum
199-
{
196+
// TODO Backward compatible for typos. Maybe removed in the future release
197+
#define CDC_LINE_CONDING_STOP_BITS_1 CDC_LINE_CODING_STOP_BITS_1
198+
#define CDC_LINE_CONDING_STOP_BITS_1_5 CDC_LINE_CODING_STOP_BITS_1_5
199+
#define CDC_LINE_CONDING_STOP_BITS_2 CDC_LINE_CODING_STOP_BITS_2
200+
201+
enum {
200202
CDC_LINE_CODING_PARITY_NONE = 0,
201203
CDC_LINE_CODING_PARITY_ODD = 1,
202204
CDC_LINE_CODING_PARITY_EVEN = 2,

src/class/cdc/cdc_host.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
4646
//#ifndef CFG_TUH_CDC_LINE_CODING_ON_ENUM
47-
//#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
47+
//#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
4848
//#endif
4949

5050
// RX FIFO size

src/class/hid/hid_host.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ typedef struct
7272
CFG_TUH_MEM_SECTION
7373
tu_static hidh_interface_t _hidh_itf[CFG_TUH_HID];
7474

75+
tu_static uint8_t _hidh_default_protocol = HID_PROTOCOL_BOOT;
76+
7577
//--------------------------------------------------------------------+
7678
// Helper
7779
//--------------------------------------------------------------------+
@@ -216,6 +218,10 @@ static void set_protocol_complete(tuh_xfer_t* xfer)
216218
}
217219
}
218220

221+
void tuh_hid_set_default_protocol(uint8_t protocol) {
222+
_hidh_default_protocol = protocol;
223+
}
224+
219225
static bool _hidh_set_protocol(uint8_t daddr, uint8_t itf_num, uint8_t protocol, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
220226
{
221227
TU_LOG_DRV("HID Set Protocol = %d\r\n", protocol);
@@ -526,7 +532,7 @@ bool hidh_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *desc_
526532
p_hid->report_desc_len = tu_unaligned_read16(&desc_hid->wReportLength);
527533

528534
// Per HID Specs: default is Report protocol, though we will force Boot protocol when set_config
529-
p_hid->protocol_mode = HID_PROTOCOL_BOOT;
535+
p_hid->protocol_mode = _hidh_default_protocol;
530536
if ( HID_SUBCLASS_BOOT == desc_itf->bInterfaceSubClass )
531537
{
532538
p_hid->itf_protocol = desc_itf->bInterfaceProtocol;
@@ -596,7 +602,7 @@ static void process_set_config(tuh_xfer_t* xfer)
596602
break;
597603

598604
case CONFIG_SET_PROTOCOL:
599-
_hidh_set_protocol(daddr, p_hid->itf_num, HID_PROTOCOL_BOOT, process_set_config, CONFIG_GET_REPORT_DESC);
605+
_hidh_set_protocol(daddr, p_hid->itf_num, _hidh_default_protocol, process_set_config, CONFIG_GET_REPORT_DESC);
600606
break;
601607

602608
case CONFIG_GET_REPORT_DESC:

src/class/hid/hid_host.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* reports_info_arr,
9797
// Application can use set_protocol() to switch back to Report protocol.
9898
uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t idx);
9999

100+
// Device by default is enumerated in Boot protocol for simplicity. Application
101+
// can use this to modify the default protocol for next enumeration.
102+
void tuh_hid_set_default_protocol(uint8_t protocol);
103+
100104
// Set protocol to HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
101105
// This function is only supported by Boot interface (tuh_n_hid_interface_protocol() != NONE)
102106
bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t idx, uint8_t protocol);

src/common/tusb_mcu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@
388388
#elif TU_CHECK_MCU(OPT_MCU_CH32V307)
389389
#define TUP_DCD_ENDPOINT_MAX 16
390390
#define TUP_RHPORT_HIGHSPEED 1
391+
392+
#elif TU_CHECK_MCU(OPT_MCU_CH32F20X)
393+
#define TUP_DCD_ENDPOINT_MAX 16
394+
#define TUP_RHPORT_HIGHSPEED 1
391395
#endif
392396

393397

src/device/dcd.h

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
// MACRO CONSTANT TYPEDEF PROTYPES
4848
//--------------------------------------------------------------------+
4949

50-
typedef enum
51-
{
50+
typedef enum {
5251
DCD_EVENT_INVALID = 0,
5352
DCD_EVENT_BUS_RESET,
5453
DCD_EVENT_UNPLUGGED,
@@ -65,13 +64,11 @@ typedef enum
6564
DCD_EVENT_COUNT
6665
} dcd_eventid_t;
6766

68-
typedef struct TU_ATTR_ALIGNED(4)
69-
{
67+
typedef struct TU_ATTR_ALIGNED(4) {
7068
uint8_t rhport;
7169
uint8_t event_id;
7270

73-
union
74-
{
71+
union {
7572
// BUS RESET
7673
struct {
7774
tusb_speed_t speed;
@@ -123,7 +120,7 @@ void dcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) TU_ATTR_W
123120
//--------------------------------------------------------------------+
124121

125122
// Initialize controller to device mode
126-
void dcd_init (uint8_t rhport);
123+
void dcd_init(uint8_t rhport);
127124

128125
// Interrupt Handler
129126
void dcd_int_handler(uint8_t rhport);
@@ -184,11 +181,11 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr);
184181
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr);
185182

186183
// Allocate packet buffer used by ISO endpoints
187-
// Some MCU need manual packet buffer allocation, we allocation largest size to avoid clustering
184+
// Some MCU need manual packet buffer allocation, we allocate the largest size to avoid clustering
188185
TU_ATTR_WEAK bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size);
189186

190187
// Configure and enable an ISO endpoint according to descriptor
191-
TU_ATTR_WEAK bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
188+
TU_ATTR_WEAK bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
192189

193190
//--------------------------------------------------------------------+
194191
// Event API (implemented by stack)
@@ -198,32 +195,28 @@ TU_ATTR_WEAK bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t co
198195
extern void dcd_event_handler(dcd_event_t const * event, bool in_isr);
199196

200197
// helper to send bus signal event
201-
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr)
202-
{
198+
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr) {
203199
dcd_event_t event = { .rhport = rhport, .event_id = eid };
204200
dcd_event_handler(&event, in_isr);
205201
}
206202

207203
// helper to send bus reset event
208-
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_bus_reset (uint8_t rhport, tusb_speed_t speed, bool in_isr)
209-
{
204+
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_bus_reset (uint8_t rhport, tusb_speed_t speed, bool in_isr) {
210205
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_BUS_RESET };
211206
event.bus_reset.speed = speed;
212207
dcd_event_handler(&event, in_isr);
213208
}
214209

215210
// helper to send setup received
216-
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr)
217-
{
211+
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr) {
218212
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SETUP_RECEIVED };
219213
memcpy(&event.setup_received, setup, sizeof(tusb_control_request_t));
220214

221215
dcd_event_handler(&event, in_isr);
222216
}
223217

224218
// helper to send transfer complete event
225-
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr)
226-
{
219+
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr) {
227220
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_XFER_COMPLETE };
228221

229222
event.xfer_complete.ep_addr = ep_addr;
@@ -233,8 +226,7 @@ TU_ATTR_ALWAYS_INLINE static inline void dcd_event_xfer_complete (uint8_t rhport
233226
dcd_event_handler(&event, in_isr);
234227
}
235228

236-
static inline void dcd_event_sof(uint8_t rhport, uint32_t frame_count, bool in_isr)
237-
{
229+
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_sof(uint8_t rhport, uint32_t frame_count, bool in_isr) {
238230
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SOF };
239231
event.sof.frame_count = frame_count;
240232
dcd_event_handler(&event, in_isr);

src/device/usbd.c

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -268,22 +268,17 @@ tu_static uint8_t _app_driver_count = 0;
268268

269269
// virtually joins built-in and application drivers together.
270270
// Application is positioned first to allow overwriting built-in ones.
271-
static inline usbd_class_driver_t const * get_driver(uint8_t drvid)
272-
{
271+
TU_ATTR_ALWAYS_INLINE static inline usbd_class_driver_t const * get_driver(uint8_t drvid) {
273272
usbd_class_driver_t const * driver = NULL;
274-
275273
if ( drvid < _app_driver_count ) {
276274
// Application drivers
277275
driver = &_app_driver[drvid];
278276
} else if ( drvid < TOTAL_DRIVER_COUNT && BUILTIN_DRIVER_COUNT > 0 ){
279277
driver = &_usbd_driver[drvid - _app_driver_count];
280278
}
281-
282279
return driver;
283280
}
284281

285-
286-
287282
//--------------------------------------------------------------------+
288283
// DCD Event
289284
//--------------------------------------------------------------------+
@@ -304,6 +299,11 @@ tu_static osal_queue_t _usbd_q;
304299
#define _usbd_mutex NULL
305300
#endif
306301

302+
TU_ATTR_ALWAYS_INLINE static inline bool queue_event(dcd_event_t const * event, bool in_isr) {
303+
bool ret = osal_queue_send(_usbd_q, event, in_isr);
304+
if (tud_event_hook_cb) tud_event_hook_cb(event->rhport, event->event_id, in_isr);
305+
return ret;
306+
}
307307

308308
//--------------------------------------------------------------------+
309309
// Prototypes
@@ -400,8 +400,7 @@ bool tud_connect(void)
400400
//--------------------------------------------------------------------+
401401
// USBD Task
402402
//--------------------------------------------------------------------+
403-
bool tud_inited(void)
404-
{
403+
bool tud_inited(void) {
405404
return _usbd_rhport != RHPORT_INVALID;
406405
}
407406

@@ -1103,66 +1102,64 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
11031102
//--------------------------------------------------------------------+
11041103
// DCD Event Handler
11051104
//--------------------------------------------------------------------+
1106-
TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const * event, bool in_isr)
1107-
{
1108-
switch (event->event_id)
1109-
{
1105+
TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const* event, bool in_isr) {
1106+
bool send = false;
1107+
switch (event->event_id) {
11101108
case DCD_EVENT_UNPLUGGED:
1111-
_usbd_dev.connected = 0;
1112-
_usbd_dev.addressed = 0;
1113-
_usbd_dev.cfg_num = 0;
1114-
_usbd_dev.suspended = 0;
1115-
osal_queue_send(_usbd_q, event, in_isr);
1116-
break;
1109+
_usbd_dev.connected = 0;
1110+
_usbd_dev.addressed = 0;
1111+
_usbd_dev.cfg_num = 0;
1112+
_usbd_dev.suspended = 0;
1113+
send = true;
1114+
break;
11171115

11181116
case DCD_EVENT_SUSPEND:
11191117
// NOTE: When plugging/unplugging device, the D+/D- state are unstable and
11201118
// can accidentally meet the SUSPEND condition ( Bus Idle for 3ms ).
11211119
// In addition, some MCUs such as SAMD or boards that haven no VBUS detection cannot distinguish
11221120
// suspended vs disconnected. We will skip handling SUSPEND/RESUME event if not currently connected
1123-
if ( _usbd_dev.connected )
1124-
{
1121+
if (_usbd_dev.connected) {
11251122
_usbd_dev.suspended = 1;
1126-
osal_queue_send(_usbd_q, event, in_isr);
1123+
send = true;
11271124
}
1128-
break;
1125+
break;
11291126

11301127
case DCD_EVENT_RESUME:
11311128
// skip event if not connected (especially required for SAMD)
1132-
if ( _usbd_dev.connected )
1133-
{
1129+
if (_usbd_dev.connected) {
11341130
_usbd_dev.suspended = 0;
1135-
osal_queue_send(_usbd_q, event, in_isr);
1131+
send = true;
11361132
}
1137-
break;
1133+
break;
11381134

11391135
case DCD_EVENT_SOF:
1140-
// SOF driver handler in ISR context
1141-
for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++)
1142-
{
1143-
usbd_class_driver_t const * driver = get_driver(i);
1144-
if (driver && driver->sof)
1145-
{
1146-
driver->sof(event->rhport, event->sof.frame_count);
1147-
}
1148-
}
1149-
11501136
// Some MCUs after running dcd_remote_wakeup() does not have way to detect the end of remote wakeup
11511137
// which last 1-15 ms. DCD can use SOF as a clear indicator that bus is back to operational
1152-
if ( _usbd_dev.suspended )
1153-
{
1138+
if (_usbd_dev.suspended) {
11541139
_usbd_dev.suspended = 0;
11551140

1156-
dcd_event_t const event_resume = { .rhport = event->rhport, .event_id = DCD_EVENT_RESUME };
1157-
osal_queue_send(_usbd_q, &event_resume, in_isr);
1141+
dcd_event_t const event_resume = {.rhport = event->rhport, .event_id = DCD_EVENT_RESUME};
1142+
queue_event(&event_resume, in_isr);
1143+
}
1144+
1145+
// SOF driver handler in ISR context
1146+
for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) {
1147+
usbd_class_driver_t const* driver = get_driver(i);
1148+
if (driver && driver->sof) {
1149+
driver->sof(event->rhport, event->sof.frame_count);
1150+
}
11581151
}
11591152

11601153
// skip osal queue for SOF in usbd task
1161-
break;
1154+
break;
11621155

11631156
default:
1164-
osal_queue_send(_usbd_q, event, in_isr);
1165-
break;
1157+
send = true;
1158+
break;
1159+
}
1160+
1161+
if (send) {
1162+
queue_event(event, in_isr);
11661163
}
11671164
}
11681165

@@ -1206,18 +1203,15 @@ bool usbd_open_edpt_pair(uint8_t rhport, uint8_t const* p_desc, uint8_t ep_count
12061203
}
12071204

12081205
// Helper to defer an isr function
1209-
void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr)
1210-
{
1211-
dcd_event_t event =
1212-
{
1206+
void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr) {
1207+
dcd_event_t event = {
12131208
.rhport = 0,
12141209
.event_id = USBD_EVENT_FUNC_CALL,
12151210
};
1216-
12171211
event.func_call.func = func;
12181212
event.func_call.param = param;
12191213

1220-
dcd_event_handler(&event, in_isr);
1214+
queue_event(&event, in_isr);
12211215
}
12221216

12231217
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)