nios2.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (c) 2017 Linaro Limited.
  2. #
  3. # SPDX-License-Identifier: Apache-2.0
  4. '''Runner for NIOS II, based on quartus-flash.py and GDB.'''
  5. from runners.core import ZephyrBinaryRunner, NetworkPortHelper
  6. class Nios2BinaryRunner(ZephyrBinaryRunner):
  7. '''Runner front-end for NIOS II.'''
  8. # From the original shell script:
  9. #
  10. # "XXX [flash] only support[s] cases where the .elf is sent
  11. # over the JTAG and the CPU directly boots from __start. CONFIG_XIP
  12. # and CONFIG_INCLUDE_RESET_VECTOR must be disabled."
  13. def __init__(self, cfg, quartus_py=None, cpu_sof=None, tui=False):
  14. super().__init__(cfg)
  15. self.hex_name = cfg.hex_file
  16. self.elf_name = cfg.elf_file
  17. self.cpu_sof = cpu_sof
  18. self.quartus_py = quartus_py
  19. self.gdb_cmd = [cfg.gdb] if cfg.gdb else None
  20. self.tui_arg = ['-tui'] if tui else []
  21. @classmethod
  22. def name(cls):
  23. return 'nios2'
  24. @classmethod
  25. def do_add_parser(cls, parser):
  26. # TODO merge quartus-flash.py script into this file.
  27. parser.add_argument('--quartus-flash', required=True)
  28. parser.add_argument('--cpu-sof', required=True,
  29. help='path to the the CPU .sof data')
  30. parser.add_argument('--tui', default=False, action='store_true',
  31. help='if given, GDB uses -tui')
  32. @classmethod
  33. def do_create(cls, cfg, args):
  34. return Nios2BinaryRunner(cfg,
  35. quartus_py=args.quartus_flash,
  36. cpu_sof=args.cpu_sof,
  37. tui=args.tui)
  38. def do_run(self, command, **kwargs):
  39. if command == 'flash':
  40. self.flash(**kwargs)
  41. else:
  42. self.debug_debugserver(command, **kwargs)
  43. def flash(self, **kwargs):
  44. if self.quartus_py is None:
  45. raise ValueError('Cannot flash; --quartus-flash not given.')
  46. if self.cpu_sof is None:
  47. raise ValueError('Cannot flash; --cpu-sof not given.')
  48. self.ensure_output('hex')
  49. self.logger.info('Flashing file: {}'.format(self.hex_name))
  50. cmd = [self.quartus_py,
  51. '--sof', self.cpu_sof,
  52. '--kernel', self.hex_name]
  53. self.require(cmd[0])
  54. self.check_call(cmd)
  55. def print_gdbserver_message(self, gdb_port):
  56. self.logger.info('Nios II GDB server running on port {}'.
  57. format(gdb_port))
  58. def debug_debugserver(self, command, **kwargs):
  59. # Per comments in the shell script, the NIOSII GDB server
  60. # doesn't exit gracefully, so it's better to explicitly search
  61. # for an unused port. The script picks a random value in
  62. # between 1024 and 49151, but we'll start with the
  63. # "traditional" 3333 choice.
  64. gdb_start = 3333
  65. nh = NetworkPortHelper()
  66. gdb_port = nh.get_unused_ports([gdb_start])[0]
  67. server_cmd = (['nios2-gdb-server',
  68. '--tcpport', str(gdb_port),
  69. '--stop', '--reset-target'])
  70. self.require(server_cmd[0])
  71. if command == 'debugserver':
  72. self.print_gdbserver_message(gdb_port)
  73. self.check_call(server_cmd)
  74. else:
  75. if self.elf_name is None:
  76. raise ValueError('Cannot debug; elf is missing')
  77. if self.gdb_cmd is None:
  78. raise ValueError('Cannot debug; no gdb specified')
  79. gdb_cmd = (self.gdb_cmd +
  80. self.tui_arg +
  81. [self.elf_name,
  82. '-ex', 'target remote :{}'.format(gdb_port)])
  83. self.require(gdb_cmd[0])
  84. self.print_gdbserver_message(gdb_port)
  85. self.run_server_and_client(server_cmd, gdb_cmd)