From cfab857c7b9808db6441183cad8d9e61dd196b4d Mon Sep 17 00:00:00 2001 From: curliph Date: Tue, 8 Mar 2022 13:16:01 +0800 Subject: [PATCH 1/2] win/powershell support. add gowin programmer support. --- litex/build/gowin/gowin.py | 1 + litex/build/gowin/programmer.py | 98 +++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100755 litex/build/gowin/programmer.py diff --git a/litex/build/gowin/gowin.py b/litex/build/gowin/gowin.py index c48bb1a89..92068a843 100644 --- a/litex/build/gowin/gowin.py +++ b/litex/build/gowin/gowin.py @@ -67,6 +67,7 @@ def _build_tcl(name, partnumber, files, options): # Add Sources. for f, typ, lib in files: + f = f.replace("\\", "\\\\") tcl.append(f"add_file {f}") # Set Options. diff --git a/litex/build/gowin/programmer.py b/litex/build/gowin/programmer.py new file mode 100755 index 000000000..45f6fa6fd --- /dev/null +++ b/litex/build/gowin/programmer.py @@ -0,0 +1,98 @@ +# +# This file is part of LiteX. +# +# Copyright (c) 2022-2023 Franz Zhou +# SPDX-License-Identifier: BSD-2-Clause + +import os +import sys +from shutil import which + +from litex.build.generic_programmer import GenericProgrammer +from litex.build import tools + +# GowinProgrammer -------------------------------------------------------------------------------- + +GOWIN_PMODE_SRAM = 4 +GOWIN_PMODE_EMBFLASH = 5 +GOWIN_PMODE_EXTFLASH = 31 # for bin + +GOWIN_CABLE_GWU2X = 0 +GOWIN_CABLE_FT2CH = 1 + +# for all other options, please run 'programmer_cli -h' for details +# feel free to add any options for your purpose. + +class GowinProgrammer(GenericProgrammer): + needs_bitreverse = False + + def __init__(self, devname): + self.device = str(devname) + + # windows/powershell or msys2 + self.is_win32 = True if sys.platform == "win32" else False + + self.is_wsl = False + if sys.platform.find("linux") >= 0: + self.is_wsl = os.uname().release.find("WSL") > 0 + + self.programmer = "programmer_cli" + + # note for WSL: + # gowin programmer_cli not working out of it's directory + if self.is_wsl or self.is_win32: + self.programmer += ".exe" + + gw_dir = which(self.programmer) + if gw_dir is not None: + gw_dir = os.path.dirname(gw_dir) + os.chdir(gw_dir) + + # follow the help information: + # 1. Gowin programmer does not support start address for embflash! + # 2. External SPI FLASH programming is also not detailed (for programmer_cli) + # 3. Verify usually got stuck, so we disable it by now. patch is welcome! + def flash(self, address = 0, data_file = None, external = False, fifile = None, verify = False, cable = GOWIN_CABLE_FT2CH): + pmode = (GOWIN_PMODE_EMBFLASH + 1) if verify else GOWIN_PMODE_EMBFLASH + + if external is True: + pmode = (GOWIN_PMODE_EXTFLASH + 1) if verify else GOWIN_PMODE_EXTFLASH + + if data_file is None and fifile is None: + print("GowinProgrammer: fsFile or fiFile should be given!"); + exit(1) + + if self.is_wsl is True and data_file is not None: + data_file = os.popen("wslpath -w {}".format(data_file)).read().strip("\n") + + if self.is_wsl is True and fifile is not None: + fifile = os.popen("wslpath -w {}".format(fifile)).read().strip("\n") + + cmd_line = [self.programmer, + "--spiaddr", str(address), + "--device", str(self.device)] + + if data_file is not None: + if external is True: + cmd_line += ["--mcuFile", str(data_file)] + else: + cmd_line += ["--fsFile", str(data_file)] + + if fifile is not None: + cmd_line += ["--fiFile", str(fifile)] + + cmd_line += ["--cable-index", str(cable), "--operation_index", str(pmode)] + self.call(cmd_line) + + def load_bitstream(self, bitstream_file, verify = False, cable = GOWIN_CABLE_FT2CH): + pmode = 4 if verify else 2 + bitfile = bitstream_file + if self.is_wsl is True: + bitfile = os.popen("wslpath -w {}".format(bitstream_file)).read().strip("\n") + + cmd_line = [self.programmer, + "--device", str(self.device), + "--fsFile", str(bitfile), + "--cable-index", str(cable), + "--operation_index", str(pmode)] + self.call(cmd_line) From 700077e4a11810bf319c07013482e68d475fc8d2 Mon Sep 17 00:00:00 2001 From: curliph Date: Tue, 8 Mar 2022 13:52:03 +0800 Subject: [PATCH 2/2] powershell and WSL support --- litex/build/gowin/gowin.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/litex/build/gowin/gowin.py b/litex/build/gowin/gowin.py index 92068a843..ae7b24638 100644 --- a/litex/build/gowin/gowin.py +++ b/litex/build/gowin/gowin.py @@ -6,6 +6,7 @@ # SPDX-License-Identifier: BSD-2-Clause import os +import sys import math import subprocess from shutil import which @@ -67,7 +68,9 @@ def _build_tcl(name, partnumber, files, options): # Add Sources. for f, typ, lib in files: - f = f.replace("\\", "\\\\") + # Support windows/powershell + if sys.platform == "win32": + f = f.replace("\\", "\\\\") tcl.append(f"add_file {f}") # Set Options. @@ -162,12 +165,19 @@ class GowinToolchain: # Run if run: - if which("gw_sh") is None: + # Support Powershell/WSL platform + # Some python distros for windows (e.g, oss-cad-suite) + # which does not have 'os.uname' support, we should check 'sys.platform' firstly. + gw_sh = "gw_sh" + if sys.platform.find("linux") >= 0: + if os.uname().release.find("WSL") > 0: + gw_sh += ".exe" + if which(gw_sh) is None: msg = "Unable to find Gowin toolchain, please:\n" msg += "- Add Gowin toolchain to your $PATH." raise OSError(msg) - if subprocess.call(["gw_sh", "run.tcl"]) != 0: + if subprocess.call([gw_sh, "run.tcl"]) != 0: raise OSError("Error occured during Gowin's script execution.") os.chdir(cwd)