1010pinctrl definitions for Zephyr
1111"""
1212
13+ from bdb import set_trace
1314import xml .etree .ElementTree as ET
1415import re
1516import collections
@@ -453,33 +454,27 @@ def _write_pins(self, which_port, pins, file):
453454
454455 seen_nodes = []
455456
456- file .write (f"&port{ which_port } {{\n " )
457- for pin_data in port_pins :
458- port_name = f"PT{ pin_data .get_port ().upper ()} { pin_data .get_pin ()} "
459- # Special case handle GPIO so that it will be of the form:
460- # PT[A-E][0-31]: GPIO[A-E]_PT[A-E][0-31]: ...
461- #
462- # eg:
463- # PTA3: GPIOA_PTA3: gpioa_pta3 { .... };
464- if pin_data .get_periph ().startswith ("GPIO" ):
465- node = f"{ pin_data .get_periph ().lower ()} _{ port_name .lower ()} "
466- label = f"{ port_name } : { node .upper ()} "
467- else :
468- label = pin_data .get_name ()
469- node = label .lower ()
470- k_port_pins_prop = f"< { pin_data .get_pin ()} { pin_data .get_mux ()} >"
471457
472- if node in seen_nodes :
458+ for pin_data in port_pins :
459+ label = pin_data .get_name ()
460+ port = pin_data .get_port ()
461+ 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 )
472+
473+ if label in seen_nodes :
473474 continue
474- seen_nodes .append (node )
475-
476- file .write (f"\t { label } : { node } {{\n " )
477- file .write ("\t \t /* < PIN PCR[MUX] > */\n " )
478- file .write (f"\t \t nxp,kinetis-port-pins = { k_port_pins_prop } ;\n " )
479- file .write ("\t };\n " )
475+ seen_nodes .append (label )
480476
481- file .write ("};\n " )
482- file .write ("\n " )
477+ file .write (f"#define { label } 0x{ pin_val :x} /* PT{ port } { pin } */\n " )
483478
484479 def write_pinctrl_defs (self , outputfile ):
485480 """
@@ -501,11 +496,19 @@ def write_pinctrl_defs(self, outputfile):
501496 "\n " )
502497 with open (outputfile , "w" , encoding = "utf8" ) as file :
503498 file .write (file_header )
499+ # ifdef guard
500+ file .write (f"#ifndef _ZEPHYR_DTS_BINDING_{ self ._part_num .upper ()} _\n " )
501+ file .write (f"#define _ZEPHYR_DTS_BINDING_{ self ._part_num .upper ()} _\n \n " )
502+ # 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 " )
504506 self ._write_pins ('a' , pcr_pins , file )
505507 self ._write_pins ('b' , pcr_pins , file )
506508 self ._write_pins ('c' , pcr_pins , file )
507509 self ._write_pins ('d' , pcr_pins , file )
508510 self ._write_pins ('e' , pcr_pins , file )
511+ file .write ("#endif\n " )
509512
510513 def _parse_mex_cfg (self , mexfile ):
511514 """
@@ -542,7 +545,7 @@ def write_pinctrl_groups(self, mexfile, outputfile):
542545 pin_groups = self ._parse_mex_cfg (mexfile )
543546 with open (outputfile , "w" , encoding = "utf8" ) as file :
544547 file .write (file_header )
545- file .write (f"\n #include <nxp/kinetis/{ get_package_name (mexfile )} -pinctrl.dtsi >\n \n " )
548+ file .write (f"\n #include <nxp/kinetis/{ get_package_name (mexfile )} -pinctrl.h >\n \n " )
546549 file .write ("&pinctrl {\n " )
547550 # Write pin groups back out to disk
548551 for group in pin_groups .values ():
@@ -558,7 +561,7 @@ def write_pinctrl_groups(self, mexfile, outputfile):
558561 # Write all pin names
559562 group_str += "\t \t \t pinmux = <"
560563 for pin in group .get_pins (pin_prop ):
561- group_str += f"& { pin .get_name ()} \n \t \t \t \t "
564+ group_str += f"{ pin .get_name ()} \n \t \t \t \t "
562565 # Strip out last 3 tabs and close pin name list
563566 group_str = re .sub (r'\n\t\t\t\t$' , '>;\n ' , group_str )
564567 idx += 1
0 commit comments