build/openocd: add find_config method to allow using local config file or download...
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Tue, 5 May 2020 07:56:13 +0000 (09:56 +0200)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Tue, 5 May 2020 07:56:13 +0000 (09:56 +0200)
litex/build/generic_programmer.py
litex/build/openocd.py

index 3944403007c939c1866f1d72e52875d8ca58d775..2927d82987141f02ab447fb5858a7a416201f2a1 100644 (file)
@@ -20,7 +20,10 @@ class GenericProgrammer:
         self.flash_proxy_repos = [
             "https://github.com/quartiq/bscan_spi_bitstreams/raw/master/",
         ]
-        self.flash_proxy_local = "flash_proxies"
+        self.config_repos = [
+            "https://raw.githubusercontent.com/litex-hub/litex-boards/master/litex_boards/prog/",
+        ]
+        self.prog_local = "prog"
 
     def set_flash_proxy_dir(self, flash_proxy_dir):
         if flash_proxy_dir is not None:
@@ -34,14 +37,14 @@ class GenericProgrammer:
             if os.path.exists(fullname):
                 return fullname
         # Search in local flash_proxy directory
-        fullname = tools.cygpath(os.path.join(self.flash_proxy_local, self.flash_proxy_basename))
+        fullname = tools.cygpath(os.path.join(self.prog_local, self.flash_proxy_basename))
         if os.path.exists(fullname):
             return fullname
         # Search in repositories and download it
         import requests
-        os.makedirs(self.flash_proxy_local, exist_ok=True)
+        os.makedirs(self.prog_local, exist_ok=True)
         for d in self.flash_proxy_repos:
-            fullname = tools.cygpath(os.path.join(self.flash_proxy_local, self.flash_proxy_basename))
+            fullname = tools.cygpath(os.path.join(self.prog_local, self.flash_proxy_basename))
             try:
                 r = requests.get(d + self.flash_proxy_basename)
                 with open(fullname, "wb") as f:
@@ -51,6 +54,29 @@ class GenericProgrammer:
                 pass
         raise OSError("Failed to find flash proxy bitstream")
 
+    def find_config(self):
+        # Search in local directory
+        fullname = tools.cygpath(self.config)
+        if os.path.exists(fullname):
+            return self.config
+        # Search in local config directory
+        fullname = tools.cygpath(os.path.join(self.prog_local, self.config))
+        if os.path.exists(fullname):
+            return fullname
+        # Search in repositories and download it
+        import requests
+        os.makedirs(self.prog_local, exist_ok=True)
+        for d in self.config_repos:
+            fullname = tools.cygpath(os.path.join(self.prog_local, self.config))
+            try:
+                r = requests.get(d + self.config)
+                with open(fullname, "wb") as f:
+                    f.write(r.content)
+                return fullname
+            except:
+                pass
+        raise OSError("Failed to find config file")
+
     # Must be overloaded by specific programmer
     def load_bitstream(self, bitstream_file):
         raise NotImplementedError
index c149ca2ccd78e2baede767a1b1c52dfa0d168849..d3680d60990fc6aff8d657711ebb3df8f287d154 100644 (file)
@@ -17,14 +17,16 @@ class OpenOCD(GenericProgrammer):
         self.config = config
 
     def load_bitstream(self, bitstream):
+        config = self.find_config()
         script = "; ".join([
             "init",
             "pld load 0 {{{}}}".format(bitstream),
             "exit",
         ])
-        subprocess.call(["openocd", "-f", self.config, "-c", script])
+        subprocess.call(["openocd", "-f", config, "-c", script])
 
     def flash(self, address, data, set_qe=False):
+        config      = self.find_config()
         flash_proxy = self.find_flash_proxy()
         script = "; ".join([
             "init",
@@ -34,7 +36,7 @@ class OpenOCD(GenericProgrammer):
             "fpga_program",
             "exit"
         ])
-        subprocess.call(["openocd", "-f", self.config, "-c", script])
+        subprocess.call(["openocd", "-f", config, "-c", script])
 
 
     def stream(self, port=20000):