2015-02-06 14:35:48 +00:00
|
|
|
#!/usr/bin/env python
|
2015-09-04 13:36:52 +01:00
|
|
|
"""Generates the pins file for the CC3200."""
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
import csv
|
|
|
|
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
SUPPORTED_AFS = {
|
|
|
|
"UART": ("TX", "RX", "RTS", "CTS"),
|
|
|
|
"SPI": ("CLK", "MOSI", "MISO", "CS0"),
|
|
|
|
#'I2S': ('CLK', 'FS', 'DAT0', 'DAT1'),
|
|
|
|
"I2C": ("SDA", "SCL"),
|
|
|
|
"TIM": ("PWM"),
|
|
|
|
"SD": ("CLK", "CMD", "DAT0"),
|
|
|
|
"ADC": ("CH0", "CH1", "CH2", "CH3"),
|
|
|
|
}
|
|
|
|
|
2015-09-04 13:36:52 +01:00
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
def parse_port_pin(name_str):
|
|
|
|
"""Parses a string and returns a (port, gpio_bit) tuple."""
|
2015-07-28 22:03:53 +01:00
|
|
|
if len(name_str) < 3:
|
|
|
|
raise ValueError("Expecting pin name to be at least 3 characters")
|
2020-02-27 04:36:53 +00:00
|
|
|
if name_str[:2] != "GP":
|
2015-07-28 22:03:53 +01:00
|
|
|
raise ValueError("Expecting pin name to start with GP")
|
|
|
|
if not name_str[2:].isdigit():
|
2015-02-06 14:35:48 +00:00
|
|
|
raise ValueError("Expecting numeric GPIO number")
|
2015-07-28 22:03:53 +01:00
|
|
|
port = int(int(name_str[2:]) / 8)
|
|
|
|
gpio_bit = 1 << int(int(name_str[2:]) % 8)
|
2015-02-06 14:35:48 +00:00
|
|
|
return (port, gpio_bit)
|
|
|
|
|
|
|
|
|
2015-09-04 13:36:52 +01:00
|
|
|
class AF:
|
|
|
|
"""Holds the description of an alternate function"""
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2015-09-04 13:36:52 +01:00
|
|
|
def __init__(self, name, idx, fn, unit, type):
|
|
|
|
self.name = name
|
|
|
|
self.idx = idx
|
2015-09-14 19:03:32 +01:00
|
|
|
if self.idx > 15:
|
|
|
|
self.idx = -1
|
2015-09-04 13:36:52 +01:00
|
|
|
self.fn = fn
|
|
|
|
self.unit = unit
|
|
|
|
self.type = type
|
|
|
|
|
|
|
|
def print(self):
|
2020-02-27 04:36:53 +00:00
|
|
|
print(
|
|
|
|
" AF({:16s}, {:4d}, {:8s}, {:4d}, {:8s}), // {}".format(
|
|
|
|
self.name, self.idx, self.fn, self.unit, self.type, self.name
|
|
|
|
)
|
|
|
|
)
|
2015-09-04 13:36:52 +01:00
|
|
|
|
2016-02-14 12:46:04 +00:00
|
|
|
|
2015-09-04 13:36:52 +01:00
|
|
|
class Pin:
|
2015-02-06 14:35:48 +00:00
|
|
|
"""Holds the information associated with a pin."""
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
def __init__(self, name, port, gpio_bit, pin_num):
|
|
|
|
self.name = name
|
|
|
|
self.port = port
|
|
|
|
self.gpio_bit = gpio_bit
|
|
|
|
self.pin_num = pin_num
|
|
|
|
self.board_pin = False
|
2015-09-04 13:36:52 +01:00
|
|
|
self.afs = []
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-09-04 13:36:52 +01:00
|
|
|
def add_af(self, af):
|
|
|
|
self.afs.append(af)
|
2015-03-12 11:36:56 +00:00
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
def print(self):
|
2020-02-27 04:36:53 +00:00
|
|
|
print("// {}".format(self.name))
|
2015-09-14 19:03:32 +01:00
|
|
|
if len(self.afs):
|
2020-02-27 04:36:53 +00:00
|
|
|
print("const pin_af_t pin_{}_af[] = {{".format(self.name))
|
2015-09-14 19:03:32 +01:00
|
|
|
for af in self.afs:
|
|
|
|
af.print()
|
2020-02-27 04:36:53 +00:00
|
|
|
print("};")
|
|
|
|
print(
|
|
|
|
"pin_obj_t pin_{:4s} = PIN({:6s}, {:1d}, {:3d}, {:2d}, pin_{}_af, {});\n".format(
|
|
|
|
self.name,
|
|
|
|
self.name,
|
|
|
|
self.port,
|
|
|
|
self.gpio_bit,
|
|
|
|
self.pin_num,
|
|
|
|
self.name,
|
|
|
|
len(self.afs),
|
|
|
|
)
|
|
|
|
)
|
2015-09-14 19:03:32 +01:00
|
|
|
else:
|
2020-02-27 04:36:53 +00:00
|
|
|
print(
|
|
|
|
"pin_obj_t pin_{:4s} = PIN({:6s}, {:1d}, {:3d}, {:2d}, NULL, 0);\n".format(
|
|
|
|
self.name, self.name, self.port, self.gpio_bit, self.pin_num
|
|
|
|
)
|
|
|
|
)
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
def print_header(self, hdr_file):
|
2020-02-27 04:36:53 +00:00
|
|
|
hdr_file.write("extern pin_obj_t pin_{:s};\n".format(self.name))
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
|
2015-09-04 13:36:52 +01:00
|
|
|
class Pins:
|
2015-02-06 14:35:48 +00:00
|
|
|
def __init__(self):
|
2020-02-27 04:36:53 +00:00
|
|
|
self.board_pins = [] # list of pin objects
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
def find_pin(self, port, gpio_bit):
|
2015-09-04 13:36:52 +01:00
|
|
|
for pin in self.board_pins:
|
2015-02-06 14:35:48 +00:00
|
|
|
if pin.port == port and pin.gpio_bit == gpio_bit:
|
|
|
|
return pin
|
2015-03-12 11:36:56 +00:00
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
def find_pin_by_num(self, pin_num):
|
2015-09-04 13:36:52 +01:00
|
|
|
for pin in self.board_pins:
|
2015-02-06 14:35:48 +00:00
|
|
|
if pin.pin_num == pin_num:
|
|
|
|
return pin
|
|
|
|
|
2015-03-12 11:36:56 +00:00
|
|
|
def find_pin_by_name(self, name):
|
2015-09-04 13:36:52 +01:00
|
|
|
for pin in self.board_pins:
|
2015-03-12 11:36:56 +00:00
|
|
|
if pin.name == name:
|
|
|
|
return pin
|
|
|
|
|
2015-09-04 13:36:52 +01:00
|
|
|
def parse_af_file(self, filename, pin_col, pinname_col, af_start_col):
|
2020-02-27 04:36:53 +00:00
|
|
|
with open(filename, "r") as csvfile:
|
2015-02-06 14:35:48 +00:00
|
|
|
rows = csv.reader(csvfile)
|
|
|
|
for row in rows:
|
|
|
|
try:
|
|
|
|
(port_num, gpio_bit) = parse_port_pin(row[pinname_col])
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
if not row[pin_col].isdigit():
|
2020-02-27 04:36:53 +00:00
|
|
|
raise ValueError(
|
|
|
|
"Invalid pin number {:s} in row {:s}".format(row[pin_col]), row
|
|
|
|
)
|
2015-02-06 14:35:48 +00:00
|
|
|
# Pin numbers must start from 0 when used with the TI API
|
2020-02-27 04:36:53 +00:00
|
|
|
pin_num = int(row[pin_col]) - 1
|
2015-02-06 14:35:48 +00:00
|
|
|
pin = Pin(row[pinname_col], port_num, gpio_bit, pin_num)
|
2015-09-04 13:36:52 +01:00
|
|
|
self.board_pins.append(pin)
|
|
|
|
af_idx = 0
|
|
|
|
for af in row[af_start_col:]:
|
2020-02-27 04:36:53 +00:00
|
|
|
af_splitted = af.split("_")
|
|
|
|
fn_name = af_splitted[0].rstrip("0123456789")
|
|
|
|
if fn_name in SUPPORTED_AFS:
|
2015-09-04 13:36:52 +01:00
|
|
|
type_name = af_splitted[1]
|
|
|
|
if type_name in SUPPORTED_AFS[fn_name]:
|
|
|
|
unit_idx = af_splitted[0][-1]
|
|
|
|
pin.add_af(AF(af, af_idx, fn_name, int(unit_idx), type_name))
|
|
|
|
af_idx += 1
|
2015-03-12 11:36:56 +00:00
|
|
|
|
|
|
|
def parse_board_file(self, filename, cpu_pin_col):
|
2020-02-27 04:36:53 +00:00
|
|
|
with open(filename, "r") as csvfile:
|
2015-02-06 14:35:48 +00:00
|
|
|
rows = csv.reader(csvfile)
|
|
|
|
for row in rows:
|
|
|
|
# Pin numbers must start from 0 when used with the TI API
|
2015-03-12 11:36:56 +00:00
|
|
|
if row[cpu_pin_col].isdigit():
|
|
|
|
pin = self.find_pin_by_num(int(row[cpu_pin_col]) - 1)
|
|
|
|
else:
|
|
|
|
pin = self.find_pin_by_name(row[cpu_pin_col])
|
2015-02-06 14:35:48 +00:00
|
|
|
if pin:
|
2015-09-04 13:36:52 +01:00
|
|
|
pin.board_pin = True
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
def print_named(self, label, pins):
|
2020-02-27 04:36:53 +00:00
|
|
|
print("")
|
|
|
|
print(
|
|
|
|
"STATIC const mp_rom_map_elem_t pin_{:s}_pins_locals_dict_table[] = {{".format(label)
|
|
|
|
)
|
2015-02-06 14:35:48 +00:00
|
|
|
for pin in pins:
|
2015-09-04 13:36:52 +01:00
|
|
|
if pin.board_pin:
|
2020-02-27 04:36:53 +00:00
|
|
|
print(
|
|
|
|
" {{ MP_ROM_QSTR(MP_QSTR_{:6s}), MP_ROM_PTR(&pin_{:6s}) }},".format(
|
|
|
|
pin.name, pin.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print("};")
|
|
|
|
print(
|
|
|
|
"MP_DEFINE_CONST_DICT(pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);".format(
|
|
|
|
label, label
|
|
|
|
)
|
|
|
|
)
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
def print(self):
|
2015-09-04 13:36:52 +01:00
|
|
|
for pin in self.board_pins:
|
|
|
|
if pin.board_pin:
|
2015-02-06 14:35:48 +00:00
|
|
|
pin.print()
|
2020-02-27 04:36:53 +00:00
|
|
|
self.print_named("board", self.board_pins)
|
|
|
|
print("")
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
def print_header(self, hdr_filename):
|
2020-02-27 04:36:53 +00:00
|
|
|
with open(hdr_filename, "wt") as hdr_file:
|
2015-09-04 13:36:52 +01:00
|
|
|
for pin in self.board_pins:
|
|
|
|
if pin.board_pin:
|
2015-02-06 14:35:48 +00:00
|
|
|
pin.print_header(hdr_file)
|
|
|
|
|
|
|
|
def print_qstr(self, qstr_filename):
|
2020-02-27 04:36:53 +00:00
|
|
|
with open(qstr_filename, "wt") as qstr_file:
|
2015-09-04 13:36:52 +01:00
|
|
|
pin_qstr_set = set([])
|
|
|
|
af_qstr_set = set([])
|
|
|
|
for pin in self.board_pins:
|
|
|
|
if pin.board_pin:
|
|
|
|
pin_qstr_set |= set([pin.name])
|
|
|
|
for af in pin.afs:
|
|
|
|
af_qstr_set |= set([af.name])
|
2020-02-27 04:36:53 +00:00
|
|
|
print("// Board pins", file=qstr_file)
|
2015-09-04 13:36:52 +01:00
|
|
|
for qstr in sorted(pin_qstr_set):
|
2020-02-27 04:36:53 +00:00
|
|
|
print("Q({})".format(qstr), file=qstr_file)
|
|
|
|
print("\n// Pin AFs", file=qstr_file)
|
2015-09-04 13:36:52 +01:00
|
|
|
for qstr in sorted(af_qstr_set):
|
2020-02-27 04:36:53 +00:00
|
|
|
print("Q({})".format(qstr), file=qstr_file)
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog="make-pins.py",
|
|
|
|
usage="%(prog)s [options] [command]",
|
2020-02-27 04:36:53 +00:00
|
|
|
description="Generate board specific pin file",
|
2015-02-06 14:35:48 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2020-02-27 04:36:53 +00:00
|
|
|
"-a",
|
|
|
|
"--af",
|
2015-02-06 14:35:48 +00:00
|
|
|
dest="af_filename",
|
|
|
|
help="Specifies the alternate function file for the chip",
|
2020-02-27 04:36:53 +00:00
|
|
|
default="cc3200_af.csv",
|
2015-02-06 14:35:48 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2020-02-27 04:36:53 +00:00
|
|
|
"-b", "--board", dest="board_filename", help="Specifies the board file",
|
2015-02-06 14:35:48 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2020-02-27 04:36:53 +00:00
|
|
|
"-p",
|
|
|
|
"--prefix",
|
2015-02-06 14:35:48 +00:00
|
|
|
dest="prefix_filename",
|
|
|
|
help="Specifies beginning portion of generated pins file",
|
2020-02-27 04:36:53 +00:00
|
|
|
default="cc3200_prefix.c",
|
2015-02-06 14:35:48 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2020-02-27 04:36:53 +00:00
|
|
|
"-q",
|
|
|
|
"--qstr",
|
2015-02-06 14:35:48 +00:00
|
|
|
dest="qstr_filename",
|
|
|
|
help="Specifies name of generated qstr header file",
|
2020-02-27 04:36:53 +00:00
|
|
|
default="build/pins_qstr.h",
|
2015-02-06 14:35:48 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2020-02-27 04:36:53 +00:00
|
|
|
"-r",
|
|
|
|
"--hdr",
|
2015-02-06 14:35:48 +00:00
|
|
|
dest="hdr_filename",
|
|
|
|
help="Specifies name of generated pin header file",
|
2020-02-27 04:36:53 +00:00
|
|
|
default="build/pins.h",
|
2015-02-06 14:35:48 +00:00
|
|
|
)
|
|
|
|
args = parser.parse_args(sys.argv[1:])
|
|
|
|
|
|
|
|
pins = Pins()
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
print("// This file was automatically generated by make-pins.py")
|
|
|
|
print("//")
|
2015-02-06 14:35:48 +00:00
|
|
|
if args.af_filename:
|
2020-02-27 04:36:53 +00:00
|
|
|
print("// --af {:s}".format(args.af_filename))
|
2015-09-04 13:36:52 +01:00
|
|
|
pins.parse_af_file(args.af_filename, 0, 1, 3)
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
if args.board_filename:
|
2020-02-27 04:36:53 +00:00
|
|
|
print("// --board {:s}".format(args.board_filename))
|
2015-09-04 13:36:52 +01:00
|
|
|
pins.parse_board_file(args.board_filename, 1)
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
if args.prefix_filename:
|
2020-02-27 04:36:53 +00:00
|
|
|
print("// --prefix {:s}".format(args.prefix_filename))
|
|
|
|
print("")
|
|
|
|
with open(args.prefix_filename, "r") as prefix_file:
|
2015-02-06 14:35:48 +00:00
|
|
|
print(prefix_file.read())
|
|
|
|
pins.print()
|
|
|
|
pins.print_qstr(args.qstr_filename)
|
|
|
|
pins.print_header(args.hdr_filename)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|