@@ -123,22 +123,21 @@ def generate_c_table(plli2s_table, hse, pllm):
123123 print ("}" )
124124
125125
126- def search_header (filename , re_include , re_define , lookup , val ):
127- regex_include = re .compile (re_include )
126+ def search_header (filename , re_define , lookup ):
128127 regex_define = re .compile (re_define )
128+ val = None
129129 with open (filename ) as f :
130130 for line in f :
131131 line = line .strip ()
132- m = regex_include .match (line )
133- if m :
134- # Search included file
135- search_header (m .group (1 ), re_include , re_define , lookup , val )
136- continue
137132 m = regex_define .match (line )
138133 if m :
139134 # found lookup value
135+ found = m .group (3 )
136+ if "*" in found or "/" in found :
137+ # process define using multiply or divide to calculate value
138+ found = eval (found )
140139 if m .group (1 ) == lookup :
141- val [ 0 ] = int (m . group ( 3 ) )
140+ val = int (found )
142141 return val
143142
144143
@@ -166,35 +165,37 @@ def main():
166165 break
167166
168167 if mcu_series in mcu_support_plli2s :
169- if len (argv ) != 2 :
168+ if len (argv ) not in ( 1 , 2 ) :
170169 print ("usage: pllvalues.py [-c] [-m <mcu_series>] <hse in MHz> <pllm in MHz>" )
171170 sys .exit (1 )
172171
173172 if argv [0 ].startswith ("hse:" ):
174- # extract HSE_VALUE from header file
175- (hse ,) = search_header (
176- argv [0 ][len ("hse:" ) :],
177- r'#include "(boards/[A-Za-z0-9_./]+)"' ,
178- r"#define +(HSE_VALUE) +\((\(uint32_t\))?([0-9]+)\)" ,
179- "HSE_VALUE" ,
180- [None ],
173+ hse = int (argv [0 ][len ("hse:" ) :])
174+
175+ if argv [0 ].startswith ("pllm:" ):
176+ pllm = int (argv [0 ][len ("pllm:" ) :])
177+
178+ if argv [0 ].startswith ("file:" ):
179+ # extract hse value from processed header files
180+ hse = search_header (
181+ argv [0 ][len ("file:" ) :],
182+ r"static.* (micropy_hw_hse_value) = +\(*(\(uint32_t\))?([0-9 +-/\*]+)\)*;" ,
183+ "micropy_hw_hse_value" ,
181184 )
182185 if hse is None :
183- raise ValueError ("%s does not contain a definition of HSE_VALUE" % argv [0 ])
184- argv .pop (0 )
186+ raise ValueError (
187+ "%s does not contain a definition of micropy_hw_hse_value" % argv [0 ]
188+ )
185189
186- if argv [0 ].startswith ("pllm:" ):
187- # extract MICROPY_HW_CLK_PLLM from header file
188- (pllm ,) = search_header (
189- argv [0 ][len ("pllm:" ) :],
190- r'#include "(boards/[A-Za-z0-9_./]+)"' ,
191- r"#define +(MICROPY_HW_CLK_PLLM) +\((\(uint32_t\))?([0-9]+)\)" ,
192- "MICROPY_HW_CLK_PLLM" ,
193- [None ],
190+ # extract pllm value from processed header files
191+ pllm = search_header (
192+ argv [0 ][len ("file:" ) :],
193+ r"static.* (micropy_hw_clk_pllm) = +\(*(\(uint32_t\))?([0-9 +-/\*]+)\)*;" ,
194+ "micropy_hw_clk_pllm" ,
194195 )
195196 if pllm is None :
196197 raise ValueError (
197- "%s does not contain a definition of MICROPY_HW_CLK_PLLM " % argv [0 ]
198+ "%s does not contain a definition of micropy_hw_clk_pllm " % argv [0 ]
198199 )
199200 argv .pop (0 )
200201
0 commit comments