Skip to content

Instantly share code, notes, and snippets.

@ECP5-PCIe
Created January 16, 2022 04:40
Show Gist options
  • Save ECP5-PCIe/611e3355489ca26222c080cb67a78505 to your computer and use it in GitHub Desktop.
Save ECP5-PCIe/611e3355489ca26222c080cb67a78505 to your computer and use it in GitHub Desktop.
Time to digital test
# Based on https://gist.github.com/newhouseb/784cc0c24f8681c3224c15758be5d1b8
# Connect serial monitor with 115200 baud over the FTDI (might require fixing the FTDI with https://github.com/trabucayre/fixFT2232_ecp5evn)
# WARNING: Maybe this might damage your FPGA (this one seems fine, even after running at 1+ GHz for extended time periods)
# Code for ECP5 EVN board
from amaranth import *
from amaranth.build import *
from amaranth.lib.coding import Decoder
from amaranth_boards import ecp5_5g_evn as FPGA
from amaranth_stdio.serial import AsyncSerial
def create_delay_line(m, start: Signal, length):
def get_slice():
start_x = 10
start_y = 11
slice_i = 0
while True:
yield "X{}/Y{}/SLICE{}".format(start_x, start_y, ["A","B","C","D"][slice_i])
if slice_i == 3:
start_x += 1
slice_i = (slice_i + 1) % 4
gen_slice = get_slice()
intoff = Signal(length + 1)
thermometer = Signal(length)
carry = Signal(length//2 + 1)
m.submodules.delay_start = Instance("TRELLIS_SLICE",
a_BEL=next(gen_slice),
p_MODE="CCU2",
p_CCU2_INJECT1_0="NO",
p_CCU2_INJECT1_1="YES",
p_LUT0_INITVAL=0x00FF,
p_LUT1_INITVAL=0x00FF,
i_D1=start,
o_FCO=carry[0]
)
for i in range(length // 2):
delay = Instance("TRELLIS_SLICE",
a_BEL=next(gen_slice),
p_MODE="CCU2",
p_CCU2_INJECT1_0="NO",
p_CCU2_INJECT1_1="NO",
p_LUT0_INITVAL=0x3300,
p_LUT1_INITVAL=0x3300,
p_REG0_SD="1",
p_REG1_SD="1",
i_B0=0,
i_B1=0,
i_LSR=0,
o_F0=intoff[2*i + 0], # Out of the LUT
i_DI0=intoff[2*i + 0], # Into the FF
o_Q0=thermometer[2*i + 0], # Out of the FF
o_F1=intoff[2*i + 1],
i_DI1=intoff[2*i + 1], # Not i_F0?
o_Q1=thermometer[2*i + 1],
i_FCI=carry[i],
o_FCO=carry[i+1],
i_CLK=ClockSignal()
)
setattr(m.submodules, "delay_" + str(i), delay)
return thermometer
class FabricTDC1(Elaboratable):
def __init__(self):
pass
def elaborate(self, platform):
m = Module()
time = Signal(64)
m.d.sync += time.eq(time + 1)
intermediate = Signal() # 840 MHz
fb = Signal()
test_sig = Signal()
fb2 = Signal()
# Adjust this one
m.submodules += Instance("EHXPLLL",
a_LOC = "PLL3", # Location, one of PLL0, 1, 2, 3
p_OUTDIVIDER_MUXA='DIVA',
p_CLKOP_ENABLE='ENABLED',
p_CLKOS_ENABLE='ENABLED',
p_CLKOP_DIV=5, # PLL frequency in 100 MHz steps (in this case 500 MHz)
p_CLKFB_DIV=1,
p_CLKI_DIV=1, # Reduce PLL frequency steps to 100 MHz / n
p_CLKOS_DIV=2, # Output frequency, fed into the delay line, PLL frequency / n (in this case 500 MHz / 2 = 250 MHz)
p_FEEDBK_PATH='CLKOP',
i_CLKI=intermediate,
i_ENCLKOP=1,
i_ENCLKOS=1,
o_CLKOS=test_sig,
i_CLKFB=fb2,
o_CLKOP=fb2,
)
# This PLL outputs a 100 MHz frequency (12 * 25 / 3)
m.submodules += Instance("EHXPLLL",
p_OUTDIVIDER_MUXA='DIVA',
p_CLKOP_ENABLE='ENABLED',
p_CLKOS_ENABLE='ENABLED',
p_CLKOP_DIV=25,
p_CLKFB_DIV=1,
p_CLKI_DIV=1,
p_CLKOS_DIV=3,
p_FEEDBK_PATH='CLKOP',
i_CLKI=ClockSignal("sync"),
i_ENCLKOP=1,
i_ENCLKOS=1,
o_CLKOS=intermediate,
i_CLKFB=fb,
o_CLKOP=fb,
)
t_len = 512
thermometer = create_delay_line(m, test_sig, t_len)
m.submodules.uart = uart = AsyncSerial(divisor = int(104), pins = platform.request("uart", 0)) # 115200 Baud
buffer = Signal(t_len)
digit = Signal(range(t_len))
with m.FSM(name="Debug"):
with m.State("Start"):
with m.If(uart.tx.rdy):
m.d.comb += uart.tx.data.eq(ord("\n"))
m.d.comb += uart.tx.ack.eq(1)
m.d.sync += buffer.eq(thermometer)
m.d.sync += digit.eq(0)
m.next = "Wait"
with m.State("Wait"):
with m.If(uart.tx.rdy & (time[:22] == 0)):
m.next = "Send"
with m.State("Send"):
with m.If(uart.tx.rdy):
with m.If(digit == t_len - 1):
m.next = "Start"
m.d.comb += uart.tx.data.eq(buffer.bit_select(digit, 1) + ord("0"))
m.d.comb += uart.tx.ack.eq(1)
m.d.sync += digit.eq(digit + 1)
return m
if __name__ == "__main__":
FPGA.ECP55GEVNPlatform().build(FabricTDC1(), do_program=True, nextpnr_opts="--timing-allow-fail -r")
with open("build/top.tim") as logfile:
for line in logfile.read().split("\n"):
if "test_sig" in line:
print()
print("Clock frequency:", line.split("MHz")[0].split()[-1], "MHz")
# Examples:
#11111111111111111010000000000000000000000000000111111111111111111111111111111111111010000000000000000000000000000101111111111111
#11111111111111111111111111111111101000000000000000000000000000000111111111111111111111111111111111101000000000000000000000000000
#11111111111111111111111111111111111010000000000000000000000000000001011111111111111111111111111111111110100000000000000000000000
#11111111111111111110100000000000000000000000000000000000000000011111111111111111111111111111111111111111111110100000000000000000
#00000000000000000001111111111111111111111111111111111111111111111110100000000000000000000000000000000000000000011111111111111111
#11101000000000000000000111111111111111111111111110100000000000000000000111111111111111111111111010000000000000000000010111111111111111111110100000000000000000000000011111111111111111111010000000000000000000000001011111111111111111101000000000000000000000000000111111111111111110000000000000000000000000000001111111111111101000000000000000000000000000000001111111111110100000000000000000000000000000000111111111101000000000000000000000000000000000001111111110101000000000000000000000000000000010101110101000000000
#11111111111111111110100000000000000001111111111111111111101000000000000000000111111111111111111010000000000000000001111111111111111110100000000000000000000111111111111111101000000000000000000000011111111111111010000000000000000000000011111111111110100000000000000000000000001011111111101000000000000000000000000000101110111010000000000000000000000000000010101010100000000000000000000000000000101010100000000000000000000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000
#00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#11111111101000000000000001111111111111111111101000000000000000011111111111111111101000000000000000000111111111111111101000000000000000000001111111111111101000000000000000000001111111111111111010000000000000000000001111111111101010000000000000000000000011111111111010000000000000000000000000101011111010000000000000000000000000000010101010100000000000000000000000000000101010100000000000000000000000000000100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#11101000000000000001111111111111111110100000000000000011111111111111111110100000000000000001111111111111111010000000000000000001111111111111101000000000000000000000111111111111101000000000000000000001111111111110100000000000000000000000101111111110100000000000000000000000001010111010100000000000000000000000001010101010000000000000000000000000000010101000000000000000000000000000000010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#11111111111111111111111110100000000000000000000000000111111111111111111111111110100000000000000000000000000001111111111111111111111010000000000000000000000000000000001111111111111111111010100000000000000000000000000000000101111111111111111010000000000000000000000000000000000000011111111111111110100000000000000000000000000000000000000011111111111010000000000000000000000000000000000000000011111111111010000000000000000000000000000000000000000000111111111110000000000000000000000000000000000000000000101111101000
#00000000101111111010000000000000000000000000101110101000000000000000000000000000101010100000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#00000000000000000000000011111111101000000000000000000000000010111110111000000000000000000000000000101011101000000000000000000000000000000010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#10100000000000000000001111111111111010000000000000000000001111111111111000000000000000000000001111111111100000000000000000000000001010111010100000000000000000000000001010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#00000000001111111111111111111000000000000001111111111111111110100000000000001111111111111111111010000000000001111111111111111010000000000000000011111111111110101000000000000000111111111111111010000000000000000001111111111110100000000000000000000011111111101000000000000000000000001010101010000000000000000000000000101010100000000000000000000000000010101010000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#00000000000000000010101110100000000000000000000000001010101010000000000000000000000000001010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#00001111111111101000000000000000000011111111111000000000000000000000111111101000000000000000000000001011101010000000000000000000000000101010000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#00000011111111111010000000000000000010111111111010000000000000000000101011101000000000000000000000001010101000000000000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#00000011111111111000000000000000000000111111111000000000000000000000101011101000000000000000000000001010101000000000000000000000000010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#11111110000000000000000000111111111110100000000000000000000010101110100000000000000000000000001010101000000000000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#11111111111111111000000000000000000000000001011111111111111111111111111000000000000000000000000000011111111111111111111111101000000000000000000000000000000111111111111111111111101000000000000000000000000000000001111111111111111111101000000000000000000000000000000000011111111111111111101000000000000000000000000000000000000001111111111111101000000000000000000000000000000000000000111111111111100000000000000000000000000000000000000000001111111111101000000000000000000000000000000000000000111111111110100000000000
#11111111111111101000000000000000000000000001111111111111111111111110101000000000000000000000000000011111111111111111111111101000000000000000000000000000000111111111111111111110100000000000000000000000000000000001111111111111111111101000000000000000000000000000000000011111111111111111101000000000000000000000000000000000000011111111111111101000000000000000000000000000000000000001111111111111100000000000000000000000000000000000000000001111111111100000000000000000000000000000000000000000111111111010000000000000
#11111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101000000000000000000000000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment