Skip to content

Commit f833c7c

Browse files
Replace unused TinyUSB with stubs, enable NCM, 512b MSC (#3272)
Replace the specific device callbacks with weak stubs unless the application actually uses a library which needs it. MSC_Device saves 2136b flash, 128b RAM MIDI_Device saves 680b flash, 328b RAM HID_Device saves 816b flash, 408b RAM In total 3.6K flash and 850b RAM. Apps which manually try and build their own usage of the low-level TinyUSB code need to include the appropriate <tusb-xxx.h> file. Enable TinyUSB NCM (network) option Expand MSDCbuffer to a full sector (512b). Will speed up FatFSUSB and SingleFileDrive without causing memory pressure for apps not using MSD. Update MSD examples with add'l delay: There is what seems to be a known race condition in TinyUSB on the Pico, see hathach/tinyusb#1764 . Delay text output a little bit after the MSD is started to avoid.
1 parent b41e31f commit f833c7c

File tree

31 files changed

+258
-7
lines changed

31 files changed

+258
-7
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdint.h>
2+
#include <tusb.h>
3+
4+
extern void hidd_init() __attribute__((weak));
5+
void hidd_init() {
6+
}
7+
8+
extern bool hidd_deinit() __attribute__((weak));
9+
bool hidd_deinit() {
10+
return true;
11+
}
12+
13+
extern void hidd_reset(uint8_t rhport) __attribute__((weak));
14+
void hidd_reset(uint8_t rhport) {
15+
(void) rhport;
16+
}
17+
18+
extern uint16_t hidd_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint16_t max_len) __attribute__((weak));
19+
uint16_t hidd_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint16_t max_len) {
20+
(void) rhport;
21+
(void) desc_itf;
22+
(void) max_len;
23+
return 0;
24+
}
25+
26+
extern bool hidd_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) __attribute__((weak));
27+
bool hidd_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) {
28+
(void) rhport;
29+
(void) stage;
30+
(void) request;
31+
return false;
32+
}
33+
34+
extern bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) __attribute__((weak));
35+
bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
36+
(void) rhport;
37+
(void) ep_addr;
38+
(void) result;
39+
(void) xferred_bytes;
40+
return false;
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdint.h>
2+
#include <tusb.h>
3+
4+
extern void midid_init() __attribute__((weak));
5+
void midid_init() {
6+
}
7+
8+
extern bool midid_deinit() __attribute__((weak));
9+
bool midid_deinit() {
10+
return true;
11+
}
12+
13+
extern void midid_reset(uint8_t rhport) __attribute__((weak));
14+
void midid_reset(uint8_t rhport) {
15+
(void) rhport;
16+
}
17+
18+
extern uint16_t midid_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint16_t max_len) __attribute__((weak));
19+
uint16_t midid_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint16_t max_len) {
20+
(void) rhport;
21+
(void) desc_itf;
22+
(void) max_len;
23+
return 0;
24+
}
25+
26+
extern bool midid_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) __attribute__((weak));
27+
bool midid_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) {
28+
(void) rhport;
29+
(void) stage;
30+
(void) request;
31+
return false;
32+
}
33+
34+
extern bool midid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) __attribute__((weak));
35+
bool midid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
36+
(void) rhport;
37+
(void) ep_addr;
38+
(void) result;
39+
(void) xferred_bytes;
40+
return false;
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdint.h>
2+
#include <tusb.h>
3+
4+
extern void mscd_init() __attribute__((weak));
5+
void mscd_init() {
6+
}
7+
8+
extern bool mscd_deinit() __attribute__((weak));
9+
bool mscd_deinit() {
10+
return true;
11+
}
12+
13+
extern void mscd_reset(uint8_t rhport) __attribute__((weak));
14+
void mscd_reset(uint8_t rhport) {
15+
(void) rhport;
16+
}
17+
18+
extern uint16_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) __attribute__((weak));
19+
uint16_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) {
20+
(void) rhport;
21+
(void) itf_desc;
22+
(void) max_len;
23+
return 0;
24+
}
25+
26+
extern bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) __attribute__((weak));
27+
bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) {
28+
(void) rhport;
29+
(void) stage;
30+
(void) request;
31+
return false;
32+
}
33+
34+
bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) __attribute__((weak));
35+
bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) {
36+
(void) rhport;
37+
(void) ep_addr;
38+
(void) event;
39+
(void) xferred_bytes;
40+
return false;
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdint.h>
2+
#include <tusb.h>
3+
4+
extern void netd_init() __attribute__((weak));
5+
void netd_init() {
6+
}
7+
8+
extern bool netd_deinit() __attribute__((weak));
9+
bool netd_deinit() {
10+
return true;
11+
}
12+
13+
extern void netd_reset(uint8_t rhport) __attribute__((weak));
14+
void netd_reset(uint8_t rhport) {
15+
(void) rhport;
16+
}
17+
18+
extern uint16_t netd_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint16_t max_len) __attribute__((weak));
19+
uint16_t netd_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint16_t max_len) {
20+
(void) rhport;
21+
(void) desc_itf;
22+
(void) max_len;
23+
return 0;
24+
}
25+
26+
extern bool netd_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) __attribute__((weak));
27+
bool netd_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) {
28+
(void) rhport;
29+
(void) stage;
30+
(void) request;
31+
return false;
32+
}
33+
34+
extern bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) __attribute__((weak));
35+
bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
36+
(void) rhport;
37+
(void) ep_addr;
38+
(void) result;
39+
(void) xferred_bytes;
40+
return false;
41+
}

docs/usb.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ to the polling period:
102102
....
103103
}
104104
105+
Native TinyUSB Sketches
106+
-----------------------
107+
Sketches or libraries which want to implement their own low-level TinyUSB
108+
callbacks need to include the appropriate header to have real TinyUSB driver
109+
support for HID, MSC (USB stick), MIDI, and NCM (network).
110+
111+
.. code:: cpp
112+
113+
// Include one or more headers from the following list
114+
#include <tusb-hid.h>
115+
#include <tusb-midi.h>
116+
#include <tush-msc.h>
117+
#include <tusb-ncm.h>
118+
105119
Adafruit TinyUSB Arduino Support
106120
--------------------------------
107121
Examples are provided in the Adafruit_TinyUSB_Arduino for the more

include/tusb_config.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,42 @@
7575
#define CFG_TUD_MSC (1)
7676
#define CFG_TUD_MIDI (1)
7777
#define CFG_TUD_VENDOR (0)
78+
#define CFG_TUD_NCM (1)
7879

7980
#define CFG_TUD_CDC_RX_BUFSIZE (256)
8081
#define CFG_TUD_CDC_TX_BUFSIZE (256)
8182

8283
#define CFG_TUD_MSC_EP_BUFSIZE (64)
8384

8485
// HID buffer size Should be sufficient to hold ID (if any) + Data
85-
#define CFG_TUD_HID_EP_BUFSIZE (64)
86+
#define CFG_TUD_HID_EP_BUFSIZE (512)
8687

8788
// MIDI
8889
#define CFG_TUD_MIDI_RX_BUFSIZE (64)
8990
#define CFG_TUD_MIDI_TX_BUFSIZE (64)
9091

92+
//--------------------------------------------------------------------
93+
// NCM CLASS CONFIGURATION, SEE "ncm.h" FOR PERFORMANCE TUNING
94+
//--------------------------------------------------------------------
95+
#include "lwipopts.h"
96+
// Must be >> MTU
97+
// Can be set to 2048 without impact
98+
#define CFG_TUD_NCM_IN_NTB_MAX_SIZE (2 * TCP_MSS + 100)
99+
100+
// Must be >> MTU
101+
// Can be set to smaller values if wNtbOutMaxDatagrams==1
102+
#define CFG_TUD_NCM_OUT_NTB_MAX_SIZE (2 * TCP_MSS + 100)
103+
104+
// Number of NCM transfer blocks for reception side
105+
#ifndef CFG_TUD_NCM_OUT_NTB_N
106+
#define CFG_TUD_NCM_OUT_NTB_N 1
107+
#endif
108+
109+
// Number of NCM transfer blocks for transmission side
110+
#ifndef CFG_TUD_NCM_IN_NTB_N
111+
#define CFG_TUD_NCM_IN_NTB_N 1
112+
#endif
113+
91114
#ifdef __cplusplus
92115
}
93116
#endif

lib/rp2040/libpico.a

-116 KB
Binary file not shown.

lib/rp2350-riscv/libpico.a

-330 KB
Binary file not shown.

lib/rp2350/libpico.a

-117 KB
Binary file not shown.

libraries/FatFSUSB/examples/Listfiles-USB/Listfiles-USB.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void setup() {
5353
FatFSUSB.driveReady(mountable);
5454
// Start FatFS USB drive mode
5555
FatFSUSB.begin();
56+
delay(2000); // TinyUSB seems to have a race condition, see https://github.com/hathach/tinyusb/discussions/1764
5657
Serial.println("FatFSUSB started.");
5758
Serial.println("Connect drive via USB to upload/erase files and re-display");
5859
}

0 commit comments

Comments
 (0)