Skip to content

Instantly share code, notes, and snippets.

@jimmo
Created January 23, 2018 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmo/618bd4342822a46ed1f16bd1e67b84f5 to your computer and use it in GitHub Desktop.
Save jimmo/618bd4342822a46ed1f16bd1e67b84f5 to your computer and use it in GitHub Desktop.
from litex.gen import *
# Module that toggles an LED every `cycles` clock cycles.
class BlinkerFlash(Module):
def __init__(self, led, cycles):
# Create a counter that can hold a value at least as big as `cycles`.
counter = Signal(max=cycles)
# Every time the counter gets to zero, toggle the LED.
self.sync += [
If(counter == 0,
# Reset the counter back to `cycles`.
counter.eq(cycles - 1),
# Toggle LED.
led.eq(~led),
).Else(
# Otherwise (i.e. most of the time) -- decrement the counter.
counter.eq(counter - 1),
),
]
class Blinker(Module):
def __init__(self, platform, *args, **kwargs):
led1 = platform.request('user_led')
self.submodules.blinker = BlinkerFlash(led1, cycles=int(1e9 / platform.default_clk_period))
SoC = Blinker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment