Skip to content

Instantly share code, notes, and snippets.

@agrif
Created August 23, 2025 02:03
Show Gist options
  • Save agrif/4d2305258a8ab6026e00358c13c30aa5 to your computer and use it in GitHub Desktop.
Save agrif/4d2305258a8ab6026e00358c13c30aa5 to your computer and use it in GitHub Desktop.
import os
import subprocess
from amaranth.build import *
from amaranth.vendor import GowinPlatform
from .resources import *
__all__ = [
"TangPrimer20kPlatform",
"TangPrimer20kLitePlatform",
"TangPrimer20kDockPlatform",
]
class TangPrimer20kPlatform(GowinPlatform):
part = "GW2A-LV18PG256C8/I7"
family = "GW2A-18C"
default_clk = "clk27"
resources = [
Resource("clk27", 0, Pins("H11", dir="i"),
Clock(27e6), Attrs(IO_TYPE="LVCMOS33")),
]
connectors = [
# GND, 5V, 3V3 are power pins
# V01 and V7 are IO bank voltages, tied to 3V3 by R5 and R9 (resp.)
Connector("sodimm", 0,
# GND GND 5V 5V 5V 5V GND GND NC ( 1- 10)
" T13 - - - - - - - - - "
# NC GND GND NC NC NC GND GND ( 11- 20)
" M11 - - - T10 - - - - - "
# NC 3V3 NC 3V3 GND GND ( 21- 30)
" - - - - - - T6 R16 P6 P16"
# GND GND GND GND ( 31- 40)
" - - T7 P16 R8 N15 - - T8 N16"
# GND GND GND ( 41- 50)
" P8 N14 - L16 T9 L14 P9 - - K15"
# GND GND GND ( 51- 60)
" P11 K14 T11 - - K16 R11 J15 T12 - "
# GND GND ( 61- 70)
" - H16 R12 H14 P13 - R13 G16 T14 H15"
# GND GND ( 71- 80)
" - - M15 L13 M14 K11 F13 K12 G12 K13"
# NC NC NC ( 81- 90)
" T15 - J16 H13 J14 J12 - - G14 H12"
# NC NC NC NC ( 91-100)
" G15 G11 - - F14 B10 F16 A13 - - "
# NC NC NC NC NC NC (101-110)
"E15 - D14 - - - A15 - B14 - "
# NC NC NC NC NC NC NC (111-120)
" - - A14 - B13 - - - C12 - "
# NC NC NC GND GND (121-130)
" B12 - A12 - C11 - - - B11 E16"
# GND GND NC GND GND (131-140)
" A11 F15 - - C10 C13 - - - D16"
# NC GND GND GND (141-150)
" - E14 B8 - - C9 C6 A9 A7 - "
# GND GND GND (151-160)
" - L12 A6 J11 - - C7 E9 D7 E8 "
# GND GND V01 V01 GND GND V7 (161-170)
" - - T2 - T3 - - - T4 - "
# GND GND V7 GND GND (171-180)
" T5 - - - N6 F10 N7 - - D11"
# GND GND NC NC GND GND (181-190)
" N9 D10 R9 - - E10 - - - - "
# GND GND NC NC (191-200)
" N8 R7 L9 P7 - - - M6 - L8 "
# NC NC NC NC (201-204)
" - - - - "
)
]
def toolchain_prepare(self, fragment, name, **kwargs):
overrides = {
"add_options":
"set_option -use_mspi_as_gpio 1 -use_sspi_as_gpio 1 -use_ready_as_gpio 1 -use_done_as_gpio 1 -rw_check_on_ram 1",
"gowin_pack_opts":
"--mspi_as_gpio --sspi_as_gpio --ready_as_gpio --done_as_gpio --",
}
return super().toolchain_prepare(fragment, name, **overrides, **kwargs)
def toolchain_program(self, products, name):
with products.extract("{}.fs".format(name)) as bitstream_filename:
subprocess.check_call(["openFPGALoader", "-b", "tangprimer20k", bitstream_filename])
class TangPrimer20kLitePlatform(TangPrimer20kPlatform):
pass
class TangPrimer20kDockPlatform(TangPrimer20kPlatform):
resources = TangPrimer20kPlatform.resources + [
*LEDResources(pins="C13 A13 N16 N14 L14 L16", invert=True,
attrs=Attrs(IO_TYPE="LVCMOS33")),
# WS2812 RGB LED
Resource("ws2812", 0, Pins("T9", dir="o"),
Attrs(IO_TYPE="LVCMOS33")),
*ButtonResources(pins={0: "T10"}, invert=True,
attrs=Attrs(IO_TYPE="LVCMOS33")),
*ButtonResources(pins={1: "T3", 2: "T2", 3: "D7", 4: "C7"}, invert=True,
attrs=Attrs(IO_TYPE="LVCMOS15")),
# FIXME these don't work. why?
*SwitchResources(pins={0: "B10"}, invert=True,
attrs=Attrs(IO_TYPE="LVCMOS33")),
*SwitchResources(pins={1: "E9", 2: "E8", 3: "T4", 4: "T5"}, invert=True,
attrs=Attrs(IO_TYPE="LVCMOS15")),
]
if __name__ == "__main__":
from .test.blinky import *
print(TangPrimer20kDockPlatform().resources)
TangPrimer20kDockPlatform().build(Blinky(), do_program=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment