🛠 Using USB OTG as Mass Storage
Objectives
This tutorial guides you through transforming your HPM5100 board into a USB mass storage device. Although the chip integrates 1 MB of flash and 416 KB of SRAM, we will simulate a storage space accessible via the USB 2.0 OTG interface. This is an ideal introduction to understanding USB communication and the TinyUSB peripheral included in the HPMicro SDK. You will learn to initialize the USB controller and handle simple data exchanges.
Required Hardware
- A HPM5100 development board.
- A USB Type-C (or micro-USB depending on your board's connector) cable.
- A computer (Windows, Linux, or macOS) with standard USB drivers.
- A development environment compatible with the HPMicro SDK (version 1.13.0 or higher).
- A serial terminal for displaying debug messages (baudrate 115200).
Steps
- Environment Preparation: Install the HPMicro SDK and configure your build tool (Make or CMake). Ensure the environment variable points to the SDK root directory.
- Access the Example: Navigate to the USB examples folder in the SDK. The mass storage example is usually located in
examples/usb/device/msc. Open the main filemain.cto understand the structure.
- Code Configuration: In the configuration file, enable the USB Device interface. The HPM5100 has an integrated USB PHY, so no external components are required for the physical layer. Define the size of the virtual 'disk'. For example, use a portion of SRAM or a static memory buffer to simulate storage.
- Compilation: Run the compilation with the following command:
make BOARD=hpm5100evk
If using CMake:
cmake -B build -DBOARD=hpm5100evk
make -C build
- Flashing: Connect the board to the computer via USB. Use the SDK flashing tool (often
hpm_manufactoryor via GDB) to load the binary onto the chip.
./tools/openocd/openocd -f board/hpm5100.cfg -c "program build/hpm5100evk.elf verify reset exit"
- Test: Unplug and replug the USB cable. Your computer should detect a new removable storage device.