Skip to content

Commit 1452221

Browse files
committed
Auto-generate the stmhal/pybcdc_inf header file from static files
The USB VID&PID are automatically extracted from usbd_desc_cdc_msc.c and inserted into pybcdc_inf.template, ensuring that the same USB IDs get used everywhere
1 parent 2822d4e commit 1452221

6 files changed

Lines changed: 85 additions & 95 deletions

File tree

stmhal/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,23 @@ PREFIX_FILE = boards/stm32f4xx-prefix.c
210210
GEN_PINS_SRC = $(BUILD)/pins_$(BOARD).c
211211
GEN_PINS_HDR = $(BUILD)/pins.h
212212

213+
INSERT_USB_IDS = ../tools/insert-usb-ids.py
214+
FILE2H = ../tools/file2h.py
215+
216+
USB_IDS_FILE = usbd_desc_cdc_msc.c
217+
CDCINF_TEMPLATE = pybcdc.inf_template
218+
GEN_CDCINF_FILE = $(BUILD)/pybcdc.inf
219+
GEN_CDCINF_HEADER = $(BUILD)/pybcdc_inf.h
220+
213221
# Making OBJ use an order-only depenedency on the generated pins.h file
214222
# has the side effect of making the pins.h file before we actually compile
215223
# any of the objects. The normal dependency generation will deal with the
216224
# case when pins.h is modified. But when it doesn't exist, we don't know
217225
# which source files might need it.
218226
$(OBJ): | $(BUILD)/pins.h
219227

228+
$(BUILD)/main.o: $(GEN_CDCINF_HEADER)
229+
220230
# Use a pattern rule here so that make will only call make-pins.py once to make
221231
# both pins_$(BOARD).c and pins.h
222232
$(BUILD)/%_$(BOARD).c $(BUILD)/%.h: boards/$(BOARD)/%.csv $(MAKE_PINS) $(AF_FILE) $(PREFIX_FILE)
@@ -226,4 +236,12 @@ $(BUILD)/%_$(BOARD).c $(BUILD)/%.h: boards/$(BOARD)/%.csv $(MAKE_PINS) $(AF_FILE
226236
$(BUILD)/pins_$(BOARD).o: $(BUILD)/pins_$(BOARD).c
227237
$(call compile_c)
228238

239+
$(GEN_CDCINF_HEADER): $(GEN_CDCINF_FILE) $(FILE2H)
240+
$(ECHO) "Create $@"
241+
$(Q)$(PYTHON) $(FILE2H) $< > $@
242+
243+
$(GEN_CDCINF_FILE): $(CDCINF_TEMPLATE) $(INSERT_USB_IDS) $(USB_IDS_FILE)
244+
$(ECHO) "Create $@"
245+
$(Q)$(PYTHON) $(INSERT_USB_IDS) $(USB_IDS_FILE) $< > $@
246+
229247
include ../py/mkrules.mk

stmhal/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static const char fresh_main_py[] =
134134
;
135135

136136
static const char fresh_pybcdc_inf[] =
137-
#include "pybcdc.h"
137+
#include "build/pybcdc_inf.h"
138138
;
139139

140140
static const char fresh_readme_txt[] =

stmhal/pybcdc.h

Lines changed: 0 additions & 92 deletions
This file was deleted.
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_0483&PID_5740&MI_00, USB\VID_0483&PID_5740&MI_01
80+
%DESCRIPTION%=DriverInstall, USB\VID_${USB_VID}&PID_${USB_PID}&MI_00, USB\VID_${USB_VID}&PID_${USB_PID}&MI_01
8181

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

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

tools/file2h.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Reads in a text file, and performs the necessary escapes so that it
2+
# can be #included as a static string like:
3+
# static const char string_from_textfile[] =
4+
# #include "build/textfile.h"
5+
# ;
6+
# This script simply prints the escaped string straight to stdout
7+
8+
from __future__ import print_function
9+
10+
import sys
11+
12+
# Can either be set explicitly, or left blank to auto-detect
13+
line_end = ''
14+
15+
if __name__ == "__main__":
16+
filename = sys.argv[1]
17+
for line in open(filename, 'r').readlines():
18+
if not line_end:
19+
for ending in ('\r\n', '\r', '\n'):
20+
if line.endswith(ending):
21+
line_end = ending.replace('\r', '\\r').replace('\n', '\\n')
22+
break
23+
if not line_end:
24+
raise Exception("Couldn't auto-detect line-ending of %s" % filename)
25+
line = line.rstrip('\r\n')
26+
line = line.replace('\\', '\\\\')
27+
line = line.replace('"', '\\"')
28+
print('"%s%s"' % (line, line_end))

tools/insert-usb-ids.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Reads the USB VID and PID from the file specifed by sys.arg[1] and then
2+
# inserts those values into the template file specified by sys.argv[2],
3+
# printing the result to stdout
4+
5+
from __future__ import print_function
6+
7+
import sys
8+
import re
9+
import string
10+
11+
def parse_usb_ids(filename):
12+
rv = dict()
13+
if filename == 'usbd_desc_cdc_msc.c':
14+
for line in open(filename).readlines():
15+
line = line.rstrip('\r\n')
16+
match = re.match('^#define\s+(\w+)\s+0x(\d+)$', 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'):
27+
if k not in rv:
28+
raise Exception("Unable to parse %s from %s" % (k, filename))
29+
return rv
30+
31+
if __name__ == "__main__":
32+
usb_ids_file = sys.argv[1]
33+
template_file = sys.argv[2]
34+
replacements = parse_usb_ids(usb_ids_file)
35+
for line in open(template_file, 'r').readlines():
36+
print(string.Template(line).safe_substitute(replacements), end='')

0 commit comments

Comments
 (0)