55"""
66
77
8+ from collections import defaultdict
89import xml .etree .ElementTree as ET
910import datetime
1011import re
@@ -227,13 +228,12 @@ def get_pin_property_default(self, prop):
227228
228229 def get_pin_defaults (self ):
229230 """
230- Gets mapping of all pin property names to default values
231+ Gets mapping of all pin property names to default value names
231232 """
232233 pin_defaults = {}
233234 for prop in self .get_pin_properties ():
234235 pin_default = self .get_pin_property_default (prop )
235- pin_default_val = self .get_pin_property_value (prop , pin_default )
236- pin_defaults [prop ] = pin_default_val
236+ pin_defaults [prop ] = pin_default
237237 return pin_defaults
238238
239239 def get_pin_property_value (self , prop , selection ):
@@ -605,7 +605,7 @@ def write_pinctrl_overrides(self, mexfile, outputfile):
605605 dts_file .write (dts_entry )
606606 # DTS file write is done, close it
607607 dts_file .close ()
608-
608+
609609 def write_pinctrl_groups (self , mexfile , outputfile ):
610610 """
611611 Write pinctrl groups to disk as a parsed DTS file. Intended for use
@@ -636,17 +636,35 @@ def write_pinctrl_groups(self, mexfile, outputfile):
636636 pins = pin_groups [pin_group ]
637637 # Write pin group name
638638 dts_file .write (f"\t { pin_group } : { pin_group } {{\n " )
639- for pin in sorted (pins .keys ()):
639+ # First step- take all pin configurations, and generate zephyr
640+ # dts strings. Use those strings as keys in a dictionary to identify
641+ # pins in the pin group with the same configuration
642+ cfg_dict = defaultdict (lambda : [])
643+ for pin in pins .keys ():
640644 props = pins [pin ]
641645 iomux = props ['mux' ]
642646 overrides = props ['overrides' ]
643647 defaults = props ['defaults' ]
644- dts_entry = f"\t \t { pin } : { pin } {{\n "
645- dts_entry += f"\t \t \t pinmux = <{ iomux .get_name ()} >;\n "
646648 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 "
649+ cfg_dict [dts_strings ].append (iomux .get_name ())
650+ sorted_keys = sorted (cfg_dict .keys ())
651+ # Second step- take each dictionary, and write out all pinmuxes
652+ # as well as their shared configuration into a group
653+ for idx in range (len (cfg_dict .keys ())):
654+ pinmuxes = cfg_dict [sorted_keys [idx ]]
655+ pinmux_str = ""
656+ for mux in pinmuxes :
657+ pinmux_str += f"<{ mux } >,\n \t \t \t \t "
658+ pinmux_str = pinmux_str .strip (',\n \t \t \t \t ' )
659+ # Name of group
660+ dts_entry = f"\t \t group{ idx } {{\n "
661+ # Pinmuxes for group
662+ dts_entry += f"\t \t \t pins = { pinmux_str } ;\n "
663+ # Properties of group
664+ for prop in sorted_keys [idx ]:
665+ dts_entry += f"\t \t \t { prop } ;\n "
666+ # Closing brace of group
667+ dts_entry += "\t \t };\n "
650668 dts_file .write (dts_entry )
651669 # Write closing brace of pin group
652670 dts_file .write ("\t };\n \n " )
@@ -692,8 +710,7 @@ def _generate_pin_overrides(self, signal_pin, pin):
692710 # Get bit field value of feature
693711 name = feature .attrib ['name' ]
694712 value = feature .attrib ['value' ]
695- bit_value = signal_pin .get_pin_property_value (name , value )
696- overrides [name ] = bit_value
713+ overrides [name ] = value
697714 return overrides
698715
699716 def _props_to_dts (self , props , defaults ):
@@ -705,44 +722,69 @@ def _props_to_dts(self, props, defaults):
705722 @return array of strings suitable for writing to DTS
706723 """
707724 zephyr_props = []
725+ # Lambda to convert property names to zephyr formatted strings
726+ sanitize = lambda x : "\" " + re .sub (r'[\W_]' , '-' , x ).lower () + "\" "
708727 # Check pin defaults and overrides to see if the pin will have a pull or keeper
709728 if 'pull_keeper_enable' in props :
710- pull_keeper = props ['pull_keeper_enable' ] == 1
729+ pull_keeper = props ['pull_keeper_enable' ] == 'Enable'
711730 else :
712- pull_keeper = defaults ['pull_keeper_enable' ] == 1
731+ pull_keeper = defaults ['pull_keeper_enable' ] == 'Enable'
713732 keeper = False
714733 if pull_keeper :
715734 # Determine if pull or keeper is selected
716735 if 'pull_keeper_select' in props :
717- keeper = props ['pull_keeper_select' ] == 0
736+ keeper = props ['pull_keeper_select' ] == 'Keeper'
718737 else :
719- keeper = defaults ['pull_keeper_select' ] == 0
738+ keeper = defaults ['pull_keeper_select' ] == 'Keeper'
720739 if keeper :
721740 zephyr_props .append ('bias-bus-hold' )
722- for (prop , value ) in props .items ():
723- if prop == 'drive_strength' :
724- zephyr_props .append (f"drive-strength = <{ value } >" )
725- elif prop == 'hysteresis_enable' and value == 1 :
726- zephyr_props .append ('input-schmitt-enable' )
727- elif prop == 'open_drain' and value == 1 :
728- zephyr_props .append ('drive-open-drain' )
729- elif prop == 'pull_up_down_config' :
730- if value == 0 and (pull_keeper and not keeper ):
731- # Pull down the pin
732- zephyr_props .append ('bias-pull-down' )
733- elif (pull_keeper and not keeper ):
734- # Pull up the pin
735- zephyr_props .append (f"bias-pull-up = <{ value } >" )
736- elif prop == 'slew_rate' :
737- zephyr_props .append (f"slew-rate = <{ value } >" )
738- elif prop == 'speed' :
739- zephyr_props .append (f"nxp,speed = <{ value } >" )
740- elif prop == 'software_input_on' and value == 1 :
741+ # For each property, append the provided override or the default
742+ if 'drive_strength' in props :
743+ zephyr_props .append (f"drive-strength = { sanitize (props ['drive_strength' ])} " )
744+ else :
745+ zephyr_props .append (f"drive-strength = { sanitize (defaults ['drive_strength' ])} " )
746+ if 'hysteresis_enable' in props and props ['hysteresis_enable' ] == 'Enable' :
747+ zephyr_props .append ('input-schmitt-enable' )
748+ elif defaults ['hysteresis_enable' ] == 'Enable' :
749+ zephyr_props .append ('input-schmitt-enable' )
750+ if 'open_drain' in props and props ['open_drain' ] == 'Enable' :
751+ zephyr_props .append ('drive-open-drain' )
752+ elif defaults ['open_drain' ] == 'Enable' :
753+ zephyr_props .append ('drive-open-drain' )
754+ if 'pull_up_down_config' in props :
755+ if (props ['pull_up_down_config' ] == 'Pull_Down_100K_Ohm'
756+ and (pull_keeper and not keeper )):
757+ # Pull down the pin
758+ zephyr_props .append ('bias-pull-down' )
759+ zephyr_props .append (f"bias-pull-down-value = { sanitize (props ['pull_up_down_config' ])} " )
760+ elif (pull_keeper and not keeper ):
761+ # Pull up the pin
762+ zephyr_props .append ('bias-pull-up' )
763+ zephyr_props .append (f"bias-pull-up-value = { sanitize (props ['pull_up_down_config' ])} " )
764+ else :
765+ if (defaults ['pull_up_down_config' ] == 'Pull_Down_100K_Ohm'
766+ and (pull_keeper and not keeper )):
767+ # Pull down the pin
768+ zephyr_props .append ('bias-pull-down' )
769+ zephyr_props .append (f"bias-pull-down-value = { sanitize (defaults ['pull_up_down_config' ])} " )
770+ elif (pull_keeper and not keeper ):
771+ # Pull up the pin
772+ zephyr_props .append ('bias-pull-up' )
773+ zephyr_props .append (f"bias-pull-up-value = { sanitize (defaults ['bias-pull-up' ])} " )
774+ if 'slew_rate' in props :
775+ zephyr_props .append (f"slew-rate = { sanitize (props ['slew_rate' ])} " )
776+ else :
777+ zephyr_props .append (f"slew-rate = { sanitize (defaults ['slew_rate' ])} " )
778+ if 'speed' in props :
779+ zephyr_props .append (f"nxp,speed = { sanitize (props ['speed' ])} " )
780+ elif 'speed' in defaults :
781+ zephyr_props .append (f"nxp,speed = { sanitize (defaults ['speed' ])} " )
782+ if 'software_input_on' in props and props ['software_input_on' ] == 'Enable' :
783+ zephyr_props .append ('input-enable' )
784+ elif 'software_input_on' in defaults :
785+ if defaults ['software_input_on' ] == 'Enable' :
741786 zephyr_props .append ('input-enable' )
742- elif prop == 'pull_keeper_enable' or prop == 'pull_keeper_select' :
743- continue
744-
745- return zephyr_props
787+ return tuple (zephyr_props )
746788
747789 def _parse_mex_cfg (self , mex_xml , pin_mapping ):
748790 """
0 commit comments