build/generic_programmer: add automatic search/download of flash_proxy in repositorie...
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Wed, 15 Apr 2020 06:59:03 +0000 (08:59 +0200)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Wed, 15 Apr 2020 06:59:03 +0000 (08:59 +0200)
litex/build/generic_programmer.py

index ac22c74f2957d9f611b971f70499c790cc352037..2fb344443f0e5e18adb8e9e3e6d3a9fb29292cc3 100644 (file)
@@ -1,8 +1,11 @@
 # This file is Copyright (c) 2015 Sebastien Bourdeauducq <sb@m-labs.hk>
+# This file is Copyright (c) 2020 Florent Kermarrec <florent@enjoy-digital.fr>
 # License: BSD
 
 
-import os, sys
+import os
+import sys
+import requests
 from litex.build import tools
 
 
@@ -12,25 +15,46 @@ class GenericProgrammer:
         self.flash_proxy_dirs = [
             "~/.migen", "/usr/local/share/migen", "/usr/share/migen",
             "~/.mlabs", "/usr/local/share/mlabs", "/usr/share/mlabs",
-            "~/.litex", "/usr/local/share/litex", "/usr/share/litex"]
+            "~/.litex", "/usr/local/share/litex", "/usr/share/litex"
+        ]
+        self.flash_proxy_repos = [
+            "https://github.com/quartiq/bscan_spi_bitstreams/raw/master/",
+        ]
+        self.flash_proxy_local = "flash_proxies"
 
     def set_flash_proxy_dir(self, flash_proxy_dir):
         if flash_proxy_dir is not None:
             self.flash_proxy_dirs = [flash_proxy_dir]
 
     def find_flash_proxy(self):
+        # Search in installed flash_proxy_directories
         for d in self.flash_proxy_dirs:
-            fulldir = os.path.abspath(os.path.expanduser(d))
+            fulldir  = os.path.abspath(os.path.expanduser(d))
             fullname = tools.cygpath(os.path.join(fulldir, self.flash_proxy_basename))
             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))
+        if os.path.exists(fullname):
+            return fullname
+        # Search in repositories and download it
+        os.makedirs(self.flash_proxy_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))
+            try:
+                r = requests.get(d + self.flash_proxy_basename)
+                with open(fullname, "wb") as f:
+                    f.write(r.content)
+                return fullname
+            except:
+                pass
         raise OSError("Failed to find flash proxy bitstream")
 
-    # must be overloaded by specific programmer
+    # Must be overloaded by specific programmer
     def load_bitstream(self, bitstream_file):
         raise NotImplementedError
 
-    # must be overloaded by specific programmer
+    # Must be overloaded by specific programmer
     def flash(self, address, data_file):
         raise NotImplementedError