From: whitequark Date: Tue, 4 Jun 2019 11:06:01 +0000 (+0000) Subject: Blink all LEDs at 1 Hz if any board is run as __main__. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=ecda2a1d08af7c226c87c5bd40fc5d634a4b06a9;p=nmigen-boards.git Blink all LEDs at 1 Hz if any board is run as __main__. --- diff --git a/nmigen_boards/_blinky.py b/nmigen_boards/_blinky.py new file mode 100644 index 0000000..1e533ef --- /dev/null +++ b/nmigen_boards/_blinky.py @@ -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) diff --git a/nmigen_boards/ice40_hx1k_blink_evn.py b/nmigen_boards/ice40_hx1k_blink_evn.py index fc80d64..ce72be6 100644 --- a/nmigen_boards/ice40_hx1k_blink_evn.py +++ b/nmigen_boards/ice40_hx1k_blink_evn.py @@ -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) diff --git a/nmigen_boards/icestick.py b/nmigen_boards/icestick.py index 1fe7954..fa58df4 100644 --- a/nmigen_boards/icestick.py +++ b/nmigen_boards/icestick.py @@ -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) diff --git a/nmigen_boards/tinyfpga_bx.py b/nmigen_boards/tinyfpga_bx.py index 087f6d9..3793a58 100644 --- a/nmigen_boards/tinyfpga_bx.py +++ b/nmigen_boards/tinyfpga_bx.py @@ -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)