Jump to main content

##

# 1.1 MSD Host and HID Device Example (host_msd_device_hid)

This application demonstrates the ability of the MPLAB Harmony USB Stack to support USB dual role operation - USB Device and Host.

## Description

This application creates a USB Dual Role application that can switch between USB Host MSD Stack and USB Device HID function. The role switch is trigger by a switch press. In the USB Host mode, the application performs read and write operations to a USB pen drive. In the USB Device mode, the application emulates an HID mouse.

## Downloading and building the application

To clone or download this application from Github, go to the main page of this repository and then click Clone button to clone this repository or download it as a zip file. This content can be downloaded using content manager by following these instructions.

Path of the application within the repository is usb_apps_dual_role/apps/host_msd_device_hid.

The following table gives the details of project configurations, target device used, hardware, and its IDE. Open the project using the respective IDE and build it.

Project Name | IDE | Target Device | Hardware / Configuration
--- | --- | --- | ---
pic32mz_ef_curiosity_2.X | MPLABX | PIC32MZ2048EFM144 | Curiosity PIC32MZ EF 2.0 Development Board
pic32mz_ef_sk.X | MPLABX | PIC32MZ2048EFH144 | PIC32MZ Embedded Connectivity with FPU (EF) Starter Kit
pic32cz_ca80_curiosity_ultra.X | MPLABX | PIC32CZ8110CA80208 | PIC32CZ CA80 Curiosity Ultra development board
pic32ck_gc01_curiosity_ultra.X | MPLABX | PIC32CK2051GC01144 | PIC32CK GC Curiosity Ultra Development Board

## Configuring the Hardware

PIC32CZ CA80 Curiosity Ultra development board

- Initially, the application will not operate in any role. Press the corresponding switch to change the operating role.
  - Press SW0 to change to USB Device mode.
  - Press SW1 to change to USB Host mode
- Device mode operation
  - Use the USB port J102 to connect the USB device to the USB Host PC.
  - LED0 indicates USB Device Configuration Set Complete event (the USB device functionality has been activated by the USB Host).
  - The application will emulate a USB HID mouse function. The cursor on the PC will rotate.
- Host mode Operation
  - Connect the USB device to the USB connector (J102) using a USB Type-A Female to micro-B male cable (This cable is not included in the kit).
  - LED1 indicates the file write is complete.

Curiosity PIC32MZ EF 2.0 Development Board

- Initially, the application will not operate in any role. Press the corresponding switch to change the operating role.
  - Press SW1 to change to USB Device mode.
  - Press SW2 to change to USB Host mode
- Device mode operation
  - Use the USB micro-B port J201 to connect the USB device to the USB Host PC.
  - LED1 indicates USB Device Configuration Set Complete event (the USB device functionality has been activated by the USB Host).
  - The application will emulate a USB HID mouse function. The cursor on the PC will rotate.
  - Pressing SW1 will enable and disable the cursor movements.
- Host mode Operation
  - Connect the USB device to the "TARGET USB" connector (J201) using a USB Type-A Female to micro-B male cable (This cable is not included in the kit).
  - LED2 indicates the file write is complete.

PIC32CK GC Curiosity Ultra Development Board

- Use the Barrel jack connector(J200) to connect an external power supply to the board.
- Initially, the application will not operate in any role. Press the corresponding switch to change the operating role.
  - Press SW0 to change to USB Device mode.
  - Press SW1 to change to USB Host mode
- Device mode operation
  - Use the USB Type-C connector (J202) to connect the USB Device to the USB Host PC.
  - LED0 indicates USB Device Configuration Set Complete event (the USB device functionality has been activated by the USB Host).
  - The application will emulate a USB HID mouse function. The cursor on the PC will rotate.
- Host mode Operation
  - Connect the USB device to the USB Type-C connector (J202). A USB Type-C to Type-A Female adapter is required to connect the USB device to the board. (This cable is not included in the kit).
  - LED1 indicates the file write is complete.

PIC32MZ Embedded Connectivity with FPU (EF) Starter Kit

- Initially, the application will not operate in any role. Press the corresponding switch to change the operating role.
  - Press SW1 to change to USB Device mode.
  - Press SW2 to change to USB Host mode
- Device mode operation
  - Use the micro-A/B port J6 (which is located on the bottom side of the board) to connect the USB device to the USB Host PC.
  - LED1 indicates USB Device Configuration Set Complete event (the USB device functionality has been activated by the USB Host).
  - The application will emulate a USB HID mouse function. The cursor on the PC will rotate. Pressing SW1 will enable and disable the cursor movements.
- Host mode Operation
  - Connect the device to the Type A connector J5, which is located on the top side of the starter kit.
  - LED2 indicates the file write is complete.

## Running the Application

This application demonstrates the Dual Role capability of the MPLAB Harmony USB Stack. The application project includes both, the USB Host and Device Stacks. Both the stacks are initialized during application initialization. During operation, the application polls a switch on the starter kit to trigger a USB role switch. Note that the application cannot simultaneously operate as a host and device. The one USB role is exclusive of the other.

Prior to using this demonstration, it is recommended to review the MPLAB Harmony USB Release Notes for any known issues.

1. Open the project and in MPLAB X IDE.
2. Build the code and program the device. The application initially will not operate in any USB role.
3. Press SW0 for PIC32CZ CA80 SW1 for PIC32MZ on the starter kit. This places the application in a USB Device mode.
4. Press SW1 for PIC32CZ CA80 SW2 for PIC32MZ on the starter kit. This places the application in a USB Host mode.
5. Connect a USB cable between the micro-B USB connector on the starter kit and a PC USB host. The application will emulate a USB HID mouse function. The cursor on the PC will rotate. Pressing SW1 will enable and disable the cursor movements. Exercise device plug-n-play operation to confirm USB Device operation.
6. Try switching the USB role to Host mode. Disconnect the USB cable between the micro USB connector on the starter kit and a PC USB host. Press SW3 on the starter kit.
7. The application will be in the USB Host role. Connect a USB pen drive to the USB Host connector on the board. The application will create a file (file.txt) on the pen drive. The completion of the operation is indicated by LED2 on the starter kit. Disconnect the pen drive and connect it to a PC to verify the contents of the file.
8. Repeat steps 3 through 6 to exercise the role switching capability.