__init__.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright 2024 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from .targets import STM32F4FlashProgrammer, STM32F7FlashProgrammer
  15. from .swd_port import SerialWireDebugPort
  16. from .ftdi_swd import FTDISerialWireDebug
  17. def get_device(board, reset=True, frequency=None):
  18. boards = {
  19. 'silk_bb': (0x7893, 10E6, STM32F4FlashProgrammer),
  20. 'robert_bb2': (0x7894, 3E6, STM32F7FlashProgrammer)
  21. }
  22. if board not in boards:
  23. raise Exception('Invalid board: {}'.format(board))
  24. usb_pid, default_frequency, board_ctor = boards[board]
  25. if not frequency:
  26. frequency = default_frequency
  27. ftdi = FTDISerialWireDebug(vid=0x0403, pid=usb_pid, interface=0, direction=0x1b,
  28. output_mask=0x02, reset_mask=0x40, frequency=frequency)
  29. swd_port = SerialWireDebugPort(ftdi, reset)
  30. return board_ctor(swd_port)
  31. def flash(board, hex_files):
  32. with get_device(board) as programmer:
  33. programmer.execute_loader()
  34. for hex_file in hex_files:
  35. programmer.load_hex(hex_file)
  36. programmer.reset_core()