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.
This commit is contained in:
Damien George 2014-10-25 22:55:07 +01:00
parent 627852019b
commit d7353fe6fe
3 changed files with 93 additions and 146 deletions

View File

@ -58,10 +58,14 @@ void pyb_usb_dev_init(usb_device_mode_t mode, usb_storage_medium_t medium) {
#ifdef USE_DEVICE_MODE #ifdef USE_DEVICE_MODE
if (!dev_is_enabled) { if (!dev_is_enabled) {
// only init USB once in the device's power-lifetime // only init USB once in the device's power-lifetime
// Windows needs a different PID to distinguish different device
// configurations, so we set it here depending on mode.
if (mode == USB_DEVICE_MODE_CDC_MSC) { if (mode == USB_DEVICE_MODE_CDC_MSC) {
USBD_SelectMode(USBD_MODE_CDC_MSC); USBD_SelectMode(USBD_MODE_CDC_MSC);
USBD_SetPID(0x9800);
} else { } else {
USBD_SelectMode(USBD_MODE_CDC_HID); USBD_SelectMode(USBD_MODE_CDC_HID);
USBD_SetPID(0x9801);
} }
USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&VCP_Desc, 0); USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&VCP_Desc, 0);
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC_HID); USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC_HID);

View File

@ -25,3 +25,5 @@
*/ */
extern const USBD_DescriptorsTypeDef VCP_Desc; extern const USBD_DescriptorsTypeDef VCP_Desc;
void USBD_SetPID(uint16_t pid);

View File

@ -29,14 +29,10 @@
****************************************************************************** ******************************************************************************
*/ */
/* Includes ------------------------------------------------------------------*/
#include "usbd_core.h" #include "usbd_core.h"
#include "usbd_desc.h" #include "usbd_desc.h"
#include "usbd_conf.h" #include "usbd_conf.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
// So we don't clash with existing ST boards, we use the unofficial FOSS VID. // So we don't clash with existing ST boards, we use the unofficial FOSS VID.
// This needs a proper solution. // This needs a proper solution.
#define USBD_VID 0xf055 #define USBD_VID 0xf055
@ -52,87 +48,42 @@
#define USBD_CONFIGURATION_FS_STRING "VCP Config" #define USBD_CONFIGURATION_FS_STRING "VCP Config"
#define USBD_INTERFACE_FS_STRING "VCP Interface" #define USBD_INTERFACE_FS_STRING "VCP Interface"
/* // USB Standard Device Descriptor
#define USBD_VID 0x0483
#define USBD_PID 0x5740
#define USBD_LANGID_STRING 0x409
#define USBD_MANUFACTURER_STRING "STMicroelectronics"
#define USBD_PRODUCT_HS_STRING "STM32 Virtual ComPort in HS Mode"
#define USBD_SERIALNUMBER_HS_STRING "00000000001A"
#define USBD_PRODUCT_FS_STRING "STM32 Virtual ComPort in FS Mode"
#define USBD_SERIALNUMBER_FS_STRING "00000000001B"
#define USBD_CONFIGURATION_HS_STRING "VCP Config"
#define USBD_INTERFACE_HS_STRING "VCP Interface"
#define USBD_CONFIGURATION_FS_STRING "VCP Config"
#define USBD_INTERFACE_FS_STRING "VCP Interface"
*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
#ifdef USB_SUPPORT_USER_STRING_DESC
uint8_t *USBD_VCP_USRStringDesc (USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
#endif /* USB_SUPPORT_USER_STRING_DESC */
/* Private variables ---------------------------------------------------------*/
const USBD_DescriptorsTypeDef VCP_Desc = {
USBD_VCP_DeviceDescriptor,
USBD_VCP_LangIDStrDescriptor,
USBD_VCP_ManufacturerStrDescriptor,
USBD_VCP_ProductStrDescriptor,
USBD_VCP_SerialStrDescriptor,
USBD_VCP_ConfigStrDescriptor,
USBD_VCP_InterfaceStrDescriptor,
};
/* USB Standard Device Descriptor */
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
#pragma data_alignment=4
#endif
__ALIGN_BEGIN static uint8_t hUSBDDeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = { __ALIGN_BEGIN static uint8_t hUSBDDeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
0x12, /* bLength */ 0x12, // bLength
USB_DESC_TYPE_DEVICE, /* bDescriptorType */ USB_DESC_TYPE_DEVICE, // bDescriptorType
0x00, /* bcdUSB */ 0x00, // bcdUSB
0x02, 0x02,
0x00, /* bDeviceClass */ 0x00, // bDeviceClass
0x00, /* bDeviceSubClass */ 0x00, // bDeviceSubClass
0x00, /* bDeviceProtocol */ 0x00, // bDeviceProtocol
USB_MAX_EP0_SIZE, /* bMaxPacketSize */ USB_MAX_EP0_SIZE, // bMaxPacketSize
LOBYTE(USBD_VID), /* idVendor */ LOBYTE(USBD_VID), // idVendor
HIBYTE(USBD_VID), /* idVendor */ HIBYTE(USBD_VID), // idVendor
LOBYTE(USBD_PID), /* idVendor */ LOBYTE(USBD_PID), // idVendor
HIBYTE(USBD_PID), /* idVendor */ HIBYTE(USBD_PID), // idVendor
0x00, /* bcdDevice rel. 2.00 */ 0x00, // bcdDevice rel. 2.00
0x02, 0x02,
USBD_IDX_MFC_STR, /* Index of manufacturer string */ USBD_IDX_MFC_STR, // Index of manufacturer string
USBD_IDX_PRODUCT_STR, /* Index of product string */ USBD_IDX_PRODUCT_STR, // Index of product string
USBD_IDX_SERIAL_STR, /* Index of serial number string */ USBD_IDX_SERIAL_STR, // Index of serial number string
USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */ USBD_MAX_NUM_CONFIGURATION // bNumConfigurations
}; /* USB_DeviceDescriptor */ };
/* USB Standard Device Descriptor */ __ALIGN_BEGIN static uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = {
#if defined ( __ICCARM__ ) /*!< IAR Compiler */ USB_LEN_LANGID_STR_DESC,
#pragma data_alignment=4 USB_DESC_TYPE_STRING,
#endif LOBYTE(USBD_LANGID_STRING),
__ALIGN_BEGIN static uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = { HIBYTE(USBD_LANGID_STRING),
USB_LEN_LANGID_STR_DESC,
USB_DESC_TYPE_STRING,
LOBYTE(USBD_LANGID_STRING),
HIBYTE(USBD_LANGID_STRING),
}; };
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
#pragma data_alignment=4
#endif
__ALIGN_BEGIN static uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END; __ALIGN_BEGIN static uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
/* Private functions ---------------------------------------------------------*/ // set the PID
void USBD_SetPID(uint16_t pid) {
hUSBDDeviceDesc[10] = LOBYTE(pid);
hUSBDDeviceDesc[11] = HIBYTE(pid);
}
/** /**
* @brief Returns the device descriptor. * @brief Returns the device descriptor.
@ -140,10 +91,9 @@ __ALIGN_BEGIN static uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
* @param length: Pointer to data length variable * @param length: Pointer to data length variable
* @retval Pointer to descriptor buffer * @retval Pointer to descriptor buffer
*/ */
uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) STATIC uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
{ *length = sizeof(hUSBDDeviceDesc);
*length = sizeof(hUSBDDeviceDesc); return hUSBDDeviceDesc;
return hUSBDDeviceDesc;
} }
/** /**
@ -152,10 +102,9 @@ uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
* @param length: Pointer to data length variable * @param length: Pointer to data length variable
* @retval Pointer to descriptor buffer * @retval Pointer to descriptor buffer
*/ */
uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) STATIC uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
{ *length = sizeof(USBD_LangIDDesc);
*length = sizeof(USBD_LangIDDesc); return USBD_LangIDDesc;
return USBD_LangIDDesc;
} }
/** /**
@ -164,17 +113,13 @@ uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
* @param length: Pointer to data length variable * @param length: Pointer to data length variable
* @retval Pointer to descriptor buffer * @retval Pointer to descriptor buffer
*/ */
uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) STATIC uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
{ if(speed == 0) {
if(speed == 0) USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length);
{ } else {
USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length); USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length);
} }
else return USBD_StrDesc;
{
USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length);
}
return USBD_StrDesc;
} }
/** /**
@ -183,10 +128,9 @@ uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length
* @param length: Pointer to data length variable * @param length: Pointer to data length variable
* @retval Pointer to descriptor buffer * @retval Pointer to descriptor buffer
*/ */
uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) STATIC uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
{ USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length); return USBD_StrDesc;
return USBD_StrDesc;
} }
/** /**
@ -195,17 +139,13 @@ uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *l
* @param length: Pointer to data length variable * @param length: Pointer to data length variable
* @retval Pointer to descriptor buffer * @retval Pointer to descriptor buffer
*/ */
uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) STATIC uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
{ if(speed == USBD_SPEED_HIGH) {
if(speed == USBD_SPEED_HIGH) USBD_GetString((uint8_t *)USBD_SERIALNUMBER_HS_STRING, USBD_StrDesc, length);
{ } else {
USBD_GetString((uint8_t *)USBD_SERIALNUMBER_HS_STRING, USBD_StrDesc, length); USBD_GetString((uint8_t *)USBD_SERIALNUMBER_FS_STRING, USBD_StrDesc, length);
} }
else return USBD_StrDesc;
{
USBD_GetString((uint8_t *)USBD_SERIALNUMBER_FS_STRING, USBD_StrDesc, length);
}
return USBD_StrDesc;
} }
/** /**
@ -214,17 +154,13 @@ uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
* @param length: Pointer to data length variable * @param length: Pointer to data length variable
* @retval Pointer to descriptor buffer * @retval Pointer to descriptor buffer
*/ */
uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) STATIC uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
{ if(speed == USBD_SPEED_HIGH) {
if(speed == USBD_SPEED_HIGH) USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length);
{ } else {
USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length); USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length);
} }
else return USBD_StrDesc;
{
USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length);
}
return USBD_StrDesc;
} }
/** /**
@ -233,18 +169,23 @@ uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
* @param length: Pointer to data length variable * @param length: Pointer to data length variable
* @retval Pointer to descriptor buffer * @retval Pointer to descriptor buffer
*/ */
uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) STATIC uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
{ if(speed == 0) {
if(speed == 0) USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length);
{ } else {
USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length); USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length);
} }
else return USBD_StrDesc;
{
USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length);
}
return USBD_StrDesc;
} }
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ const USBD_DescriptorsTypeDef VCP_Desc = {
USBD_VCP_DeviceDescriptor,
USBD_VCP_LangIDStrDescriptor,
USBD_VCP_ManufacturerStrDescriptor,
USBD_VCP_ProductStrDescriptor,
USBD_VCP_SerialStrDescriptor,
USBD_VCP_ConfigStrDescriptor,
USBD_VCP_InterfaceStrDescriptor,
};
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/