11import argparse
22import datetime
3- import fnmatch
43import json
5- import os
64import re
75import string
86import sys
97import textwrap
8+ from pathlib import Path
109from xml .dom .minidom import parse , Node
1110from argparse import RawTextHelpFormatter
1211
13- mcu_file = ""
1412mcu_list = [] # 'name'
1513io_list = [] # 'PIN','name'
1614alt_list = [] # 'PIN','name'
@@ -63,7 +61,7 @@ def find_gpio_file():
6361
6462
6563def get_gpio_af_num (pintofind , iptofind ):
66- if "STM32F10" in mcu_file :
64+ if "STM32F10" in mcu_file . stem :
6765 return get_gpio_af_numF1 (pintofind , iptofind )
6866 # DBG print ('pin to find ' + pintofind)
6967 i = 0
@@ -314,7 +312,7 @@ def print_header():
314312 */
315313""" .format (
316314 datetime .datetime .now ().year ,
317- os . path . basename ( input_file_name ) ,
315+ mcu_file . name ,
318316 cubemx_db_version ,
319317 cubemx_db_release ,
320318 re .sub ("\\ .c$" , "" , out_c_filename ),
@@ -484,7 +482,7 @@ def print_adc():
484482 s_pin_data = "STM_PIN_DATA_EXT(STM_MODE_ANALOG"
485483 # For STM32L47xxx/48xxx, it is necessary to configure
486484 # the GPIOx_ASCR register
487- if re .match ("STM32L4[78]+" , mcu_file ):
485+ if re .match ("STM32L4[78]+" , mcu_file . stem ):
488486 s_pin_data += "_ADC_CONTROL"
489487 s_pin_data += ", GPIO_NOPULL, 0, "
490488
@@ -590,7 +588,7 @@ def print_uart(lst):
590588 # 2nd element is the UART_XX signal
591589 b = p [2 ].split ("_" )[0 ]
592590 s1 += "{:9}" .format ((b [: len (b ) - 1 ] + b [len (b ) - 1 :] + "," ))
593- if "STM32F10" in mcu_file and lst == uartrx_list :
591+ if "STM32F10" in mcu_file . stem and lst == uartrx_list :
594592 s1 += "STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLUP, "
595593 else :
596594 s1 += "STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, "
@@ -629,7 +627,7 @@ def print_can(lst):
629627 instance_number = instance_name .replace ("FD" , "" ).replace ("CAN" , "" )
630628 if len (instance_number ) == 0 :
631629 instance_name += "1"
632- if "STM32F10" in mcu_file and lst == canrd_list :
630+ if "STM32F10" in mcu_file . stem and lst == canrd_list :
633631 s1 += instance_name + ", STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, "
634632 else :
635633 s1 += instance_name + ", STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, "
@@ -987,7 +985,7 @@ def parse_pins():
987985
988986
989987# main
990- cur_dir = os . getcwd ()
988+ cur_dir = Path . cwd ()
991989out_c_filename = "PeripheralPins.c"
992990out_h_filename = "PinNamesVar.h"
993991config_filename = "config.json"
@@ -999,25 +997,25 @@ def parse_pins():
999997 config_file = open (config_filename , "w" , newline = "\n " )
1000998 if sys .platform .startswith ("win32" ):
1001999 print ("Platform is Windows" )
1002- cubemxdir = "C:\\ Program Files\\ STMicroelectronics\\ STM32Cube\\ STM32CubeMX"
1000+ cubemxdir = Path ( r "C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX")
10031001 elif sys .platform .startswith ("linux" ):
10041002 print ("Platform is Linux" )
1005- cubemxdir = os . getenv ( "HOME" ) + "/ STM32CubeMX"
1003+ cubemxdir = Path . home () / " STM32CubeMX"
10061004 elif sys .platform .startswith ("darwin" ):
10071005 print ("Platform is Mac OSX" )
1008- cubemxdir = (
1006+ cubemxdir = Path (
10091007 "/Applications/STMicroelectronics/STM32CubeMX.app/Contents/Resources"
10101008 )
10111009 else :
10121010 print ("Platform unknown" )
10131011 cubemxdir = "<Set CubeMX install directory>"
1014- config_file .write (json .dumps ({"CUBEMX_DIRECTORY" : cubemxdir }))
1012+ config_file .write (json .dumps ({"CUBEMX_DIRECTORY" : str ( cubemxdir ) }))
10151013 config_file .close ()
10161014 exit (1 )
10171015
10181016config = json .load (config_file )
10191017config_file .close ()
1020- cubemxdir = config ["CUBEMX_DIRECTORY" ]
1018+ cubemxdir = Path ( config ["CUBEMX_DIRECTORY" ])
10211019
10221020# by default, generate for all mcu xml files description
10231021parser = argparse .ArgumentParser (
@@ -1061,7 +1059,7 @@ def parse_pins():
10611059)
10621060args = parser .parse_args ()
10631061
1064- if not (os . path . isdir ( cubemxdir )):
1062+ if not (cubemxdir . is_dir ( )):
10651063 print ("\n Cube Mx seems not to be installed or not at the requested location." )
10661064 print (
10671065 "\n Please check the value you set for 'CUBEMX_DIRECTORY' in '{}' file." .format (
@@ -1070,12 +1068,12 @@ def parse_pins():
10701068 )
10711069 quit ()
10721070
1073- cubemxdirMCU = os . path . join ( cubemxdir , "db" , "mcu" )
1074- cubemxdirIP = os . path . join ( cubemxdirMCU , "IP" )
1075- version_file = os . path . join ( cubemxdir , "db" , "package.xml" )
1071+ cubemxdirMCU = cubemxdir / "db" / "mcu"
1072+ cubemxdirIP = cubemxdirMCU / "IP"
1073+ version_file = cubemxdir / "db" / "package.xml"
10761074cubemx_db_version = "Unknown"
10771075cubemx_db_release = "Unknown"
1078- xml_file = parse (version_file )
1076+ xml_file = parse (str ( version_file ) )
10791077Package_item = xml_file .getElementsByTagName ("Package" )
10801078for item in Package_item :
10811079 cubemx_db_version = item .attributes ["DBVersion" ].value
@@ -1086,54 +1084,45 @@ def parse_pins():
10861084
10871085if args .mcu :
10881086 # check input file exists
1089- if not (os . path . isfile ( os . path . join ( cubemxdirMCU , args .mcu ))):
1087+ if not (( cubemxdirMCU / args .mcu ). is_file ( )):
10901088 print ("\n " + args .mcu + " file not found" )
10911089 print ("\n Check in " + cubemxdirMCU + " the correct name of this file" )
10921090 print ("\n You may use double quotes for file containing special characters" )
10931091 quit ()
1094- mcu_list .append (args .mcu )
1092+ mcu_list .append (cubemxdirMCU / args .mcu )
10951093else :
1096- mcu_list = fnmatch . filter ( os . listdir ( cubemxdirMCU ), "STM32*.xml" )
1094+ mcu_list = cubemxdirMCU . glob ( "STM32*.xml" )
10971095
10981096if args .list :
10991097 print ("Available xml files description:" )
11001098 for f in mcu_list :
1101- print (f )
1099+ print (f . name )
11021100 quit ()
11031101
11041102for mcu_file in mcu_list :
11051103 print (
11061104 "Generating {} and {} for '{}'..." .format (
1107- out_c_filename , out_h_filename , mcu_file
1105+ out_c_filename , out_h_filename , mcu_file . name
11081106 )
11091107 )
1110- input_file_name = os .path .join (cubemxdirMCU , mcu_file )
1111- out_path = os .path .join (
1112- cur_dir ,
1113- "Arduino" ,
1114- os .path .splitext (mcu_file )[0 ][:7 ],
1115- os .path .splitext (mcu_file )[0 ],
1116- )
1117- output_c_filename = os .path .join (out_path , out_c_filename )
1118- output_h_filename = os .path .join (out_path , out_h_filename )
1119- if not (os .path .isdir (out_path )):
1120- os .makedirs (out_path )
1108+ out_path = cur_dir / "Arduino" / mcu_file .name [:7 ] / mcu_file .stem
1109+ output_c_filename = out_path / out_c_filename
1110+ output_h_filename = out_path / out_h_filename
1111+ out_path .mkdir (parents = True , exist_ok = True )
11211112
11221113 # open output file
1123- if os .path .isfile (output_c_filename ):
1124- os .remove (output_c_filename )
1114+ output_c_filename .unlink (missing_ok = True )
11251115 out_c_file = open (output_c_filename , "w" , newline = "\n " )
1126- if os .path .isfile (output_h_filename ):
1127- os .remove (output_h_filename )
1116+ output_h_filename .unlink (missing_ok = True )
11281117 out_h_file = open (output_h_filename , "w" , newline = "\n " )
11291118
11301119 # open input file
1131- xml_mcu = parse (input_file_name )
1120+ xml_mcu = parse (str ( mcu_file ) )
11321121 gpiofile = find_gpio_file ()
11331122 if gpiofile == "ERROR" :
11341123 print ("Could not find GPIO file" )
11351124 quit ()
1136- xml_gpio = parse (os . path . join (cubemxdirIP , "GPIO-" + gpiofile + "_Modes.xml" ))
1125+ xml_gpio = parse (str (cubemxdirIP / ( "GPIO-" + gpiofile + "_Modes.xml" ) ))
11371126
11381127 parse_pins ()
11391128 sort_my_lists ()
0 commit comments