8000 stmhal: Fix USB CDC-only mode under Windows. · micropython/micropython@01d6491 · GitHub
[go: up one dir, main page]

Skip to content

Commit 01d6491

Browse files
dhylandsdpgeorge
authored andcommitted
stmhal: Fix USB CDC-only mode under Windows.
This fix adds PIDs 9801 and 9802 to the pybcdc.inf file. When in CDC only mode, it presents itself as a Communcations device rather than as a composite device. Presenting as a composite device with only the CDC interface seems to confuse windows. To test and make sure that the correct pybcdc.inf was being used, I used USBDeview from http://www.nirsoft.net/utils/usb_devices_view.html to uninstall any old pyboard drivers (Use Control-F and search for pyboard). I found running USBDeview as administrator worked best. Installing the driver in CDC+MSC mode first is recommended (since the pybcdc.inf file in on the internal flash drive). Then when you switch modes everything seems to work properly. I used https://github.com/dhylands/upy-examples/blob/master/boot_switch.py to easily switch the pyboard between the various USB modes for testing.
1 parent 366239b commit 01d6491

File tree

6 files changed

+31
-20
lines changed

6 files changed

+31
-20
lines changed

stmhal/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ GEN_PINS_AF_PY = $(BUILD)/pins_af.py
299299
INSERT_USB_IDS = ../tools/insert-usb-ids.py
300300
FILE2H = ../tools/file2h.py
301301

302-
USB_IDS_FILE = usbd_desc.c
302+
USB_IDS_FILE = usb.h
303303
CDCINF_TEMPLATE = pybcdc.inf_template
304304
GEN_CDCINF_FILE = $(HEADER_BUILD)/pybcdc.inf
305305
GEN_CDCINF_HEADER = $(HEADER_BUILD)/pybcdc_inf.h

stmhal/pybcdc.inf_template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ ServiceBinary=%12%\usbser.sys
7777
[SourceDisksFiles]
7878
[SourceDisksNames]
7979
[DeviceList]
80-
%DESCRIPTION%=DriverInstall, USB\VID_${USB_VID}&PID_${USB_PID}&MI_00, USB\VID_${USB_VID}&PID_${USB_PID}&MI_01
80+
%DESCRIPTION%=DriverInstall, USB\VID_${USB_VID}&PID_${USB_PID_CDC_MSC}&MI_00, USB\VID_${USB_VID}&PID_${USB_PID_CDC_MSC}&MI_01, USB\VID_${USB_VID}&PID_${USB_PID_CDC_HID}&MI_00, USB\VID_${USB_VID}&PID_${USB_PID_CDC_HID}&MI_01, USB\VID_${USB_VID}&PID_${USB_PID_CDC}
8181

8282
[DeviceList.NTamd64]
83-
%DESCRIPTION%=DriverInstall, USB\VID_${USB_VID}&PID_${USB_PID}&MI_00, USB\VID_${USB_VID}&PID_${USB_PID}&MI_01
83+
%DESCRIPTION%=DriverInstall, USB\VID_${USB_VID}&PID_${USB_PID_CDC_MSC}&MI_00, USB\VID_${USB_VID}&PID_${USB_PID_CDC_MSC}&MI_01, USB\VID_${USB_VID}&PID_${USB_PID_CDC_HID}&MI_00, USB\VID_${USB_VID}&PID_${USB_PID_CDC_HID}&MI_01, USB\VID_${USB_VID}&PID_${USB_PID_CDC}
8484

8585
;---------------------------------------------------------------------
8686
; String Definitions

stmhal/usb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ bool pyb_usb_dev_init(uint16_t vid, uint16_t pid, usb_device_mode_t mode, USBD_H
9898
#ifdef USE_DEVICE_MODE
9999
if (!(pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED)) {
100100
// only init USB once in the device's power-lifetime
101-
USBD_SetVIDPIDRelease(vid, pid, 0x0200);
101+
USBD_SetVIDPIDRelease(vid, pid, 0x0200, mode == USBD_MODE_CDC);
102102
if (USBD_SelectMode(mode, hid_info) != 0) {
103103
return false;
104104
}

stmhal/usbd_desc.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,20 @@ __ALIGN_BEGIN static uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_EN
8181
__ALIGN_BEGIN static uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
8282

8383
// set the VID, PID and device release number
84-
void USBD_SetVIDPIDRelease(uint16_t vid, uint16_t pid, uint16_t device_release_num) {
84+
void USBD_SetVIDPIDRelease(uint16_t vid, uint16_t pid, uint16_t device_release_num, int cdc_only) {
85+
if (cdc_only) {
86+
// Make it look like a Communications device if we're only
87+
// using CDC. Otherwise, windows gets confused when we tell it that
88+
// its a composite device with only a cdc serial interface.
89+
hUSBDDeviceDesc[4] = 0x02;
90+
hUSBDDeviceDesc[5] = 0x00;
91+
hUSBDDeviceDesc[6] = 0x00;
92+
} else {
93+
// For the other modes, we make this look like a composite device.
94+
hUSBDDeviceDesc[4] = 0xef;
95+
hUSBDDeviceDesc[5] = 0x02;
96+
hUSBDDeviceDesc[6] = 0x01;
97+
}
8598
hUSBDDeviceDesc[8] = LOBYTE(vid);
8699
hUSBDDeviceDesc[9] = HIBYTE(vid);
87100
hUSBDDeviceDesc[10] = LOBYTE(pid);

stmhal/usbd_desc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626

2727
extern const USBD_DescriptorsTypeDef USBD_Descriptors;
2828

29-
void USBD_SetVIDPIDRelease(uint16_t vid, uint16_t pid, uint16_t device_release_num);
29+
void USBD_SetVIDPIDRelease(uint16_t vid, uint16_t pid, uint16_t device_release_num, int cdc_only);

tools/insert-usb-ids.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,20 @@
88
import re
99
import string
1010

11+
needed_keys = ('USB_PID_CDC_MSC', 'USB_PID_CDC_HID', 'USB_PID_CDC', 'USB_VID')
12+
1113
def parse_usb_ids(filename):
1214
rv = dict()
13-
if filename == 'usbd_desc.c':
14-
for line in open(filename).readlines():
15-
line = line.rstrip('\r\n')
16-
match = re.match('^#define\s+(\w+)\s+0x([0-9A-Fa-f]+)$', line)
17-
if match:
18-
if match.group(1) == 'USBD_VID':
19-
rv['USB_VID'] = match.group(2)
20-
elif match.group(1) == 'USBD_PID':
21-
rv['USB_PID'] = match.group(2)
22-
if 'USB_VID' in rv and 'USB_PID' in rv:
23-
break
24-
else:
25-
raise Exception("Don't (yet) know how to parse USB IDs from %s" % filename)
26-
for k in ('USB_PID', 'USB_VID'):
15+
for line in open(filename).readlines():
16+
line = line.rstrip('\r\n')
17+
match = re.match('^#define\s+(\w+)\s+\(0x([0-9A-Fa-f]+)\)$', line)
18+
if match and match.group(1).startswith('USBD_'):
19+
key = match.group(1).replace('USBD', 'USB')
20+
val = match.group(2)
21+
print("key =", key, "val =", val)
22+
if key in needed_keys:
23+
rv[key] = val
24+
for k in needed_keys:
2725
if k not in rv:
2826
raise Exception("Unable to parse %s from %s" % (k, filename))
2927
return rv

0 commit comments

Comments
 (0)
0