Skip to content

Commit ebe31e8

Browse files
tools: Enabled rt_cfg_utils script to generate pin node or pin groups
Enabled rt_cfg_utils script to generate pin node DTS and DTSI files, or pin group/iomuxc header formats, since the format for RT parts is not yet standardized. Signed-off-by: Daniel DeGrasse <[email protected]>
1 parent d52c4b3 commit ebe31e8

File tree

2 files changed

+109
-9
lines changed

2 files changed

+109
-9
lines changed

tools/rt_cfg_utils.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,50 @@ def write_pinctrl_nodes(self, outputfile):
523523
soc_dtsi.write("};\n\n")
524524
soc_dtsi.close()
525525

526+
def write_pinctrl_header(self, outfile):
527+
"""
528+
Write pinctrl header to disk in a format suitable for inclusion into
529+
DTSI file
530+
@param outfile: output file destination
531+
"""
532+
try:
533+
soc_header = open(outfile, "w", encoding='utf8')
534+
except IOError:
535+
print(f"Could not write to file {outfile}")
536+
return
537+
# Start by writing header
538+
soc_header.write(self._header)
539+
# Write documentation block
540+
soc_header.write("/*\n"
541+
" * SOC level pinctrl defintions\n"
542+
" * These definitions define SOC level defaults for each pin,\n"
543+
" * and select the pinmux for the pin. Pinmux entries are a tuple of:\n"
544+
" * <mux_register mux_mode input_register input_daisy config_register>\n"
545+
" * the mux_register and input_daisy reside in the IOMUXC peripheral, and\n"
546+
" * the pinctrl driver will write the mux_mode and input_daisy values into\n"
547+
" * each register, respectively. The config_register is used to configure\n"
548+
" * the pin based on the devicetree properties set\n"
549+
" */\n\n")
550+
for pin in sorted(self._signal_map.values()):
551+
for iomux_opt in sorted(pin.get_mux_options()):
552+
# Get iomuxc constant values
553+
iomuxc_name = iomux_opt.get_name()
554+
register = iomux_opt.get_mux_reg()
555+
mode = iomux_opt.get_mux_val()
556+
input_reg = iomux_opt.get_daisy_reg()
557+
input_daisy = iomux_opt.get_daisy_val()
558+
config_reg = iomux_opt.get_cfg_reg()
559+
header_line = (f"#define {iomuxc_name} 0x{register:x} {mode:d}"
560+
f" 0x{input_reg:x} {input_daisy:d} 0x{config_reg:x}\n")
561+
soc_header.write(header_line)
562+
soc_header.write("\n")
563+
soc_header.close()
564+
565+
526566
def write_pinctrl_overrides(self, mexfile, outputfile):
527567
"""
528-
Write pinctrl overrides to disk as a parsed DTS file
568+
Write pinctrl overrides to disk as a parsed DTS file. Intended for use
569+
with the output of @ref write_pinctrl_nodes
529570
@param mexfile: mex file to parse for pin configuration
530571
@param outputfile: file to write dts to
531572
"""
@@ -564,6 +605,54 @@ def write_pinctrl_overrides(self, mexfile, outputfile):
564605
dts_file.write(dts_entry)
565606
# DTS file write is done, close it
566607
dts_file.close()
608+
609+
def write_pinctrl_groups(self, mexfile, outputfile):
610+
"""
611+
Write pinctrl groups to disk as a parsed DTS file. Intended for use
612+
with the output of @ref write_pinctrl_header
613+
"""
614+
if self._signal_map is None:
615+
print("Cannot write pinctrl groups without a signal map")
616+
try:
617+
config_tree = ET.parse(mexfile)
618+
except ET.ParseError:
619+
print(f"Malformed XML tree {mexfile}")
620+
return
621+
except IOError:
622+
print(f"File {mexfile} could not be opened")
623+
return
624+
try:
625+
dts_file = open(outputfile, "w", encoding='utf8')
626+
except IOError:
627+
print(f"Could not write to file {outputfile}")
628+
return
629+
# Parse the mex file
630+
pin_groups = self._parse_mex_cfg(config_tree, self._signal_map)
631+
# Start by writing header
632+
dts_file.write(self._header)
633+
dts_file.write(f"#include <nxp/nxp_imx/rt/{self.get_soc().lower()}-iomuxc.h>\n\n")
634+
dts_file.write("&pinctrl {\n")
635+
for pin_group in sorted(pin_groups.keys()):
636+
pins = pin_groups[pin_group]
637+
# Write pin group name
638+
dts_file.write(f"\t{pin_group}: {pin_group} {{\n")
639+
for pin in sorted(pins.keys()):
640+
props = pins[pin]
641+
iomux = props['mux']
642+
overrides = props['overrides']
643+
defaults = props['defaults']
644+
dts_entry = f"\t\t{pin}: {pin} {{\n"
645+
dts_entry += f"\t\t\tpinmux = <{iomux.get_name()}>;\n"
646+
dts_strings = self._props_to_dts(overrides, defaults)
647+
for entry in sorted(dts_strings):
648+
dts_entry += f"\t\t\t{entry};\n"
649+
dts_entry += "\t\t};\n\n"
650+
dts_file.write(dts_entry)
651+
# Write closing brace of pin group
652+
dts_file.write("\t};\n\n")
653+
# Write closing brace of pinctrl node
654+
dts_file.write("};\n\n")
655+
567656

568657
"""
569658
Private class methods

tools/rt_dts_gen.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
help='Output path for board level DTS file')
4747
parser.add_argument('--soc-output', metavar = 'SOC_OUT', type=str,
4848
help='Output path for soc level DTSI file')
49+
parser.add_argument('--pin-groups', action='store_true',
50+
help='Use pin groups instead of pin nodes')
4951

5052
args = parser.parse_args()
5153

@@ -54,12 +56,15 @@
5456
package_name = rt_cfg_utils.get_package_name(args.mex_file)
5557
soc_name = re.match(r'MIMXRT[0-9]+', processor_name).group(0)
5658

59+
# Set defaults for optional arguments
5760
if not args.board_output:
5861
args.board_output = f"{board_name.lower().replace('-', '_')}-pinctrl.dtsi"
5962

60-
# Set defaults for optional arguments
6163
if not args.soc_output:
62-
args.soc_output = f"{soc_name.lower()}-pinctrl.dtsi"
64+
if args.pin_groups:
65+
args.soc_output = f"{soc_name.lower()}-iomuxc.h"
66+
else:
67+
args.soc_output = f"{soc_name.lower()}-pinctrl.dtsi"
6368

6469
if args.cfg_tool_root:
6570
# Attempt to locate all the required config files automatically, using
@@ -98,9 +103,15 @@
98103
else:
99104
util = rt_cfg_utils.NXPSdkUtil(args.register_file, args.iomuxc_file, args.snvs_file,
100105
args.signal_file)
101-
102-
print(f"Generating configuration for {board_name}")
103-
util.write_pinctrl_nodes(args.soc_output)
104-
print(f"Wrote pinctrl dtsi file to {args.soc_output}")
105-
util.write_pinctrl_overrides(args.mex_file, args.board_output)
106-
print(f"Wrote pinctrl dts file to {args.board_output}")
106+
if not args.pin_groups:
107+
print(f"Generating configuration for {board_name}")
108+
util.write_pinctrl_nodes(args.soc_output)
109+
print(f"Wrote pinctrl dtsi file to {args.soc_output}")
110+
util.write_pinctrl_overrides(args.mex_file, args.board_output)
111+
print(f"Wrote pinctrl dts file to {args.board_output}")
112+
else:
113+
print(f"Generating configuration for {board_name}")
114+
util.write_pinctrl_header(args.soc_output)
115+
print(f"Wrote pinctrl header file to {args.soc_output}")
116+
util.write_pinctrl_groups(args.mex_file, args.board_output)
117+
print(f"Wrote pinctrl dts file to {args.board_output}")

0 commit comments

Comments
 (0)