Skip to content

Commit 39ce2db

Browse files
committed
stmhal: Add "CDC" option to pyb.usb_mode, for CDC device only.
1 parent d39c7aa commit 39ce2db

8 files changed

Lines changed: 253 additions & 100 deletions

File tree

stmhal/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ SRC_C = \
9494
system_stm32f4xx.c \
9595
stm32f4xx_it.c \
9696
usbd_conf.c \
97-
usbd_desc_cdc_msc.c \
97+
usbd_desc.c \
9898
usbd_cdc_interface.c \
9999
usbd_msc_storage.c \
100100
mphal.c \
@@ -281,7 +281,7 @@ GEN_PINS_AF_PY = $(BUILD)/pins_af.py
281281
INSERT_USB_IDS = ../tools/insert-usb-ids.py
282282
FILE2H = ../tools/file2h.py
283283

284-
USB_IDS_FILE = usbd_desc_cdc_msc.c
284+
USB_IDS_FILE = usbd_desc.c
285285
CDCINF_TEMPLATE = pybcdc.inf_template
286286
GEN_CDCINF_FILE = $(HEADER_BUILD)/pybcdc.inf
287287
GEN_CDCINF_HEADER = $(HEADER_BUILD)/pybcdc_inf.h

stmhal/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ int main(void) {
473473
#if defined(USE_DEVICE_MODE)
474474
// init USB device to default setting if it was not already configured
475475
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
476-
pyb_usb_dev_init(USBD_PID_DEFAULT, USBD_MODE_CDC_MSC, NULL);
476+
pyb_usb_dev_init(USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL);
477477
}
478478
#endif
479479

stmhal/usb.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2013, 2014 Damien P. George
6+
* Copyright (c) 2013, 2014, 2015 Damien P. George
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -60,7 +60,7 @@ const mp_obj_tuple_t pyb_usb_hid_mouse_obj = {
6060
{&mp_type_tuple},
6161
5,
6262
{
63-
MP_OBJ_NEW_SMALL_INT(USBD_PID_SECONDARY),
63+
MP_OBJ_NEW_SMALL_INT(USBD_PID_CDC_HID),
6464
MP_OBJ_NEW_SMALL_INT(1), // subclass: boot
6565
MP_OBJ_NEW_SMALL_INT(2), // protocol: mouse
6666
MP_OBJ_NEW_SMALL_INT(USBD_HID_MOUSE_MAX_PACKET),
@@ -79,7 +79,7 @@ const mp_obj_tuple_t pyb_usb_hid_keyboard_obj = {
7979
{&mp_type_tuple},
8080
5,
8181
{
82-
MP_OBJ_NEW_SMALL_INT(USBD_PID_SECONDARY),
82+
MP_OBJ_NEW_SMALL_INT(USBD_PID_CDC_HID),
8383
MP_OBJ_NEW_SMALL_INT(1), // subclass: boot
8484
MP_OBJ_NEW_SMALL_INT(1), // protocol: keyboard
8585
MP_OBJ_NEW_SMALL_INT(USBD_HID_KEYBOARD_MAX_PACKET),
@@ -100,7 +100,7 @@ void pyb_usb_dev_init(uint16_t pid, usb_device_mode_t mode, USBD_HID_ModeInfoTyp
100100
// only init USB once in the device's power-lifetime
101101
USBD_SetPID(pid);
102102
USBD_SelectMode(mode, hid_info);
103-
USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&VCP_Desc, 0);
103+
USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&USBD_Descriptors, 0);
104104
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC_HID);
105105
USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops);
106106
switch (pyb_usb_storage_medium) {
@@ -213,7 +213,7 @@ STATIC mp_obj_t pyb_usb_mode(mp_uint_t n_args, const mp_obj_t *args) {
213213
#elif defined(USE_DEVICE_MODE)
214214
// USB device
215215
if (strcmp(mode_str, "CDC+MSC") == 0) {
216-
pyb_usb_dev_init(USBD_PID_DEFAULT, USBD_MODE_CDC_MSC, NULL);
216+
pyb_usb_dev_init(USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL);
217217
} else if (strcmp(mode_str, "CDC+HID") == 0) {
218218
mp_obj_t hid_info_obj = (mp_obj_t)&pyb_usb_hid_mouse_obj; // default is mouse mode
219219
if (n_args == 2) {
@@ -232,6 +232,8 @@ STATIC mp_obj_t pyb_usb_mode(mp_uint_t n_args, const mp_obj_t *args) {
232232
hid_info.report_desc = bufinfo.buf;
233233
hid_info.report_desc_len = bufinfo.len;
234234
pyb_usb_dev_init(pid, USBD_MODE_CDC_HID, &hid_info);
235+
} else if (strcmp(mode_str, "CDC") == 0) {
236+
pyb_usb_dev_init(USBD_PID_CDC, USBD_MODE_CDC, NULL);
235237
} else {
236238
goto bad_mode;
237239
}

stmhal/usb.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2013, 2014 Damien P. George
6+
* Copyright (c) 2013, 2014, 2015 Damien P. George
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -30,8 +30,9 @@
3030
#define PYB_USB_FLAG_USB_MODE_CALLED (0x0002)
3131

3232
// Windows needs a different PID to distinguish different device configurations
33-
#define USBD_PID_DEFAULT (0x9800)
34-
#define USBD_PID_SECONDARY (0x9801)
33+
#define USBD_PID_CDC_MSC (0x9800)
34+
#define USBD_PID_CDC_HID (0x9801)
35+
#define USBD_PID_CDC (0x9802)
3536

3637
typedef enum {
3738
PYB_USB_STORAGE_MEDIUM_NONE = 0,
Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,21 @@
4343
#define USBD_SERIALNUMBER_HS_STRING "000000000010"
4444
#define USBD_PRODUCT_FS_STRING "Pyboard Virtual Comm Port in FS Mode"
4545
#define USBD_SERIALNUMBER_FS_STRING "000000000011"
46-
#define USBD_CONFIGURATION_HS_STRING "VCP Config"
47-
#define USBD_INTERFACE_HS_STRING "VCP Interface"
48-
#define USBD_CONFIGURATION_FS_STRING "VCP Config"
49-
#define USBD_INTERFACE_FS_STRING "VCP Interface"
46+
#define USBD_CONFIGURATION_HS_STRING "Pyboard Config"
47+
#define USBD_INTERFACE_HS_STRING "Pyboard Interface"
48+
#define USBD_CONFIGURATION_FS_STRING "Pyboard Config"
49+
#define USBD_INTERFACE_FS_STRING "Pyboard Interface"
5050

5151
// USB Standard Device Descriptor
52+
// needs to be in RAM because we modify the VID and PID
5253
__ALIGN_BEGIN static uint8_t hUSBDDeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
5354
0x12, // bLength
5455
USB_DESC_TYPE_DEVICE, // bDescriptorType
5556
0x00, // bcdUSB
5657
0x02,
57-
0x00, // bDeviceClass
58-
0x00, // bDeviceSubClass
59-
0x00, // bDeviceProtocol
58+
0xef, // bDeviceClass: Miscellaneous Device Class
59+
0x02, // bDeviceSubClass: Common Class
60+
0x01, // bDeviceProtocol: Interface Association Descriptor
6061
USB_MAX_EP0_SIZE, // bMaxPacketSize
6162
LOBYTE(USBD_VID), // idVendor
6263
HIBYTE(USBD_VID), // idVendor
@@ -91,7 +92,7 @@ void USBD_SetPID(uint16_t pid) {
9192
* @param length: Pointer to data length variable
9293
* @retval Pointer to descriptor buffer
9394
*/
94-
STATIC uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
95+
STATIC uint8_t *USBD_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
9596
*length = sizeof(hUSBDDeviceDesc);
9697
return hUSBDDeviceDesc;
9798
}
@@ -102,7 +103,7 @@ STATIC uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *len
102103
* @param length: Pointer to data length variable
103104
* @retval Pointer to descriptor buffer
104105
*/
105-
STATIC uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
106+
STATIC uint8_t *USBD_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
106107
*length = sizeof(USBD_LangIDDesc);
107108
return USBD_LangIDDesc;
108109
}
@@ -113,7 +114,7 @@ STATIC uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *
113114
* @param length: Pointer to data length variable
114115
* @retval Pointer to descriptor buffer
115116
*/
116-
STATIC uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
117+
STATIC uint8_t *USBD_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
117118
if(speed == 0) {
118119
USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length);
119120
} else {
@@ -128,7 +129,7 @@ STATIC uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t
128129
* @param length: Pointer to data length variable
129130
* @retval Pointer to descriptor buffer
130131
*/
131-
STATIC uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
132+
STATIC uint8_t *USBD_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
132133
USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
133134
return USBD_StrDesc;
134135
}
@@ -139,7 +140,7 @@ STATIC uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint
139140
* @param length: Pointer to data length variable
140141
* @retval Pointer to descriptor buffer
141142
*/
142-
STATIC uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
143+
STATIC uint8_t *USBD_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
143144
if(speed == USBD_SPEED_HIGH) {
144145
USBD_GetString((uint8_t *)USBD_SERIALNUMBER_HS_STRING, USBD_StrDesc, length);
145146
} else {
@@ -154,7 +155,7 @@ STATIC uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *
154155
* @param length: Pointer to data length variable
155156
* @retval Pointer to descriptor buffer
156157
*/
157-
STATIC uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
158+
STATIC uint8_t *USBD_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
158159
if(speed == USBD_SPEED_HIGH) {
159160
USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length);
160161
} else {
@@ -169,7 +170,7 @@ STATIC uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *
169170
* @param length: Pointer to data length variable
170171
* @retval Pointer to descriptor buffer
171172
*/
172-
STATIC uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
173+
STATIC uint8_t *USBD_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) {
173174
if(speed == 0) {
174175
USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length);
175176
} else {
@@ -178,14 +179,14 @@ STATIC uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_
178179
return USBD_StrDesc;
179180
}
180181

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,
182+
const USBD_DescriptorsTypeDef USBD_Descriptors = {
183+
USBD_DeviceDescriptor,
184+
USBD_LangIDStrDescriptor,
185+
USBD_ManufacturerStrDescriptor,
186+
USBD_ProductStrDescriptor,
187+
USBD_SerialStrDescriptor,
188+
USBD_ConfigStrDescriptor,
189+
USBD_InterfaceStrDescriptor,
189190
};
190191

191192
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

stmhal/usbd_desc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2013, 2014 Damien P. George
6+
* Copyright (c) 2013, 2014, 2015 Damien P. George
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -24,6 +24,6 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
extern const USBD_DescriptorsTypeDef VCP_Desc;
27+
extern const USBD_DescriptorsTypeDef USBD_Descriptors;
2828

2929
void USBD_SetPID(uint16_t pid);

0 commit comments

Comments
 (0)