Skip to content

Commit

Permalink
Blink all LEDs at 1 Hz if any board is run as __main__.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Jun 4, 2019
1 parent 50403d6 commit ecda2a1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
35 changes: 35 additions & 0 deletions nmigen_boards/_blinky.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions nmigen_boards/ice40_hx1k_blink_evn.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ def toolchain_program(self, products, name):
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)
5 changes: 5 additions & 0 deletions nmigen_boards/icestick.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ def toolchain_program(self, products, name):
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)
5 changes: 5 additions & 0 deletions nmigen_boards/tinyfpga_bx.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ def toolchain_program(self, products, name):
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)

0 comments on commit ecda2a1

Please sign in to comment.