Skip to content

Commit

Permalink
Auto-generate the stmhal/pybcdc_inf header file from static files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lurch committed Apr 16, 2014
1 parent 2822d4e commit 1452221
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 95 deletions.
18 changes: 18 additions & 0 deletions stmhal/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,23 @@ PREFIX_FILE = boards/stm32f4xx-prefix.c
GEN_PINS_SRC = $(BUILD)/pins_$(BOARD).c
GEN_PINS_HDR = $(BUILD)/pins.h

INSERT_USB_IDS = ../tools/insert-usb-ids.py
FILE2H = ../tools/file2h.py

USB_IDS_FILE = usbd_desc_cdc_msc.c
CDCINF_TEMPLATE = pybcdc.inf_template
GEN_CDCINF_FILE = $(BUILD)/pybcdc.inf
GEN_CDCINF_HEADER = $(BUILD)/pybcdc_inf.h

# Making OBJ use an order-only depenedency on the generated pins.h file
# has the side effect of making the pins.h file before we actually compile
# any of the objects. The normal dependency generation will deal with the
# case when pins.h is modified. But when it doesn't exist, we don't know
# which source files might need it.
$(OBJ): | $(BUILD)/pins.h

$(BUILD)/main.o: $(GEN_CDCINF_HEADER)

# Use a pattern rule here so that make will only call make-pins.py once to make
# both pins_$(BOARD).c and pins.h
$(BUILD)/%_$(BOARD).c $(BUILD)/%.h: boards/$(BOARD)/%.csv $(MAKE_PINS) $(AF_FILE) $(PREFIX_FILE)
Expand All @@ -226,4 +236,12 @@ $(BUILD)/%_$(BOARD).c $(BUILD)/%.h: boards/$(BOARD)/%.csv $(MAKE_PINS) $(AF_FILE
$(BUILD)/pins_$(BOARD).o: $(BUILD)/pins_$(BOARD).c
$(call compile_c)

$(GEN_CDCINF_HEADER): $(GEN_CDCINF_FILE) $(FILE2H)
$(ECHO) "Create $@"
$(Q)$(PYTHON) $(FILE2H) $< > $@

$(GEN_CDCINF_FILE): $(CDCINF_TEMPLATE) $(INSERT_USB_IDS) $(USB_IDS_FILE)
$(ECHO) "Create $@"
$(Q)$(PYTHON) $(INSERT_USB_IDS) $(USB_IDS_FILE) $< > $@

include ../py/mkrules.mk
2 changes: 1 addition & 1 deletion stmhal/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static const char fresh_main_py[] =
;

static const char fresh_pybcdc_inf[] =
#include "pybcdc.h"
#include "build/pybcdc_inf.h"
;

static const char fresh_readme_txt[] =
Expand Down
92 changes: 0 additions & 92 deletions stmhal/pybcdc.h

This file was deleted.

4 changes: 2 additions & 2 deletions stmhal/pybcdc.inf → stmhal/pybcdc.inf_template
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ ServiceBinary=%12%\usbser.sys
[SourceDisksFiles]
[SourceDisksNames]
[DeviceList]
%DESCRIPTION%=DriverInstall, USB\VID_0483&PID_5740&MI_00, USB\VID_0483&PID_5740&MI_01
%DESCRIPTION%=DriverInstall, USB\VID_${USB_VID}&PID_${USB_PID}&MI_00, USB\VID_${USB_VID}&PID_${USB_PID}&MI_01

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

;---------------------------------------------------------------------
; String Definitions
Expand Down
28 changes: 28 additions & 0 deletions tools/file2h.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Reads in a text file, and performs the necessary escapes so that it
# can be #included as a static string like:
# static const char string_from_textfile[] =
# #include "build/textfile.h"
# ;
# This script simply prints the escaped string straight to stdout

from __future__ import print_function

import sys

# Can either be set explicitly, or left blank to auto-detect
line_end = ''

if __name__ == "__main__":
filename = sys.argv[1]
for line in open(filename, 'r').readlines():
if not line_end:
for ending in ('\r\n', '\r', '\n'):
if line.endswith(ending):
line_end = ending.replace('\r', '\\r').replace('\n', '\\n')
break
if not line_end:
raise Exception("Couldn't auto-detect line-ending of %s" % filename)
line = line.rstrip('\r\n')
line = line.replace('\\', '\\\\')
line = line.replace('"', '\\"')
print('"%s%s"' % (line, line_end))
36 changes: 36 additions & 0 deletions tools/insert-usb-ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Reads the USB VID and PID from the file specifed by sys.arg[1] and then
# inserts those values into the template file specified by sys.argv[2],
# printing the result to stdout

from __future__ import print_function

import sys
import re
import string

def parse_usb_ids(filename):
rv = dict()
if filename == 'usbd_desc_cdc_msc.c':
for line in open(filename).readlines():
line = line.rstrip('\r\n')
match = re.match('^#define\s+(\w+)\s+0x(\d+)$', line)
if match:
if match.group(1) == 'USBD_VID':
rv['USB_VID'] = match.group(2)
elif match.group(1) == 'USBD_PID':
rv['USB_PID'] = match.group(2)
if 'USB_VID' in rv and 'USB_PID' in rv:
break
else:
raise Exception("Don't (yet) know how to parse USB IDs from %s" % filename)
for k in ('USB_PID', 'USB_VID'):
if k not in rv:
raise Exception("Unable to parse %s from %s" % (k, filename))
return rv

if __name__ == "__main__":
usb_ids_file = sys.argv[1]
template_file = sys.argv[2]
replacements = parse_usb_ids(usb_ids_file)
for line in open(template_file, 'r').readlines():
print(string.Template(line).safe_substitute(replacements), end='')

0 comments on commit 1452221

Please sign in to comment.