stm32_firmware_utils.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2023 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import sys
  16. import firmware_utils
  17. STM32_OPTIONS = {
  18. 'configuration': {
  19. 'stm32cubeprogrammer': {
  20. 'help': 'Path to the STM32CubeProgrammer executable',
  21. 'default': 'STM32_Programmer_CLI',
  22. 'argparse': {
  23. 'metavar': 'FILE'
  24. },
  25. 'verify': ['{stm32cubeprogrammer}', '-v'],
  26. 'error':
  27. """\
  28. Unable to execute STM32CubeProgrammer.
  29. Please ensure that this tool is installed and
  30. available. See the STM32 example README for
  31. installation instructions.
  32. """,
  33. },
  34. 'device': {
  35. 'help': 'Device family or platform to target',
  36. 'default': 'STM32',
  37. 'alias': ['-d'],
  38. 'argparse': {
  39. 'metavar': 'DEVICE'
  40. },
  41. },
  42. 'port': {
  43. 'help': 'Serial port of the device to flash',
  44. 'default': None,
  45. 'alias': ['-p'],
  46. 'argparse': {
  47. 'metavar': 'PORT'
  48. },
  49. },
  50. },
  51. }
  52. class Flasher(firmware_utils.Flasher):
  53. """Manage STM32 flashing."""
  54. def __init__(self, **options):
  55. super().__init__(platform='STM32', module=__name__, **options)
  56. self.define_options(STM32_OPTIONS)
  57. def erase(self):
  58. """Erase the device."""
  59. return self.run_tool(
  60. 'stm32cubeprogrammer',
  61. ['--connect', 'port={port}', '-c', 'port=SWD', '--erase', 'all'],
  62. name='Erase device')
  63. def verify(self, image):
  64. """Verify image."""
  65. return self.run_tool(
  66. 'stm32cubeprogrammer',
  67. ['--connect', 'port={port}', '-c', 'port=SWD', '--verify', image],
  68. name='Verify',
  69. pass_message='Verified',
  70. fail_message='Not verified',
  71. fail_level=2)
  72. def flash(self, image):
  73. """Flash image."""
  74. return self.run_tool(
  75. 'stm32cubeprogrammer',
  76. ['--connect', 'port={port}', '-c', 'port=SWD', '--write', image, '--format', 'bin', '--start-address',
  77. '0x8000000'],
  78. name='Flash')
  79. def reset(self):
  80. """Reset the device."""
  81. return self.run_tool(
  82. 'stm32cubeprogrammer',
  83. ['--connect', 'port={port}', '-c', 'port=SWD', '--rst'],
  84. name='Reset')
  85. def actions(self):
  86. """Perform actions on the device according to self.option."""
  87. self.log(3, 'Options:', self.option)
  88. if self.option.erase:
  89. if self.erase().err:
  90. return self
  91. if self.option.application:
  92. application = self.option.application
  93. if self.flash(application).err:
  94. return self
  95. if self.option.verify_application:
  96. if self.verify(application).err:
  97. return self
  98. if self.option.reset is None:
  99. self.option.reset = True
  100. if self.option.reset:
  101. if self.reset().err:
  102. return self
  103. return self
  104. if __name__ == '__main__':
  105. sys.exit(Flasher().flash_command(sys.argv))