nmigen-learning/fpga/icebreaker_vga.dg

95 lines
3.0 KiB
Plaintext
Raw Normal View History

2020-08-15 15:46:37 +02:00
import "/collections/namedtuple"
2020-08-15 02:20:42 +02:00
import "/nmigen/lib/cdc/ResetSynchronizer"
import "/nmigen_boards.icebreaker/ICEBreakerPlatform"
import "/nmigen_dg/*"
2020-08-15 15:46:37 +02:00
import "/subprocess"
2020-08-15 02:20:42 +02:00
import "/sys"
2020-08-15 15:46:37 +02:00
import "common/pipeline"
2020-08-15 02:20:42 +02:00
import "resources/pmod"
2020-08-15 15:46:37 +02:00
run_icepll = current target ->
current /= 1e6 # Hz -> MHz
target /= 1e6 # Hz -> MHz
out = subprocess.run ["icepll", "-i", str current, "-o", str target]
capture_output: True
check: True
get_field = field substring:None ->
pipeline out.stdout.decode!.splitlines!
bind filter $ x -> x.startswith field
if
substring => bind filter $ x -> substring in x
otherwise => x -> x # nop
head
str.split
tail
head
(namedtuple "icepll_config" "FEEDBACK FILTER_RANGE DIVR DIVF DIVQ given requested achieved")
str$ get_field "FEEDBACK"
int$ get_field "FILTER_RANGE"
int$ get_field "DIVR"
int$ get_field "DIVF"
int$ get_field "DIVQ"
float$ get_field "F_PLLIN"
float$ get_field "F_PLLOUT" "(requested)"
float$ get_field "F_PLLOUT" "(achieved)"
2020-08-15 02:20:42 +02:00
Top = subclass Elaboratable where
elaborate = platform ~> m where with m = Module! =>
# setup clock
2020-08-15 15:46:37 +02:00
default_clk = platform.request platform.default_clk dir:"-"
default_freq = platform.default_clk_frequency
default_reset = if
platform.default_rst => platform.request platform.default_rst
otherwise => Const 1
2020-08-15 02:20:42 +02:00
2020-08-15 15:46:37 +02:00
pll_config = run_icepll
default_freq
40e6
print dvi.timings
print pll_config
pll_clk, pll_lock = Signal!, Signal!
2020-08-15 02:20:42 +02:00
Submodule.pll$ Instance "SB_PLL40_PAD" # "SB_PLL40_CORE"
2020-08-15 15:46:37 +02:00
i_PACKAGEPIN : default_clk # i_REFERENCECLK
2020-08-15 02:20:42 +02:00
i_BYPASS : (Const 0)
2020-08-15 15:46:37 +02:00
i_RESETB : default_reset
2020-08-15 02:20:42 +02:00
o_LOCK : pll_lock
2020-08-15 15:46:37 +02:00
o_PLLOUTGLOBAL : pll_clk
p_FEEDBACK_PATH : pll_config.FEEDBACK
p_FILTER_RANGE : pll_config.FILTER_RANGE
p_DIVR : pll_config.DIVR
p_DIVF : pll_config.DIVF
p_DIVQ : pll_config.DIVQ
2020-08-15 02:20:42 +02:00
Domains.sync = ClockDomain "sync"
2020-08-15 15:46:37 +02:00
Comb$ ClockSignal "sync" :== pll_clk
Submodule.rs$ ResetSynchronizer
~pll_lock
domain: "sync"
2020-08-15 02:20:42 +02:00
2020-08-15 15:46:37 +02:00
# Simple test to see if the PLL works
2020-08-15 02:20:42 +02:00
state = Signal!
counter = Signal$ range (int plat.default_clk_frequency)
Sync$ counter :== counter - 1
When (counter==0) $ ->
2020-08-15 15:46:37 +02:00
Sync$ counter :== int plat.default_clk_frequency
2020-08-15 02:20:42 +02:00
Sync$ state :== ~state
2020-08-15 15:46:37 +02:00
Comb$ platform.request "led_r" :== state
Comb$ platform.request "led_g" :== ~state
2020-08-15 02:20:42 +02:00
if __name__ == "__main__" =>
plat = ICEBreakerPlatform!
plat.add_resources$ pmod.dvi_12bit 0
plat.add_resources$ plat.break_off_pmod
plat.build Top! do_program: ("--flash" in sys.argv)