cyw30739_firmware_utils.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 CYW30739 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.
  19. """
  20. import sys
  21. import firmware_utils
  22. # Additional options that can be use to configure an `Flasher`
  23. # object (as dictionary keys) and/or passed as command line options.
  24. CYW30739_OPTIONS = {
  25. # Configuration options define properties used in flashing operations.
  26. "configuration": {
  27. "direct": {
  28. "help": "Set 1 to enable direct load",
  29. "default": 0,
  30. "argparse": {"action": "store"},
  31. },
  32. "sdk_scripts_dir": {
  33. "help": "The SDK scripts directory",
  34. "default": None,
  35. "argparse": {"action": "store"},
  36. },
  37. "sdk_tools_dir": {
  38. "help": "The SDK tools directory",
  39. "default": None,
  40. "argparse": {"action": "store"},
  41. },
  42. "program": {
  43. "help": "The script to program the flash.",
  44. "command": [
  45. "bash",
  46. "--norc",
  47. "--noprofile",
  48. "{sdk_scripts_dir}/bt_program.bash",
  49. "--tools={sdk_tools_dir}",
  50. "--scripts={sdk_scripts_dir}",
  51. "--elf={application}",
  52. "--direct={direct}",
  53. (),
  54. ],
  55. },
  56. "port": {
  57. "help": "The serial port of device to flash",
  58. "default": None,
  59. "argparse": {},
  60. },
  61. },
  62. }
  63. class Flasher(firmware_utils.Flasher):
  64. """Manage CYW30739 flashing."""
  65. def __init__(self, **options):
  66. super().__init__(platform="CYW30739", module=__name__, **options)
  67. self.define_options(CYW30739_OPTIONS)
  68. def erase(self):
  69. """Not supported"""
  70. self.log(0, "Do not support erasing device.")
  71. self.err = 1
  72. return self
  73. def verify(self):
  74. """Not supported"""
  75. self.log(0, "Do not support verifying image.")
  76. self.err = 1
  77. return self
  78. def flash(self):
  79. """Flash image."""
  80. arguments = [
  81. "--hex={}/{}_download.hex".format(
  82. self.option.application.parent, self.option.application.stem
  83. ),
  84. ]
  85. if self.option.port:
  86. arguments.append("--uart={port}")
  87. if self.option.verbose > 0:
  88. arguments.append("--verbose")
  89. return self.run_tool("program", arguments, name="Flash")
  90. def reset(self):
  91. """Not supported"""
  92. self.log(0, "Do not support resetting device.")
  93. self.err = 1
  94. return self
  95. def actions(self):
  96. """Perform actions on the device according to self.option."""
  97. self.log(3, "Options:", self.option)
  98. if self.option.erase:
  99. if self.erase().err:
  100. return self
  101. if self.option.verify_application:
  102. if self.verify().err:
  103. return self
  104. if self.option.reset:
  105. if self.reset().err:
  106. return self
  107. if self.option.application:
  108. if self.flash().err:
  109. return self
  110. return self
  111. if __name__ == "__main__":
  112. sys.exit(Flasher().flash_command(sys.argv))