psoc6_firmware_utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2021 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 Infineon PSoC6 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: psoc6_firmware_utils.py [-h] [--verbose] [--erase] [--application FILE]
  21. [--verify_application] [--reset] [--skip_reset]
  22. [--device DEVICE]
  23. Flash PSoC6 device
  24. optional arguments:
  25. -h, --help show this help message and exit
  26. configuration:
  27. --verbose, -v Report more verbosely
  28. --device DEVICE, -d DEVICE
  29. Device family or platform to target
  30. operations:
  31. --erase Erase device
  32. --application FILE Flash an image
  33. --verify_application, --verify-application
  34. Verify the image after flashing
  35. --reset Reset device after flashing
  36. --skip_reset, --skip-reset
  37. Do not reset device after flashing
  38. """
  39. import sys
  40. from shutil import which
  41. import firmware_utils
  42. # Additional options that can be use to configure an `Flasher`
  43. # object (as dictionary keys) and/or passed as command line options.
  44. P6_OPTIONS = {
  45. # Configuration options define properties used in flashing operations.
  46. 'configuration': {
  47. 'make': {
  48. 'help': 'File name of the make executable',
  49. 'default': None,
  50. 'argparse': {
  51. 'metavar': 'FILE',
  52. },
  53. 'command': [
  54. "make",
  55. "-C",
  56. {'option': 'sdk_path'},
  57. ['TARGET={device}'],
  58. ['CY_OPENOCD_PROGRAM_IMG=../../../../{application}'],
  59. {'option': 'mtb_target'}
  60. ],
  61. 'verify': ['{make}', '--version'],
  62. 'error':
  63. """\
  64. Unable to execute {make}.
  65. Please ensure that make is installed.
  66. """,
  67. },
  68. 'device': {
  69. 'help': 'Device family or platform to target',
  70. 'default': 'PSoC6',
  71. 'alias': ['-d'],
  72. 'argparse': {
  73. 'metavar': 'DEVICE'
  74. },
  75. },
  76. 'sdk_path': {
  77. 'help': 'Path to psoc6 sdk',
  78. 'default': 'third_party/infineon/psoc6/psoc6_sdk',
  79. 'alias': ['-p'],
  80. 'argparse': {
  81. 'metavar': 'SDK_PATH'
  82. },
  83. },
  84. },
  85. }
  86. class Flasher(firmware_utils.Flasher):
  87. """Manage PSoC6 flashing."""
  88. def __init__(self, **options):
  89. super().__init__(platform='PSOC6', module=__name__, **options)
  90. self.define_options(P6_OPTIONS)
  91. def erase(self):
  92. raise NotImplementedError()
  93. def verify(self, image):
  94. raise NotImplementedError()
  95. def flash(self, image):
  96. """Flash image."""
  97. return self.run_tool(
  98. 'make',
  99. [],
  100. options={"mtb_target": "qprogram", "application": image},
  101. name='Flash')
  102. def reset(self):
  103. raise NotImplementedError()
  104. def actions(self):
  105. """Perform actions on the device according to self.option."""
  106. self.log(3, 'Options:', self.option)
  107. if self.option.erase:
  108. if self.erase().err:
  109. return self
  110. if self.option.application:
  111. application = self.option.application
  112. if self.flash(application).err:
  113. return self
  114. if self.option.verify_application:
  115. if self.verify(application).err:
  116. return self
  117. if self.option.reset:
  118. if self.reset().err:
  119. return self
  120. return self
  121. def locate_tool(self, tool):
  122. if tool == "make":
  123. return which("make")
  124. else:
  125. return tool
  126. if __name__ == '__main__':
  127. sys.exit(Flasher().flash_command(sys.argv))