Use generated header files in calibration.py

Change-Id: I970f2ba31adb2b6bfe6fcce4801941185d526e5e
This commit is contained in:
Henrik Wahlqvist 2025-03-19 09:46:38 +01:00
parent ef5cdd083f
commit 81640e5e0f
2 changed files with 123 additions and 56 deletions

View File

@ -3,6 +3,7 @@
"""Module for handling ZoneController calibration."""
import re
from pathlib import Path
from powertrain_build.problem_logger import ProblemLogger
@ -25,6 +26,7 @@ class ZoneControllerCalibration(ProblemLogger):
calib_data (dict): Dictionary containing calibration data for a ZoneController project.
"""
self.swc_name = build_cfg.get_composition_config("softwareComponentName")
self.asil_level = re.sub("ASIL(?=[^_])", "ASIL_", build_cfg.get_composition_config("asil"))
self.src_code_dst_dir = build_cfg.get_src_code_dst_dir()
self.calibration_variables = calib_data['class_info']
cal_interface_filename = f"calibration_interface_{build_cfg.get_scheduler_prefix()}".rstrip("_")
@ -57,10 +59,10 @@ class ZoneControllerCalibration(ProblemLogger):
return write_string_list
def _get_source_file_init_content(self):
header = [
f'#define {self.swc_name}_START_SEC_CODE\n',
f'#include "{self.swc_name}_MemMap.h"\n',
]
start_include = "CVC_CODE_START.h" if self.asil_level == "QM" else f"CVC_CODE_{self.asil_level}_START.h"
stop_include = "CVC_CODE_END.h" if self.asil_level == "QM" else f"CVC_CODE_{self.asil_level}_END.h"
header = [f'#include "{start_include}"\n']
footer = [f'#include "{stop_include}"\n', '\n']
body = [
f'void {self.calibration_function_init_template.format(swc_name=self.swc_name)}(void)\n',
@ -69,22 +71,15 @@ class ZoneControllerCalibration(ProblemLogger):
body.extend(self._get_calibration_variables_write_string_list())
body.append('}\n')
footer = [
f'#define {self.swc_name}_STOP_SEC_CODE\n',
f'#include "{self.swc_name}_MemMap.h"\n',
'\n'
]
return header + body + footer
def _get_source_file_step_content(self):
start_include = "CVC_CODE_START.h" if self.asil_level == "QM" else f"CVC_CODE_{self.asil_level}_START.h"
stop_include = "CVC_CODE_END.h" if self.asil_level == "QM" else f"CVC_CODE_{self.asil_level}_END.h"
header = [f'#include "{start_include}"\n']
footer = [f'#include "{stop_include}"\n']
trigger_read_calibration_function = f'Rte_CData_{self.swc_name}_{self.trigger_read_rte_cdata_signal_name}()'
header = [
f'#define {self.swc_name}_START_SEC_CODE\n',
f'#include "{self.swc_name}_MemMap.h"\n'
]
body = [
f'void {self.calibration_function_step_template.format(swc_name=self.swc_name)}(void)\n',
'{\n',
@ -95,11 +90,6 @@ class ZoneControllerCalibration(ProblemLogger):
body.append(' }\n')
body.append('}\n')
footer = [
f'#define {self.swc_name}_STOP_SEC_CODE\n',
f'#include "{self.swc_name}_MemMap.h"\n'
]
return header + body + footer
def get_header_file_content(self):
@ -109,6 +99,8 @@ class ZoneControllerCalibration(ProblemLogger):
(list(str)): List of lines to write to calibration header file.
"""
lines_to_write = []
start_include = "CVC_CAL_START.h" if self.asil_level == "QM" else f"CVC_CAL_{self.asil_level}_START.h"
stop_include = "CVC_CAL_END.h" if self.asil_level == "QM" else f"CVC_CAL_{self.asil_level}_END.h"
header = [
f'#ifndef {self._get_header_guard()}\n',
f'#define {self._get_header_guard()}\n',
@ -123,10 +115,7 @@ class ZoneControllerCalibration(ProblemLogger):
f'#endif /* {self._get_header_guard()} */\n'
]
lines_to_write.extend([
f'#define {self.swc_name}_START_SEC_VCC_CAL\n',
f'#include "{self.swc_name}_MemMap.h"\n'
])
lines_to_write.append(f'#include "{start_include}"\n')
for signal_name, signal_data in self.calibration_variables.items():
if isinstance(signal_data["width"], list):
rows, cols = signal_data["width"]
@ -137,10 +126,7 @@ class ZoneControllerCalibration(ProblemLogger):
else:
declaration = f'extern CVC_CAL {signal_data["type"]} {signal_name};\n'
lines_to_write.append(declaration)
lines_to_write.extend([
f'#define {self.swc_name}_STOP_SEC_VCC_CAL\n',
f'#include "{self.swc_name}_MemMap.h"\n'
])
lines_to_write.append(f'#include "{stop_include}"\n')
lines_to_write.append('\n')
for signal_name, signal_data in self.calibration_variables.items():
@ -168,15 +154,15 @@ class ZoneControllerCalibration(ProblemLogger):
Returns:
(list(str)): List of lines to write to calibration source file.
"""
start_include = "CVC_CAL_START.h" if self.asil_level == "QM" else f"CVC_CAL_{self.asil_level}_START.h"
stop_include = "CVC_CAL_END.h" if self.asil_level == "QM" else f"CVC_CAL_{self.asil_level}_END.h"
header = [
f'#include "{self.calibration_interface_header}"\n',
'\n',
f'#define {self.swc_name}_START_SEC_VCC_CAL\n',
f'#include "{self.swc_name}_MemMap.h"\n',
f'#include "{start_include}"\n',
f'CVC_CAL {self.trigger_read_rte_cdata_signal["data_type"]} '
f'{self.trigger_read_rte_cdata_signal_name} = 0;\n',
f'#define {self.swc_name}_STOP_SEC_VCC_CAL\n',
f'#include "{self.swc_name}_MemMap.h"\n',
f'#include "{stop_include}"\n',
'\n',
]

View File

@ -8,17 +8,33 @@ from unittest.mock import MagicMock
from powertrain_build.zone_controller.calibration import ZoneControllerCalibration
def mock_get_composition_config(key):
"""Function to mock BuildProjConfig.get_composition_config."""
return {
"asil": "QM",
"softwareComponentName": "testName_SC",
}[key]
def mock_get_composition_config_asil(key):
"""Function to mock BuildProjConfig.get_composition_config."""
return {
"asil": "ASILC",
"softwareComponentName": "testName_SC",
}[key]
class TestZoneControllerCalibration(TestCase):
"""Test case for testing composition_yaml."""
def setUp(self):
"""Set-up common data structures for all tests in the test case."""
build_cfg = MagicMock()
build_cfg.name = "XVC"
build_cfg.get_src_code_dst_dir.return_value = None
build_cfg.get_composition_config.return_value = "testName_SC"
build_cfg.get_scheduler_prefix.return_value = "suffix"
dummy_calib_data = {
self.build_cfg = MagicMock()
self.build_cfg.name = "XVC"
self.build_cfg.get_src_code_dst_dir.return_value = None
self.build_cfg.get_composition_config.side_effect = mock_get_composition_config
self.build_cfg.get_scheduler_prefix.return_value = "suffix"
self.dummy_calib_data = {
"class_info": {
"dummy_one": {
"type": "Float32",
@ -39,7 +55,7 @@ class TestZoneControllerCalibration(TestCase):
},
"data_types": {}
}
self.zone_controller_calibation = ZoneControllerCalibration(build_cfg, dummy_calib_data)
self.zone_controller_calibation = ZoneControllerCalibration(self.build_cfg, self.dummy_calib_data)
def test_get_header_file_content(self):
"""Test ZoneControllerCalibration.get_header_file_content."""
@ -52,15 +68,47 @@ class TestZoneControllerCalibration(TestCase):
'#include "tl_basetypes.h"\n',
'#include "Rte_testName_SC.h"\n',
"\n",
"#define testName_SC_START_SEC_VCC_CAL\n",
'#include "testName_SC_MemMap.h"\n',
'#include "CVC_CAL_START.h"\n',
"extern CVC_CAL Float32 dummy_one;\n",
"extern CVC_CAL UInt8 dummy_two;\n",
"extern CVC_CAL UInt8 dummy_three[2];\n",
"extern CVC_CAL UInt8 dummy_four[2][2];\n",
"extern CVC_CAL Float32 ctestName_SC_TriggerReadRteCData;\n",
"#define testName_SC_STOP_SEC_VCC_CAL\n",
'#include "testName_SC_MemMap.h"\n',
'#include "CVC_CAL_END.h"\n',
"\n",
"extern Float32 Rte_CData_testName_SC_dummy_one(void);\n",
"extern UInt8 Rte_CData_testName_SC_dummy_two(void);\n",
"extern const UInt8* Rte_CData_testName_SC_dummy_three(void);\n",
"extern const UInt8* Rte_CData_testName_SC_dummy_four(void);\n",
"extern Float32 Rte_CData_testName_SC_ctestName_SC_TriggerReadRteCData(void);\n",
"\n",
"void testName_SC_ZcCalibrationInit(void);\n",
"void testName_SC_ZcCalibrationStep(void);\n",
"\n",
"#endif /* CALIBRATION_INTERFACE_SUFFIX_H */\n"
]
self.assertListEqual(result, expected)
def test_get_header_file_content_asil(self):
"""Test ZoneControllerCalibration.get_header_file_content with ASIL level."""
self.build_cfg.get_composition_config.side_effect = mock_get_composition_config_asil
self.zone_controller_calibation = ZoneControllerCalibration(self.build_cfg, self.dummy_calib_data)
result = self.zone_controller_calibation.get_header_file_content()
expected = [
"#ifndef CALIBRATION_INTERFACE_SUFFIX_H\n",
"#define CALIBRATION_INTERFACE_SUFFIX_H\n",
"#define CVC_CAL\n",
'#include <string.h>\n',
'#include "tl_basetypes.h"\n',
'#include "Rte_testName_SC.h"\n',
"\n",
'#include "CVC_CAL_ASIL_C_START.h"\n',
"extern CVC_CAL Float32 dummy_one;\n",
"extern CVC_CAL UInt8 dummy_two;\n",
"extern CVC_CAL UInt8 dummy_three[2];\n",
"extern CVC_CAL UInt8 dummy_four[2][2];\n",
"extern CVC_CAL Float32 ctestName_SC_TriggerReadRteCData;\n",
'#include "CVC_CAL_ASIL_C_END.h"\n',
"\n",
"extern Float32 Rte_CData_testName_SC_dummy_one(void);\n",
"extern UInt8 Rte_CData_testName_SC_dummy_two(void);\n",
@ -81,14 +129,11 @@ class TestZoneControllerCalibration(TestCase):
expected = [
'#include "calibration_interface_suffix.h"\n',
"\n",
"#define testName_SC_START_SEC_VCC_CAL\n",
'#include "testName_SC_MemMap.h"\n',
'#include "CVC_CAL_START.h"\n',
"CVC_CAL Float32 ctestName_SC_TriggerReadRteCData = 0;\n",
"#define testName_SC_STOP_SEC_VCC_CAL\n",
'#include "testName_SC_MemMap.h"\n',
'#include "CVC_CAL_END.h"\n',
"\n",
"#define testName_SC_START_SEC_CODE\n",
'#include "testName_SC_MemMap.h"\n',
'#include "CVC_CODE_START.h"\n',
"void testName_SC_ZcCalibrationInit(void)\n",
"{\n",
" dummy_one = Rte_CData_testName_SC_dummy_one();\n",
@ -97,11 +142,9 @@ class TestZoneControllerCalibration(TestCase):
" memcpy(dummy_four, Rte_CData_testName_SC_dummy_four(), sizeof(dummy_four));\n",
" ctestName_SC_TriggerReadRteCData = Rte_CData_testName_SC_ctestName_SC_TriggerReadRteCData();\n",
"}\n",
"#define testName_SC_STOP_SEC_CODE\n",
'#include "testName_SC_MemMap.h"\n',
'#include "CVC_CODE_END.h"\n',
"\n",
"#define testName_SC_START_SEC_CODE\n",
'#include "testName_SC_MemMap.h"\n',
'#include "CVC_CODE_START.h"\n',
"void testName_SC_ZcCalibrationStep(void)\n",
"{\n",
" if (ctestName_SC_TriggerReadRteCData != Rte_CData_testName_SC_ctestName_SC_TriggerReadRteCData())\n",
@ -113,7 +156,45 @@ class TestZoneControllerCalibration(TestCase):
" ctestName_SC_TriggerReadRteCData = Rte_CData_testName_SC_ctestName_SC_TriggerReadRteCData();\n",
" }\n",
"}\n",
"#define testName_SC_STOP_SEC_CODE\n",
'#include "testName_SC_MemMap.h"\n'
'#include "CVC_CODE_END.h"\n'
]
self.assertListEqual(result, expected)
def test_get_source_file_content_asil(self):
"""Test ZoneControllerCalibration.get_source_file_content with ASIL level."""
self.build_cfg.get_composition_config.side_effect = mock_get_composition_config_asil
self.zone_controller_calibation = ZoneControllerCalibration(self.build_cfg, self.dummy_calib_data)
result = self.zone_controller_calibation.get_source_file_content()
expected = [
'#include "calibration_interface_suffix.h"\n',
"\n",
'#include "CVC_CAL_ASIL_C_START.h"\n',
"CVC_CAL Float32 ctestName_SC_TriggerReadRteCData = 0;\n",
'#include "CVC_CAL_ASIL_C_END.h"\n',
"\n",
'#include "CVC_CODE_ASIL_C_START.h"\n',
"void testName_SC_ZcCalibrationInit(void)\n",
"{\n",
" dummy_one = Rte_CData_testName_SC_dummy_one();\n",
" dummy_two = Rte_CData_testName_SC_dummy_two();\n",
" memcpy(dummy_three, Rte_CData_testName_SC_dummy_three(), sizeof(dummy_three));\n",
" memcpy(dummy_four, Rte_CData_testName_SC_dummy_four(), sizeof(dummy_four));\n",
" ctestName_SC_TriggerReadRteCData = Rte_CData_testName_SC_ctestName_SC_TriggerReadRteCData();\n",
"}\n",
'#include "CVC_CODE_ASIL_C_END.h"\n',
"\n",
'#include "CVC_CODE_ASIL_C_START.h"\n',
"void testName_SC_ZcCalibrationStep(void)\n",
"{\n",
" if (ctestName_SC_TriggerReadRteCData != Rte_CData_testName_SC_ctestName_SC_TriggerReadRteCData())\n",
" {\n",
" dummy_one = Rte_CData_testName_SC_dummy_one();\n",
" dummy_two = Rte_CData_testName_SC_dummy_two();\n",
" memcpy(dummy_three, Rte_CData_testName_SC_dummy_three(), sizeof(dummy_three));\n",
" memcpy(dummy_four, Rte_CData_testName_SC_dummy_four(), sizeof(dummy_four));\n",
" ctestName_SC_TriggerReadRteCData = Rte_CData_testName_SC_ctestName_SC_TriggerReadRteCData();\n",
" }\n",
"}\n",
'#include "CVC_CODE_ASIL_C_END.h"\n'
]
self.assertListEqual(result, expected)