Skip to content

Instantly share code, notes, and snippets.

@opensiriusfox
Created October 7, 2023 22:23
Show Gist options
  • Save opensiriusfox/755a3fa9967ebeaac0182c7a18b0727e to your computer and use it in GitHub Desktop.
Save opensiriusfox/755a3fa9967ebeaac0182c7a18b0727e to your computer and use it in GitHub Desktop.
ULX3S custom Pmod example
#!/usr/bin/env python3
from amaranth import *
from amaranth_boards.ulx3s import ULX3S_12F_Platform as ulx3s_tgt
class LEDBlinker(Elaboratable):
"""
LED Blinker reference code pulled directly from the Amaranth reference implemenetation
https://amaranth-lang.org/docs/amaranth/latest/start.html#a-blinking-led
"""
def elaborate(self, platform):
m = Module()
led = platform.request("led")
# The following lines requests the platform give us a custom thing
# I've called `sf_led` which is a 4-pin PmodLED device.
led_pmod = platform.request("sf_led")
half_freq = int(platform.default_clk_frequency // 2)
timer = Signal(range(half_freq + 1), reset=0x00)
with m.If(timer == half_freq):
m.d.sync += led.eq(~led)
m.d.sync += timer.eq(0)
with m.Else():
m.d.sync += timer.eq(timer + 1)
# Just for visibility, make the four LEDs on the module toggle in step
# with the main LED. the top most will be aligned to the main LED,
# while the other three LEDs will be inverted
m.d.sync += led_pmod.bit_select(0,1).eq(led)
m.d.sync += led_pmod.bit_select(1,1).eq(~led)
m.d.sync += led_pmod.bit_select(2,1).eq(~led)
m.d.sync += led_pmod.bit_select(3,1).eq(~led)
return m
from amaranth.build import Resource
from amaranth.build import Pins
# This last bit of code is for the synthesis step. We define the pin-collection
# for the PMOD in the physical configuration we intend to use. I plugged in the
# PMOD into the bottom row of connectors on the side of my ulx3s board, running
# from the pins labeled 14 to the pin labeled 17.
pin_set = Pins("14+ 15+ 16+ 17+", dir="o", invert=False, conn=("gpio", 0))
# Next I indicate this is a resource that a module can request by name as `sf_led`
# and indicate on my system it is pluged into the pin set
sf_pmod_resource = Resource.family(0, default_name="sf_led", ios=[pin_set])
# The "correct" way to do this is likely to define a custom configuration for
# the hardware I'm using that inherits ULX3S_12F_Platform and further extends
# the resources based upon my use of the pins.
ULX3S = ulx3s_tgt()
ULX3S.add_resources([sf_pmod_resource])
ULX3S.build(LEDBlinker(), do_program=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment