copro.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import logging
  2. import datetime
  3. import shutil
  4. import json
  5. import xml.etree.ElementTree as ET
  6. from .utils import *
  7. CUBE_COPRO_PATH = "Projects/STM32WB_Copro_Wireless_Binaries"
  8. MANIFEST_TEMPLATE = {
  9. "manifest": {"version": 0, "timestamp": 0},
  10. "copro": {
  11. "fus": {"version": {"major": 1, "minor": 2, "sub": 0}, "files": []},
  12. "radio": {
  13. "version": {
  14. "type": 1,
  15. "major": 1,
  16. "minor": 12,
  17. "sub": 1,
  18. "branch": 0,
  19. "release": 1,
  20. },
  21. "files": [],
  22. },
  23. },
  24. }
  25. class Copro:
  26. def __init__(self, mcu):
  27. self.mcu = mcu
  28. self.version = None
  29. self.cube_dir = None
  30. self.mcu_copro = None
  31. self.logger = logging.getLogger(self.__class__.__name__)
  32. def loadCubeInfo(self, cube_dir):
  33. if not os.path.isdir(cube_dir):
  34. raise Exception(f'"{cube_dir}" doesn\'t exists')
  35. self.cube_dir = cube_dir
  36. self.mcu_copro = os.path.join(self.cube_dir, CUBE_COPRO_PATH, self.mcu)
  37. if not os.path.isdir(self.mcu_copro):
  38. raise Exception(f'"{self.mcu_copro}" doesn\'t exists')
  39. cube_manifest_file = os.path.join(self.cube_dir, "package.xml")
  40. cube_manifest = ET.parse(cube_manifest_file)
  41. cube_package = cube_manifest.find("PackDescription")
  42. if not cube_package:
  43. raise Exception(f"Unknown Cube manifest format")
  44. cube_version = cube_package.get("Patch") or cube_package.get("Release")
  45. if not cube_version or not cube_version.startswith("FW.WB"):
  46. raise Exception(f"Incorrect Cube package or version info")
  47. cube_version = cube_version.replace("FW.WB.", "", 1)
  48. if cube_version != "1.12.1":
  49. raise Exception(f"Unknonwn cube version")
  50. self.version = cube_version
  51. def addFile(self, array, filename, **kwargs):
  52. source_file = os.path.join(self.mcu_copro, filename)
  53. destination_file = os.path.join(self.output_dir, filename)
  54. shutil.copyfile(source_file, destination_file)
  55. array.append(
  56. {"name": filename, "sha256": file_sha256(destination_file), **kwargs}
  57. )
  58. def bundle(self, output_dir):
  59. if not os.path.isdir(output_dir):
  60. raise Exception(f'"{output_dir}" doesn\'t exists')
  61. self.output_dir = output_dir
  62. manifest_file = os.path.join(self.output_dir, "Manifest.json")
  63. # Form Manifest
  64. manifest = dict(MANIFEST_TEMPLATE)
  65. manifest["manifest"]["timestamp"] = timestamp()
  66. # Old FUS Update
  67. self.addFile(
  68. manifest["copro"]["fus"]["files"],
  69. "stm32wb5x_FUS_fw_for_fus_0_5_3.bin",
  70. condition="==0.5.3",
  71. address="0x080EC000",
  72. )
  73. # New FUS Update
  74. self.addFile(
  75. manifest["copro"]["fus"]["files"],
  76. "stm32wb5x_FUS_fw.bin",
  77. condition=">0.5.3",
  78. address="0x080EC000",
  79. )
  80. # BLE Full Stack
  81. self.addFile(
  82. manifest["copro"]["radio"]["files"],
  83. "stm32wb5x_BLE_Stack_full_fw.bin",
  84. address="0x080CA000",
  85. )
  86. # Save manifest to
  87. json.dump(manifest, open(manifest_file, "w"))