This code example demonstrates the usage of the emUSB device middleware stack to configure the USB block in a supported Infineon MCU as a generic Human Interface Device (HID). It takes help from an external HID software tool to send in the generic HID inputs which it then relays back.This example is currently supported on XMC™ MCU evaluation kits.
- ModusToolbox™ software v3.0
- SEGGER J-Link software
- Programming language: C
- Associated parts: All XMC™ MCU parts
- GNU Arm® embedded compiler v10.3.1 (
GCC_ARM
) - Default value ofTOOLCHAIN
- XMC4200 Platform2Go kit (
KIT_XMC_PLT2GO_XMC4200
) - Default value ofTARGET
- XMC4300 relax EtherCAT kit (
KIT_XMC43_RELAX_ECAT_V1
) - XMC4400 Platform2Go kit (
KIT_XMC_PLT2GO_XMC4400
) - XMC4500 relax kit (
KIT_XMC45_RELAX_V1
) - XMC4700 relax kit (
KIT_XMC47_RELAX_V1
) - XMC4800 relax EtherCAT kit (
KIT_XMC48_RELAX_ECAT_V1
)
This example uses the board's default configuration. See the kit user guide to ensure that the board is configured correctly.
This example requires an external HID software tool to act as a generic HID host for the emUSB device.
Instructions in this document use the USB-HID tool to write and read data to/from a HID device.
Download the USB-HID tool GitHub project. You should see a /bin folder that contains the 'usbhidtool' executable available for different systems.
Install a terminal emulator if you don't have one. Instructions in this document use Tera Term.
Create the project and open it using one of the following:
In Eclipse IDE for ModusToolbox™ software
-
Click the New Application link in the Quick Panel (or, use File > New > ModusToolbox™ Application). This launches the Project Creator tool.
-
Pick a kit supported by the code example from the list shown in the Project Creator - Choose Board Support Package (BSP) dialog.
When you select a supported kit, the example is reconfigured automatically to work with the kit. To work with a different supported kit later, use the Library Manager to choose the BSP for the supported kit. You can use the Library Manager to select or update the BSP and firmware libraries used in this application. To access the Library Manager, click the link from the Quick Panel.
You can also just start the application creation process again and select a different kit.
If you want to use the application for a kit not listed here, you may need to update the source files. If the kit does not have the required resources, the application may not work.
-
In the Project Creator - Select Application dialog, choose the example by enabling the checkbox.
-
(Optionally) change the suggested New Application Name.
-
Enter the local path in the Application(s) Root Path field to indicate where the application needs to be created.
Applications that can share libraries can be placed in the same root path.
-
Click Create to complete the application creation process.
For more details, see the Eclipse IDE for ModusToolbox™ software user guide (locally available at {ModusToolbox™ software install directory}/ide_{version}/docs/mtb_ide_user_guide.pdf).
In command-line interface (CLI)
ModusToolbox™ software provides the Project Creator as both a GUI tool and the command line tool, "project-creator-cli". The CLI tool can be used to create applications from a CLI terminal or from within batch files or shell scripts. This tool is available in the {ModusToolbox™ software install directory}/tools_{version}/project-creator/ directory.
Use a CLI terminal to invoke the "project-creator-cli" tool. On Windows, use the command line "modus-shell" program provided in the ModusToolbox™ software installation instead of a standard Windows command-line application. This shell provides access to all ModusToolbox™ software tools. You can access it by typing modus-shell
in the search box in the Windows menu. In Linux and macOS, you can use any terminal application.
The "project-creator-cli" tool has the following arguments:
Argument | Description | Required/optional |
---|---|---|
--board-id |
Defined in the <id> field of the BSP manifest |
Required |
--app-id |
Defined in the <id> field of the CE manifest |
Required |
--target-dir |
Specify the directory in which the application is to be created if you prefer not to use the default current working directory | Optional |
--user-app-name |
Specify the name of the application if you prefer to have a name other than the example's default name | Optional |
The following example will clone the "USBD-HID-Generic" application with the desired name "USBD_HID_generic" configured for the KIT_XMC_PLT2GO_XMC4200 BSP into the specified working directory, C:/mtb_projects:
project-creator-cli --board-id KIT_XMC_PLT2GO_XMC4200 --app-id mtb-example-xmc-usb-device-hid-generic --user-app-name USB_HID_Generic --target-dir "C:/mtb_projects"
Note: The project-creator-cli tool uses the git clone
and make getlibs
commands to fetch the repository and import the required libraries. For details, see the "Project creator tools" section of the ModusToolbox™ software user guide (locally available at {ModusToolbox™ software install directory}/docs_{version}/mtb_user_guide.pdf).
In third-party IDEs
Note: Only VS Code is supported.
-
Follow the instructions from the In command-line interface (CLI) section to create the application, and import the libraries using the
make getlibs
command. -
Export the application to a supported IDE using the
make <ide>
command.For a list of supported IDEs and more details, see the "Exporting to IDEs" section of the ModusToolbox™ software user guide (locally available at {ModusToolbox™ software install directory}/docs_{version}/mtb_user_guide.pdf).
-
Follow the instructions displayed in the terminal to create or import the application as an IDE project.
-
Connect the board to your PC using the provided USB cable through the KitProg3 USB connector.
-
Open a terminal program and select the KitProg3 COM port. Set the serial port parameters to 8-N-1 and 115200 baud.
-
Program the board using one of the following:
Using Eclipse IDE for ModusToolbox™ software
-
Select the application project in the Project Explorer.
-
In the Quick Panel, scroll down, and click <Application Name> Program (Jlink).
Using CLI
From the terminal, execute the
make program
command to build and program the application using the default toolchain to the default target. The default toolchain is specified in the application's Makefile but you can override this value manually:make program TOOLCHAIN=<toolchain>
Example:
make program TOOLCHAIN=GCC_ARM
-
-
After programming, the application starts automatically. Confirm that "emUSB Device: HID generic application" is displayed on the UART terminal.
Figure 1. Terminal output on program startup
-
The user LED blinks at 2 Hz to indicate that the board is ready to be configured and used as an emUSB HID device.
-
Connect the board to your PC from the XMC™ USB Device port (see the kit user guide for its location) via a micro USB cable.
-
The user LED stops blinking to indicate that the USB HID device has been detected successfully.
-
On the computer, open a terminal console and run the usbhidtool to send a few bytes. The tool requires as argument the VID, PID, number of bytes to send, and data. See the USB-HID tool documentation or use the following example:
In Windows:
usbhidtool.exe 0x058B 0x0274 4 0xDE 0xAD 0xBE 0xEF
In Linux:
./usbhidtool 0x058B 0x0274 4 0xDE 0xAD 0xBE 0xEF
After writing to the device, you should see the same data written back to the terminal console.
de ad be ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Note: The usbhidtool always sends 64 bytes to the device, independent of how many bytes provided. Any remaining data is written with zeros.
You can debug the example to step through the code. In the IDE, use the <Application Name> Debug (Jlink) configuration in the Quick Panel. For details, see the "Program and debug" section in the Eclipse IDE for ModusToolbox™ software user guide.
This code example intends to provide the functionality of a generic Human Interface Device (HID) using the Segger emUSB device middleware library. The USB Implementers Forum has defined the Human Interface Device (HID) class as an abstract USB class protocol which was developed for handling devices that would be used directly by users use to control the operation of computer systems.
The firmware consists of a main()
function and two separate application functions (usbd_hid_init()
and usbd_hid_echo_task()
) to implement the desired functionality. The main function is responsible for calling the relevant emUSB device middleware target APIs and application functions (USBD_Init()
) respectively for the initialization of the emUSB device stack, and setting up the endpoint information for HID class. For more information regarding the usage of emUSB device Target APIs, see the emUSB Device user guide (locally available at <mtb_shared>/emusb-device//docs)
A report descriptor is a structure which is used to transmit the HID control data to and from an HID device. A report descriptor defines the format of a report and is composed of report items that define one or more top-level collections. Each collection defines one or more HID reports. Usages are a part of HID report descriptors which is used to indicate the vendor's suggested use for a specific control or a group of controls. The usage of an HID application is defined using a usage page ID.
This example uses the usage table for a generic USB device. See the USB Specification and the HID Usage Tables guide for detailed information about HID input, output, and feature reports.
The usbd_hid_init()
application function utilizes usb_hid_generic_report
to capture the generic HID description of the emUSB device and set up the USB device endpoint. The generic USB-HID report in usb_hid_generic_report
is generated using the following documents and HID descriptor tool given by the USB Implementers Forum (USB-IF) on their website.
Name | Description |
---|---|
HID Spec Definition | Device Class Definition for Human Interface Devices (HID) Firmware Specification (5/27/2001), Version 1.11 |
HID Usage Tables | HID Usage Tables for Universal Serial Bus (USB) Version 1.3 |
HID Descriptor Tool | Generic HID Descriptor Tool by USB Implementers Forum |
The firmware then configures the device information for enumeration using the USBD_SetDeviceInfo()
target API. After the USB core configuration is done, the USBD_Start()
target API is called, which initiates a hardware attach and updates the endpoint configuration. The usbd_hid_echo_task()
application function is where the USB device waits to be configured. The user LED blinks at 2 Hz to signify the wait state.
When the USB cable is connected to the device, the host will start enumeration of the device and the device will function as a generic USB HID device which echoes the generic HID inputs to the terminal using the USBD_HID_Read()
and USBD_HID_Write()
target APIs to read and write the HID data respectively.
Figure 2. Software stack
Resources | Links |
---|---|
emUSB Device Reference | emUSB Device – USB Device stack for embedded applications |
USB HID documentation | Device Class Definition for Human Interface Devices (HID) Firmware Specification – 5/27/2001, Version 1.11 HID Usage Tables for Universal Serial Bus (USB) Version 1.3 HID Descriptor Tool by the USB Implementers Forum |
Code examples | Using ModusToolbox™ software on GitHub |
Device documentation | XMC4000 family datasheets |
Libraries on GitHub | mtb-xmclib-cat3 – XMC™ peripheral driver library (XMCLib)and docs |
Tools | Eclipse IDE for ModusToolbox™ software – ModusToolbox™ software is a collection of easy-to-use software and tools enabling rapid development with Infineon MCUs, covering applications from embedded sense and control to wireless and cloud-connected systems using AIROC™ Wi-Fi and Bluetooth® connectivity devices. |
Infineon provides a wealth of data at www.infineon.com to help you select the right device, and quickly and effectively integrate it into your design.
For XMC™ MCU devices, see 32-bit XMC™ Industrial microcontroller based on Arm® Cortex®-M.
Document title: CE237133 - XMC™ MCU: emUSB Device HID generic application
Version | Description of change |
---|---|
1.0.0 | New code example |
All other trademarks or registered trademarks referenced herein are the property of their respective owners.
© 2022 Infineon Technologies AG
All Rights Reserved.
The information given in this document shall in no event be regarded as a guarantee of conditions or characteristics. With respect to any examples or hints given herein, any typical values stated herein and/or any information regarding the application of the device, Infineon Technologies hereby disclaims any and all warranties and liabilities of any kind, including without limitation, warranties of non-infringement of intellectual property rights of any third party.
For further information on technology, delivery terms and conditions and prices, please contact the nearest Infineon Technologies Office (www.infineon.com).
Due to technical requirements, components may contain dangerous substances. For information on the types in question, please contact the nearest Infineon Technologies Office.
Infineon Technologies components may be used in life-support devices or systems only with the express written approval of Infineon Technologies, if a failure of such components can reasonably be expected to cause the failure of that life-support device or system or to affect the safety or effectiveness of that device or system. Life support devices or systems are intended to be implanted in the human body or to support and/or maintain and sustain and/or protect human life. If they fail, it is reasonable to assume that the health of the user or other persons may be endangered.