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 K32W 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: k32w_firmware_utils.py [-h] [--verbose] [--erase] [--application FILE]
23 [--verify_application] [--reset] [--skip_reset]
24 [--commander FILE] [--serialno SERIAL]
29 -h, --help show this help message and exit
32 --verbose, -v Report more verbosely
33 --commander FILE File name of the commander executable
34 --serialno SERIAL, -s SERIAL
35 Serial number of device to flash
39 --application FILE Flash an image
40 --verify_application, --verify-application
41 Verify the image after flashing
42 --reset Reset device after flashing
43 --skip_reset, --skip-reset
44 Do not reset device after flashing
51 # Additional options that can be use to configure an `Flasher`
52 # object (as dictionary keys) and/or passed as command line options.
54 # Configuration options define properties used in flashing operations.
56 # Tool configuration options.
58 'help': 'File name of the commander executable',
59 'default': 'commander',
63 'verify': ['{commander}', '--version'],
66 Unable to execute {commander}.
68 Please ensure that this tool is installed and
69 available. See the K32W example README for
70 installation instructions.
75 'help': 'Serial number of device to flash',
86 class Flasher(firmware_utils.Flasher):
87 """Manage k32w flashing."""
89 def __init__(self, **options):
90 super().__init__(platform='K32W', module=__name__, **options)
91 self.define_options(K32W_OPTIONS)
93 # Common command line arguments for commander device subcommands.
94 DEVICE_ARGUMENTS = [{'optional': 'serialno'}]
97 """Perform `commander device masserase`."""
99 'commander', ['device', 'masserase', self.DEVICE_ARGUMENTS],
102 def verify(self, image):
104 return self.run_tool(
106 ['verify', self.DEVICE_ARGUMENTS, image],
108 pass_message='Verified',
109 fail_message='Not verified',
112 def flash(self, image):
114 return self.run_tool(
116 ['flash', self.DEVICE_ARGUMENTS, image],
120 """Reset the device."""
121 return self.run_tool(
123 ['device', 'reset', self.DEVICE_ARGUMENTS],
127 """Perform actions on the device according to self.option."""
128 self.log(3, 'Options:', self.option)
130 if self.option.erase:
134 application = self.optional_file(self.option.application)
136 if self.flash(application).err:
138 if self.option.verify_application:
139 if self.verify(application).err:
141 if self.option.reset is None:
142 self.option.reset = True
144 if self.option.reset:
151 if __name__ == '__main__':
152 sys.exit(Flasher().flash_command(sys.argv))