yzamstm | 0b9dfec | 2023-09-06 21:05:35 +0200 | [diff] [blame] | 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 |
|
| 16 | import sys
|
| 17 |
|
| 18 | import firmware_utils
|
| 19 |
|
| 20 | STM32_OPTIONS = {
|
| 21 | 'configuration': {
|
| 22 | 'stm32cubeprogrammer': {
|
| 23 | 'help': 'Path to the STM32CubeProgrammer executable',
|
| 24 | 'default': 'STM32_Programmer_CLI',
|
| 25 | 'argparse': {
|
| 26 | 'metavar': 'FILE'
|
| 27 | },
|
| 28 | 'verify': ['{stm32cubeprogrammer}', '-v'],
|
| 29 | 'error':
|
| 30 | """\
|
| 31 | Unable to execute STM32CubeProgrammer.
|
| 32 |
|
| 33 | Please ensure that this tool is installed and
|
| 34 | available. See the STM32 example README for
|
| 35 | installation instructions.
|
| 36 |
|
| 37 | """,
|
| 38 | },
|
| 39 | 'device': {
|
| 40 | 'help': 'Device family or platform to target',
|
| 41 | 'default': 'STM32',
|
| 42 | 'alias': ['-d'],
|
| 43 | 'argparse': {
|
| 44 | 'metavar': 'DEVICE'
|
| 45 | },
|
| 46 | },
|
| 47 | 'port': {
|
| 48 | 'help': 'Serial port of the device to flash',
|
| 49 | 'default': None,
|
| 50 | 'alias': ['-p'],
|
| 51 | 'argparse': {
|
| 52 | 'metavar': 'PORT'
|
| 53 | },
|
| 54 | },
|
| 55 | },
|
| 56 | }
|
| 57 |
|
| 58 |
|
| 59 | class Flasher(firmware_utils.Flasher):
|
| 60 | """Manage STM32 flashing."""
|
| 61 |
|
| 62 | def __init__(self, **options):
|
| 63 | super().__init__(platform='STM32', module=__name__, **options)
|
| 64 | self.define_options(STM32_OPTIONS)
|
| 65 |
|
| 66 | def erase(self):
|
| 67 | """Erase the device."""
|
| 68 | return self.run_tool(
|
| 69 | 'stm32cubeprogrammer',
|
| 70 | ['--connect', 'port={port}', '-c', 'port=SWD', '--erase', 'all'],
|
| 71 | name='Erase device')
|
| 72 |
|
| 73 | def verify(self, image):
|
| 74 | """Verify image."""
|
| 75 | return self.run_tool(
|
| 76 | 'stm32cubeprogrammer',
|
| 77 | ['--connect', 'port={port}', '-c', 'port=SWD', '--verify', image],
|
| 78 | name='Verify',
|
| 79 | pass_message='Verified',
|
| 80 | fail_message='Not verified',
|
| 81 | fail_level=2)
|
| 82 |
|
| 83 | def flash(self, image):
|
| 84 | """Flash image."""
|
| 85 | return self.run_tool(
|
| 86 | 'stm32cubeprogrammer',
|
| 87 | ['--connect', 'port={port}', '-c', 'port=SWD', '--write', image, '--format', 'bin', '--start-address',
|
| 88 | '0x8000000'],
|
| 89 | name='Flash')
|
| 90 |
|
| 91 | def reset(self):
|
| 92 | """Reset the device."""
|
| 93 | return self.run_tool(
|
| 94 | 'stm32cubeprogrammer',
|
| 95 | ['--connect', 'port={port}', '-c', 'port=SWD', '--rst'],
|
| 96 | name='Reset')
|
| 97 |
|
| 98 | def actions(self):
|
| 99 | """Perform actions on the device according to self.option."""
|
| 100 | self.log(3, 'Options:', self.option)
|
| 101 |
|
| 102 | if self.option.erase:
|
| 103 | if self.erase().err:
|
| 104 | return self
|
| 105 |
|
| 106 | if self.option.application:
|
| 107 | application = self.option.application
|
| 108 | if self.flash(application).err:
|
| 109 | return self
|
| 110 | if self.option.verify_application:
|
| 111 | if self.verify(application).err:
|
| 112 | return self
|
| 113 | if self.option.reset is None:
|
| 114 | self.option.reset = True
|
| 115 |
|
| 116 | if self.option.reset:
|
| 117 | if self.reset().err:
|
| 118 | return self
|
| 119 |
|
| 120 | return self
|
| 121 |
|
| 122 |
|
| 123 | if __name__ == '__main__':
|
| 124 | sys.exit(Flasher().flash_command(sys.argv))
|