Fundamentals 6 min read

Migrating STM32 USB Stack to HPMicro with cherryUSB: A Step-by-Step Guide

This article walks through migrating the USB stack of the STM32‑based candleLight_fw project to HPMicro's cherryUSB, detailing key source files, descriptor conversions, endpoint registration, vendor‑request handling, and showing how four CAN channels are enabled with code snippets and screenshots.

Linux Code Review Hub
Linux Code Review Hub
Linux Code Review Hub
Migrating STM32 USB Stack to HPMicro with cherryUSB: A Step-by-Step Guide

Background

The open‑source candleLight_fw project was written for STM32 and relies on ST’s USB stack. Developers want to run the same firmware on the HPMicro platform, which provides the cherryUSB stack with dual‑direction 16‑endpoint support, enabling expansion from a single GS‑CAN channel to four channels.

Key source files in the original firmware

usbd_conf.c
usbd_desc.c
usbd_gs_can.c

These files contain USB descriptor definitions, endpoint handling, and the WinUSB MSOSv1 descriptor structures that must be understood before migration.

Migration to cherryUSB (HPM SDK 1.8.0)

Example code in the SDK is located at hpm_sdk/samples/cherryusb/winusb/winusb10.

1. MSOSv1 descriptor migration

In cherryUSB the MSOS descriptor is wrapped in a registration structure. The original MSOS string descriptor maps to USBD_GS_CAN_WINUSB_STR. The USBD_MS_COMP_ID_FEATURE_DESC descriptor is moved to WINUSB_WCIDDescriptor and its length is extended to describe four CAN channels (the SDK’s winusb10 example shows two‑channel support). The USBD_MS_EXT_PROP_FEATURE_DESC descriptor is moved to WINUSB_IF0_WCIDProperties.

0
0

2. Device, configuration and string descriptors

cherryUSB provides macros that simplify descriptor creation: USB_DEVICE_DESCRIPTOR_INIT – requires PID and VID

USB_INTERFACE_DESCRIPTOR_INIT
USB_ENDPOINT_DESCRIPTOR_INIT

The configuration descriptor must reflect the number of CAN channels. Each GS‑CAN instance needs one interface and two endpoints; four instances therefore require eight endpoints. Because HPMicro endpoints are bidirectional, only four physical endpoints are needed.

0
0

Other descriptors (device‑qualified, low‑speed, string) follow the pattern shown in the SDK’s winusb10 configuration.

3. Interface and endpoint registration

Endpoints are registered by number together with a data‑callback function via cherryUSB’s registration API. The SDK examples illustrate the exact calls; the original candleLight_fw implementation is not required.

0
0

4. CAN configuration through vendor requests

CAN bitrate and other parameters are handled via vendor‑specific USB requests. The original function USBD_GS_CAN_Vendor_Request is ported by assigning it to the class_endpoint_handler member of the cherryUSB interface. The internal logic of the request handler remains unchanged.

0
0
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

embeddedUSBSTM32CANcherryUSBHPMicrostack migration
Linux Code Review Hub
Written by

Linux Code Review Hub

A professional Linux technology community and learning platform covering the kernel, memory management, process management, file system and I/O, performance tuning, device drivers, virtualization, and cloud computing.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.