diff --git a/source/dev_LaminaAdapter.py b/source/dev_LaminaAdapter.py index 09377ad..a5adfd9 100644 --- a/source/dev_LaminaAdapter.py +++ b/source/dev_LaminaAdapter.py @@ -1,10 +1,8 @@ import time from pathlib import Path +from typing import Callable, Any from device.LaminaAdapter import LaminaAdapter -from device.LaminaAdapter import GeneratePackage_SLCP101_p460 -from device.LaminaAdapter import GenerateImage_SLCP101_p460 -from device.LaminaAdapter import GeneratePackage_DLSY001_p460 -from device.LaminaAdapter import GenerateImage_DLSY001_p460 +from device.LaminaAdapter import GeneratePackage_SLCP001_p4a0, GeneratePackage_SLCP101_p460, GeneratePackage_DLSY001_p460 from device.tools.ByteConv import trans_list_to_str def test_communication(time_out=2): @@ -30,14 +28,14 @@ def test_communication(time_out=2): dev_lamina.flag_print, dev_lamina.retry, dev_lamina.time_out = param_saved -def make_Pakeage(fp: Path): +def make_Pakeage(fp: Path, func): """ 生成升级包 """ hex_update = fp file_package = fp.parent / f'{hex_update.stem}.dat' file_update_bin = fp.parent / f'{hex_update.stem}.bin' - data_package, data_update_bin = GeneratePackage_DLSY001_p460(hex_update) + data_package, data_update_bin = func(hex_update) file_package.write_bytes(data_package) file_update_bin.write_bytes(data_update_bin) @@ -45,97 +43,6 @@ def make_Pakeage(fp: Path): return file_package -def Process1(): - """ 适配器镜像生成流程 """ - root = Path(r"test\p460") - result = Path(r"test\p460\result") - # 正常启动镜像 - hex_boot = Path(r"D:\WorkingProject\LightStackAdapter\software\umon\460-PROJ_STACKLIGHT_PARALLEL_ADAPTOR\bootloader.hex") - hex_main = Path(r"D:\WorkingProject\LightStackAdapter\software\lamina_adapter\lamina_adapter\Debug\lamina_adapter.hex") - hex_back = root / r"lamina_adapter_back.hex" - hex_update = hex_main - - file_image = result / f'{hex_main.stem}_ROM.bin' - file_main_header = result / 'SLCP101_header_main.bin' - file_back_header = result / 'SLCP101_header_back.bin' - file_package = result / f'{hex_update.stem}.dat' - file_bin = result / f'{hex_update.stem}.bin' - - data_bins = GenerateImage_SLCP101_p460(hex_boot, hex_main, hex_back) - data_package, data_bin = GeneratePackage_SLCP101_p460(hex_update) - - file_image.write_bytes(data_bins[0].copy()) - file_main_header.write_bytes(data_bins[1].copy()) - file_back_header.write_bytes(data_bins[2].copy()) - file_package.write_bytes(data_package) - file_bin.write_bytes(data_bin) - - # 异常镜像-主分区md5错误 - file_image1 = result / f'{file_image.stem}_b1.bin' - data_image = data_bins[0].copy() - data_image[0x054018: 0x05401A] = [0x00, 0x01] - file_image1.write_bytes(data_image) - - # 异常镜像-备份分区md5错误 - file_image2 = result / f'{file_image.stem}_b2.bin' - data_image = data_bins[0].copy() - data_image[0x056018: 0x05601A] = [0x00, 0x01] - file_image2.write_bytes(data_image) - - # 异常镜像-双分区md5错误 - file_image3 = result / f'{file_image.stem}_b3.bin' - data_image = data_bins[0].copy() - data_image[0x054018: 0x05401A] = [0x00, 0x01] - data_image[0x056018: 0x05601A] = [0x00, 0x01] - file_image3.write_bytes(data_image) - - -def Process2(): - """ 优化镜像生成流程 """ - root = Path(r"D:\WorkingProject\LightStackOptimizer\software\lamina_optimizer\lamina_optimizer\Debug") - # result = Path(r"test\p460_o1\result") - result = root - # 正常启动镜像 - hex_boot = Path(r"test\p460_o1\bootloader.hex") - hex_main = root / r"lamina_optimizer.hex" - hex_back = root / r"lamina_optimizer.hex" - hex_update = root / r"lamina_optimizer.hex" - - file_image = result / f'{hex_main.stem}_ROM.bin' - file_main_header = result / 'DLSY001_header_main.bin' - file_back_header = result / 'DLSY001_header_back.bin' - file_package = result / f'{hex_update.stem}.dat' - file_bin = result / f'{hex_update.stem}.bin' - - data_bins = GenerateImage_DLSY001_p460(hex_boot, hex_main, hex_back) - data_package, data_bin = GeneratePackage_DLSY001_p460(hex_update) - - file_image.write_bytes(data_bins[0].copy()) - file_main_header.write_bytes(data_bins[1].copy()) - file_back_header.write_bytes(data_bins[2].copy()) - file_package.write_bytes(data_package) - file_bin.write_bytes(data_bin) - - # 异常镜像-主分区md5错误 - file_image1 = result / f'{file_image.stem}_b1.bin' - data_image = data_bins[0].copy() - data_image[0x054018: 0x05401A] = [0x00, 0x01] - file_image1.write_bytes(data_image) - - # 异常镜像-备份分区md5错误 - file_image2 = result / f'{file_image.stem}_b2.bin' - data_image = data_bins[0].copy() - data_image[0x056018: 0x05601A] = [0x00, 0x01] - file_image2.write_bytes(data_image) - - # 异常镜像-双分区md5错误 - file_image3 = result / f'{file_image.stem}_b3.bin' - data_image = data_bins[0].copy() - data_image[0x054018: 0x05401A] = [0x00, 0x01] - data_image[0x056018: 0x05601A] = [0x00, 0x01] - file_image3.write_bytes(data_image) - - def test(): if 0: dev_lamina.frame_read(0xA9, 1) # 读ADC参考电压 @@ -239,10 +146,6 @@ if __name__=='__main__': 'time_out': 3, 'time_gap': 0.1, 'retry': 3, 'retry_sub': 10}, } - # Process1() - # Process2() - # exit(0) - dev_lamina = LaminaAdapter(**mode_config['Debug']) dev_lamina.frame_read(0x0100, 0x20) @@ -250,17 +153,17 @@ if __name__=='__main__': if not hasattr(__builtins__,"__IPYTHON__"): # 工程-即时转换 - # file_hex = Path(r"D:\WorkingProject\LightStackAdapter\software\lamina_adapter\lamina_adapter\Debug\lamina_adapter.hex") - file_hex = Path(r"D:\WorkingProject\LightStackOptimizer\software\lamina_optimizer\lamina_optimizer\Debug\lamina_optimizer.hex") + file_hex = Path(r"D:\WorkingProject\LightStackAdapter\software\lamina_adapter\lamina_adapter\Debug\lamina_adapter.hex") + # file_hex = Path(r"D:\WorkingProject\LightStackOptimizer\software\lamina_optimizer\lamina_optimizer\Debug\lamina_optimizer.hex") if not file_hex.exists(): raise Exception("工程编译目标文件不存在.") - file_package = make_Pakeage(file_hex) + file_package = make_Pakeage(file_hex, GeneratePackage_SLCP101_p460) - version = "DLSY001_241030_1430_V1.02" + version = "SLCP101_241030_2000_V2.03" addr = [0x24, 0x09, 0x12, 0x00, 0x00, 0x00] while True: - """ 自动检测升级流程(需要调试) """ + """ 自动检测升级流程 """ ret = False while not ret or ('Regs' not in dev_lamina.output.keys()) or (version == dev_lamina.output['Regs'][0x0100][1].strip('\000')): # dev_lamina.frame_read(0x82, 3) diff --git a/source/device/DeviceSerial.py b/source/device/DeviceSerial.py index 25c24fc..0f94ae4 100644 --- a/source/device/DeviceSerial.py +++ b/source/device/DeviceSerial.py @@ -6,6 +6,7 @@ from . import function class DeviceSerial: """ 串口通信设备原型 + Note: 串口资源释放与重复开启 """ def __init__(self, com_name="COM1", **kwargs): """ 初始化设备 """ diff --git a/source/device/LaminaAdapter.py b/source/device/LaminaAdapter.py index b942280..f1e8fb6 100644 --- a/source/device/LaminaAdapter.py +++ b/source/device/LaminaAdapter.py @@ -106,12 +106,13 @@ ParamMap_LaminaAdapter = { 0xAD: ["保留", 1], 0xAE: ["保留", 1], 0xAF: ["保留", 1], - 0x100: ["版本", 4, 16], + 0xB0: ["版本", 4, 16], + 0x100: ["版本(ODM)", 4, 16], 0x110: ["型号", 4, 16], 0x120: ["载波芯片地址", 4, 16], 0x130: ["厂商", 4, 8], - 0x138: ["ODM版本", 4, 16], - 0x148: ["保留", 4, 8], + 0x138: ["保留", 4, 8], + 0x140: ["保留", 4, 16], 0x150: ["保留", 4, 16], 0x160: ["硬件", 4, 16], 0x170: ["SN", 4, 16], @@ -304,6 +305,41 @@ class LaminaAdapter: return True +def GeneratePackage_SLCP001_p4a0(path_hex: Path): + """ 叠光适配器-460平台版本 生成升级包 """ + config = { + 'prod_type': [0x45, 0x00], # 产品类型 + 'method_compress': False, # 文件压缩 + 'prog_id': list(b"SLCP001"), # 程序识别号 + 'prog_type': 'app', # 程序类型 + 'area_code': [0x00, 0x00], # 地区 + } + bin_main = tools.IntelHex.file_IntelHex_to_Bin(path_hex.read_text(), len_max=0x040000) + encrypt_main = function.file_upgrade.file_encryption(bin_main) + + md5_ctx = hashlib.md5() + md5_ctx.update(bin_main) + config["md5"] = list(md5_ctx.digest()) + config['file_length'] = tools.ByteConv.conv_int_to_array(len(bin_main)) + config['hex_name'] = list(path_hex.name.encode())[:64] + config['hex_name'] += [0] * (64 - len(config['hex_name'])) + if (main_header:=function.file_upgrade.build_header_new(config)) is None: + raise Exception("Header tag oversize. ") + + print("Package generated successfully.") + print(f"File name: {path_hex.name}") + print(f"File Info:") + print(f"\t header_length={len(main_header)}, bin_length={len(bin_main)}[{hex(len(bin_main))}]") + + # 组装镜像 + Image = [0xFF] * (len(main_header) + len(encrypt_main)) + offset_image = 0 + Image[offset_image: offset_image + len(main_header)] = main_header + offset_image += len(main_header) + Image[offset_image: offset_image + len(encrypt_main)] = encrypt_main + + return bytearray(Image), bin_main + def GeneratePackage_SLCP101_p460(path_hex: Path): """ 叠光适配器-460平台版本 生成升级包 """ config = { diff --git a/source/post_work.py b/source/post_work.py new file mode 100644 index 0000000..14819ca --- /dev/null +++ b/source/post_work.py @@ -0,0 +1,158 @@ +from pathlib import Path +from device.LaminaAdapter import GenerateImage_SLCP001_p4a0, GeneratePackage_SLCP001_p4a0 +from device.LaminaAdapter import GenerateImage_SLCP101_p460, GeneratePackage_SLCP101_p460 +from device.LaminaAdapter import GenerateImage_DLSY001_p460, GeneratePackage_DLSY001_p460 + + +def Process0(path_boot: Path, path_project: Path): + """ 适配器-SLCP001 镜像生成流程 """ + root_boot = path_boot + root_main = path_project + result = root_main + + # 正常启动镜像 + hex_boot = root_boot / "bootloader.hex" + hex_main = root_main / "lamina_adapter.hex" + hex_back = root_main / "lamina_adapter_back.hex" + hex_update = hex_main + if (not hex_boot.exists()) or (not hex_main.exists()) or (not hex_back.exists()): + raise Exception("缺失必要程序文件") + + file_image = result / f'{hex_main.stem}_ROM.bin' + file_main_header = result / 'SLCP001_main.header' + file_back_header = result / 'SLCP001_back.header' + file_package = result / f'{hex_update.stem}.dat' + file_bin = result / f'{hex_update.stem}.bin' + + data_bins = GenerateImage_SLCP001_p4a0(hex_boot, hex_main, hex_back) + data_package, data_bin = GeneratePackage_SLCP001_p4a0(hex_update) + + file_image.write_bytes(data_bins[0].copy()) + file_main_header.write_bytes(data_bins[1].copy()) + file_back_header.write_bytes(data_bins[2].copy()) + file_package.write_bytes(data_package) + file_bin.write_bytes(data_bin) + + # 异常镜像-主分区md5错误 + file_image1 = result / f'{file_image.stem}_b1.bin' + data_image = data_bins[0].copy() + data_image[0x0FC018: 0x0FC01A] = [0x00, 0x01] + file_image1.write_bytes(data_image) + + # 异常镜像-备份分区md5错误 + file_image2 = result / f'{file_image.stem}_b2.bin' + data_image = data_bins[0].copy() + data_image[0x0FE018: 0x0FE01A] = [0x00, 0x01] + file_image2.write_bytes(data_image) + + # 异常镜像-双分区md5错误 + file_image3 = result / f'{file_image.stem}_b3.bin' + data_image = data_bins[0].copy() + data_image[0x0FC018: 0x0FC01A] = [0x00, 0x01] + data_image[0x0FE018: 0x0FE01A] = [0x00, 0x01] + file_image3.write_bytes(data_image) + + +def Process1(path_boot: Path, path_project: Path): + """ 适配器-SLCP101 镜像生成流程 """ + root_boot = path_boot + root_main = path_project + result = root_main + + # 正常启动镜像 + hex_boot = root_boot / "bootloader.hex" + hex_main = root_main / "lamina_adapter.hex" + hex_back = root_main / "lamina_adapter_back.hex" + hex_update = hex_main + if (not hex_boot.exists()) or (not hex_main.exists()) or (not hex_back.exists()): + raise Exception("缺失必要程序文件") + + file_image = result / f'{hex_main.stem}_ROM.bin' + file_main_header = result / 'SLCP101_main.header' + file_back_header = result / 'SLCP101_back.header' + file_package = result / f'{hex_update.stem}.dat' + file_bin = result / f'{hex_update.stem}.bin' + + data_bins = GenerateImage_SLCP101_p460(hex_boot, hex_main, hex_back) + data_package, data_bin = GeneratePackage_SLCP101_p460(hex_update) + + file_image.write_bytes(data_bins[0].copy()) + file_main_header.write_bytes(data_bins[1].copy()) + file_back_header.write_bytes(data_bins[2].copy()) + file_package.write_bytes(data_package) + file_bin.write_bytes(data_bin) + + # 异常镜像-主分区md5错误 + file_image1 = result / f'{file_image.stem}_b1.bin' + data_image = data_bins[0].copy() + data_image[0x054018: 0x05401A] = [0x00, 0x01] + file_image1.write_bytes(data_image) + + # 异常镜像-备份分区md5错误 + file_image2 = result / f'{file_image.stem}_b2.bin' + data_image = data_bins[0].copy() + data_image[0x056018: 0x05601A] = [0x00, 0x01] + file_image2.write_bytes(data_image) + + # 异常镜像-双分区md5错误 + file_image3 = result / f'{file_image.stem}_b3.bin' + data_image = data_bins[0].copy() + data_image[0x054018: 0x05401A] = [0x00, 0x01] + data_image[0x056018: 0x05601A] = [0x00, 0x01] + file_image3.write_bytes(data_image) + + +def Process2(): + """ 优化器-DLSY001 镜像生成流程 """ + root = Path(r"D:\WorkingProject\LightStackOptimizer\software\lamina_optimizer\lamina_optimizer\Debug") + # result = Path(r"test\p460_o1\result") + result = root + # 正常启动镜像 + hex_boot = Path(r"test\p460_o1\bootloader.hex") + hex_main = root / r"lamina_optimizer.hex" + hex_back = root / r"lamina_optimizer.hex" + hex_update = root / r"lamina_optimizer.hex" + + file_image = result / f'{hex_main.stem}_ROM.bin' + file_main_header = result / 'DLSY001_header_main.bin' + file_back_header = result / 'DLSY001_header_back.bin' + file_package = result / f'{hex_update.stem}.dat' + file_bin = result / f'{hex_update.stem}.bin' + + data_bins = GenerateImage_DLSY001_p460(hex_boot, hex_main, hex_back) + data_package, data_bin = GeneratePackage_DLSY001_p460(hex_update) + + file_image.write_bytes(data_bins[0].copy()) + file_main_header.write_bytes(data_bins[1].copy()) + file_back_header.write_bytes(data_bins[2].copy()) + file_package.write_bytes(data_package) + file_bin.write_bytes(data_bin) + + # 异常镜像-主分区md5错误 + file_image1 = result / f'{file_image.stem}_b1.bin' + data_image = data_bins[0].copy() + data_image[0x054018: 0x05401A] = [0x00, 0x01] + file_image1.write_bytes(data_image) + + # 异常镜像-备份分区md5错误 + file_image2 = result / f'{file_image.stem}_b2.bin' + data_image = data_bins[0].copy() + data_image[0x056018: 0x05601A] = [0x00, 0x01] + file_image2.write_bytes(data_image) + + # 异常镜像-双分区md5错误 + file_image3 = result / f'{file_image.stem}_b3.bin' + data_image = data_bins[0].copy() + data_image[0x054018: 0x05401A] = [0x00, 0x01] + data_image[0x056018: 0x05601A] = [0x00, 0x01] + file_image3.write_bytes(data_image) + + +if __name__ == "__main__": + path_boot1 = Path(r"D:\WorkingProject\LightStackAdapter\software\umon\4A0-PROJ_STACKLIGHT_PARALLEL_ADAPTOR") + path_boot2 = Path(r"D:\WorkingProject\LightStackAdapter\software\umon\460-PROJ_STACKLIGHT_PARALLEL_ADAPTOR") + path_main = Path(r"D:\WorkingProject\LightStackAdapter\software\lamina_adapter\lamina_adapter\Debug") + + Process0(path_boot1, path_main) # 适配器SLCP001 + # Process1(path_boot2, path_main) # 适配器SLCP101 + # Process2()