1010pinctrl definitions for Zephyr
1111"""
1212
13- from bdb import set_trace
1413import xml .etree .ElementTree as ET
1514import re
1615import collections
@@ -70,7 +69,7 @@ def get_name(self):
7069 Get mux option name
7170 """
7271 return self ._name
73-
72+
7473 def get_mux_name (self ):
7574 """
7675 Get name of the mux option, without pin name
@@ -261,9 +260,6 @@ def _get_pin_properties(self, props):
261260 """
262261 prop_mapping = {}
263262 for prop in props .findall ('functional_property' ):
264- if len (prop .findall ('state/configuration/assign' )) == 0 :
265- # Not configurable property. Skip
266- continue
267263 prop_id = prop .attrib ['id' ]
268264 if not 'default' in prop .attrib :
269265 # No default property. Skip
@@ -272,7 +268,11 @@ def _get_pin_properties(self, props):
272268 prop_mapping [prop_id ]['default' ] = prop .attrib ['default' ]
273269 for state in prop .findall ('state' ):
274270 reg_assign = state .find ('configuration/assign' )
275- bit_value = int (reg_assign .attrib ['bit_field_value' ], 0 )
271+ if reg_assign :
272+ bit_value = int (reg_assign .attrib ['bit_field_value' ], 0 )
273+ else :
274+ # Assume writing zero to register will select default
275+ bit_value = 0
276276 prop_mapping [prop_id ][state .attrib ['id' ]] = bit_value
277277 return prop_mapping
278278
@@ -322,6 +322,20 @@ def __repr__(self):
322322 Get string representation of the object
323323 """
324324 return "PinGroup(%s)" % (self ._name )
325+
326+ def __eq__ (self , obj ):
327+ """
328+ return true if two objects have the same pin group name
329+ """
330+ return isinstance (obj , PinGroup ) and self ._name == obj ._name
331+
332+ def __lt__ (self , obj ):
333+ """
334+ Compare objects based on name
335+ """
336+ if not isinstance (obj , PinGroup ):
337+ return True
338+ return self ._name < obj ._name
325339
326340 def get_pin_props (self ):
327341 """
@@ -459,22 +473,19 @@ def _write_pins(self, which_port, pins, file):
459473 label = pin_data .get_name ()
460474 port = pin_data .get_port ()
461475 pin = pin_data .get_pin ()
462-
463- # Port values range from 'A'-'E'. Store with 4 bits
464- # Shift port value so it will not conflict with pin configuration
465- pin_val = (((ord (port ) - ord ('A' )) & 0xF ) << 28 )
466- # Shift pin value to where it will be stored in the pin setting reg
467- # Pin values range from 0-31, give 6 bits for future expansion
468- # Shift pin value so it will not conflict with pin configuration
469- pin_val |= ((int (pin ) & 0x3F ) << 22 )
470- # Put pin mux value in at the correct offset for the pin config register
471- pin_val |= ((pin_data .get_mux () & 0x7 ) << 8 )
476+ mux = pin_data .get_mux ()
472477
473478 if label in seen_nodes :
474479 continue
475480 seen_nodes .append (label )
476481
477- file .write (f"#define { label } 0x{ pin_val :x} /* PT{ port } { pin } */\n " )
482+ file .write (f"#define { label } KINETIS_MUX('{ port } ',{ pin } ,{ mux } ) /* PT{ port } { pin } */\n " )
483+
484+ def get_part_num (self ):
485+ """
486+ Return the part number this class is instantiated for
487+ """
488+ return self ._part_num
478489
479490 def write_pinctrl_defs (self , outputfile ):
480491 """
@@ -494,15 +505,27 @@ def write_pinctrl_defs(self, outputfile):
494505 f" * { self ._copyright } \n "
495506 " */\n "
496507 "\n " )
508+
509+ # Notes on the below macro:
510+ # Port values range from 'A'-'E', so we store them with 4 bits,
511+ # with port A being 0, B=1,...
512+ # Pin values range from 0-31, so we give 6 bits for future expansion
513+ # Mux values range from 0-8, so we give 3 bits
514+ # shift the port and pin values to the MSBs of the mux value, so they
515+ # don't conflict with pin configuration settings
516+ # Store the mux value at the offset it will actually be written to the
517+ # configuration register
518+ mux_macro = ("#define KINETIS_MUX(port, pin, mux)\t \t \\ \n "
519+ "\t (((((port) - 'A') & 0xF) << 28) |\t \\ \n "
520+ "\t (((pin) & 0x3F) << 22) |\t \t \\ \n "
521+ "\t (((mux) & 0x7) << 8))\n \n " )
497522 with open (outputfile , "w" , encoding = "utf8" ) as file :
498523 file .write (file_header )
499524 # ifdef guard
500525 file .write (f"#ifndef _ZEPHYR_DTS_BINDING_{ self ._part_num .upper ()} _\n " )
501526 file .write (f"#define _ZEPHYR_DTS_BINDING_{ self ._part_num .upper ()} _\n \n " )
502527 # Write macro to make port name
503- file .write ("#define MUX(port, pin) (((((port) - 'A') & 0xF) << 28) | (((pin) & 0x3F)) << 22)\n \n " )
504- file .write ("#define PIN(mux) (((mux) & 0xFC00000) >> 22\n \n " )
505- file .write ("#define PORT(mux) (((mux) & 0xF0000000) >> 28)\n \n " )
528+ file .write (mux_macro )
506529 self ._write_pins ('a' , pcr_pins , file )
507530 self ._write_pins ('b' , pcr_pins , file )
508531 self ._write_pins ('c' , pcr_pins , file )
@@ -548,7 +571,7 @@ def write_pinctrl_groups(self, mexfile, outputfile):
548571 file .write (f"\n #include <nxp/kinetis/{ get_package_name (mexfile )} -pinctrl.h>\n \n " )
549572 file .write ("&pinctrl {\n " )
550573 # Write pin groups back out to disk
551- for group in pin_groups .values ():
574+ for group in sorted ( pin_groups .values () ):
552575 pin_props = group .get_pin_props ()
553576 if len (pin_props ) == 0 :
554577 # Do not write to disk
@@ -559,11 +582,11 @@ def write_pinctrl_groups(self, mexfile, outputfile):
559582 for pin_prop in sorted (pin_props ):
560583 group_str = f"\t \t group{ idx } {{\n "
561584 # Write all pin names
562- group_str += "\t \t \t pinmux = < "
585+ group_str += "\t \t \t pinmux = "
563586 for pin in group .get_pins (pin_prop ):
564- group_str += f"{ pin .get_name ()} \n \t \t \t \t "
587+ group_str += f"< { pin .get_name ()} >, \n \t \t \t \t "
565588 # Strip out last 3 tabs and close pin name list
566- group_str = re .sub (r'\n\t\t\t\t$' , '> ;\n ' , group_str )
589+ group_str = re .sub (r', \n\t\t\t\t$' , ';\n ' , group_str )
567590 idx += 1
568591 # Write all pin props
569592 for prop in pin_prop :
0 commit comments