efr32_firmware_utils.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2020 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. """Flash an EFR32 device.
  16. This is layered so that a caller can perform individual operations
  17. through an `Flasher` instance, or operations according to a command line.
  18. For `Flasher`, see the class documentation. For the parse_command()
  19. interface or standalone execution:
  20. usage: efr32_firmware_utils.py [-h] [--verbose] [--erase] [--application FILE]
  21. [--verify_application] [--reset] [--skip_reset]
  22. [--commander FILE] [--device DEVICE]
  23. [--serialno SERIAL] [--ip ADDRESS]
  24. Flash EFR32 device
  25. optional arguments:
  26. -h, --help show this help message and exit
  27. configuration:
  28. --verbose, -v Report more verbosely
  29. --commander FILE File name of the commander executable
  30. --device DEVICE, -d DEVICE
  31. Device family or platform to target
  32. --serialno SERIAL, -s SERIAL
  33. Serial number of device to flash
  34. --ip ADDRESS, -a ADDRESS
  35. Ip Address of the targeted flasher
  36. operations:
  37. --erase Erase device
  38. --application FILE Flash an image
  39. --verify_application, --verify-application
  40. Verify the image after flashing
  41. --reset Reset device after flashing
  42. --skip_reset, --skip-reset
  43. Do not reset device after flashing
  44. """
  45. import sys
  46. import firmware_utils
  47. # Additional options that can be use to configure an `Flasher`
  48. # object (as dictionary keys) and/or passed as command line options.
  49. EFR32_OPTIONS = {
  50. # Configuration options define properties used in flashing operations.
  51. 'configuration': {
  52. # Tool configuration options.
  53. 'commander': {
  54. 'help': 'File name of the commander executable',
  55. 'default': 'commander',
  56. 'argparse': {
  57. 'metavar': 'FILE'
  58. },
  59. 'verify': ['{commander}', '--version'],
  60. 'error':
  61. """\
  62. Unable to execute {commander}.
  63. Please ensure that this tool is installed and
  64. available. See the EFR32 example README for
  65. installation instructions.
  66. """,
  67. },
  68. 'device': {
  69. 'help': 'Device family or platform to target (EFR32 or MGM240)',
  70. 'default': None,
  71. 'alias': ['-d'],
  72. 'argparse': {
  73. 'metavar': 'DEVICE'
  74. },
  75. },
  76. 'serialno': {
  77. 'help': 'Serial number of device to flash',
  78. 'default': None,
  79. 'alias': ['-s'],
  80. 'argparse': {
  81. 'metavar': 'SERIAL'
  82. },
  83. },
  84. 'ip': {
  85. 'help': 'Ip Address of the probe connected to the target',
  86. 'default': None,
  87. 'alias': ['-a'],
  88. 'argparse': {
  89. 'metavar': 'ADDRESS'
  90. },
  91. },
  92. },
  93. }
  94. class Flasher(firmware_utils.Flasher):
  95. """Manage efr32 flashing."""
  96. def __init__(self, **options):
  97. super().__init__(platform='EFR32', module=__name__, **options)
  98. self.define_options(EFR32_OPTIONS)
  99. # Common command line arguments for commander device subcommands.
  100. DEVICE_ARGUMENTS = [{'optional': 'serialno'}, {
  101. 'optional': 'ip'}, {'optional': 'device'}]
  102. def erase(self):
  103. """Perform `commander device masserase`."""
  104. return self.run_tool(
  105. 'commander', ['device', 'masserase', self.DEVICE_ARGUMENTS],
  106. name='Erase device')
  107. def verify(self, image):
  108. """Verify image."""
  109. return self.run_tool(
  110. 'commander',
  111. ['verify', self.DEVICE_ARGUMENTS, image],
  112. name='Verify',
  113. pass_message='Verified',
  114. fail_message='Not verified',
  115. fail_level=2)
  116. def flash(self, image):
  117. """Flash image."""
  118. return self.run_tool(
  119. 'commander',
  120. ['flash', self.DEVICE_ARGUMENTS, image],
  121. name='Flash')
  122. def reset(self):
  123. """Reset the device."""
  124. return self.run_tool(
  125. 'commander',
  126. ['device', 'reset', self.DEVICE_ARGUMENTS],
  127. name='Reset')
  128. def actions(self):
  129. """Perform actions on the device according to self.option."""
  130. self.log(3, 'Options:', self.option)
  131. if self.option.erase:
  132. if self.erase().err:
  133. return self
  134. if self.option.application:
  135. application = self.option.application
  136. if self.flash(application).err:
  137. return self
  138. if self.option.verify_application:
  139. if self.verify(application).err:
  140. return self
  141. if self.option.reset is None:
  142. self.option.reset = True
  143. if self.option.reset:
  144. if self.reset().err:
  145. return self
  146. return self
  147. if __name__ == '__main__':
  148. sys.exit(Flasher().flash_command(sys.argv))