litex/examples/de1/top.py

204 lines
5.0 KiB
Python
Raw Normal View History

2012-09-12 11:56:36 -04:00
################################################################################
# _____ _ ____ _ _ _ _
# | __|___ |_|___ _ _ | \|_|___|_| |_ ___| |
# | __| | | | . | | | | | | | . | | _| .'| |
# |_____|_|_|_| |___|_ | |____/|_|_ |_|_| |__,|_|
# |___| |___| |___|
#
# Copyright 2012 / Florent Kermarrec / florent@enjoy-digital.fr
#
2013-02-26 17:00:28 -05:00
# miscope Example on De1 Board
2012-09-12 11:56:36 -04:00
# ----------------------------------
################################################################################
#
# In this example signals are generated in the FPGA.
2013-02-26 17:00:28 -05:00
# We use miscope to record those signals and visualize them.
2012-09-12 11:56:36 -04:00
#
# Example architecture:
# ----------------------
2013-02-26 17:00:28 -05:00
# miscope Config --> Python Client (Host) --> Vcd Output
2012-09-12 11:56:36 -04:00
# & Trig |
# Arduino (Uart<-->Spi Bridge)
# |
2012-09-12 12:07:36 -04:00
# De1
2012-09-12 11:56:36 -04:00
# |
# +--------------------+-----------------------+
2013-02-26 17:00:28 -05:00
# miIo Signal Generator miLa
2012-09-12 11:56:36 -04:00
# Control of Signal Ramp, Sinus, Logic Analyzer
# generator Square, ...
###############################################################################
#==============================================================================
# I M P O R T
#==============================================================================
from migen.fhdl.structure import *
from migen.fhdl import verilog, autofragment
from migen.bus import csr
from migen.bus.transactions import *
from migen.bank import description, csrgen
from migen.bank.description import *
2013-02-26 17:00:28 -05:00
from miscope import trigger, recorder, miIo, miLa
2012-09-12 11:56:36 -04:00
import sys
sys.path.append("../../")
import spi2Csr
from timings import *
from constraints import Constraints
from math import sin
2012-09-12 11:56:36 -04:00
#==============================================================================
# P A R A M E T E R S
#==============================================================================
#Timings Param
clk_freq = 50*MHz
clk_period_ns = clk_freq*ns
n = t2n(clk_period_ns)
# Bus Width
trig0_width = 16
dat0_width = 16
trig1_width = 32
dat1_width = 32
2012-09-12 11:56:36 -04:00
# Record Size
2012-09-15 16:29:50 -04:00
record_size = 4096
2012-09-12 11:56:36 -04:00
# Csr Addr
2013-02-26 17:00:28 -05:00
MIIO0_ADDR = 0x0000
MILA0_ADDR = 0x0200
MILA1_ADDR = 0x0600
2012-09-12 11:56:36 -04:00
#==============================================================================
# M I S C O P E E X A M P L E
#==============================================================================
def get():
# migIo0
2013-02-26 17:00:28 -05:00
miIo0 = miIo.MiIo(MIIO0_ADDR, 8, "IO")
# migLa0
term0 = trigger.Term(trig0_width)
trigger0 = trigger.Trigger(trig0_width, [term0])
recorder0 = recorder.Recorder(dat0_width, record_size)
2013-02-26 17:00:28 -05:00
miLa0 = miLa.MiLa(MILA0_ADDR, trigger0, recorder0)
2012-09-14 06:57:09 -04:00
# migLa1
term1 = trigger.Term(trig1_width)
trigger1 = trigger.Trigger(trig1_width, [term1])
recorder1 = recorder.Recorder(dat1_width, record_size)
2013-02-26 17:00:28 -05:00
miLa1 = miLa.MiLa(MILA1_ADDR, trigger1, recorder1)
2012-09-12 11:56:36 -04:00
# Spi2Csr
2012-09-14 08:08:20 -04:00
spi2csr0 = spi2Csr.Spi2Csr(16,8)
2012-09-12 11:56:36 -04:00
# Csr Interconnect
csrcon0 = csr.Interconnect(spi2csr0.csr,
[
2013-02-26 17:00:28 -05:00
miIo0.bank.bus,
miLa0.trig.bank.bus,
miLa0.rec.bank.bus,
miLa1.trig.bank.bus,
miLa1.rec.bank.bus,
2012-09-12 11:56:36 -04:00
])
comb = []
sync = []
2012-09-15 16:29:50 -04:00
#
2012-09-12 11:56:36 -04:00
# Signal Generator
2012-09-15 16:29:50 -04:00
#
# Counter
2013-01-03 16:57:26 -05:00
cnt_gen = Signal(8)
2012-09-15 16:29:50 -04:00
sync += [
cnt_gen.eq(cnt_gen+1)
]
# Square
2013-01-03 16:57:26 -05:00
square_gen = Signal(8)
2012-09-15 16:29:50 -04:00
sync += [
If(cnt_gen[7],
square_gen.eq(255)
).Else(
square_gen.eq(0)
)
]
sinus = [int(128*sin((2*3.1415)/256*(x+1)))+128 for x in range(256)]
sinus_re = Signal()
2013-01-03 16:57:26 -05:00
sinus_gen = Signal(8)
comb +=[sinus_re.eq(1)]
2013-01-03 16:57:26 -05:00
sinus_mem = Memory(8, 256, init = sinus)
sinus_port = sinus_mem.get_port(has_re=True)
comb += [
sinus_port.adr.eq(cnt_gen),
sinus_port.re.eq(sinus_re),
sinus_gen.eq(sinus_port.dat_r)
]
2012-09-15 16:29:50 -04:00
# Signal Selection
2013-01-03 16:57:26 -05:00
sig_gen = Signal(8)
2012-09-15 16:29:50 -04:00
comb += [
2013-02-26 17:00:28 -05:00
If(miIo0.o == 0,
2012-09-15 16:29:50 -04:00
sig_gen.eq(cnt_gen)
2013-02-26 17:00:28 -05:00
).Elif(miIo0.o == 1,
2012-09-15 16:29:50 -04:00
sig_gen.eq(square_gen)
2013-02-26 17:00:28 -05:00
).Elif(miIo0.o == 2,
sig_gen.eq(sinus_gen)
2012-09-15 16:29:50 -04:00
).Else(
sig_gen.eq(0)
)
]
2012-09-12 11:56:36 -04:00
# Led
2013-01-03 16:57:26 -05:00
led0 = Signal(8)
2013-02-26 17:00:28 -05:00
comb += [led0.eq(miIo0.o[:8])]
2012-09-12 16:20:07 -04:00
#Switch
2013-01-03 16:57:26 -05:00
sw0 = Signal(8)
2013-02-26 17:00:28 -05:00
comb += [miIo0.i.eq(sw0)]
2012-09-12 11:56:36 -04:00
2012-09-17 11:00:47 -04:00
# MigLa0 input
2012-09-12 11:56:36 -04:00
comb += [
2013-02-26 17:00:28 -05:00
miLa0.in_trig.eq(sig_gen),
miLa0.in_dat.eq(sig_gen)
2012-09-12 11:56:36 -04:00
]
# MigLa1 input
comb += [
2013-02-26 17:00:28 -05:00
miLa1.in_trig[:8].eq(spi2csr0.csr.dat_w),
miLa1.in_trig[8:24].eq(spi2csr0.csr.adr),
miLa1.in_trig[24].eq(spi2csr0.csr.we),
miLa1.in_dat[:8].eq(spi2csr0.csr.dat_w),
miLa1.in_dat[8:24].eq(spi2csr0.csr.adr),
miLa1.in_dat[24].eq(spi2csr0.csr.we)
]
2012-09-12 11:56:36 -04:00
# HouseKeeping
cd_in = ClockDomain("in")
2012-09-12 11:56:36 -04:00
in_rst_n = Signal()
comb += [
cd_in.rst.eq(~in_rst_n)
2012-09-12 11:56:36 -04:00
]
2012-09-12 11:56:36 -04:00
frag = autofragment.from_local()
frag += Fragment(sync=sync,comb=comb,memories=[sinus_mem])
cst = Constraints(in_rst_n, cd_in, spi2csr0, led0, sw0)
2012-09-12 11:56:36 -04:00
src_verilog, vns = verilog.convert(frag,
cst.get_ios(),
2012-09-12 12:07:36 -04:00
name="de1",
clock_domains={
"sys": cd_in
},
2012-09-12 11:56:36 -04:00
return_ns=True)
src_qsf = cst.get_qsf(vns)
return (src_verilog, src_qsf)