Skip to content

Commit 3483905

Browse files
committed
updates
1 parent 11f2fef commit 3483905

File tree

3 files changed

+61
-59
lines changed

3 files changed

+61
-59
lines changed

micropython/usb/examples/device/mtp_example.py

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,38 @@
1818

1919
import os
2020
import time
21-
import machine
21+
import usb.device
2222
from usb.device.mtp import MTPInterface
2323

24-
def main():
25-
"""Main function to run the MTP example."""
26-
# Print startup message
27-
print("MicroPython USB MTP Example")
28-
print("===========================")
29-
30-
# Create MTP interface to expose the filesystem
31-
# The root_dir parameter sets which directory to expose via MTP
32-
# Here we use '/' to expose the entire filesystem
33-
mtp = MTPInterface(root_dir="/")
34-
35-
# Create a USB device using machine.USBDevice
36-
usb_dev = machine.USBDevice(0xF055, 0x9802, "MicroPython", "MTP Device", "123456789")
37-
38-
# Add the MTP interface to the USB device
39-
usb_dev.add_interface(mtp)
40-
41-
# Enable the USB device
42-
usb_dev.enable()
43-
44-
# Wait for USB to be configured
45-
print("Waiting for USB connection...")
46-
while not usb_dev.configured():
47-
time.sleep(0.1)
48-
49-
print("USB MTP device connected!")
50-
print("You can now access files via the host computer")
51-
print("Press Ctrl+C to exit")
24+
# Print startup message
25+
print("MicroPython USB MTP Example")
26+
print("===========================")
27+
28+
# Create MTP interface to expose the filesystem
29+
# The root_dir parameter sets which directory to expose via MTP
30+
# Here we use '/' to expose the entire filesystem
31+
mtp = MTPInterface(root_dir="/")
32+
33+
# Initialize the USB device with the MTP interface
34+
# This registers the device with USB and starts accepting connections
35+
usb.device.get().init(mtp, builtin_driver=True)
36+
37+
# Wait for USB to be configured
38+
print("Waiting for USB connection...")
39+
# while not mtp.is_open():
40+
# time.sleep(0.1)
41+
42+
print("USB MTP device connected!")
43+
print("You can now access files via the host computer")
44+
print("Press Ctrl+C to exit")
5245

53-
try:
54-
# Keep the program running to maintain the USB connection
55-
while True:
56-
time.sleep(1)
57-
except KeyboardInterrupt:
58-
# Clean up on exit
59-
print("Exiting MTP example")
60-
usb_dev.disable()
61-
62-
if __name__ == "__main__":
63-
main()
46+
# try:
47+
# # Keep the program running to maintain the USB connection
48+
# while True:
49+
# time.sleep(1)
50+
# except KeyboardInterrupt:
51+
# # Clean up on exit
52+
# print("Exiting MTP example")
53+
54+
# if __name__ == "__main__":
55+
# main()

micropython/usb/usb-device-mtp/usb/device/mtp.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import io
1414
import uctypes
1515

16-
from usb.device import Interface, Descriptor, Buffer
16+
from .core import Interface, Descriptor, Buffer
1717
import usb.device
1818

1919
# MTP Class-specific constants
@@ -135,6 +135,11 @@
135135
# Handles follow
136136
}
137137

138+
_EP_IN_FLAG = const(1 << 7)
139+
EP_TYPE_BULK = "bulk"
140+
EP_TYPE_INTERRUPT = "interrupt"
141+
142+
138143
def string_to_utf16(s):
139144
"""Convert ASCII string to UTF-16 with length prefix as required by MTP."""
140145
utf16_bytes = s.encode('utf-16-le')
@@ -305,36 +310,40 @@ def desc_cfg(self, desc, itf_num, ep_num, strs):
305310
ep_num: First endpoint number to use
306311
strs: String descriptor array
307312
"""
313+
# Add the interface identifier to the strings
314+
iInterface = len(strs)
315+
strs.append("MTP")
316+
308317
# Store interface number
309318
self._itf_num = itf_num
310319

311320
# Interface Association Descriptor for MTP
312-
desc.interface_assoc(itf_num, 1, MTP_CLASS, MTP_SUBCLASS, MTP_PROTOCOL)
313-
314-
# Interface descriptor
315-
desc.interface(itf_num, 3, MTP_CLASS, MTP_SUBCLASS, MTP_PROTOCOL)
316-
317-
# Class-specific functional descriptor
318-
desc.pack(
319-
"<BBBBB",
320-
5, # bFunctionLength
321-
0x24, # bDescriptorType - CS_INTERFACE
322-
0x00, # bDescriptorSubtype
323-
0x01, # bcdMTPVersion - 1.0 LSB
324-
0x00, # bcdMTPVersion - 1.0 MSB
325-
)
321+
# desc.interface_assoc(itf_num, 1, MTP_CLASS, MTP_SUBCLASS, MTP_PROTOCOL)
322+
323+
# Interface descriptor
324+
desc.interface(itf_num, 3, MTP_CLASS, MTP_SUBCLASS, MTP_PROTOCOL, iInterface)
325+
326+
# # Class-specific functional descriptor
327+
# desc.pack(
328+
# "<BBBBB",
329+
# 5, # bFunctionLength
330+
# 0x24, # bDescriptorType - CS_INTERFACE
331+
# 0x00, # bDescriptorSubtype
332+
# 0x01, # bcdMTPVersion - 1.0 LSB
333+
# 0x00, # bcdMTPVersion - 1.0 MSB
334+
# )
326335

327336
# Endpoint descriptors
328337
# Bulk OUT endpoint
329338
self._bulk_out_ep = ep_num
330339
desc.endpoint(self._bulk_out_ep, "bulk", MTP_BULK_EP_SIZE, 0)
331340

332341
# Bulk IN endpoint
333-
self._bulk_in_ep = (ep_num + 1) | 0x80
342+
self._bulk_in_ep = ep_num | _EP_IN_FLAG
334343
desc.endpoint(self._bulk_in_ep, "bulk", MTP_BULK_EP_SIZE, 0)
335344

336345
# Interrupt IN endpoint for events
337-
self._intr_ep = (ep_num + 2) | 0x80
346+
self._intr_ep = (ep_num + 1) | _EP_IN_FLAG
338347
desc.endpoint(self._intr_ep, "interrupt", MTP_INTERRUPT_EP_SIZE, 10) # 10ms interval
339348

340349
def on_interface_control_xfer(self, stage, request):

micropython/usb/usb-device/usb/device/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def active(self, *optional_value):
242242
return self._usbd.active(*optional_value)
243243

244244
def _open_itf_cb(self, desc):
245+
self.did_open = desc
245246
# Singleton callback from TinyUSB custom class driver, when USB host does
246247
# Set Configuration. Called once per interface or IAD.
247248

@@ -277,8 +278,8 @@ def _open_itf_cb(self, desc):
277278
#
278279
# This means on_open() is only called once, and that it can
279280
# safely submit transfers on any of the USB interfaces' endpoints.
280-
if self._itfs.get(max_itf + 1, None) != itf:
281-
itf.on_open()
281+
# if self._itfs.get(max_itf + 1, None) != itf:
282+
itf.on_open()
282283

283284
def _reset_cb(self):
284285
# Callback when the USB device is reset by the host

0 commit comments

Comments
 (0)