Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
7f96cc8e3This file is pinned to the selected release. Download the original to open it in your design tool.
#
# This file is part of HackRF.
#
# Copyright (c) 2025 Great Scott Gadgets <info@greatscottgadgets.com>
# SPDX-License-Identifier: BSD-3-Clause
from amaranth import Elaboratable, Module, Cat, DomainRenamer, Signal
from amaranth.lib import cdc
from amaranth.lib.wiring import connect
from amaranth_future import fixed
from board import PralinePlatform, ClockDomainGenerator
from interface import MAX586xInterface, SGPIOInterface, SPIRegisterInterface
from dsp.fir import FIRFilter
from dsp.fir_mac16 import HalfBandInterpolatorMAC16
from dsp.cic import CICInterpolator
from util import ClockConverter, IQSample, StreamSkidBuffer
class Top(Elaboratable):
def elaborate(self, platform):
m = Module()
m.submodules.clkgen = ClockDomainGenerator()
adc_clk = "adclk"
dac_clk = "daclk"
# Submodules.
m.submodules.adcdac_intf = adcdac_intf = MAX586xInterface(adc_domain=adc_clk, dac_domain=dac_clk)
m.submodules.mcu_intf = mcu_intf = SGPIOInterface(
sample_width=24,
rx_assignments=[
lambda w: w[0:8],
lambda w: Cat(w[8:12], w[11].replicate(4)),
lambda w: w[12:20],
lambda w: Cat(w[20:24], w[23].replicate(4)),
],
tx_assignments=[
lambda w, v: w[0:8].eq(v),
lambda w, v: w[8:12].eq(v),
lambda w, v: w[12:20].eq(v),
lambda w, v: w[20:24].eq(v),
],
domain="sync"
)
m.d.comb += adcdac_intf.q_invert.eq(platform.request("q_invert").i)
# Half-band filter taps.
taps_hb1 = [-2, 0, 5, 0, -10, 0, 18, 0, -30, 0, 53, 0, -101, 0, 323, 512, 323, 0, -101, 0, 53, 0, -30, 0, 18, 0, -10, 0, 5, 0, -2]
taps_hb1 = [ tap/1024 for tap in taps_hb1 ]
taps_hb2 = [3, 0, -16, 0, 77, 128, 77, 0, -16, 0, 3]
taps_hb2 = [ tap/256 for tap in taps_hb2 ]
tx_chain = {
# Clock domain conversion.
"clkconv": ClockConverter(IQSample(12), 8, "sync", dac_clk, always_ready=False),
# Half-band interpolation stages (+ skid buffers for timing closure).
"hbfir1": HalfBandInterpolatorMAC16(taps_hb1, data_shape=fixed.SQ(1,11), shape_out=fixed.SQ(1,11),
overclock_rate=8, num_channels=2, always_ready=False, domain=dac_clk),
"skid1": DomainRenamer(dac_clk)(StreamSkidBuffer(IQSample(12), always_ready=False)),
"hbfir2": HalfBandInterpolatorMAC16(taps_hb2, data_shape=fixed.SQ(1,11), shape_out=fixed.SQ(1,11),
overclock_rate=4, num_channels=2, always_ready=False, domain=dac_clk),
"skid2": DomainRenamer(dac_clk)(StreamSkidBuffer(IQSample(12), always_ready=False)),
# CIC interpolation stage.
"cic_comp": DomainRenamer(dac_clk)(FIRFilter([-0.125, 0, 0.75, 0, -0.125],
shape=fixed.SQ(1,11), shape_out=fixed.SQ(1,11), always_ready=False, num_channels=2)),
"skid3": DomainRenamer(dac_clk)(StreamSkidBuffer(IQSample(12), always_ready=False)),
"cic_interpolator": CICInterpolator(2, 4, (4, 8, 16, 32), 12, 8, num_channels=2,
always_ready=False, domain=dac_clk),
}
for k,v in tx_chain.items():
m.submodules[f"tx_{k}"] = v
# Connect transmitter chain.
last = mcu_intf.dac_stream
for block in tx_chain.values():
connect(m, last, block.input)
last = block.output
connect(m, last, adcdac_intf.dac_stream)
# SPI register interface.
spi_port = platform.request("spi")
m.submodules.spi_regs = spi_regs = SPIRegisterInterface(spi_port)
# Add control registers.
ctrl = spi_regs.add_register(0x01, init=0)
tx_intrp = Signal(3, init=4)
tx_intrp_cic = Signal.like(tx_chain["cic_interpolator"].factor, init=2)
tx_intrp_new = Signal(3)
tx_intrp_stb = Signal()
spi_regs.add_sfr(0x05, read=tx_intrp, write_signal=tx_intrp_new, write_strobe=tx_intrp_stb)
m.d.comb += [
# Trigger enable.
mcu_intf.trigger_en .eq(ctrl[7]),
]
# TX interpolation rate.
m.submodules.rx_decim_cdc = cdc.FFSynchronizer(tx_intrp_cic, tx_chain["cic_interpolator"].factor, o_domain=dac_clk)
with m.If(tx_intrp_stb):
with m.If(tx_intrp_new < 4):
m.d.sync += tx_intrp.eq(4)
m.d.sync += tx_intrp_cic.eq(2)
with m.Else():
m.d.sync += tx_intrp.eq(tx_intrp_new)
m.d.sync += tx_intrp_cic.eq(tx_intrp_new-2)
return m
if __name__ == "__main__":
plat = PralinePlatform()
plat.build(Top())