Skip to content

Commit 1a91da2

Browse files
committed
scripts: stm32wrapper: use jinja2 formatting
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent bf2d081 commit 1a91da2

File tree

4 files changed

+140
-65
lines changed

4 files changed

+140
-65
lines changed

CI/utils/stm32wrapper.py

Lines changed: 89 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import re
3+
from jinja2 import Environment, FileSystemLoader, Template
34
from pathlib import Path
45
from stm32common import createFolder, deleteFolder, genSTM32List
56

@@ -26,10 +27,28 @@
2627
# Out startup files
2728
CMSIS_Startupfile = ""
2829

29-
all_LL_file = "stm32yyxx_ll.h"
30-
30+
# List of STM32 series
3131
stm32_series = []
3232

33+
# Templating
34+
templates_dir = script_path / "templates"
35+
all_ll_h_file = "stm32yyxx_ll.h"
36+
ll_h_file = "stm32yyxx_ll_ppp.h"
37+
c_file = "stm32yyxx_zz_ppp.c"
38+
39+
# Create the jinja2 environment.
40+
j2_env = Environment(
41+
loader=FileSystemLoader(str(templates_dir)), trim_blocks=True, lstrip_blocks=True
42+
)
43+
all_ll_header_file_template = j2_env.get_template(all_ll_h_file)
44+
ll_h_file_template = j2_env.get_template(ll_h_file)
45+
c_file_template = j2_env.get_template(c_file)
46+
dsp_file_template = Template('#include "../Source/{{ dsp }}/{{ dsp }}.c"')
47+
48+
# re
49+
peripheral_c_regex = re.compile(r"stm32\w+_[h]?[al][l]_(.*).c$")
50+
peripheral_h_regex = re.compile(r"stm32\w+_(\w+).h$")
51+
3352

3453
def checkConfig(arg_core, arg_cmsis):
3554
global core_path
@@ -69,24 +88,6 @@ def checkConfig(arg_core, arg_cmsis):
6988
CMSIS_DSPSrc_path = CMSIS_path / "CMSIS" / "DSP" / "Source"
7089

7190

72-
# Add some pragma to ll header files to avoid several warnings
73-
# which will be corrected along Cube update
74-
def print_LL_header(open_file, name):
75-
upper = name.upper().replace(".", "_")
76-
open_file.write(
77-
"""#ifndef _{0}_
78-
#define _{0}_
79-
/* LL raised several warnings, ignore them */
80-
#pragma GCC diagnostic push
81-
#pragma GCC diagnostic ignored \"-Wunused-parameter\"
82-
#pragma GCC diagnostic ignored \"-Wstrict-aliasing\"
83-
84-
""".format(
85-
upper
86-
)
87-
)
88-
89-
9091
def printCMSISStartup(log):
9192
filelist = sorted(CMSIS_Device_ST_path.glob("**/startup_*.s"))
9293
if len(filelist):
@@ -159,7 +160,11 @@ def wrap(arg_core, arg_cmsis, log):
159160
createFolder(LLoutInc_path)
160161
if CMSIS_Startupfile.is_file():
161162
CMSIS_Startupfile.unlink()
162-
full_ll_list = []
163+
all_ll_h_list = []
164+
# key: peripheral, value: serie list
165+
ll_h_dict = {}
166+
ll_c_dict = {}
167+
hal_c_dict = {}
163168
# Search all files for each series
164169
for serie in stm32_series:
165170
src = HALDrivers_path / ("STM32" + serie + "xx_HAL_Driver") / "Src"
@@ -169,64 +174,83 @@ def wrap(arg_core, arg_cmsis, log):
169174
if log:
170175
print("Generating for " + serie + "...")
171176
lower = serie.lower()
172-
# Generate stm32yyxx_[hal|ll]*.c file
177+
178+
# Search stm32yyxx_[hal|ll]*.c file
173179
filelist = src.glob("stm32" + lower + "xx_*.c")
174180
for fp in filelist:
175181
# File name
176182
fn = fp.name
183+
found = peripheral_c_regex.match(fn)
177184
if "_template" in fn:
178185
continue
179-
outp = HALoutSrc_path
186+
peripheral = found.group(1) if found else "hal"
180187
if "_ll_" in fn:
181-
outp = LLoutSrc_path
182-
# Compute generic file name with path
183-
gp = outp / fn.replace(lower, "yy")
184-
out_file = open(gp, "a", newline="\n")
185-
# Amend file name under serie switch
186-
out_file.write("#ifdef STM32" + serie + "xx\n")
187-
out_file.write(' #include "' + fn + '"\n')
188-
out_file.write("#endif\n")
189-
out_file.close()
190-
# Generate stm32yyxx_ll_*.h file
188+
if peripheral in ll_c_dict:
189+
ll_c_dict[peripheral].append(lower)
190+
else:
191+
ll_c_dict[peripheral] = [lower]
192+
else:
193+
if peripheral in hal_c_dict:
194+
hal_c_dict[peripheral].append(lower)
195+
else:
196+
hal_c_dict[peripheral] = [lower]
197+
198+
# Search stm32yyxx_ll_*.h file
191199
filelist = inc.glob("stm32" + lower + "xx_ll_*.h")
192200
for fp in filelist:
193-
outp = LLoutInc_path
194201
# File name
195202
fn = fp.name
196-
# Compute generic file name
197-
gn = fn.replace(lower, "yy")
198-
# with path
199-
gp = outp / gn
200-
out_file = open(gp, "a", newline="\n")
201-
if gp.stat().st_size == 0:
202-
print_LL_header(out_file, gn)
203-
# Amend full LL header file
204-
full_ll_list.append(gn)
205-
# Amend file name under serie switch
206-
out_file.write("#ifdef STM32" + serie + "xx\n")
207-
out_file.write(' #include "' + fn + '"\n')
208-
out_file.write("#endif\n")
203+
found = peripheral_h_regex.match(fn)
204+
if not found:
205+
continue
206+
peripheral = found.group(1)
207+
# Amend all LL header list
208+
all_ll_h_list.append(fn.replace(lower, "yy"))
209+
if peripheral in ll_h_dict:
210+
ll_h_dict[peripheral].append(lower)
211+
else:
212+
ll_h_dict[peripheral] = [lower]
213+
214+
# Generate stm32yyxx_hal_*.c file
215+
for key, value in hal_c_dict.items():
216+
if key == "hal":
217+
filepath = HALoutSrc_path / c_file.replace("zz", "hal").replace(
218+
"_ppp", ""
219+
)
220+
else:
221+
filepath = HALoutSrc_path / c_file.replace("zz", "hal").replace(
222+
"ppp", key
223+
)
224+
out_file = open(filepath, "w", newline="\n")
225+
out_file.write(
226+
c_file_template.render(periph=key, type="hal", serieslist=value)
227+
)
228+
out_file.close()
229+
# Generate stm32yyxx_ll_*.c file
230+
for key, value in ll_c_dict.items():
231+
filepath = LLoutSrc_path / c_file.replace("zz", "ll").replace(
232+
"ppp", key
233+
)
234+
out_file = open(filepath, "w", newline="\n")
235+
out_file.write(
236+
c_file_template.render(periph=key, type="ll", serieslist=value)
237+
)
238+
out_file.close()
239+
# Generate stm32yyxx_ll_*.h file
240+
for key, value in ll_h_dict.items():
241+
filepath = LLoutInc_path / ll_h_file.replace("ppp", key)
242+
out_file = open(filepath, "w", newline="\n")
243+
out_file.write(ll_h_file_template.render(periph=key, serieslist=value))
209244
out_file.close()
210245
if log:
211246
print("done")
212247

213-
# Filter full LL header file
214-
full_ll_file = open(LLoutInc_path / all_LL_file, "w", newline="\n")
215-
print_LL_header(full_ll_file, all_LL_file)
216-
full_ll_file.write("/* Include Low Layers drivers */\n")
217-
full_ll_list = sorted(set(full_ll_list))
218-
for hn in full_ll_list:
219-
full_ll_file.write('#include "' + hn + '"\n')
220-
full_ll_file.close()
221-
222-
# Search all LL header files to end guard
223-
filelist = LLoutInc_path.glob("stm32yyxx_ll*.h")
224-
for fp in filelist:
225-
out_file = open(fp, "a", newline="\n")
226-
upper = fp.name.upper().replace(".", "_")
227-
out_file.write("#pragma GCC diagnostic pop\n")
228-
out_file.write("#endif /* _" + upper + "_ */\n")
229-
out_file.close()
248+
# Filter all LL header file
249+
all_ll_h_list = sorted(set(all_ll_h_list))
250+
# Generate the all LL header file
251+
all_ll_file = open(LLoutInc_path / all_ll_h_file, "w", newline="\n")
252+
all_ll_file.write(all_ll_header_file_template.render(ll_header_list=all_ll_h_list))
253+
all_ll_file.close()
230254

231255
# CMSIS startup files
232256
printCMSISStartup(log)
@@ -249,7 +273,7 @@ def wrap(arg_core, arg_cmsis, log):
249273
if not fdn.is_dir():
250274
createFolder(fdn)
251275
out_file = open(fdn / (dn + ".c"), "w", newline="\n")
252-
out_file.write('#include "../Source/{0}/{0}.c"\n'.format(dn))
276+
all_ll_file.write(dsp_file_template.render(dsp_path=dn))
253277
out_file.close()
254278
return 0
255279

CI/utils/templates/stm32yyxx_ll.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef _STM32YYXX_LL_H_
2+
#define _STM32YYXX_LL_H_
3+
/* LL raised several warnings, ignore them */
4+
#pragma GCC diagnostic push
5+
#pragma GCC diagnostic ignored "-Wunused-parameter"
6+
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
7+
8+
/* Include Low Layers drivers */
9+
{% for ll_header_name in ll_header_list %}
10+
#include "{{ll_header_name}}"
11+
{% endfor %}
12+
#pragma GCC diagnostic pop
13+
#endif /* _STM32YYXX_LL_H_ */
14+

CI/utils/templates/stm32yyxx_ll_ppp.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef _STM32YYXX_LL_{{periph.upper()}}_H_
2+
#define _STM32YYXX_LL_{{periph.upper()}}_H_
3+
/* LL raised several warnings, ignore them */
4+
#pragma GCC diagnostic push
5+
#pragma GCC diagnostic ignored "-Wunused-parameter"
6+
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
7+
8+
{% for serie in serieslist %}
9+
{% if loop.first %}
10+
#ifdef STM32{{serie.upper()}}xx
11+
{% else %}
12+
#elif STM32{{serie.upper()}}xx
13+
{% endif %}
14+
#include "stm32{{serie}}xx_ll_{{periph}}.h"
15+
{% if loop.last %}
16+
#endif
17+
{% endif %}
18+
{% endfor %}
19+
#pragma GCC diagnostic pop
20+
#endif /* _STM32YYXX_LL_{{periph.upper()}}_H_ */
21+

CI/utils/templates/stm32yyxx_zz_ppp.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% for serie in serieslist %}
2+
{% if loop.first %}
3+
#ifdef STM32{{serie.upper()}}xx
4+
{% else %}
5+
#elif STM32{{serie.upper()}}xx
6+
{% endif %}
7+
{% if type == periph %}
8+
#include "stm32{{serie}}xx_{{type}}.c"
9+
{% else %}
10+
#include "stm32{{serie}}xx_{{type}}_{{periph}}.c"
11+
{% endif %}
12+
{% if loop.last %}
13+
#endif
14+
{% endif %}
15+
{% endfor %}
16+

0 commit comments

Comments
 (0)