cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement a USB device custom HID class on STM32 part 1

B.Montanari
ST Employee

Introduction

In this article we will cover the basic steps to develop an application using the USB Custom HID Class on an STM32 as a Device. For this example, the 32F072BDISCOVERY Board (with STM32F072RBT6 microcontroller) is used, but the steps can be easily tailored to another STM32 or ST Board.

Even though the STM32 chosen for this article can only implement USB device in Full Speed (FS) mode, there are STM32 USB hardware and software libraries that are compliant with USB 1.1 and USB 2.0 specifications FS and HS (High Speed) and can work as a Host, Device, and OTG. There are 2 libraries available. This article focuses in ST’s USB Middleware, a new article covers the same application using USBX (Microsoft’s library). For ST’s own USB library, the following classes are available in device mode:

  • Human Interface Devices (HID, that allows the interaction between a human and a machine),
  • Custom HID Class (this class is implemented according to the “Device Class Definition for Human Interface Devices version 1.11”),
  • Mass Storage Class (MSC, allows the implementation of mass storage devices ensuring data storage and exchange via USB),
  • Communication Devices Class (CDC, this class allows the implementation of virtual COM ports and modems),
  • Audio Class (AC, that allows the implementation of audio devices such as headphones and microphones)
  • DFU (device firmware update)

As mentioned, this example shows a USB implementation on STM32 as Device with the Custom HID Class. If you want more information regarding the USB content available for the STM32, please refer the following link: Introduction to USB with STM32.

Prerequisites

          To develop this application, the following material is needed:
          - 32F072BDISCOVERY (or another board with USB Capabilities).
          - Two USB cables.
          - STM32CubeIDE version 1.10.1 - STM32CubeIDE - STMicroelectronics.
          - SimpleHIDWrite: it is a free and easy to use software that allows the Send and Get Report to be validated. It can be downloaded at SimpleHIDWrite.

Implementation


First, let us start creating a project for STM32F072RBT6 on STM32CubeIDE (the steps regarding the microcontroller’s settings can be done on STM32CubeMX instead). After opening the software, click on File -> New -> STM32 Project:

485.png


In the opened menu, search for STM32F072RBT6 microcontroller, then select it in the list, then click Next:

486.png


Give a name to your project, let the standard settings, then click on Finish:

487.png


          After the microcontroller shows up, set the SWD (Serial Wire Debug) pins. This step is optional but is a good practice to know which pins will be used to program and debug the microcontroller to avoid wrong settings. To do this, go to System Core -> SYS then enable the Debug Serial Wire:

488.png


          There are 4 User LEDs on the current board. We use only two of them, User LD3 (red LED) and User LD6 (blue LED) that are connected to the PC6 and PC7 microcontroller’s pins, according to the Discovery kit for STM32F0 Series microcontrollers with STM32F072RB - User manual. So, set these pins as GPIO_Output and give their respective labels:

489.png


          There is also a User Button on the board. It is connected to PA0 pin. So, set this pin as GPIO_EXTI0, then give the settings below (there is a pull-up and low-pass filter circuit connected to the button on the board):

490.png


          Enable the external interruption checking the EXTI line 0 and 1 interrupts on System Core -> NVIC:

491.png


          Now, let us start to set the USB configuration. Go to Connectivity -> USB then enable the Device (FS) resource. Keeping the default settings work just fine (Note that two pins will automatically be enabled when the USB is activated).

492.png


          A keen eye might notice that some clock issues appear with the icon showing them in the Clock Configuration tab after the previous step, but do not worry, we will address them shortly.
          Open the Middleware -> USB_DEVICE and select the Custom Human Interface Device Class (HID) on Class for FS IP:

493.png


          In the USB DEVICE settings (the same menu of the previous step) change the USBD_CUSTOM_HID_REPORT_DESC_SIZE to 33 (it is necessary to increase the data of the USB Descriptor to add some information that will be used to describe the custom class that will be implemented). We must edit the USBD_CUSTOMHID_OUTREPORT_BUF_SIZE to 64, this is done to increase the data package length. All other settings can be kept as default:

494.png


          Now, we adjust clock configuration. As mentioned earlier, the Clock Configuration tab should have an icon showing that we have an error on it. Moving to this tab a window appears asking whether you want to solve the clock issues or not, click Yes:

495.png


          If the window did not appear, click on Resolve Clock Issues, and let the tool calculate the best values for the settings done until here. Note that this particular STM32 does not require an external clock source (HSE) for the USB application, thanks to the Crystal-less USB 2.0 feature of the STM32.
Congratulations! All settings are done, the code can be generated by clicking on Device Configuration Tool Code Generation or going to Project -> Generate Code or typing the keyboard shortcut Alt+K

In
part 2 we focus in the code implementation and conclusion.

I hope to see you there!

 

 
Version history
Last update:
‎2024-06-17 07:19 AM
Updated by:
Contributors