2014-03-12 02:55:41 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""Creates the pin file for the STM32F4xx."""
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
import csv
|
|
|
|
|
2018-07-17 16:22:11 -04:00
|
|
|
# Must have matching entries in AF_FN_* enum in ../pin_defs_stm32.h
|
2014-03-12 02:55:41 -04:00
|
|
|
SUPPORTED_FN = {
|
2020-02-26 23:36:53 -05:00
|
|
|
"TIM": ["CH1", "CH2", "CH3", "CH4", "CH1N", "CH2N", "CH3N", "CH1_ETR", "ETR", "BKIN"],
|
|
|
|
"I2C": ["SDA", "SCL"],
|
|
|
|
"I2S": ["CK", "MCK", "SD", "WS", "EXTSD"],
|
|
|
|
"USART": ["RX", "TX", "CTS", "RTS", "CK"],
|
|
|
|
"UART": ["RX", "TX", "CTS", "RTS"],
|
2021-02-16 19:07:34 -05:00
|
|
|
"LPUART": ["RX", "TX", "CTS", "RTS"],
|
2020-02-26 23:36:53 -05:00
|
|
|
"SPI": ["NSS", "SCK", "MISO", "MOSI"],
|
|
|
|
"SDMMC": ["CK", "CMD", "D0", "D1", "D2", "D3"],
|
|
|
|
"CAN": ["TX", "RX"],
|
2014-03-12 02:55:41 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 18:59:59 -04:00
|
|
|
CONDITIONAL_VAR = {
|
2020-02-26 23:36:53 -05:00
|
|
|
"I2C": "MICROPY_HW_I2C{num}_SCL",
|
2021-04-17 00:27:40 -04:00
|
|
|
"I2S": "MICROPY_HW_I2S{num}",
|
2020-02-26 23:36:53 -05:00
|
|
|
"SPI": "MICROPY_HW_SPI{num}_SCK",
|
|
|
|
"UART": "MICROPY_HW_UART{num}_TX",
|
2021-02-16 19:07:34 -05:00
|
|
|
"LPUART": "MICROPY_HW_LPUART{num}_TX",
|
2020-02-26 23:36:53 -05:00
|
|
|
"USART": "MICROPY_HW_UART{num}_TX",
|
|
|
|
"SDMMC": "MICROPY_HW_SDMMC{num}_CK",
|
|
|
|
"CAN": "MICROPY_HW_CAN{num}_TX",
|
2015-07-02 18:59:59 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2014-03-12 02:55:41 -04:00
|
|
|
def parse_port_pin(name_str):
|
|
|
|
"""Parses a string and returns a (port-num, pin-num) tuple."""
|
|
|
|
if len(name_str) < 3:
|
|
|
|
raise ValueError("Expecting pin name to be at least 3 charcters.")
|
2020-02-26 23:36:53 -05:00
|
|
|
if name_str[0] != "P":
|
2014-03-12 02:55:41 -04:00
|
|
|
raise ValueError("Expecting pin name to start with P")
|
2020-02-26 23:36:53 -05:00
|
|
|
if name_str[1] < "A" or name_str[1] > "K":
|
2015-08-02 19:18:40 -04:00
|
|
|
raise ValueError("Expecting pin port to be between A and K")
|
2020-02-26 23:36:53 -05:00
|
|
|
port = ord(name_str[1]) - ord("A")
|
2014-03-12 02:55:41 -04:00
|
|
|
pin_str = name_str[2:]
|
|
|
|
if not pin_str.isdigit():
|
|
|
|
raise ValueError("Expecting numeric pin number.")
|
|
|
|
return (port, int(pin_str))
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2014-03-12 02:55:41 -04:00
|
|
|
def split_name_num(name_num):
|
|
|
|
num = None
|
|
|
|
for num_idx in range(len(name_num) - 1, -1, -1):
|
|
|
|
if not name_num[num_idx].isdigit():
|
2020-02-26 23:36:53 -05:00
|
|
|
name = name_num[0 : num_idx + 1]
|
|
|
|
num_str = name_num[num_idx + 1 :]
|
2014-03-12 02:55:41 -04:00
|
|
|
if len(num_str) > 0:
|
|
|
|
num = int(num_str)
|
|
|
|
break
|
|
|
|
return name, num
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2015-07-02 18:59:59 -04:00
|
|
|
def conditional_var(name_num):
|
|
|
|
# Try the specific instance first. For example, if name_num is UART4_RX
|
|
|
|
# then try UART4 first, and then try UART second.
|
|
|
|
name, num = split_name_num(name_num)
|
2015-08-02 19:23:47 -04:00
|
|
|
var = []
|
|
|
|
if name in CONDITIONAL_VAR:
|
|
|
|
var.append(CONDITIONAL_VAR[name].format(num=num))
|
2015-07-02 18:59:59 -04:00
|
|
|
if name_num in CONDITIONAL_VAR:
|
2015-08-02 19:23:47 -04:00
|
|
|
var.append(CONDITIONAL_VAR[name_num])
|
|
|
|
return var
|
2015-07-02 18:59:59 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2015-07-02 18:59:59 -04:00
|
|
|
def print_conditional_if(cond_var, file=None):
|
|
|
|
if cond_var:
|
2015-08-02 19:23:47 -04:00
|
|
|
cond_str = []
|
|
|
|
for var in cond_var:
|
2020-02-26 23:36:53 -05:00
|
|
|
if var.find("ENABLE") >= 0:
|
|
|
|
cond_str.append("(defined({0}) && {0})".format(var))
|
2015-08-02 19:23:47 -04:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
cond_str.append("defined({0})".format(var))
|
|
|
|
print("#if " + " || ".join(cond_str), file=file)
|
|
|
|
|
2015-07-02 18:59:59 -04:00
|
|
|
|
|
|
|
def print_conditional_endif(cond_var, file=None):
|
|
|
|
if cond_var:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("#endif", file=file)
|
2015-07-02 18:59:59 -04:00
|
|
|
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
class AlternateFunction(object):
|
|
|
|
"""Holds the information associated with a pins alternate function."""
|
|
|
|
|
|
|
|
def __init__(self, idx, af_str):
|
|
|
|
self.idx = idx
|
2015-07-02 18:59:59 -04:00
|
|
|
# Special case. We change I2S2ext_SD into I2S2_EXTSD so that it parses
|
|
|
|
# the same way the other peripherals do.
|
2020-02-26 23:36:53 -05:00
|
|
|
af_str = af_str.replace("ext_", "_EXT")
|
2015-07-02 18:59:59 -04:00
|
|
|
|
2014-03-12 02:55:41 -04:00
|
|
|
self.af_str = af_str
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
self.func = ""
|
2014-03-12 02:55:41 -04:00
|
|
|
self.fn_num = None
|
2020-02-26 23:36:53 -05:00
|
|
|
self.pin_type = ""
|
2014-03-12 02:55:41 -04:00
|
|
|
self.supported = False
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
af_words = af_str.split("_", 1)
|
2014-03-12 02:55:41 -04:00
|
|
|
self.func, self.fn_num = split_name_num(af_words[0])
|
|
|
|
if len(af_words) > 1:
|
|
|
|
self.pin_type = af_words[1]
|
|
|
|
if self.func in SUPPORTED_FN:
|
|
|
|
pin_types = SUPPORTED_FN[self.func]
|
|
|
|
if self.pin_type in pin_types:
|
|
|
|
self.supported = True
|
|
|
|
|
|
|
|
def is_supported(self):
|
|
|
|
return self.supported
|
|
|
|
|
|
|
|
def ptr(self):
|
|
|
|
"""Returns the numbered function (i.e. USART6) for this AF."""
|
|
|
|
if self.fn_num is None:
|
|
|
|
return self.func
|
2020-02-26 23:36:53 -05:00
|
|
|
return "{:s}{:d}".format(self.func, self.fn_num)
|
2014-03-12 02:55:41 -04:00
|
|
|
|
2014-07-22 10:57:36 -04:00
|
|
|
def mux_name(self):
|
2020-02-26 23:36:53 -05:00
|
|
|
return "AF{:d}_{:s}".format(self.idx, self.ptr())
|
2014-07-22 10:57:36 -04:00
|
|
|
|
2014-03-12 02:55:41 -04:00
|
|
|
def print(self):
|
|
|
|
"""Prints the C representation of this AF."""
|
2015-07-02 18:59:59 -04:00
|
|
|
cond_var = None
|
2014-03-12 02:55:41 -04:00
|
|
|
if self.supported:
|
2020-02-26 23:36:53 -05:00
|
|
|
cond_var = conditional_var("{}{}".format(self.func, self.fn_num))
|
2015-07-02 18:59:59 -04:00
|
|
|
print_conditional_if(cond_var)
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" AF", end="")
|
2014-03-12 02:55:41 -04:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" //", end="")
|
2014-03-12 02:55:41 -04:00
|
|
|
fn_num = self.fn_num
|
|
|
|
if fn_num is None:
|
|
|
|
fn_num = 0
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
"({:2d}, {:8s}, {:2d}, {:10s}, {:8s}), // {:s}".format(
|
|
|
|
self.idx, self.func, fn_num, self.pin_type, self.ptr(), self.af_str
|
|
|
|
)
|
|
|
|
)
|
2015-07-02 18:59:59 -04:00
|
|
|
print_conditional_endif(cond_var)
|
2014-03-12 02:55:41 -04:00
|
|
|
|
2014-07-22 10:57:36 -04:00
|
|
|
def qstr_list(self):
|
|
|
|
return [self.mux_name()]
|
|
|
|
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
class Pin(object):
|
|
|
|
"""Holds the information associated with a pin."""
|
|
|
|
|
|
|
|
def __init__(self, port, pin):
|
|
|
|
self.port = port
|
|
|
|
self.pin = pin
|
|
|
|
self.alt_fn = []
|
|
|
|
self.alt_fn_count = 0
|
2014-03-24 13:49:23 -04:00
|
|
|
self.adc_num = 0
|
|
|
|
self.adc_channel = 0
|
2014-04-08 18:42:19 -04:00
|
|
|
self.board_pin = False
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
def port_letter(self):
|
2020-02-26 23:36:53 -05:00
|
|
|
return chr(self.port + ord("A"))
|
2014-03-12 02:55:41 -04:00
|
|
|
|
2014-04-08 18:42:19 -04:00
|
|
|
def cpu_pin_name(self):
|
2020-02-26 23:36:53 -05:00
|
|
|
return "{:s}{:d}".format(self.port_letter(), self.pin)
|
2014-03-12 02:55:41 -04:00
|
|
|
|
2014-04-08 18:42:19 -04:00
|
|
|
def is_board_pin(self):
|
|
|
|
return self.board_pin
|
|
|
|
|
|
|
|
def set_is_board_pin(self):
|
|
|
|
self.board_pin = True
|
|
|
|
|
2014-03-24 13:49:23 -04:00
|
|
|
def parse_adc(self, adc_str):
|
2020-02-26 23:36:53 -05:00
|
|
|
if adc_str[:3] != "ADC":
|
2014-03-24 13:49:23 -04:00
|
|
|
return
|
2019-02-05 21:34:23 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
if adc_str.find("_INP") != -1:
|
2019-02-05 21:34:23 -05:00
|
|
|
# STM32H7xx, entries have the form: ADCxx_IN[PN]yy/...
|
|
|
|
# for now just pick the entry with the most ADC periphs
|
|
|
|
adc, channel = None, None
|
2020-02-26 23:36:53 -05:00
|
|
|
for ss in adc_str.split("/"):
|
|
|
|
if ss.find("_INP") != -1:
|
|
|
|
a, c = ss.split("_")
|
2019-02-05 21:34:23 -05:00
|
|
|
if adc is None or len(a) > len(adc):
|
|
|
|
adc, channel = a, c
|
|
|
|
if adc is None:
|
|
|
|
return
|
|
|
|
channel = channel[3:]
|
|
|
|
else:
|
|
|
|
# all other MCUs, entries have the form: ADCxx_INyy
|
2020-02-26 23:36:53 -05:00
|
|
|
adc, channel = adc_str.split("_")
|
2019-02-05 21:34:23 -05:00
|
|
|
channel = channel[2:]
|
|
|
|
|
2014-03-24 13:49:23 -04:00
|
|
|
for idx in range(3, len(adc)):
|
2020-02-26 23:36:53 -05:00
|
|
|
adc_num = int(adc[idx]) # 1, 2, or 3
|
|
|
|
self.adc_num |= 1 << (adc_num - 1)
|
2019-02-05 21:34:23 -05:00
|
|
|
self.adc_channel = int(channel)
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2014-03-12 02:55:41 -04:00
|
|
|
def parse_af(self, af_idx, af_strs_in):
|
|
|
|
if len(af_strs_in) == 0:
|
|
|
|
return
|
|
|
|
# If there is a slash, then the slash separates 2 aliases for the
|
|
|
|
# same alternate function.
|
2020-02-26 23:36:53 -05:00
|
|
|
af_strs = af_strs_in.split("/")
|
2014-03-12 02:55:41 -04:00
|
|
|
for af_str in af_strs:
|
|
|
|
alt_fn = AlternateFunction(af_idx, af_str)
|
|
|
|
self.alt_fn.append(alt_fn)
|
|
|
|
if alt_fn.is_supported():
|
|
|
|
self.alt_fn_count += 1
|
|
|
|
|
2014-03-24 13:49:23 -04:00
|
|
|
def alt_fn_name(self, null_if_0=False):
|
|
|
|
if null_if_0 and self.alt_fn_count == 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
return "NULL"
|
|
|
|
return "pin_{:s}_af".format(self.cpu_pin_name())
|
2014-03-24 13:49:23 -04:00
|
|
|
|
|
|
|
def adc_num_str(self):
|
2020-02-26 23:36:53 -05:00
|
|
|
str = ""
|
|
|
|
for adc_num in range(1, 4):
|
2014-03-24 13:49:23 -04:00
|
|
|
if self.adc_num & (1 << (adc_num - 1)):
|
|
|
|
if len(str) > 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
str += " | "
|
|
|
|
str += "PIN_ADC"
|
|
|
|
str += chr(ord("0") + adc_num)
|
2014-03-24 13:49:23 -04:00
|
|
|
if len(str) == 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
str = "0"
|
2014-03-24 13:49:23 -04:00
|
|
|
return str
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
def print(self):
|
|
|
|
if self.alt_fn_count == 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("// ", end="")
|
|
|
|
print("const pin_af_obj_t {:s}[] = {{".format(self.alt_fn_name()))
|
2014-03-12 02:55:41 -04:00
|
|
|
for alt_fn in self.alt_fn:
|
|
|
|
alt_fn.print()
|
|
|
|
if self.alt_fn_count == 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("// ", end="")
|
|
|
|
print("};")
|
|
|
|
print("")
|
|
|
|
print(
|
|
|
|
"const pin_obj_t pin_{:s}_obj = PIN({:s}, {:d}, {:s}, {:s}, {:d});".format(
|
|
|
|
self.cpu_pin_name(),
|
|
|
|
self.port_letter(),
|
|
|
|
self.pin,
|
|
|
|
self.alt_fn_name(null_if_0=True),
|
|
|
|
self.adc_num_str(),
|
|
|
|
self.adc_channel,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print("")
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
def print_header(self, hdr_file):
|
2018-03-28 01:13:21 -04:00
|
|
|
n = self.cpu_pin_name()
|
2020-02-26 23:36:53 -05:00
|
|
|
hdr_file.write("extern const pin_obj_t pin_{:s}_obj;\n".format(n))
|
|
|
|
hdr_file.write("#define pin_{:s} (&pin_{:s}_obj)\n".format(n, n))
|
2014-03-12 02:55:41 -04:00
|
|
|
if self.alt_fn_count > 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
hdr_file.write("extern const pin_af_obj_t pin_{:s}_af[];\n".format(n))
|
2014-04-08 18:42:19 -04:00
|
|
|
|
2014-07-22 10:57:36 -04:00
|
|
|
def qstr_list(self):
|
|
|
|
result = []
|
|
|
|
for alt_fn in self.alt_fn:
|
|
|
|
if alt_fn.is_supported():
|
|
|
|
result += alt_fn.qstr_list()
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2014-04-08 18:42:19 -04:00
|
|
|
class NamedPin(object):
|
|
|
|
def __init__(self, name, pin):
|
2020-02-26 23:36:53 -05:00
|
|
|
if name.startswith("-"):
|
2019-05-31 07:41:30 -04:00
|
|
|
self._is_hidden = True
|
|
|
|
self._name = name[1:]
|
|
|
|
else:
|
|
|
|
self._is_hidden = False
|
|
|
|
self._name = name
|
2014-04-08 18:42:19 -04:00
|
|
|
self._pin = pin
|
|
|
|
|
2019-05-31 07:41:30 -04:00
|
|
|
def is_hidden(self):
|
|
|
|
return self._is_hidden
|
|
|
|
|
2014-04-08 18:42:19 -04:00
|
|
|
def pin(self):
|
|
|
|
return self._pin
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Pins(object):
|
|
|
|
def __init__(self):
|
2020-02-26 23:36:53 -05:00
|
|
|
self.cpu_pins = [] # list of NamedPin objects
|
|
|
|
self.board_pins = [] # list of NamedPin objects
|
2021-06-30 22:04:48 -04:00
|
|
|
self.adc_table_size = {} # maps ADC number X to size of pin_adcX table
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
def find_pin(self, port_num, pin_num):
|
2014-04-08 18:42:19 -04:00
|
|
|
for named_pin in self.cpu_pins:
|
|
|
|
pin = named_pin.pin()
|
2014-03-12 02:55:41 -04:00
|
|
|
if pin.port == port_num and pin.pin == pin_num:
|
|
|
|
return pin
|
|
|
|
|
|
|
|
def parse_af_file(self, filename, pinname_col, af_col):
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(filename, "r") as csvfile:
|
2014-03-12 02:55:41 -04:00
|
|
|
rows = csv.reader(csvfile)
|
|
|
|
for row in rows:
|
|
|
|
try:
|
|
|
|
(port_num, pin_num) = parse_port_pin(row[pinname_col])
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
pin = Pin(port_num, pin_num)
|
|
|
|
for af_idx in range(af_col, len(row)):
|
2014-03-24 13:49:23 -04:00
|
|
|
if af_idx < af_col + 16:
|
|
|
|
pin.parse_af(af_idx - af_col, row[af_idx])
|
|
|
|
elif af_idx == af_col + 16:
|
|
|
|
pin.parse_adc(row[af_idx])
|
2014-04-08 18:42:19 -04:00
|
|
|
self.cpu_pins.append(NamedPin(pin.cpu_pin_name(), pin))
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
def parse_board_file(self, filename):
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(filename, "r") as csvfile:
|
2014-03-12 02:55:41 -04:00
|
|
|
rows = csv.reader(csvfile)
|
|
|
|
for row in rows:
|
|
|
|
try:
|
|
|
|
(port_num, pin_num) = parse_port_pin(row[1])
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
pin = self.find_pin(port_num, pin_num)
|
|
|
|
if pin:
|
2014-04-08 18:42:19 -04:00
|
|
|
pin.set_is_board_pin()
|
2020-02-26 23:36:53 -05:00
|
|
|
if row[0]: # Only add board pins that have a name
|
2019-05-31 07:41:30 -04:00
|
|
|
self.board_pins.append(NamedPin(row[0], pin))
|
2014-03-12 02:55:41 -04:00
|
|
|
|
2014-04-08 18:42:19 -04:00
|
|
|
def print_named(self, label, named_pins):
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
"STATIC const mp_rom_map_elem_t pin_{:s}_pins_locals_dict_table[] = {{".format(label)
|
|
|
|
)
|
2014-04-08 18:42:19 -04:00
|
|
|
for named_pin in named_pins:
|
|
|
|
pin = named_pin.pin()
|
2019-05-31 07:41:30 -04:00
|
|
|
if pin.is_board_pin() and not named_pin.is_hidden():
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
" {{ MP_ROM_QSTR(MP_QSTR_{:s}), MP_ROM_PTR(&pin_{:s}_obj) }},".format(
|
|
|
|
named_pin.name(), pin.cpu_pin_name()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print("};")
|
|
|
|
print(
|
|
|
|
"MP_DEFINE_CONST_DICT(pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);".format(
|
|
|
|
label, label
|
|
|
|
)
|
|
|
|
)
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
def print(self):
|
2014-04-08 18:42:19 -04:00
|
|
|
for named_pin in self.cpu_pins:
|
|
|
|
pin = named_pin.pin()
|
|
|
|
if pin.is_board_pin():
|
2014-03-12 02:55:41 -04:00
|
|
|
pin.print()
|
2020-02-26 23:36:53 -05:00
|
|
|
self.print_named("cpu", self.cpu_pins)
|
|
|
|
print("")
|
|
|
|
self.print_named("board", self.board_pins)
|
2014-03-12 02:55:41 -04:00
|
|
|
|
2014-03-24 13:49:23 -04:00
|
|
|
def print_adc(self, adc_num):
|
2021-06-30 22:04:48 -04:00
|
|
|
adc_pins = {}
|
|
|
|
for named_pin in self.cpu_pins:
|
|
|
|
pin = named_pin.pin()
|
|
|
|
if (
|
|
|
|
pin.is_board_pin()
|
|
|
|
and not named_pin.is_hidden()
|
|
|
|
and (pin.adc_num & (1 << (adc_num - 1)))
|
|
|
|
):
|
|
|
|
adc_pins[pin.adc_channel] = pin
|
|
|
|
if adc_pins:
|
|
|
|
table_size = max(adc_pins) + 1
|
|
|
|
self.adc_table_size[adc_num] = table_size
|
|
|
|
print("")
|
|
|
|
print("const pin_obj_t * const pin_adc{:d}[{:d}] = {{".format(adc_num, table_size))
|
|
|
|
for channel in range(table_size):
|
|
|
|
if channel in adc_pins:
|
|
|
|
obj = "&pin_{:s}_obj".format(adc_pins[channel].cpu_pin_name())
|
|
|
|
else:
|
|
|
|
obj = "NULL"
|
|
|
|
print(" [{:d}] = {},".format(channel, obj))
|
|
|
|
print("};")
|
2014-03-24 13:49:23 -04:00
|
|
|
|
2019-02-13 21:35:39 -05:00
|
|
|
def print_header(self, hdr_filename, obj_decls):
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(hdr_filename, "wt") as hdr_file:
|
2019-02-13 21:35:39 -05:00
|
|
|
if obj_decls:
|
|
|
|
for named_pin in self.cpu_pins:
|
|
|
|
pin = named_pin.pin()
|
|
|
|
if pin.is_board_pin():
|
|
|
|
pin.print_header(hdr_file)
|
2021-06-30 22:04:48 -04:00
|
|
|
for adc_num, table_size in self.adc_table_size.items():
|
|
|
|
hdr_file.write(
|
|
|
|
"extern const pin_obj_t * const pin_adc{:d}[{:d}];\n".format(
|
|
|
|
adc_num, table_size
|
|
|
|
)
|
|
|
|
)
|
2015-02-22 17:32:04 -05:00
|
|
|
# provide #define's mapping board to cpu name
|
|
|
|
for named_pin in self.board_pins:
|
2020-02-26 23:36:53 -05:00
|
|
|
hdr_file.write(
|
|
|
|
"#define pyb_pin_{:s} pin_{:s}\n".format(
|
|
|
|
named_pin.name(), named_pin.pin().cpu_pin_name()
|
|
|
|
)
|
|
|
|
)
|
2014-03-12 02:55:41 -04:00
|
|
|
|
2014-07-22 10:57:36 -04:00
|
|
|
def print_qstr(self, qstr_filename):
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(qstr_filename, "wt") as qstr_file:
|
2014-07-22 10:57:36 -04:00
|
|
|
qstr_set = set([])
|
|
|
|
for named_pin in self.cpu_pins:
|
|
|
|
pin = named_pin.pin()
|
|
|
|
if pin.is_board_pin():
|
|
|
|
qstr_set |= set(pin.qstr_list())
|
|
|
|
qstr_set |= set([named_pin.name()])
|
|
|
|
for named_pin in self.board_pins:
|
2019-05-31 07:41:30 -04:00
|
|
|
if not named_pin.is_hidden():
|
|
|
|
qstr_set |= set([named_pin.name()])
|
2014-07-22 10:57:36 -04:00
|
|
|
for qstr in sorted(qstr_set):
|
2015-07-02 18:59:59 -04:00
|
|
|
cond_var = None
|
2020-02-26 23:36:53 -05:00
|
|
|
if qstr.startswith("AF"):
|
|
|
|
af_words = qstr.split("_")
|
2015-07-02 18:59:59 -04:00
|
|
|
cond_var = conditional_var(af_words[1])
|
|
|
|
print_conditional_if(cond_var, file=qstr_file)
|
2020-02-26 23:36:53 -05:00
|
|
|
print("Q({})".format(qstr), file=qstr_file)
|
2015-07-02 18:59:59 -04:00
|
|
|
print_conditional_endif(cond_var, file=qstr_file)
|
2014-07-22 10:57:36 -04:00
|
|
|
|
|
|
|
def print_af_hdr(self, af_const_filename):
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(af_const_filename, "wt") as af_const_file:
|
2014-07-22 10:57:36 -04:00
|
|
|
af_hdr_set = set([])
|
|
|
|
mux_name_width = 0
|
|
|
|
for named_pin in self.cpu_pins:
|
|
|
|
pin = named_pin.pin()
|
|
|
|
if pin.is_board_pin():
|
|
|
|
for af in pin.alt_fn:
|
|
|
|
if af.is_supported():
|
|
|
|
mux_name = af.mux_name()
|
|
|
|
af_hdr_set |= set([mux_name])
|
|
|
|
if len(mux_name) > mux_name_width:
|
|
|
|
mux_name_width = len(mux_name)
|
|
|
|
for mux_name in sorted(af_hdr_set):
|
2020-02-26 23:36:53 -05:00
|
|
|
af_words = mux_name.split("_") # ex mux_name: AF9_I2C2
|
2015-07-02 18:59:59 -04:00
|
|
|
cond_var = conditional_var(af_words[1])
|
|
|
|
print_conditional_if(cond_var, file=af_const_file)
|
2020-02-26 23:36:53 -05:00
|
|
|
key = "MP_ROM_QSTR(MP_QSTR_{}),".format(mux_name)
|
|
|
|
val = "MP_ROM_INT(GPIO_{})".format(mux_name)
|
|
|
|
print(" { %-*s %s }," % (mux_name_width + 26, key, val), file=af_const_file)
|
2015-07-02 18:59:59 -04:00
|
|
|
print_conditional_endif(cond_var, file=af_const_file)
|
2014-07-22 10:57:36 -04:00
|
|
|
|
2019-02-13 21:35:39 -05:00
|
|
|
def print_af_defs(self, af_defs_filename, cmp_strings):
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(af_defs_filename, "wt") as af_defs_file:
|
2018-07-17 16:22:11 -04:00
|
|
|
|
|
|
|
STATIC_AF_TOKENS = {}
|
|
|
|
for named_pin in self.board_pins:
|
|
|
|
for af in named_pin.pin().alt_fn:
|
|
|
|
func = "%s%d" % (af.func, af.fn_num) if af.fn_num else af.func
|
2020-02-26 23:36:53 -05:00
|
|
|
pin_type = (af.pin_type or "NULL").split("(")[0]
|
2018-07-17 16:22:11 -04:00
|
|
|
tok = "#define STATIC_AF_%s_%s(pin_obj) ( \\" % (func, pin_type)
|
|
|
|
if tok not in STATIC_AF_TOKENS:
|
|
|
|
STATIC_AF_TOKENS[tok] = []
|
2019-02-13 21:35:39 -05:00
|
|
|
if cmp_strings:
|
|
|
|
pin_name = named_pin.pin().cpu_pin_name()
|
2020-02-26 23:36:53 -05:00
|
|
|
cmp_str = (
|
|
|
|
' ((strcmp( #pin_obj , "(&pin_%s_obj)") '
|
|
|
|
' & strcmp( #pin_obj , "((&pin_%s_obj))")) == 0) ? (%d) : \\'
|
|
|
|
% (pin_name, pin_name, af.idx)
|
|
|
|
)
|
2019-02-13 21:35:39 -05:00
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
cmp_str = " ((pin_obj) == (pin_%s)) ? (%d) : \\" % (
|
|
|
|
named_pin.pin().cpu_pin_name(),
|
|
|
|
af.idx,
|
|
|
|
)
|
2019-02-13 21:35:39 -05:00
|
|
|
STATIC_AF_TOKENS[tok].append(cmp_str)
|
2018-07-17 16:22:11 -04:00
|
|
|
|
|
|
|
for tok, pins in STATIC_AF_TOKENS.items():
|
|
|
|
print(tok, file=af_defs_file)
|
|
|
|
print("\n".join(sorted(pins)), file=af_defs_file)
|
|
|
|
print(" (0xffffffffffffffffULL))\n", file=af_defs_file)
|
|
|
|
|
2014-08-11 02:18:10 -04:00
|
|
|
def print_af_py(self, af_py_filename):
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(af_py_filename, "wt") as af_py_file:
|
|
|
|
print("PINS_AF = (", file=af_py_file)
|
2014-08-11 02:18:10 -04:00
|
|
|
for named_pin in self.board_pins:
|
2019-05-31 07:41:30 -04:00
|
|
|
if named_pin.is_hidden():
|
|
|
|
continue
|
2020-02-26 23:36:53 -05:00
|
|
|
print(" ('%s', " % named_pin.name(), end="", file=af_py_file)
|
2014-08-11 02:18:10 -04:00
|
|
|
for af in named_pin.pin().alt_fn:
|
|
|
|
if af.is_supported():
|
2020-02-26 23:36:53 -05:00
|
|
|
print("(%d, '%s'), " % (af.idx, af.af_str), end="", file=af_py_file)
|
|
|
|
print("),", file=af_py_file)
|
|
|
|
print(")", file=af_py_file)
|
2014-08-11 02:18:10 -04:00
|
|
|
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog="make-pins.py",
|
|
|
|
usage="%(prog)s [options] [command]",
|
2020-02-26 23:36:53 -05:00
|
|
|
description="Generate board specific pin file",
|
2014-03-12 02:55:41 -04:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2020-02-26 23:36:53 -05:00
|
|
|
"-a",
|
|
|
|
"--af",
|
2014-03-12 02:55:41 -04:00
|
|
|
dest="af_filename",
|
|
|
|
help="Specifies the alternate function file for the chip",
|
2020-02-26 23:36:53 -05:00
|
|
|
default="stm32f4xx_af.csv",
|
2014-03-12 02:55:41 -04:00
|
|
|
)
|
2014-07-22 10:57:36 -04:00
|
|
|
parser.add_argument(
|
|
|
|
"--af-const",
|
|
|
|
dest="af_const_filename",
|
|
|
|
help="Specifies header file for alternate function constants.",
|
2020-02-26 23:36:53 -05:00
|
|
|
default="build/pins_af_const.h",
|
2014-07-22 10:57:36 -04:00
|
|
|
)
|
2014-08-11 02:18:10 -04:00
|
|
|
parser.add_argument(
|
|
|
|
"--af-py",
|
|
|
|
dest="af_py_filename",
|
|
|
|
help="Specifies the filename for the python alternate function mappings.",
|
2020-02-26 23:36:53 -05:00
|
|
|
default="build/pins_af.py",
|
2014-08-11 02:18:10 -04:00
|
|
|
)
|
2018-07-17 16:22:11 -04:00
|
|
|
parser.add_argument(
|
|
|
|
"--af-defs",
|
|
|
|
dest="af_defs_filename",
|
|
|
|
help="Specifies the filename for the alternate function defines.",
|
2020-02-26 23:36:53 -05:00
|
|
|
default="build/pins_af_defs.h",
|
2018-07-17 16:22:11 -04:00
|
|
|
)
|
2019-02-13 21:35:39 -05:00
|
|
|
parser.add_argument(
|
|
|
|
"--af-defs-cmp-strings",
|
|
|
|
dest="af_defs_cmp_strings",
|
|
|
|
help="Whether to compare pin name strings for the alternate function defines instead of object values",
|
|
|
|
action="store_true",
|
|
|
|
)
|
2014-03-12 02:55:41 -04:00
|
|
|
parser.add_argument(
|
2020-08-29 01:14:29 -04:00
|
|
|
"-b",
|
|
|
|
"--board",
|
|
|
|
dest="board_filename",
|
|
|
|
help="Specifies the board file",
|
2014-03-12 02:55:41 -04:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2020-02-26 23:36:53 -05:00
|
|
|
"-p",
|
|
|
|
"--prefix",
|
2014-03-12 02:55:41 -04:00
|
|
|
dest="prefix_filename",
|
|
|
|
help="Specifies beginning portion of generated pins file",
|
2020-02-26 23:36:53 -05:00
|
|
|
default="stm32f4xx_prefix.c",
|
2014-03-12 02:55:41 -04:00
|
|
|
)
|
2014-07-22 10:57:36 -04:00
|
|
|
parser.add_argument(
|
2020-02-26 23:36:53 -05:00
|
|
|
"-q",
|
|
|
|
"--qstr",
|
2014-07-22 10:57:36 -04:00
|
|
|
dest="qstr_filename",
|
|
|
|
help="Specifies name of generated qstr header file",
|
2020-02-26 23:36:53 -05:00
|
|
|
default="build/pins_qstr.h",
|
2014-07-22 10:57:36 -04:00
|
|
|
)
|
2014-03-12 02:55:41 -04:00
|
|
|
parser.add_argument(
|
2020-02-26 23:36:53 -05:00
|
|
|
"-r",
|
|
|
|
"--hdr",
|
2014-03-12 02:55:41 -04:00
|
|
|
dest="hdr_filename",
|
|
|
|
help="Specifies name of generated pin header file",
|
2020-02-26 23:36:53 -05:00
|
|
|
default="build/pins.h",
|
2014-03-12 02:55:41 -04:00
|
|
|
)
|
2019-02-13 21:35:39 -05:00
|
|
|
parser.add_argument(
|
|
|
|
"--hdr-obj-decls",
|
|
|
|
dest="hdr_obj_decls",
|
|
|
|
help="Whether to include declarations for pin objects in pin header file",
|
2020-02-26 23:36:53 -05:00
|
|
|
action="store_true",
|
2019-02-13 21:35:39 -05:00
|
|
|
)
|
2014-03-12 02:55:41 -04:00
|
|
|
args = parser.parse_args(sys.argv[1:])
|
|
|
|
|
|
|
|
pins = Pins()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("// This file was automatically generated by make-pins.py")
|
|
|
|
print("//")
|
2014-03-12 02:55:41 -04:00
|
|
|
if args.af_filename:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("// --af {:s}".format(args.af_filename))
|
2014-03-12 02:55:41 -04:00
|
|
|
pins.parse_af_file(args.af_filename, 1, 2)
|
|
|
|
|
|
|
|
if args.board_filename:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("// --board {:s}".format(args.board_filename))
|
2014-03-12 02:55:41 -04:00
|
|
|
pins.parse_board_file(args.board_filename)
|
|
|
|
|
|
|
|
if args.prefix_filename:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("// --prefix {:s}".format(args.prefix_filename))
|
|
|
|
print("")
|
|
|
|
with open(args.prefix_filename, "r") as prefix_file:
|
2014-03-12 02:55:41 -04:00
|
|
|
print(prefix_file.read())
|
|
|
|
pins.print()
|
2021-06-30 22:04:48 -04:00
|
|
|
for i in range(1, 4):
|
|
|
|
pins.print_adc(i)
|
2019-02-13 21:35:39 -05:00
|
|
|
pins.print_header(args.hdr_filename, args.hdr_obj_decls)
|
2014-07-22 10:57:36 -04:00
|
|
|
pins.print_qstr(args.qstr_filename)
|
|
|
|
pins.print_af_hdr(args.af_const_filename)
|
2014-08-11 02:18:10 -04:00
|
|
|
pins.print_af_py(args.af_py_filename)
|
2019-02-13 21:35:39 -05:00
|
|
|
pins.print_af_defs(args.af_defs_filename, args.af_defs_cmp_strings)
|
2014-03-12 02:55:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|