2 # Copyright (c) 2020 Project CHIP Authors
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 This is layered so that a caller can perform individual operations
18 through an `Flasher` instance, or operations according to a command line.
19 For `Flasher`, see the class documentation. For the parse_command()
20 interface or standalone execution:
22 usage: efr32_firmware_utils.py [-h] [--verbose] [--erase] [--application FILE]
23 [--verify_application] [--reset] [--skip_reset]
24 [--commander FILE] [--device DEVICE]
30 -h, --help show this help message and exit
33 --verbose, -v Report more verbosely
34 --commander FILE File name of the commander executable
35 --device DEVICE, -d DEVICE
36 Device family or platform to target
37 --serialno SERIAL, -s SERIAL
38 Serial number of device to flash
42 --application FILE Flash an image
43 --verify_application, --verify-application
44 Verify the image after flashing
45 --reset Reset device after flashing
46 --skip_reset, --skip-reset
47 Do not reset device after flashing
54 # Additional options that can be use to configure an `Flasher`
55 # object (as dictionary keys) and/or passed as command line options.
57 # Configuration options define properties used in flashing operations.
59 # Tool configuration options.
61 'help': 'File name of the commander executable',
62 'default': 'commander',
66 'verify': ['{commander}', '--version'],
69 Unable to execute {commander}.
71 Please ensure that this tool is installed and
72 available. See the EFR32 example README for
73 installation instructions.
78 'help': 'Device family or platform to target',
86 'help': 'Serial number of device to flash',
97 class Flasher(firmware_utils.Flasher):
98 """Manage efr32 flashing."""
100 def __init__(self, **options):
101 super().__init__(platform='EFR32', module=__name__, **options)
102 self.define_options(EFR32_OPTIONS)
104 # Common command line arguments for commander device subcommands.
105 DEVICE_ARGUMENTS = [{'optional': 'serialno'}, {'optional': 'device'}]
108 """Perform `commander device masserase`."""
109 return self.run_tool(
110 'commander', ['device', 'masserase', self.DEVICE_ARGUMENTS],
113 def verify(self, image):
115 return self.run_tool(
117 ['verify', self.DEVICE_ARGUMENTS, image],
119 pass_message='Verified',
120 fail_message='Not verified',
123 def flash(self, image):
125 return self.run_tool(
127 ['flash', self.DEVICE_ARGUMENTS, image],
131 """Reset the device."""
132 return self.run_tool(
134 ['device', 'reset', self.DEVICE_ARGUMENTS],
138 """Perform actions on the device according to self.option."""
139 self.log(3, 'Options:', self.option)
141 if self.option.erase:
145 application = self.optional_file(self.option.application)
147 if self.flash(application).err:
149 if self.option.verify_application:
150 if self.verify(application).err:
152 if self.option.reset is None:
153 self.option.reset = True
155 if self.option.reset:
162 if __name__ == '__main__':
163 sys.exit(Flasher().flash_command(sys.argv))