Exemplo n.º 1
0
    def openocd(action: str, ctx: Context, args: PropertyDict,
                openocd_scripts: Optional[str], openocd_commands: str) -> None:
        """
        Execute openocd as external tool
        """
        OPENOCD_TAGET_CONFIG = {
            'esp32': '-f board/esp32-wrover-kit-3.3v.cfg',
            'esp32s2': '-f board/esp32s2-kaluga-1.cfg',
        }
        if os.getenv('OPENOCD_SCRIPTS') is None:
            raise FatalError(
                'OPENOCD_SCRIPTS not found in the environment: Please run export.sh/export.bat',
                ctx)
        openocd_arguments = os.getenv(
            'OPENOCD_COMMANDS'
        ) if openocd_commands is None else openocd_commands
        project_desc = get_project_desc(args, ctx)
        if openocd_arguments is None:
            # use default value if commands not defined in the environment nor command line
            target = project_desc['target']
            default_args = '-f interface/ftdi/esp32_devkitj_v1.cfg -f target/{}.cfg'.format(
                target)
            openocd_arguments = OPENOCD_TAGET_CONFIG.get(target, default_args)
            print(
                'Note: OpenOCD cfg not found (via env variable OPENOCD_COMMANDS nor as a --openocd-commands argument)\n'
                'OpenOCD arguments default to: "{}"'.format(openocd_arguments))
        # script directory is taken from the environment by OpenOCD, update only if command line arguments to override
        if openocd_scripts is not None:
            openocd_arguments += ' -s {}'.format(openocd_scripts)
        local_dir = project_desc['build_dir']
        args = ['openocd'] + shlex.split(openocd_arguments)
        openocd_out_name = os.path.join(local_dir, OPENOCD_OUT_FILE)
        openocd_out = open(openocd_out_name, 'a+')
        try:
            process = subprocess.Popen(args,
                                       stdout=openocd_out,
                                       stderr=subprocess.STDOUT,
                                       bufsize=1)
        except Exception as e:
            print(e)
            raise FatalError(
                'Error starting openocd. Please make sure it is installed and is present in executable paths',
                ctx)

        processes['openocd'] = process
        processes['openocd_outfile'] = openocd_out
        processes['openocd_outfile_name'] = openocd_out_name
        print('OpenOCD started as a background task {}'.format(process.pid))
Exemplo n.º 2
0
 def _check_openocd_errors(fail_if_openocd_failed, target, ctx):
     if fail_if_openocd_failed:
         if "openocd" in processes and processes["openocd"] is not None:
             p = processes["openocd"]
             name = processes["openocd_outfile_name"]
             # watch OpenOCD (for 5x500ms) to check if it hasn't terminated or outputs an error
             for _ in range(5):
                 if p.poll() is not None:
                     print("OpenOCD exited with {}".format(p.poll()))
                     break
                 with open(name, "r") as f:
                     content = f.read()
                     if re.search(r"no device found", content):
                         break
                     if re.search(
                             r"Listening on port \d+ for gdb connections",
                             content):
                         # expect OpenOCD has started successfully - stop watching
                         return
                 time.sleep(0.5)
             else:
                 return
             # OpenOCD exited or error message detected -> print possible output and terminate
             with open(name, "r") as f:
                 print(f.read())
             raise FatalError(
                 'Action "{}" failed due to errors in OpenOCD: Please check jtag connection!'
                 .format(target), ctx)
Exemplo n.º 3
0
    def gdbui(action: str, ctx: Context, args: PropertyDict,
              gdbgui_port: Optional[str], gdbinit: Optional[str],
              require_openocd: bool) -> None:
        """
        Asynchronous GDB-UI target
        """
        project_desc = get_project_desc(args, ctx)
        local_dir = project_desc['build_dir']
        gdb = project_desc['monitor_toolprefix'] + 'gdb'
        if gdbinit is None:
            gdbinit = os.path.join(local_dir, 'gdbinit')
            create_local_gdbinit(
                gdb, gdbinit,
                os.path.join(args.build_dir, project_desc['app_elf']))

        # this is a workaround for gdbgui
        # gdbgui is using shlex.split for the --gdb-args option. When the input is:
        # - '"-x=foo -x=bar"', would return ['foo bar']
        # - '-x=foo', would return ['-x', 'foo'] and mess up the former option '--gdb-args'
        # so for one item, use extra double quotes. for more items, use no extra double quotes.
        gdb_args_list = get_gdb_args(gdbinit, project_desc)
        gdb_args = '"{}"'.format(' '.join(gdb_args_list)) if len(
            gdb_args_list) == 1 else ' '.join(gdb_args_list)
        args = ['gdbgui', '-g', gdb, '--gdb-args', gdb_args]
        print(args)

        if gdbgui_port is not None:
            args += ['--port', gdbgui_port]
        gdbgui_out_name = os.path.join(local_dir, GDBGUI_OUT_FILE)
        gdbgui_out = open(gdbgui_out_name, 'a+')
        env = os.environ.copy()
        # The only known solution for https://github.com/cs01/gdbgui/issues/359 is to set the following environment
        # variable. The greenlet package cannot be downgraded for compatibility with other requirements (gdbgui,
        # pygdbmi).
        env['PURE_PYTHON'] = '1'
        try:
            process = subprocess.Popen(args,
                                       stdout=gdbgui_out,
                                       stderr=subprocess.STDOUT,
                                       bufsize=1,
                                       env=env)
        except Exception as e:
            print(e)
            raise FatalError(
                'Error starting gdbgui. Please make sure gdbgui has been installed with '
                '"install.{sh,bat,ps1,fish} --enable-gdbgui" and can be started.',
                ctx)

        processes['gdbgui'] = process
        processes['gdbgui_outfile'] = gdbgui_out
        processes['gdbgui_outfile_name'] = gdbgui_out_name
        print('gdbgui started as a background task {}'.format(process.pid))
        _check_openocd_errors(fail_if_openocd_failed, action, ctx)