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.
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.cThese 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.
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_INITThe 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.
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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
