22import re
33import os
44import datetime
5+ import json
56from xml .dom import minidom
67from xml .dom .minidom import parse , Node
78io_list = [] #'PIN','name'
@@ -543,6 +544,7 @@ def sort_my_lists():
543544# START MAIN PROGRAM
544545cur_dir = os .getcwd ()
545546out_filename = 'PeripheralPins.c'
547+ config_filename = 'config.json'
546548
547549if len (sys .argv ) < 3 :
548550 print ("Usage: " + sys .argv [0 ] + " <BOARD_NAME> <product xml file name>" )
@@ -561,47 +563,48 @@ def sort_my_lists():
561563 print (" for instance)" )
562564 quit ()
563565
564- if sys . platform . startswith ( 'win32' ) :
565- #print ("Windows env ")
566- cubemxdir = 'C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX\db\mcu'
567- cubemxdirIP = cubemxdir + " \\ IP \\ "
568- input_file_name = cubemxdir + " \\ " + sys . argv [ 2 ]
569- out_path = cur_dir + ' \\ Arduino \\ _' + sys .argv [ 1 ]
570- output_filename = out_path + " \\ " + out_filename
571- else :
572- #print ("Linux env")
573- if sys . platform . startswith ( 'linux' ):
566+ try :
567+ config_file = open ( config_filename , "r " )
568+ except IOError :
569+ print ( "Please set your configuration in %s file" % config_filename )
570+ config_file = open ( config_filename , "w" )
571+ if sys .platform . startswith ( 'win32' ):
572+ print ( "Platform is Windows" )
573+ cubemxdir = 'C: \\ Program Files \\ STMicroelectronics \\ STM32Cube \\ STM32CubeMX \\ db \\ mcu'
574+ elif sys . platform . startswith ( 'linux' ):
575+ print ( "Platform is Linux" )
574576 cubemxdir = os .getenv ("HOME" )+ '/STM32CubeMX/db/mcu'
575- cubemxdirIP = cubemxdir + "/IP/"
576- input_file_name = cubemxdir + '/' + sys .argv [2 ]
577- out_path = cur_dir + '/Arduino/' + sys .argv [1 ]
578- output_filename = out_path + '/' + out_filename
577+ elif sys .platform .startswith ('darwin' ):
578+ print ("Platform is Mac OSX" )
579+ cubemxdir = '/Applications/STMicroelectronics/STM32CubeMX.app/Contents/Resources/db/mcu'
579580 else :
580- #print ("Darwin env")
581- if sys .platform .startswith ('darwin' ):
582- print ("Platform is Mac OSX" )
583- cubemxdir = '/Applications/STMicroelectronics/STM32CubeMX.app/Contents/Resources/db/mcu'
584- cubemxdirIP = cubemxdir + "/IP/"
585- input_file_name = cubemxdir + '/' + sys .argv [2 ]
586- out_path = cur_dir + '/Arduino/' + sys .argv [1 ]
587- output_filename = out_path + '/' + out_filename
588- else :
589- print ("Unsupported OS" )
590- quit ()
581+ print ("Platform unknown" )
582+ cubemxdir = '<Set CubeMX install directory>/db/mcu'
583+ config_file .write (json .dumps ({"CUBEMX_DIRECTORY" :cubemxdir }))
584+ config_file .close ()
585+ exit (1 )
586+
587+ config = json .load (config_file )
588+ config_file .close ()
589+ cubemxdir = config ["CUBEMX_DIRECTORY" ]
590+
591+ cubemxdirIP = os .path .join (cubemxdir , 'IP' )
592+ input_file_name = os .path .join (cubemxdir , sys .argv [2 ])
593+ out_path = os .path .join (cur_dir , 'Arduino' , sys .argv [1 ])
594+ output_filename = os .path .join (out_path , out_filename )
591595
592- #open input file
593596#check input file exists
594597if not (os .path .isdir (cubemxdir )):
595598 print ("\n ! ! ! Cube Mx seems not to be installed or not at the requested location" )
596599 print ("\n ! ! ! please check the value you set for cubemxdir variable at the top of " + sys .argv [0 ] + " file" )
597600 quit ()
598601if not (os .path .isfile (input_file_name )):
599- print ('\n ! ! ! ' + sys .argv [2 ] + ' file not found' )
602+ print ('\n ! ! ! ' + sys .argv [2 ] + ' file not found' )
600603 print ("\n ! ! ! Check in " + cubemxdir + " the correct name of this file" )
601604 print ("\n ! ! ! You may use double quotes for this file if it contains special characters" )
602605 quit ()
603606
604-
607+ #open input file
605608print (" * * * Opening input file..." )
606609if not (os .path .isdir (out_path )):
607610 os .makedirs (out_path )
@@ -614,11 +617,10 @@ def sort_my_lists():
614617
615618out_file = open (output_filename , 'w' )
616619
617-
618620gpiofile = find_gpio_file (xmldoc )
619621if gpiofile == 'ERROR' :
620622 quit ()
621- xml = parse (cubemxdirIP + 'GPIO-' + gpiofile + '_Modes.xml' )
623+ xml = parse (os . path . join ( cubemxdirIP , 'GPIO-' + gpiofile + '_Modes.xml' ) )
622624print (" * * * Getting pins and Ips for the xml file..." )
623625pinregex = r'^(P[A-Z][0-9][0-5]?)'
624626for s in itemlist :
@@ -662,6 +664,7 @@ def sort_my_lists():
662664print (" * * * Printing lists..." )
663665print_header ()
664666print_all_lists ()
667+ out_file .close ()
665668
666669nb_pin = (len (io_list ))
667670print ("nb of I/O pins: %i" % nb_pin )
0 commit comments