Skip to content

Commit dc19960

Browse files
committed
micropython/usb/device/mtp: Add USB MTP class driver.
This adds a new driver implementing the USB MTP (Media Transfer Protocol) class, allowing host computers to access the MicroPython filesystem. Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent 60d1370 commit dc19960

File tree

4 files changed

+1172
-0
lines changed

4 files changed

+1172
-0
lines changed

micropython/usb/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,13 @@ USB MIDI devices in MicroPython.
134134

135135
The example [midi_example.py](examples/device/midi_example.py) demonstrates how
136136
to create a simple MIDI device to send MIDI data to and from the USB host.
137+
138+
### Package usb-device-mtp
139+
140+
This package provides the `usb.device.mtp` module. This allows implementing
141+
USB MTP (Media Transfer Protocol) devices in MicroPython, exposing the MicroPython
142+
filesystem to a host computer for file operations.
143+
144+
The example [mtp_example.py](examples/device/mtp_example.py) demonstrates how
145+
to create an MTP device that allows a host computer to browse, upload, download,
146+
and manipulate files on the MicroPython device.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# SPDX-License-Identifier: MIT
2+
# Copyright (c) 2023 MicroPython Team
3+
4+
"""
5+
Example showing how to implement a USB MTP device to expose the MicroPython filesystem.
6+
7+
This example uses the MicroPython USB MTP driver to expose the filesystem
8+
to a host computer as a Media Transfer Protocol (MTP) device. This allows
9+
the host to browse, download, upload, and manipulate files on the MicroPython device.
10+
11+
Usage:
12+
1. Connect to your MicroPython board's REPL
13+
2. Run this example
14+
3. Connect the device to a host computer via USB
15+
4. The device should appear as an MTP device (like a camera or media player)
16+
5. Files can be accessed through the host's file browser
17+
"""
18+
19+
import os
20+
import time
21+
import usb.device
22+
from usb.device.mtp import MTPInterface
23+
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+
# Initialize the MTP interface
36+
mtp.init()
37+
38+
# Initialize the USB device with the MTP interface
39+
# This registers the device with USB and starts accepting connections
40+
usb.device.get().init(mtp, builtin_driver=True)
41+
42+
# Wait for USB to be configured
43+
print("Waiting for USB connection...")
44+
while not mtp.is_open():
45+
time.sleep(0.1)
46+
47+
print("USB MTP device connected!")
48+
print("You can now access files via the host computer")
49+
print("Press Ctrl+C to exit")
50+
51+
try:
52+
# Keep the program running to maintain the USB connection
53+
while True:
54+
time.sleep(1)
55+
except KeyboardInterrupt:
56+
# Clean up on exit
57+
print("Exiting MTP example")
58+
59+
if __name__ == "__main__":
60+
main()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: MIT
2+
# Copyright (c) 2023 MicroPython Team
3+
4+
# This file is part of the MicroPython project, https://micropython.org/
5+
# The MIT License (MIT)
6+
# Note: this file is used by the MicroPython build system to determine module dependencies
7+
#
8+
requires = ["usb-device"]
9+
version = "0.1.0"

0 commit comments

Comments
 (0)