cube.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import logging
  2. import subprocess
  3. class CubeProgrammer:
  4. """STM32 Cube Programmer cli wrapper"""
  5. def __init__(self, config={}):
  6. assert isinstance(config, dict)
  7. # Params base
  8. self.params = []
  9. # Connect params
  10. connect = []
  11. if "port" in config and config["port"]:
  12. connect.append(f"port={config['port']}")
  13. else:
  14. connect.append("port=swd")
  15. if "serial" in config and config["serial"]:
  16. connect.append(f"sn={config['serial']}")
  17. self.params.append("-c " + " ".join(connect))
  18. # Other params
  19. if "params" in config:
  20. self.params += config["params"]
  21. # logging
  22. self.logger = logging.getLogger()
  23. def _execute(self, args):
  24. try:
  25. params = [
  26. "STM32_Programmer_CLI",
  27. "-q",
  28. *self.params,
  29. *args,
  30. ]
  31. self.logger.debug(f"_execute: {params}")
  32. output = subprocess.check_output(params)
  33. except subprocess.CalledProcessError as e:
  34. if e.output:
  35. print("Process output:\n", e.output.decode())
  36. print("Process return code:", e.returncode)
  37. raise e
  38. assert output
  39. return output.decode()
  40. def getVersion(self):
  41. self._execute(["--version"])
  42. def checkOptionBytes(self, option_bytes):
  43. output = self._execute(["-ob displ"])
  44. ob_correct = True
  45. for line in output.split("\n"):
  46. line = line.strip()
  47. if ":" not in line:
  48. self.logger.debug(f"Skipping line: {line}")
  49. continue
  50. key, data = line.split(":", 1)
  51. key = key.strip()
  52. data = data.strip()
  53. if key not in option_bytes.keys():
  54. self.logger.debug(f"Skipping key: {key}")
  55. continue
  56. self.logger.debug(f"Processing key: {key} {data}")
  57. value, comment = data.split(" ", 1)
  58. value = value.strip()
  59. comment = comment.strip()
  60. if option_bytes[key][0] != value:
  61. self.logger.error(
  62. f"Invalid OB: {key} {value}, expected: {option_bytes[key][0]}"
  63. )
  64. ob_correct = False
  65. return ob_correct
  66. def setOptionBytes(self, option_bytes):
  67. options = []
  68. for key, (value, attr) in option_bytes.items():
  69. if "w" in attr:
  70. options.append(f"{key}={value}")
  71. self._execute(["-ob", *options])
  72. return True
  73. def flashBin(self, address, filename):
  74. self._execute(
  75. [
  76. "-d",
  77. filename,
  78. f"{address}",
  79. ]
  80. )
  81. def flashCore2(self, address, filename):
  82. self._execute(
  83. [
  84. "-fwupgrade",
  85. filename,
  86. f"{address}",
  87. ]
  88. )
  89. def deleteCore2RadioStack(self):
  90. self._execute(["-fwdelete"])
  91. def resetTarget(self):
  92. self._execute([])