From d214abb5845cf7d572cc6cbe547851a68e6a3727 Mon Sep 17 00:00:00 2001 From: Robofish <1683502971@qq.com> Date: Thu, 1 Jan 2026 17:15:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DFDCAN=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E8=B7=AF=E5=BE=84=EF=BC=8C=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E7=B1=BB=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复bsp_fdcan类的模板加载路径以支持新的文件夹结构 - 删除重复的bsp_fdcan类定义 - 确保从子文件夹正确加载FDCAN模板文件 --- app/code_page/bsp_interface.py | 106 ++++++--------------------------- 1 file changed, 18 insertions(+), 88 deletions(-) diff --git a/app/code_page/bsp_interface.py b/app/code_page/bsp_interface.py index 2b44b5f..f1d5de0 100644 --- a/app/code_page/bsp_interface.py +++ b/app/code_page/bsp_interface.py @@ -643,7 +643,15 @@ class bsp_fdcan(BspPeripheralBase): def _generate_header_file(self, configs, template_dir): """重写头文件生成,添加 FDCAN 使能和 FIFO 分配定义""" - template_path = os.path.join(template_dir, self.template_names['header']) + # 从子文件夹加载模板 + periph_folder = self.peripheral_name.lower() + template_base_dir = CodeGenerator.get_assets_dir("User_code/bsp") + template_path = os.path.join(template_base_dir, periph_folder, self.template_names['header']) + + if not os.path.exists(template_path): + # 向后兼容:尝试从根目录读取 + template_path = os.path.join(template_base_dir, self.template_names['header']) + template_content = CodeGenerator.load_template(template_path) if not template_content: return False @@ -681,7 +689,15 @@ class bsp_fdcan(BspPeripheralBase): return True def _generate_source_file(self, configs, template_dir): - template_path = os.path.join(template_dir, self.template_names['source']) + # 从子文件夹加载模板 + periph_folder = self.peripheral_name.lower() + template_base_dir = CodeGenerator.get_assets_dir("User_code/bsp") + template_path = os.path.join(template_base_dir, periph_folder, self.template_names['source']) + + if not os.path.exists(template_path): + # 向后兼容:尝试从根目录读取 + template_path = os.path.join(template_base_dir, self.template_names['source']) + template_content = CodeGenerator.load_template(template_path) if not template_content: return False @@ -819,92 +835,6 @@ class bsp_spi(BspPeripheralBase): ) -class bsp_fdcan(BspPeripheralBase): - def __init__(self, project_path): - super().__init__( - project_path, - "FDCAN", - {'header': 'fdcan.h', 'source': 'fdcan.c'}, - "BSP_FDCAN", - "hfdcan", - "fdcan", - get_available_fdcan - ) - - def _generate_header_file(self, configs, template_dir): - """生成FDCAN头文件,包含动态宏定义""" - template_path = os.path.join(template_dir, self.template_names['header']) - template_content = CodeGenerator.load_template(template_path) - if not template_content: - return False - - # 生成枚举 - enum_lines = [f" {self.enum_prefix}_{name}," for name, _ in configs] - content = CodeGenerator.replace_auto_generated( - template_content, f"AUTO GENERATED {self.enum_prefix}_NAME", "\n".join(enum_lines) - ) - - # 生成FDCAN实例使能宏定义 - macro_lines = [] - for name, instance in configs: - fdcan_num = ''.join(filter(str.isdigit, instance)) # 提取数字,如FDCAN1 -> 1 - macro_lines.append(f"#define {instance}_EN") - - # 替换宏定义区域 - content = CodeGenerator.replace_auto_generated( - content, "AUTO GENERATED FDCAN_EN", "\n".join(macro_lines) - ) - - # 生成FIFO配置宏定义 - fifo_lines = [] - fdcan_count = len(configs) - for idx, (name, instance) in enumerate(configs): - fdcan_num = ''.join(filter(str.isdigit, instance)) - # FDCAN1使用FIFO0,其他使用FIFO1 - if instance == 'FDCAN1': - fifo_lines.append(f"#define {instance}_RX_FIFO 0") - else: - fifo_lines.append(f"#define {instance}_RX_FIFO 1") - - content = CodeGenerator.replace_auto_generated( - content, "AUTO GENERATED FDCAN_RX_FIFO", "\n".join(fifo_lines) - ) - - output_path = os.path.join(self.project_path, f"User/bsp/{self.template_names['header']}") - CodeGenerator.save_with_preserve(output_path, content) - return True - - def _generate_source_file(self, configs, template_dir): - """生成FDCAN源文件""" - template_path = os.path.join(template_dir, self.template_names['source']) - template_content = CodeGenerator.load_template(template_path) - if not template_content: - return False - - # Get函数 - get_lines = [] - for idx, (name, instance) in enumerate(configs): - get_lines.append(f" case {idx}: return {self.enum_prefix}_{name};") - content = CodeGenerator.replace_auto_generated( - template_content, "AUTO GENERATED FDCAN_GET", "\n".join(get_lines) - ) - - # Handle函数 - handle_lines = [] - for name, instance in configs: - fdcan_num = ''.join(filter(str.isdigit, instance)) - handle_lines.append(f"#ifdef {instance}_EN") - handle_lines.append(f" case {self.enum_prefix}_{name}: return &{self.handle_prefix}{fdcan_num};") - handle_lines.append(f"#endif") - content = CodeGenerator.replace_auto_generated( - content, f"AUTO GENERATED {self.enum_prefix}_GET_HANDLE", "\n".join(handle_lines) - ) - - output_path = os.path.join(self.project_path, f"User/bsp/{self.template_names['source']}") - CodeGenerator.save_with_preserve(output_path, content) - return True - - def patch_uart_interrupts(project_path, uart_instances): """自动修改 stm32f4xx_it.c,插入 UART BSP 相关代码""" it_path = os.path.join(project_path, "Core/Src/stm32f4xx_it.c")