Skip to content

Commit d7353fe

Browse files
committed
stmhal: Change USB PID when in CDC+HID mode.
This gets CDC+HID working on Windows, since it needs a different PID for a different USB configuration. Thanks to tmbinc and dhylands.
1 parent 6278520 commit d7353fe

File tree

3 files changed

+91
-144
lines changed

3 files changed

+91
-144
lines changed

stmhal/usb.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ void pyb_usb_dev_init(usb_device_mode_t mode, usb_storage_medium_t medium) {
5858
#ifdef USE_DEVICE_MODE
5959
if (!dev_is_enabled) {
6060
// only init USB once in the device's power-lifetime
61+
// Windows needs a different PID to distinguish different device
62+
// configurations, so we set it here depending on mode.
6163
if (mode == USB_DEVICE_MODE_CDC_MSC) {
6264
USBD_SelectMode(USBD_MODE_CDC_MSC);
65+
USBD_SetPID(0x9800);
6366
} else {
6467
USBD_SelectMode(USBD_MODE_CDC_HID);
68+
USBD_SetPID(0x9801);
6569
}
6670
USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&VCP_Desc, 0);
6771
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC_HID);

stmhal/usbd_desc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@
2525
*/
2626

2727
extern const USBD_DescriptorsTypeDef VCP_Desc;
28+
29+
void USBD_SetPID(uint16_t pid);

stmhal/usbd_desc_cdc_msc.c

Lines changed: 85 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,19 @@
2020
*
2121
* http://www.st.com/software_license_agreement_liberty_v2
2222
*
23-
* Unless required by applicable law or agreed to in writing, software
24-
* distributed under the License is distributed on an "AS IS" BASIS,
23+
* Unless required by applicable law or agreed to in writing, software
24+
* distributed under the License is distributed on an "AS IS" BASIS,
2525
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2626
* See the License for the specific language governing permissions and
2727
* limitations under the License.
2828
*
2929
******************************************************************************
3030
*/
3131

32-
/* Includes ------------------------------------------------------------------*/
3332
#include "usbd_core.h"
3433
#include "usbd_desc.h"
3534
#include "usbd_conf.h"
3635

37-
/* Private typedef -----------------------------------------------------------*/
38-
/* Private define ------------------------------------------------------------*/
39-
4036
// So we don't clash with existing ST boards, we use the unofficial FOSS VID.
4137
// This needs a proper solution.
4238
#define USBD_VID 0xf055
@@ -52,199 +48,144 @@
5248
#define USBD_CONFIGURATION_FS_STRING "VCP Config"
5349
#define USBD_INTERFACE_FS_STRING "VCP Interface"
5450

55-
/*
56-
#define USBD_VID 0x0483
57-
#define USBD_PID 0x5740
58-
#define USBD_LANGID_STRING 0x409
59-
#define USBD_MANUFACTURER_STRING "STMicroelectronics"
60-
#define USBD_PRODUCT_HS_STRING "STM32 Virtual ComPort in HS Mode"
61-
#define USBD_SERIALNUMBER_HS_STRING "00000000001A"
62-
#define USBD_PRODUCT_FS_STRING "STM32 Virtual ComPort in FS Mode"
63-
#define USBD_SERIALNUMBER_FS_STRING "00000000001B"
64-
#define USBD_CONFIGURATION_HS_STRING "VCP Config"
65-
#define USBD_INTERFACE_HS_STRING "VCP Interface"
66-
#define USBD_CONFIGURATION_FS_STRING "VCP Config"
67-
#define USBD_INTERFACE_FS_STRING "VCP Interface"
68-
*/
69-
70-
/* Private macro -------------------------------------------------------------*/
71-
/* Private function prototypes -----------------------------------------------*/
72-
uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
73-
uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
74-
uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
75-
uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
76-
uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
77-
uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
78-
uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
79-
#ifdef USB_SUPPORT_USER_STRING_DESC
80-
uint8_t *USBD_VCP_USRStringDesc (USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
81-
#endif /* USB_SUPPORT_USER_STRING_DESC */
82-
83-
/* Private variables ---------------------------------------------------------*/
84-
const USBD_DescriptorsTypeDef VCP_Desc = {
85-
USBD_VCP_DeviceDescriptor,
86-
USBD_VCP_LangIDStrDescriptor,
87-
USBD_VCP_ManufacturerStrDescriptor,
88-
USBD_VCP_ProductStrDescriptor,
89-
USBD_VCP_SerialStrDescriptor,
90-
USBD_VCP_ConfigStrDescriptor,
91-
USBD_VCP_InterfaceStrDescriptor,
51+
// USB Standard Device Descriptor
52+
__ALIGN_BEGIN static uint8_t hUSBDDeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
53+
0x12, // bLength
54+
USB_DESC_TYPE_DEVICE, // bDescriptorType
55+
0x00, // bcdUSB
56+
0x02,
57+
0x00, // bDeviceClass
58+
0x00, // bDeviceSubClass
59+
0x00, // bDeviceProtocol
60+
USB_MAX_EP0_SIZE, // bMaxPacketSize
61+
LOBYTE(USBD_VID), // idVendor
62+
HIBYTE(USBD_VID), // idVendor
63+
LOBYTE(USBD_PID), // idVendor
64+
HIBYTE(USBD_PID), // idVendor
65+
0x00, // bcdDevice rel. 2.00
66+
0x02,
67+
USBD_IDX_MFC_STR, // Index of manufacturer string
68+
USBD_IDX_PRODUCT_STR, // Index of product string
69+
USBD_IDX_SERIAL_STR, // Index of serial number string
70+
USBD_MAX_NUM_CONFIGURATION // bNumConfigurations
9271
};
9372

94-
/* USB Standard Device Descriptor */
95-
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
96-
#pragma data_alignment=4
97-
#endif
98-
__ALIGN_BEGIN static uint8_t hUSBDDeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
99-
0x12, /* bLength */
100-
USB_DESC_TYPE_DEVICE, /* bDescriptorType */
101-
0x00, /* bcdUSB */
102-
0x02,
103-
0x00, /* bDeviceClass */
104-
0x00, /* bDeviceSubClass */
105-
0x00, /* bDeviceProtocol */
106-
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
107-
LOBYTE(USBD_VID), /* idVendor */
108-
HIBYTE(USBD_VID), /* idVendor */
109-
LOBYTE(USBD_PID), /* idVendor */
110-
HIBYTE(USBD_PID), /* idVendor */
111-
0x00, /* bcdDevice rel. 2.00 */
112-
0x02,
113-
USBD_IDX_MFC_STR, /* Index of manufacturer string */
114-
USBD_IDX_PRODUCT_STR, /* Index of product string */
115-
USBD_IDX_SERIAL_STR, /* Index of serial number string */
116-
USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */
117-
}; /* USB_DeviceDescriptor */
118-
119-
/* USB Standard Device Descriptor */
120-
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
121-
#pragma data_alignment=4
122-
#endif
12373
__ALIGN_BEGIN static uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = {
124-
USB_LEN_LANGID_STR_DESC,
125-
USB_DESC_TYPE_STRING,
126-
LOBYTE(USBD_LANGID_STRING),
127-
HIBYTE(USBD_LANGID_STRING),
74+
USB_LEN_LANGID_STR_DESC,
75+
USB_DESC_TYPE_STRING,
76+
LOBYTE(USBD_LANGID_STRING),
77+
HIBYTE(USBD_LANGID_STRING),
12878
};
12979

130-
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
131-
#pragma data_alignment=4
132-
#endif
13380
__ALIGN_BEGIN static uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
13481

135-
/* Private functions ---------------------------------------------------------*/
82+
// set the PID
83+
void USBD_SetPID(uint16_t pid) {
84+
hUSBDDeviceDesc[10] = LOBYTE(pid);
85+
hUSBDDeviceDesc[11] = HIBYTE(pid);
86+
}
13687

13788
/**
138-
* @brief Returns the device descriptor.
89+
* @brief Returns the device descriptor.
13990
* @param speed: Current device speed
14091
* @param length: Pointer to data length variable
14192
* @retval Pointer to descriptor buffer
14293
*/
143-
uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
144-
{
145-
*length = sizeof(hUSBDDeviceDesc);
146-
return hUSBDDeviceDesc;
94+
STATIC uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
95+
*length = sizeof(hUSBDDeviceDesc);
96+
return hUSBDDeviceDesc;
14797
}
14898

14999
/**
150-
* @brief Returns the LangID string descriptor.
100+
* @brief Returns the LangID string descriptor.
151101
* @param speed: Current device speed
152102
* @param length: Pointer to data length variable
153103
* @retval Pointer to descriptor buffer
154104
*/
155-
uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
156-
{
157-
*length = sizeof(USBD_LangIDDesc);
158-
return USBD_LangIDDesc;
105+
STATIC uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
106+
*length = sizeof(USBD_LangIDDesc);
107+
return USBD_LangIDDesc;
159108
}
160109

161110
/**
162-
* @brief Returns the product string descriptor.
111+
* @brief Returns the product string descriptor.
163112
* @param speed: Current device speed
164113
* @param length: Pointer to data length variable
165114
* @retval Pointer to descriptor buffer
166115
*/
167-
uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
168-
{
169-
if(speed == 0)
170-
{
171-
USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length);
172-
}
173-
else
174-
{
175-
USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length);
176-
}
177-
return USBD_StrDesc;
116+
STATIC uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
117+
if(speed == 0) {
118+
USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length);
119+
} else {
120+
USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length);
121+
}
122+
return USBD_StrDesc;
178123
}
179124

180125
/**
181-
* @brief Returns the manufacturer string descriptor.
126+
* @brief Returns the manufacturer string descriptor.
182127
* @param speed: Current device speed
183128
* @param length: Pointer to data length variable
184129
* @retval Pointer to descriptor buffer
185130
*/
186-
uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
187-
{
188-
USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
189-
return USBD_StrDesc;
131+
STATIC uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
132+
USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
133+
return USBD_StrDesc;
190134
}
191135

192136
/**
193-
* @brief Returns the serial number string descriptor.
137+
* @brief Returns the serial number string descriptor.
194138
* @param speed: Current device speed
195139
* @param length: Pointer to data length variable
196140
* @retval Pointer to descriptor buffer
197141
*/
198-
uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
199-
{
200-
if(speed == USBD_SPEED_HIGH)
201-
{
202-
USBD_GetString((uint8_t *)USBD_SERIALNUMBER_HS_STRING, USBD_StrDesc, length);
203-
}
204-
else
205-
{
206-
USBD_GetString((uint8_t *)USBD_SERIALNUMBER_FS_STRING, USBD_StrDesc, length);
207-
}
208-
return USBD_StrDesc;
142+
STATIC uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
143+
if(speed == USBD_SPEED_HIGH) {
144+
USBD_GetString((uint8_t *)USBD_SERIALNUMBER_HS_STRING, USBD_StrDesc, length);
145+
} else {
146+
USBD_GetString((uint8_t *)USBD_SERIALNUMBER_FS_STRING, USBD_StrDesc, length);
147+
}
148+
return USBD_StrDesc;
209149
}
210150

211151
/**
212-
* @brief Returns the configuration string descriptor.
152+
* @brief Returns the configuration string descriptor.
213153
* @param speed: Current device speed
214154
* @param length: Pointer to data length variable
215155
* @retval Pointer to descriptor buffer
216156
*/
217-
uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
218-
{
219-
if(speed == USBD_SPEED_HIGH)
220-
{
221-
USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length);
222-
}
223-
else
224-
{
225-
USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length);
226-
}
227-
return USBD_StrDesc;
157+
STATIC uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
158+
if(speed == USBD_SPEED_HIGH) {
159+
USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length);
160+
} else {
161+
USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length);
162+
}
163+
return USBD_StrDesc;
228164
}
229165

230166
/**
231-
* @brief Returns the interface string descriptor.
167+
* @brief Returns the interface string descriptor.
232168
* @param speed: Current device speed
233169
* @param length: Pointer to data length variable
234170
* @retval Pointer to descriptor buffer
235171
*/
236-
uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
237-
{
238-
if(speed == 0)
239-
{
240-
USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length);
241-
}
242-
else
243-
{
244-
USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length);
245-
}
246-
return USBD_StrDesc;
172+
STATIC uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
173+
if(speed == 0) {
174+
USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length);
175+
} else {
176+
USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length);
177+
}
178+
return USBD_StrDesc;
247179
}
248180

249-
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
181+
const USBD_DescriptorsTypeDef VCP_Desc = {
182+
USBD_VCP_DeviceDescriptor,
183+
USBD_VCP_LangIDStrDescriptor,
184+
USBD_VCP_ManufacturerStrDescriptor,
185+
USBD_VCP_ProductStrDescriptor,
186+
USBD_VCP_SerialStrDescriptor,
187+
USBD_VCP_ConfigStrDescriptor,
188+
USBD_VCP_InterfaceStrDescriptor,
189+
};
250190

191+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 commit comments

Comments
 (0)