Blink all LEDs at 1 Hz if any board is run as __main__.
authorwhitequark <whitequark@whitequark.org>
Tue, 4 Jun 2019 11:06:01 +0000 (11:06 +0000)
committerwhitequark <whitequark@whitequark.org>
Tue, 4 Jun 2019 11:06:01 +0000 (11:06 +0000)
nmigen_boards/_blinky.py [new file with mode: 0644]
nmigen_boards/ice40_hx1k_blink_evn.py
nmigen_boards/icestick.py
nmigen_boards/tinyfpga_bx.py

diff --git a/nmigen_boards/_blinky.py b/nmigen_boards/_blinky.py
new file mode 100644 (file)
index 0000000..1e533ef
--- /dev/null
@@ -0,0 +1,35 @@
+import itertools
+
+from nmigen import *
+from nmigen.build import ConstraintError
+
+
+class Blinky(Elaboratable):
+    def elaborate(self, platform):
+        m = Module()
+
+        clk_name, clk_freq = next(iter(platform.clocks.items()))
+        clk = platform.request(*clk_name)
+        m.domains.sync = ClockDomain()
+        m.d.comb += ClockSignal().eq(clk.i)
+
+        leds = []
+        for n in itertools.count():
+            try:
+                leds.append(platform.request("user_led", n))
+            except ConstraintError:
+                break
+        leds = Cat(led.o for led in leds)
+
+        ctr = Signal(max=int(clk_freq//2), reset=int(clk_freq//2) - 1)
+        with m.If(ctr == 0):
+            m.d.sync += ctr.eq(ctr.reset)
+            m.d.sync += leds.eq(~leds)
+        with m.Else():
+            m.d.sync += ctr.eq(ctr - 1)
+
+        return m
+
+
+def build_and_program(platform_cls, **kwargs):
+    platform_cls().build(Blinky(), do_program=True, **kwargs)
index fc80d6463a70ea995db8d9a78daf0ee5e60616d8..ce72be6498190016a65f662cd4bf910a656eff24 100644 (file)
@@ -40,3 +40,8 @@ class ICE40HX1KBlinkEVNPlatform(LatticeICE40Platform):
         iceburn = os.environ.get("ICEBURN", "iCEburn")
         with products.extract("{}.bin".format(name)) as bitstream_filename:
             subprocess.run([iceburn, "-evw", bitstream_filename], check=True)
+
+
+if __name__ == "__main__":
+    from ._blinky import build_and_program
+    build_and_program(ICE40HX1KBlinkEVNPlatform)
index 1fe7954d48cf3f8d59b54293821696bf019bca31..fa58df444452bb7e742a408534cc04f5cd05575b 100644 (file)
@@ -61,3 +61,8 @@ class ICEStickPlatform(LatticeICE40Platform):
         iceprog = os.environ.get("ICEPROG", "iceprog")
         with products.extract("{}.bin".format(name)) as bitstream_filename:
             subprocess.run([iceprog, bitstream_filename], check=True)
+
+
+if __name__ == "__main__":
+    from ._blinky import build_and_program
+    build_and_program(ICEStickPlatform)
index 087f6d9f173379b41805541adab5a31aa474df2b..3793a58b8871a1105c65e8a100ea83913e246aea 100644 (file)
@@ -61,3 +61,8 @@ class TinyFPGABXPlatform(LatticeICE40Platform):
         tinyprog = os.environ.get("TINYPROG", "tinyprog")
         with products.extract("{}.bin".format(name)) as bitstream_filename:
             subprocess.run([tinyprog, "-p", bitstream_filename], check=True)
+
+
+if __name__ == "__main__":
+    from ._blinky import build_and_program
+    build_and_program(TinyFPGABXPlatform)