Skip to content

Commit c5a1ea0

Browse files
andrewleechclaude
authored andcommitted
usb-device-mtp: Add MTP device driver for MicroPython filesystem.
Implements a Media Transfer Protocol (MTP) device driver that exposes the MicroPython filesystem to USB hosts. This allows accessing files on the device via standard file managers without requiring special tools. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7337e08 commit c5a1ea0

File tree

4 files changed

+1183
-0
lines changed

4 files changed

+1183
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# MicroPython USB MTP example
2+
#
3+
# This example demonstrates creating a USB MTP device to expose the MicroPython
4+
# filesystem to a host computer.
5+
#
6+
# To run this example:
7+
#
8+
# 1. Make sure `usb-device-mtp` is installed via: mpremote mip install usb-device-mtp
9+
#
10+
# 2. Run the example via: mpremote run mtp_example.py
11+
#
12+
# 3. mpremote will exit with an error after the previous step, because when the
13+
# example runs the existing USB device disconnects and then re-enumerates with
14+
# the MTP interface present. At this point, the example is running.
15+
#
16+
# 4. To see output from the example, reconnect: mpremote connect PORTNAME
17+
#
18+
# 5. On your host computer, you should see a new storage device appear that
19+
# allows browsing the MicroPython's filesystem.
20+
#
21+
# MIT license; Copyright (c) 2024 MicroPython Developers
22+
import usb.device
23+
from usb.device.mtp import MTPInterface
24+
import time
25+
26+
# Create an MTP interface that exposes the root directory
27+
mtp = MTPInterface(storage_path="/")
28+
29+
# Initialize the USB device with the MTP interface
30+
# Keep builtin_driver=True to maintain the serial REPL alongside MTP
31+
usb.device.get().init(mtp, builtin_driver=True)
32+
33+
print("Waiting for USB host to configure the MTP interface...")
34+
35+
# Wait for the host to configure the interface
36+
while not mtp.is_open():
37+
time.sleep_ms(100)
38+
39+
print("MTP interface is now available to the host.")
40+
print("Your MicroPython filesystem should be accessible on the host computer.")
41+
print("Press Ctrl+C to exit.")
42+
43+
# Keep the script running
44+
try:
45+
while True:
46+
time.sleep(1)
47+
except KeyboardInterrupt:
48+
print("MTP example terminated by user.")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# MicroPython USB MTP package
2+
# MIT license; Copyright (c) 2024 MicroPython Developers
3+
from usb.device.mtp import MTPInterface
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# MicroPython package manifest for usb-device-mtp
2+
package(
3+
"usb-device-mtp",
4+
(
5+
"__init__.py",
6+
"usb/device/mtp.py",
7+
),
8+
base_path="..",
9+
version="1.0.0",
10+
description="USB MTP device driver for MicroPython",
11+
url="https://github.com/micropython/micropython-lib",
12+
license="MIT",
13+
author="MicroPython Developers",
14+
author_email="team@micropython.org",
15+
)
16+
17+
requires("usb-device")

0 commit comments

Comments
 (0)