Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

1. Introduction

It has been a while since the last article update, but the HPMicro CANFD tool project is still ongoing. For those unfamiliar with this project, you can refer to the article titled “Challenge: Can We Build a 4-Channel CANFD Analyzer for Under 40 Yuan? Practical Edition”.

In open-source projects based on STM32, especially those requiring USB functionality, the USB protocol stack provided by STMicroelectronics is commonly used. However, when attempting to port these projects to other hardware platforms, compatibility and performance challenges may arise. The candleLight_fw is a widely used open-source project originally designed for STM32. Recently, developers have expressed a desire to port it to the HPMicro platform to enhance the project’s versatility and flexibility, particularly to seamlessly integrate support for socketcan.

The original candleLight_fw only supports a single gs CAN (implemented via winusb). Thanks to the cherryUSB in the hpm_sdk, along with its rich example code and support for bidirectional 16 endpoints, we can now easily extend support to four gs CAN channels. Below is a demonstration of the specific effects:

The Windows Device Manager shows four winusb devices.

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

The cangaroo upper computer also provides relevant support:

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

2. Migration Process

Without further ado, let’s introduce how to migrate from ST’s USB protocol stack to the cherryUSB protocol stack. This article will take the latest version (1.8.0) of the HPMicro HPM SDK as an example, with the specific example path being

hpm_sdk/samples/cherryusb/winusb/winusb10.

(1) Key Files for USB in candleLight_fw

The USB-related implementations in the candleLight_fw project are mainly concentrated in the following files:

usbd_conf.cusbd_desc.cusbd_gs_can.c

These files contain USB descriptor enumeration, endpoint transfers, and winusb’s msosv1_desc, among other contents. Understanding these files is crucial for the migration process.

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

(2) Migration to cherryUSB

1. Migration of winusb’s msosv1_desc

The winusb-related descriptions will not be elaborated here; those interested can refer to Microsoft’s documentation. In cherryUSB, the msosv_desc is encapsulated within a structure, allowing for direct registration of the required descriptors.

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

For example, the msos string descriptor:

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

Can be directly mapped to USBD_GS_CAN_WINUSB_STR in candleLight_fw.

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

Following this approach:

We migrate the USBD_MS_COMP_ID_FEATURE_DESC descriptor from candleLight_fw to WINUSB_WCIDDescriptor. However, it is important to note that since we are porting four CAN channels, the WCID needs to support four channels, and the descriptor length needs to be supplemented to accommodate the WCID descriptor for four CAN channels. You can refer to the sdk’s winusb10, which supports two channels of WINUSB. A reference part is as follows:

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

Migrate the USBD_MS_EXT_PROP_FEATURE_DESC descriptor from candleLight_fw to WINUSB_IF0_WCIDProperties.

2. Migration of device descriptors, configuration descriptors, string descriptors, etc.

The descriptors that need to be migrated to cherryUSB are as follows:

These include device descriptors, configuration descriptors, device qualifier descriptors, low-speed device descriptors, and string descriptors.

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

1) Device descriptor: In candleLight_fw’s usbd_desc.c

cherryUSB has already encapsulated the macro, and you can use USB_DEVICE_DESCRIPTOR_INIT, which requires the pid and vid.

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

2) Configuration descriptor, in candleLight_fw’s usbd_gs_can.c

The configuration descriptor includes the interface descriptor and endpoint descriptor. One gs_can requires one interface and two endpoints. Four gs_cans require eight endpoints. Since each endpoint is bidirectional in HPMicro, only four endpoints are needed.

cherryUSB has directly encapsulated the interface descriptor and endpoint descriptor macros: USB_INTERFACE_DESCRIPTOR_INIT

USB_ENDPOINT_DESCRIPTOR_INIT

The migrated code is as follows:

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

The length of the configuration descriptor needs to be configured based on the number of channels. The length includes the configuration descriptor + (interface descriptor + endpoint descriptor) * CAN channels.

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

The remaining descriptors can be referenced from the hpm sdk’s winusb10 configuration.

3. Interface and endpoint registration

1) cherryUSB uses a registration method for endpoint transmission and reception, requiring the registration of endpoint numbers and endpoint data callbacks. For specifics, you can refer to previous articles on cherryUSB or directly check the examples in the hpm sdk. There is no need to refer to the candleLight_fw implementation.

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

2) CAN configuration, using vendor requests

CAN configuration, such as baud rate retrieval and setting, is done through vendor requests. For the candleLight_fw implementation, it is as follows:

There is a USBD_GS_CAN_Vendor_Request registration.

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

In cherryUSB, there is also a corresponding class_endpoint_handler function member for the interface:

Here, we only need to migrate the USBD_GS_CAN_Vendor_Request from candleLight_fw according to the different parameters, as the internal logic of the function remains the same.

Unlocking New Potential: The Journey of Migrating USB Protocol Stack from STM32 to HPMicro

3. Conclusion

By utilizing the rich cherryUSB examples provided by HPMicro’s hpm_sdk, not only can the migration work from ST’s USB protocol stack to cherryUSB be simplified, but it can also significantly reduce development time while enhancing the flexibility and performance of the final product. I hope this article can provide valuable references for developers and inspire more innovative possibilities for HPMicro developers.

Leave a Comment