2 # Copyright (c) 2020-2021 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 NRF5 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: nrfconnect_firmware_utils.py [-h] [--verbose] [--erase]
24 [--verify_application] [--reset]
25 [--skip_reset] [--nrfjprog FILE]
26 [--snr SERIAL] [--family FAMILY]
31 -h, --help show this help message and exit
34 --verbose, -v Report more verbosely
35 --nrfjprog FILE File name of the nrfjprog executable
36 --snr SERIAL, --serial SERIAL, -s SERIAL
37 Serial number of device to flash
38 --family FAMILY NRF5 device family
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
56 # Additional options that can be use to configure an `Flasher`
57 # object (as dictionary keys) and/or passed as command line options.
59 # Configuration options define properties used in flashing operations.
61 # Tool configuration options.
63 'help': 'File name of the nrfjprog executable',
64 'default': 'nrfjprog',
70 {'optional': 'family'},
74 'verify': ['{nrfjprog}', '--version'],
77 Unable to execute {nrfjprog}.
79 Please ensure that this tool is installed and
80 available. See the NRF5 example README for
81 installation instructions.
86 'help': 'Serial number of device to flash',
88 'alias': ['--serial', '-s'],
94 # Device configuration options.
96 'help': 'NRF5 device family',
106 class Flasher(firmware_utils.Flasher):
107 """Manage nrf5 flashing."""
109 def __init__(self, **options):
110 super().__init__(platform='NRF5', module=__name__, **options)
111 self.define_options(NRF5_OPTIONS)
114 """Perform nrfjprog --eraseall"""
115 return self.run_tool('nrfjprog', ['--eraseall'], name='Erase all')
117 def verify(self, image):
119 return self.run_tool('nrfjprog',
120 ['--quiet', '--verify', image],
122 pass_message='Verified',
123 fail_message='Not verified',
126 def flash(self, image):
128 return self.run_tool('nrfjprog',
129 ['--program', image, '--sectoranduicrerase'],
133 """Reset the device."""
134 return self.run_tool('nrfjprog', ['--pinresetenable'], name='Enable pin reset')
135 return self.run_tool('nrfjprog', ['--pinreset'], name='Apply pin reset')
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:
161 ### Mobly integration
163 def __init__(self, flasher_args):
164 self.flasher = Flasher(**flasher_args)
167 self.flasher.flash_command([os.getcwd()])
169 def verify_platform_args(platform_args):
170 required_args = ['application']
171 for r in required_args:
172 if not r in platform_args:
173 raise ValueError("Required argument %s missing" % r)
175 def create_platform(platform_args):
176 verify_platform_args(platform_args[0])
177 return Nrf5Platform(platform_args[0])
179 ### End of Mobly integration
181 if __name__ == '__main__':
182 sys.exit(Flasher().flash_command(sys.argv))