2
2
import re
3
3
import os
4
4
import datetime
5
+ import json
5
6
from xml .dom import minidom
6
7
from xml .dom .minidom import parse , Node
7
8
io_list = [] #'PIN','name'
@@ -543,6 +544,7 @@ def sort_my_lists():
543
544
# START MAIN PROGRAM
544
545
cur_dir = os .getcwd ()
545
546
out_filename = 'PeripheralPins.c'
547
+ config_filename = 'config.json'
546
548
547
549
if len (sys .argv ) < 3 :
548
550
print ("Usage: " + sys .argv [0 ] + " <BOARD_NAME> <product xml file name>" )
@@ -561,47 +563,48 @@ def sort_my_lists():
561
563
print (" for instance)" )
562
564
quit ()
563
565
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" )
574
576
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'
579
580
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 )
591
595
592
- #open input file
593
596
#check input file exists
594
597
if not (os .path .isdir (cubemxdir )):
595
598
print ("\n ! ! ! Cube Mx seems not to be installed or not at the requested location" )
596
599
print ("\n ! ! ! please check the value you set for cubemxdir variable at the top of " + sys .argv [0 ] + " file" )
597
600
quit ()
598
601
if 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' )
600
603
print ("\n ! ! ! Check in " + cubemxdir + " the correct name of this file" )
601
604
print ("\n ! ! ! You may use double quotes for this file if it contains special characters" )
602
605
quit ()
603
606
604
-
607
+ #open input file
605
608
print (" * * * Opening input file..." )
606
609
if not (os .path .isdir (out_path )):
607
610
os .makedirs (out_path )
@@ -614,11 +617,10 @@ def sort_my_lists():
614
617
615
618
out_file = open (output_filename , 'w' )
616
619
617
-
618
620
gpiofile = find_gpio_file (xmldoc )
619
621
if gpiofile == 'ERROR' :
620
622
quit ()
621
- xml = parse (cubemxdirIP + 'GPIO-' + gpiofile + '_Modes.xml' )
623
+ xml = parse (os . path . join ( cubemxdirIP , 'GPIO-' + gpiofile + '_Modes.xml' ) )
622
624
print (" * * * Getting pins and Ips for the xml file..." )
623
625
pinregex = r'^(P[A-Z][0-9][0-5]?)'
624
626
for s in itemlist :
@@ -662,6 +664,7 @@ def sort_my_lists():
662
664
print (" * * * Printing lists..." )
663
665
print_header ()
664
666
print_all_lists ()
667
+ out_file .close ()
665
668
666
669
nb_pin = (len (io_list ))
667
670
print ("nb of I/O pins: %i" % nb_pin )
0 commit comments