1
1
import argparse
2
2
import datetime
3
- import fnmatch
4
3
import json
5
- import os
6
4
import re
7
5
import string
8
6
import sys
9
7
import textwrap
8
+ from pathlib import Path
10
9
from xml .dom .minidom import parse , Node
11
10
from argparse import RawTextHelpFormatter
12
11
13
- mcu_file = ""
14
12
mcu_list = [] # 'name'
15
13
io_list = [] # 'PIN','name'
16
14
alt_list = [] # 'PIN','name'
@@ -63,7 +61,7 @@ def find_gpio_file():
63
61
64
62
65
63
def get_gpio_af_num (pintofind , iptofind ):
66
- if "STM32F10" in mcu_file :
64
+ if "STM32F10" in mcu_file . stem :
67
65
return get_gpio_af_numF1 (pintofind , iptofind )
68
66
# DBG print ('pin to find ' + pintofind)
69
67
i = 0
@@ -314,7 +312,7 @@ def print_header():
314
312
*/
315
313
""" .format (
316
314
datetime .datetime .now ().year ,
317
- os . path . basename ( input_file_name ) ,
315
+ mcu_file . name ,
318
316
cubemx_db_version ,
319
317
cubemx_db_release ,
320
318
re .sub ("\\ .c$" , "" , out_c_filename ),
@@ -484,7 +482,7 @@ def print_adc():
484
482
s_pin_data = "STM_PIN_DATA_EXT(STM_MODE_ANALOG"
485
483
# For STM32L47xxx/48xxx, it is necessary to configure
486
484
# the GPIOx_ASCR register
487
- if re .match ("STM32L4[78]+" , mcu_file ):
485
+ if re .match ("STM32L4[78]+" , mcu_file . stem ):
488
486
s_pin_data += "_ADC_CONTROL"
489
487
s_pin_data += ", GPIO_NOPULL, 0, "
490
488
@@ -590,7 +588,7 @@ def print_uart(lst):
590
588
# 2nd element is the UART_XX signal
591
589
b = p [2 ].split ("_" )[0 ]
592
590
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 :
594
592
s1 += "STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLUP, "
595
593
else :
596
594
s1 += "STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, "
@@ -629,7 +627,7 @@ def print_can(lst):
629
627
instance_number = instance_name .replace ("FD" , "" ).replace ("CAN" , "" )
630
628
if len (instance_number ) == 0 :
631
629
instance_name += "1"
632
- if "STM32F10" in mcu_file and lst == canrd_list :
630
+ if "STM32F10" in mcu_file . stem and lst == canrd_list :
633
631
s1 += instance_name + ", STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, "
634
632
else :
635
633
s1 += instance_name + ", STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, "
@@ -987,7 +985,7 @@ def parse_pins():
987
985
988
986
989
987
# main
990
- cur_dir = os . getcwd ()
988
+ cur_dir = Path . cwd ()
991
989
out_c_filename = "PeripheralPins.c"
992
990
out_h_filename = "PinNamesVar.h"
993
991
config_filename = "config.json"
@@ -999,25 +997,25 @@ def parse_pins():
999
997
config_file = open (config_filename , "w" , newline = "\n " )
1000
998
if sys .platform .startswith ("win32" ):
1001
999
print ("Platform is Windows" )
1002
- cubemxdir = "C:\\ Program Files\\ STMicroelectronics\\ STM32Cube\\ STM32CubeMX"
1000
+ cubemxdir = Path ( r "C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX")
1003
1001
elif sys .platform .startswith ("linux" ):
1004
1002
print ("Platform is Linux" )
1005
- cubemxdir = os . getenv ( "HOME" ) + "/ STM32CubeMX"
1003
+ cubemxdir = Path . home () / " STM32CubeMX"
1006
1004
elif sys .platform .startswith ("darwin" ):
1007
1005
print ("Platform is Mac OSX" )
1008
- cubemxdir = (
1006
+ cubemxdir = Path (
1009
1007
"/Applications/STMicroelectronics/STM32CubeMX.app/Contents/Resources"
1010
1008
)
1011
1009
else :
1012
1010
print ("Platform unknown" )
1013
1011
cubemxdir = "<Set CubeMX install directory>"
1014
- config_file .write (json .dumps ({"CUBEMX_DIRECTORY" : cubemxdir }))
1012
+ config_file .write (json .dumps ({"CUBEMX_DIRECTORY" : str ( cubemxdir ) }))
1015
1013
config_file .close ()
1016
1014
exit (1 )
1017
1015
1018
1016
config = json .load (config_file )
1019
1017
config_file .close ()
1020
- cubemxdir = config ["CUBEMX_DIRECTORY" ]
1018
+ cubemxdir = Path ( config ["CUBEMX_DIRECTORY" ])
1021
1019
1022
1020
# by default, generate for all mcu xml files description
1023
1021
parser = argparse .ArgumentParser (
@@ -1061,7 +1059,7 @@ def parse_pins():
1061
1059
)
1062
1060
args = parser .parse_args ()
1063
1061
1064
- if not (os . path . isdir ( cubemxdir )):
1062
+ if not (cubemxdir . is_dir ( )):
1065
1063
print ("\n Cube Mx seems not to be installed or not at the requested location." )
1066
1064
print (
1067
1065
"\n Please check the value you set for 'CUBEMX_DIRECTORY' in '{}' file." .format (
@@ -1070,12 +1068,12 @@ def parse_pins():
1070
1068
)
1071
1069
quit ()
1072
1070
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"
1076
1074
cubemx_db_version = "Unknown"
1077
1075
cubemx_db_release = "Unknown"
1078
- xml_file = parse (version_file )
1076
+ xml_file = parse (str ( version_file ) )
1079
1077
Package_item = xml_file .getElementsByTagName ("Package" )
1080
1078
for item in Package_item :
1081
1079
cubemx_db_version = item .attributes ["DBVersion" ].value
@@ -1086,54 +1084,45 @@ def parse_pins():
1086
1084
1087
1085
if args .mcu :
1088
1086
# check input file exists
1089
- if not (os . path . isfile ( os . path . join ( cubemxdirMCU , args .mcu ))):
1087
+ if not (( cubemxdirMCU / args .mcu ). is_file ( )):
1090
1088
print ("\n " + args .mcu + " file not found" )
1091
1089
print ("\n Check in " + cubemxdirMCU + " the correct name of this file" )
1092
1090
print ("\n You may use double quotes for file containing special characters" )
1093
1091
quit ()
1094
- mcu_list .append (args .mcu )
1092
+ mcu_list .append (cubemxdirMCU / args .mcu )
1095
1093
else :
1096
- mcu_list = fnmatch . filter ( os . listdir ( cubemxdirMCU ), "STM32*.xml" )
1094
+ mcu_list = cubemxdirMCU . glob ( "STM32*.xml" )
1097
1095
1098
1096
if args .list :
1099
1097
print ("Available xml files description:" )
1100
1098
for f in mcu_list :
1101
- print (f )
1099
+ print (f . name )
1102
1100
quit ()
1103
1101
1104
1102
for mcu_file in mcu_list :
1105
1103
print (
1106
1104
"Generating {} and {} for '{}'..." .format (
1107
- out_c_filename , out_h_filename , mcu_file
1105
+ out_c_filename , out_h_filename , mcu_file . name
1108
1106
)
1109
1107
)
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 )
1121
1112
1122
1113
# 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 )
1125
1115
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 )
1128
1117
out_h_file = open (output_h_filename , "w" , newline = "\n " )
1129
1118
1130
1119
# open input file
1131
- xml_mcu = parse (input_file_name )
1120
+ xml_mcu = parse (str ( mcu_file ) )
1132
1121
gpiofile = find_gpio_file ()
1133
1122
if gpiofile == "ERROR" :
1134
1123
print ("Could not find GPIO file" )
1135
1124
quit ()
1136
- xml_gpio = parse (os . path . join (cubemxdirIP , "GPIO-" + gpiofile + "_Modes.xml" ))
1125
+ xml_gpio = parse (str (cubemxdirIP / ( "GPIO-" + gpiofile + "_Modes.xml" ) ))
1137
1126
1138
1127
parse_pins ()
1139
1128
sort_my_lists ()
0 commit comments