Skip to content

Commit dcfab6c

Browse files
committed
shared/tinyusb,extmod: Improve code organization and header declarations.
Consolidated extern declarations from scattered source locations into mp_usbd.h header. Made descriptor buffer and function declarations conditional on MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE to match their definitions. Removed dead code in descriptor.c. No functional changes - build sizes unchanged at 514,396 bytes (runtime) and 512,020 bytes (static). Signed-off-by: Andrew Leech <[email protected]> Signed-off-by: Andrew Leech <[email protected]>
1 parent fade98e commit dcfab6c

File tree

4 files changed

+22
-84
lines changed

4 files changed

+22
-84
lines changed

extmod/machine_usb_device.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ static mp_obj_t usb_device_make_new(const mp_obj_type_t *type, size_t n_args, si
8484
o->base.type = &machine_usb_device_type;
8585

8686
// Initialize fields common to both minimal and full modes
87-
extern mp_usbd_class_state_t mp_usbd_class_state;
8887
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
8988
// Runtime mode: read current class state to reflect default CDC configuration
9089
o->builtin_driver = mp_usbd_class_state.flags;
@@ -213,12 +212,10 @@ static mp_obj_t usb_device_active(size_t n_args, const mp_obj_t *args) {
213212
// Update class state based on current builtin_driver
214213
if (mp_obj_is_type(usbd->builtin_driver, &mp_type_usb_builtin)) {
215214
mp_obj_usb_builtin_t *builtin = MP_OBJ_TO_PTR(usbd->builtin_driver);
216-
extern mp_usbd_class_state_t mp_usbd_class_state;
217215
mp_usbd_class_state.flags = builtin->flags;
218216
}
219217
} else {
220218
// Disable all classes when deactivating
221-
extern mp_usbd_class_state_t mp_usbd_class_state;
222219
mp_usbd_class_state.flags = USB_BUILTIN_FLAG_NONE;
223220
}
224221
#endif
@@ -413,13 +410,10 @@ static uint8_t mp_usbd_get_str_max(uint8_t flags) {
413410
}
414411

415412
static const uint8_t *mp_usbd_get_builtin_desc_cfg(uint8_t flags) {
416-
extern uint8_t mp_usbd_desc_cfg_buffer[];
417-
extern const uint8_t *mp_usbd_generate_desc_cfg_unified(uint8_t flags, uint8_t *buffer);
418413
return mp_usbd_generate_desc_cfg_unified(flags, mp_usbd_desc_cfg_buffer);
419414
}
420415

421416
static size_t mp_usbd_get_desc_cfg_len(uint8_t flags) {
422-
extern size_t mp_usbd_get_descriptor_cfg_len_from_flags(uint8_t flags);
423417
return mp_usbd_get_descriptor_cfg_len_from_flags(flags);
424418
}
425419
#endif // MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE

shared/tinyusb/mp_usbd.h

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ extern const mp_obj_type_t mp_type_usb_builtin;
100100
#define USB_BUILTIN_FLAG_NONE 0x00
101101
#define USB_BUILTIN_FLAG_CDC 0x01
102102
#define USB_BUILTIN_FLAG_MSC 0x02
103-
#define USB_BUILTIN_FLAG_NCM 0x04
104103

105104
// Helper macros for checking enabled classes
106105
#define MP_USBD_CDC_ENABLED() (mp_usbd_class_state.flags & USB_BUILTIN_FLAG_CDC)
@@ -119,38 +118,6 @@ static inline void mp_usbd_init_class_state(void) {
119118
#endif
120119
}
121120

122-
// Calculate descriptor length from flags using compile-time conditionals
123-
static inline size_t mp_usbd_get_descriptor_cfg_len_from_flags(uint8_t flags) {
124-
size_t len = TUD_CONFIG_DESC_LEN;
125-
#if CFG_TUD_CDC
126-
if (flags & USB_BUILTIN_FLAG_CDC) {
127-
len += TUD_CDC_DESC_LEN;
128-
}
129-
#endif
130-
#if CFG_TUD_MSC
131-
if (flags & USB_BUILTIN_FLAG_MSC) {
132-
len += TUD_MSC_DESC_LEN;
133-
}
134-
#endif
135-
return len;
136-
}
137-
138-
// Calculate interface count from flags using compile-time conditionals
139-
static inline uint8_t mp_usbd_get_interface_count_from_flags(uint8_t flags) {
140-
uint8_t count = 0;
141-
#if CFG_TUD_CDC
142-
if (flags & USB_BUILTIN_FLAG_CDC) {
143-
count += 2; // CDC uses 2 interfaces
144-
}
145-
#endif
146-
#if CFG_TUD_MSC
147-
if (flags & USB_BUILTIN_FLAG_MSC) {
148-
count += 1; // MSC uses 1 interface
149-
}
150-
#endif
151-
return count;
152-
}
153-
154121
// Combined descriptor info calculation using compile-time conditionals
155122
static inline usb_desc_info_t mp_usbd_get_desc_info_from_flags(uint8_t flags) {
156123
usb_desc_info_t info = { .length = TUD_CONFIG_DESC_LEN, .interface_count = 0 };
@@ -169,6 +136,11 @@ static inline usb_desc_info_t mp_usbd_get_desc_info_from_flags(uint8_t flags) {
169136
return info;
170137
}
171138

139+
// Wrapper for length-only callers
140+
static inline size_t mp_usbd_get_descriptor_cfg_len_from_flags(uint8_t flags) {
141+
return mp_usbd_get_desc_info_from_flags(flags).length;
142+
}
143+
172144
// Initialise TinyUSB device
173145
static inline void mp_usbd_init_tud(void) {
174146
// Initialize class state before TinyUSB init
@@ -203,7 +175,16 @@ size_t mp_usbd_get_descriptor_cfg_len(void);
203175

204176
// Built-in USB device and configuration descriptor values
205177
extern const tusb_desc_device_t mp_usbd_builtin_desc_dev;
178+
179+
#if !MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
180+
// Static mode: Use static descriptor array
206181
extern const uint8_t mp_usbd_builtin_desc_cfg[MP_USBD_BUILTIN_DESC_CFG_LEN];
182+
#else
183+
// Runtime mode: Use buffer and generation functions
184+
extern uint8_t mp_usbd_desc_cfg_buffer[MP_USBD_BUILTIN_DESC_CFG_LEN];
185+
const uint8_t *mp_usbd_get_default_desc(void);
186+
const uint8_t *mp_usbd_generate_desc_cfg_unified(uint8_t flags, uint8_t *buffer);
187+
#endif
207188

208189
void mp_usbd_task_callback(mp_sched_node_t *node);
209190

@@ -219,6 +200,13 @@ static inline void mp_usbd_deinit(void) {
219200
// Called in soft reset path. No-op if no runtime USB devices require cleanup.
220201
}
221202

203+
// Minimal USB device structure for static mode (builtin_driver control only)
204+
typedef struct {
205+
mp_obj_base_t base;
206+
mp_obj_t builtin_driver; // Points to a USBBuiltin constant object
207+
bool active; // Has the user set the USB device active?
208+
} mp_obj_usb_device_t;
209+
222210
#else
223211
// Runtime USB Device support requires more complex init/deinit
224212
void mp_usbd_init(void);
@@ -264,15 +252,6 @@ typedef struct {
264252
mp_obj_t pend_excs[MP_USBD_MAX_PEND_EXCS];
265253
} mp_obj_usb_device_t;
266254

267-
#else // Static USBD drivers only
268-
269-
// Minimal USB device structure for static mode (builtin_driver control only)
270-
typedef struct {
271-
mp_obj_base_t base;
272-
mp_obj_t builtin_driver; // Points to a USBBuiltin constant object
273-
bool active; // Has the user set the USB device active?
274-
} mp_obj_usb_device_t;
275-
276255
#endif
277256

278257
// Return true if any built-in driver is enabled

shared/tinyusb/mp_usbd_descriptor.c

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -165,40 +165,10 @@ const uint8_t *mp_usbd_generate_desc_cfg_unified(uint8_t flags, uint8_t *buffer)
165165

166166
// Get dynamic descriptor length based on enabled classes
167167
size_t mp_usbd_get_descriptor_cfg_len(void) {
168-
size_t len = TUD_CONFIG_DESC_LEN;
169-
170-
#if CFG_TUD_CDC
171-
if (MP_USBD_CDC_ENABLED()) {
172-
len += TUD_CDC_DESC_LEN;
173-
}
174-
#endif
175-
#if CFG_TUD_MSC
176-
if (MP_USBD_MSC_ENABLED()) {
177-
len += TUD_MSC_DESC_LEN;
178-
}
179-
#endif
180-
181-
return len;
168+
return mp_usbd_get_descriptor_cfg_len_from_flags(mp_usbd_class_state.flags);
182169
}
183170

184171

185-
#if 0
186-
// Static descriptor for maximum possible configuration
187-
static const uint8_t mp_usbd_builtin_desc_cfg_max[MP_USBD_BUILTIN_DESC_CFG_LEN] = {
188-
TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_BUILTIN_MAX, USBD_STR_0, MP_USBD_BUILTIN_DESC_CFG_LEN,
189-
0, USBD_MAX_POWER_MA),
190-
191-
#if CFG_TUD_CDC
192-
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
193-
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
194-
#endif
195-
#if CFG_TUD_MSC
196-
TUD_MSC_DESCRIPTOR(USBD_ITF_MSC, USBD_STR_MSC, USBD_MSC_EP_OUT, USBD_MSC_EP_IN, USBD_MSC_IN_OUT_MAX_SIZE),
197-
#endif
198-
};
199-
#endif
200-
201-
202172
// Dynamic descriptor buffer for runtime configuration
203173
static uint8_t mp_usbd_dynamic_desc_cfg[MP_USBD_BUILTIN_DESC_CFG_LEN];
204174

shared/tinyusb/mp_usbd_runtime.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,16 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
123123

124124
// If no runtime descriptor but builtin_driver is set, generate from flags
125125
if (!result && usbd->builtin_driver != USB_BUILTIN_FLAG_NONE) {
126-
extern uint8_t mp_usbd_desc_cfg_buffer[];
127-
extern const uint8_t *mp_usbd_generate_desc_cfg_unified(uint8_t flags, uint8_t *buffer);
128-
extern mp_usbd_class_state_t mp_usbd_class_state;
129126
mp_usbd_class_state.flags = usbd->builtin_driver;
130127
result = mp_usbd_generate_desc_cfg_unified(usbd->builtin_driver, mp_usbd_desc_cfg_buffer);
131128
}
132129
}
133130
// Fallback to descriptor (needed for boot before Python initializes)
134131
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
135132
// Runtime mode: Generate default descriptor from templates
136-
extern const uint8_t *mp_usbd_get_default_desc(void);
137133
return result ? result : mp_usbd_get_default_desc();
138134
#else
139135
// Static mode: Use static descriptor
140-
extern const uint8_t mp_usbd_builtin_desc_cfg[];
141136
return result ? result : mp_usbd_builtin_desc_cfg;
142137
#endif
143138
}

0 commit comments

Comments
 (0)