2022-06-02 12:24:20 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
#
|
|
|
|
# This file is part of LiteX-Boards.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2022 Icenowy Zheng <icenowy@aosc.io>
|
2022-08-04 10:32:21 -04:00
|
|
|
# Copyright (c) 2022 Florent Kermarrec <florent@enjoy-digital.fr>
|
2022-06-02 12:24:20 -04:00
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
|
|
from migen import *
|
|
|
|
from migen.genlib.resetsync import AsyncResetSynchronizer
|
|
|
|
|
2022-06-03 06:01:49 -04:00
|
|
|
from litex.soc.cores.clock.gowin_gw2a import GW2APLL
|
2022-06-02 12:24:20 -04:00
|
|
|
from litex.soc.integration.soc_core import *
|
|
|
|
from litex.soc.integration.soc import SoCRegion
|
|
|
|
from litex.soc.integration.builder import *
|
2022-08-04 10:32:21 -04:00
|
|
|
from litex.soc.cores.led import LedChaser, WS2812
|
|
|
|
from litex.soc.cores.gpio import GPIOIn
|
2022-07-26 06:25:10 -04:00
|
|
|
from litex.soc.cores.video import *
|
2022-06-02 12:24:20 -04:00
|
|
|
|
2022-07-26 06:53:42 -04:00
|
|
|
from liteeth.phy.rmii import LiteEthPHYRMII
|
|
|
|
|
2022-06-03 05:40:10 -04:00
|
|
|
from litex_boards.platforms import sipeed_tang_primer_20k
|
2022-06-02 12:24:20 -04:00
|
|
|
|
|
|
|
from litex.soc.cores.hyperbus import HyperRAM
|
|
|
|
|
|
|
|
kB = 1024
|
|
|
|
mB = 1024*kB
|
|
|
|
|
|
|
|
# CRG ----------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class _CRG(Module):
|
2022-07-26 06:25:10 -04:00
|
|
|
def __init__(self, platform, sys_clk_freq, with_video_pll=False):
|
2022-06-02 12:24:20 -04:00
|
|
|
self.rst = Signal()
|
|
|
|
self.clock_domains.cd_sys = ClockDomain()
|
|
|
|
self.clock_domains.cd_por = ClockDomain()
|
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
# Clk
|
|
|
|
clk27 = platform.request("clk27")
|
|
|
|
|
|
|
|
# PLL
|
2022-06-03 06:01:49 -04:00
|
|
|
self.submodules.pll = pll = GW2APLL(devicename=platform.devicename, device=platform.device)
|
2022-06-02 12:24:20 -04:00
|
|
|
pll.register_clkin(clk27, 27e6)
|
|
|
|
pll.create_clkout(self.cd_sys, sys_clk_freq)
|
|
|
|
|
|
|
|
# Power on reset (the onboard POR is not aware of reprogramming)
|
|
|
|
por_count = Signal(16, reset=2**16-1)
|
|
|
|
por_done = Signal()
|
|
|
|
self.comb += self.cd_por.clk.eq(clk27)
|
|
|
|
self.comb += por_done.eq(por_count == 0)
|
|
|
|
self.sync.por += If(~por_done, por_count.eq(por_count - 1))
|
|
|
|
self.comb += pll.reset.eq(~por_done)
|
|
|
|
|
2022-07-26 06:25:10 -04:00
|
|
|
# Video PLL
|
|
|
|
if with_video_pll:
|
|
|
|
self.submodules.video_pll = video_pll = GW2APLL(devicename=platform.devicename, device=platform.device)
|
|
|
|
video_pll.register_clkin(clk27, 27e6)
|
|
|
|
self.clock_domains.cd_hdmi = ClockDomain()
|
|
|
|
self.clock_domains.cd_hdmi5x = ClockDomain()
|
|
|
|
video_pll.create_clkout(self.cd_hdmi5x, 125e6)
|
|
|
|
self.specials += Instance("CLKDIV",
|
|
|
|
p_DIV_MODE= "5",
|
2022-08-31 07:32:23 -04:00
|
|
|
i_RESETN = 1, # disable reset signal
|
|
|
|
i_CALIB = 0, # no calibration
|
2022-07-26 06:25:10 -04:00
|
|
|
i_HCLKIN = self.cd_hdmi5x.clk,
|
|
|
|
o_CLKOUT = self.cd_hdmi.clk
|
|
|
|
)
|
|
|
|
|
2022-06-02 12:24:20 -04:00
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseSoC(SoCCore):
|
2022-08-04 10:32:21 -04:00
|
|
|
def __init__(self, sys_clk_freq=int(48e6),
|
|
|
|
with_spi_flash = False,
|
|
|
|
with_led_chaser = True,
|
|
|
|
with_rgb_led = False,
|
|
|
|
with_buttons = True,
|
2022-07-26 06:53:42 -04:00
|
|
|
with_video_terminal = False,
|
|
|
|
with_ethernet = False,
|
|
|
|
with_etherbone = False,
|
|
|
|
eth_ip = "192.168.1.50",
|
|
|
|
eth_dynamic_ip = False,
|
2022-08-26 02:17:57 -04:00
|
|
|
dock = "dock",
|
2022-07-26 06:53:42 -04:00
|
|
|
**kwargs):
|
2022-08-26 02:17:57 -04:00
|
|
|
|
|
|
|
assert dock in ["dock", "lite"]
|
|
|
|
|
|
|
|
platform = sipeed_tang_primer_20k.Platform(dock, toolchain="gowin")
|
|
|
|
|
|
|
|
if dock == "lite":
|
|
|
|
with_led_chaser = False # no leds on core board nor on dock lite
|
2022-06-02 12:24:20 -04:00
|
|
|
|
|
|
|
# CRG --------------------------------------------------------------------------------------
|
2022-07-26 06:25:10 -04:00
|
|
|
self.submodules.crg = _CRG(platform, sys_clk_freq, with_video_pll=with_video_terminal)
|
2022-06-02 12:24:20 -04:00
|
|
|
|
|
|
|
# SoCCore ----------------------------------------------------------------------------------
|
2022-06-03 05:40:10 -04:00
|
|
|
SoCCore.__init__(self, platform, sys_clk_freq, ident="LiteX SoC on Tang Primer 20K", **kwargs)
|
2022-06-02 12:24:20 -04:00
|
|
|
|
2022-07-26 04:35:44 -04:00
|
|
|
# SPI Flash --------------------------------------------------------------------------------
|
|
|
|
if with_spi_flash:
|
|
|
|
from litespi.modules import W25Q32JV as SpiFlashModule
|
|
|
|
from litespi.opcodes import SpiNorFlashOpCodes as Codes
|
|
|
|
self.add_spi_flash(mode="1x", module=SpiFlashModule(Codes.READ_1_1_1))
|
|
|
|
|
2022-08-04 10:32:21 -04:00
|
|
|
# Ethernet / Etherbone ---------------------------------------------------------------------
|
|
|
|
if with_ethernet or with_etherbone:
|
|
|
|
# FIXME: Un-tested.
|
|
|
|
from liteeth.phy.rmii import LiteEthPHYRMII
|
|
|
|
self.submodules.ethphy = LiteEthPHYRMII(
|
|
|
|
clock_pads = self.platform.request("eth_clocks"),
|
|
|
|
pads = self.platform.request("eth"),
|
|
|
|
refclk_cd = None
|
|
|
|
)
|
|
|
|
if with_ethernet:
|
|
|
|
self.add_ethernet(phy=self.ethphy, dynamic_ip=eth_dynamic_ip, with_timing_constraints=False)
|
|
|
|
if with_etherbone:
|
|
|
|
self.add_etherbone(phy=self.ethphy, ip_address=eth_ip, with_timing_constraints=False)
|
|
|
|
|
2022-07-26 06:25:10 -04:00
|
|
|
# Video ------------------------------------------------------------------------------------
|
|
|
|
if with_video_terminal:
|
2022-07-26 06:53:42 -04:00
|
|
|
# FIXME: Un-tested.
|
2022-07-26 06:25:10 -04:00
|
|
|
self.submodules.videophy = VideoHDMIPHY(platform.request("hdmi"), clock_domain="hdmi", pn_swap=["r", "g", "b"])
|
|
|
|
self.add_video_colorbars(phy=self.videophy, timings="640x480@60Hz", clock_domain="hdmi")
|
2022-07-26 06:53:42 -04:00
|
|
|
#self.add_video_terminal(phy=self.videophy, timings="640x480@75Hz", clock_domain="hdmi")
|
2022-07-26 06:25:10 -04:00
|
|
|
|
2022-07-26 05:44:03 -04:00
|
|
|
# Leds -------------------------------------------------------------------------------------
|
|
|
|
if with_led_chaser:
|
|
|
|
self.submodules.leds = LedChaser(
|
2022-08-04 10:32:21 -04:00
|
|
|
pads = platform.request_all("led"),
|
2022-07-26 05:44:03 -04:00
|
|
|
sys_clk_freq = sys_clk_freq
|
|
|
|
)
|
|
|
|
|
2022-08-04 10:32:21 -04:00
|
|
|
# RGB Led ----------------------------------------------------------------------------------
|
|
|
|
if with_rgb_led:
|
|
|
|
self.submodules.rgb_led = WS2812(
|
|
|
|
pad = platform.request("rgb_led"),
|
|
|
|
nleds = 1,
|
|
|
|
sys_clk_freq = sys_clk_freq
|
2022-07-26 06:53:42 -04:00
|
|
|
)
|
2022-08-04 10:32:21 -04:00
|
|
|
self.bus.add_slave(name="rgb_led", slave=self.rgb_led.bus, region=SoCRegion(
|
|
|
|
origin = 0x2000_0000,
|
|
|
|
size = 4,
|
|
|
|
))
|
|
|
|
|
|
|
|
# Buttons ----------------------------------------------------------------------------------
|
|
|
|
if with_buttons:
|
|
|
|
self.submodules.buttons = GPIOIn(pads=~platform.request_all("btn_n"))
|
|
|
|
|
2022-07-26 06:53:42 -04:00
|
|
|
|
2022-06-02 12:24:20 -04:00
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
|
|
|
from litex.soc.integration.soc import LiteXSoCArgumentParser
|
|
|
|
parser = LiteXSoCArgumentParser(description="LiteX SoC on Tang Primer 20K")
|
|
|
|
target_group = parser.add_argument_group(title="Target options")
|
2022-08-26 02:17:57 -04:00
|
|
|
target_group.add_argument("--dock", default="dock", help="Dock version (dock (default) or lite.")
|
2022-07-26 04:35:44 -04:00
|
|
|
target_group.add_argument("--build", action="store_true", help="Build bitstream.")
|
|
|
|
target_group.add_argument("--load", action="store_true", help="Load bitstream.")
|
|
|
|
target_group.add_argument("--flash", action="store_true", help="Flash Bitstream.")
|
|
|
|
target_group.add_argument("--sys-clk-freq", default=48e6, help="System clock frequency.")
|
2022-07-26 04:27:19 -04:00
|
|
|
sdopts = target_group.add_mutually_exclusive_group()
|
2022-07-26 04:35:44 -04:00
|
|
|
sdopts.add_argument("--with-spi-sdcard", action="store_true", help="Enable SPI-mode SDCard support.")
|
|
|
|
sdopts.add_argument("--with-sdcard", action="store_true", help="Enable SDCard support.")
|
|
|
|
target_group.add_argument("--with-spi-flash", action="store_true", help="Enable SPI Flash (MMAPed).")
|
2022-07-26 06:25:10 -04:00
|
|
|
target_group.add_argument("--with-video-terminal", action="store_true", help="Enable Video Terminal (HDMI).")
|
2022-07-26 06:53:42 -04:00
|
|
|
ethopts = target_group.add_mutually_exclusive_group()
|
|
|
|
ethopts.add_argument("--with-ethernet", action="store_true", help="Add Ethernet.")
|
|
|
|
ethopts.add_argument("--with-etherbone", action="store_true", help="Add EtherBone.")
|
|
|
|
target_group.add_argument("--eth-ip", default="192.168.1.50", help="Etherbone IP address.")
|
|
|
|
target_group.add_argument("--eth-dynamic-ip", action="store_true", help="Enable dynamic Ethernet IP addresses setting.")
|
2022-06-02 12:24:20 -04:00
|
|
|
builder_args(parser)
|
|
|
|
soc_core_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
soc = BaseSoC(
|
2022-07-26 06:25:10 -04:00
|
|
|
sys_clk_freq = int(float(args.sys_clk_freq)),
|
|
|
|
with_spi_flash = args.with_spi_flash,
|
|
|
|
with_video_terminal = args.with_video_terminal,
|
2022-07-26 06:53:42 -04:00
|
|
|
with_ethernet = args.with_ethernet,
|
|
|
|
with_etherbone = args.with_etherbone,
|
|
|
|
eth_ip = args.eth_ip,
|
|
|
|
eth_dynamic_ip = args.eth_dynamic_ip,
|
2022-08-26 02:17:57 -04:00
|
|
|
dock = args.dock,
|
2022-06-02 12:24:20 -04:00
|
|
|
**soc_core_argdict(args)
|
|
|
|
)
|
2022-07-26 04:27:19 -04:00
|
|
|
if args.with_spi_sdcard:
|
|
|
|
soc.add_spi_sdcard()
|
|
|
|
if args.with_sdcard:
|
|
|
|
soc.add_sdcard()
|
2022-06-02 12:24:20 -04:00
|
|
|
|
|
|
|
builder = Builder(soc, **builder_argdict(args))
|
2022-06-29 04:00:47 -04:00
|
|
|
if args.build:
|
|
|
|
builder.build()
|
2022-06-02 12:24:20 -04:00
|
|
|
|
|
|
|
if args.load:
|
|
|
|
prog = soc.platform.create_programmer()
|
|
|
|
prog.load_bitstream(builder.get_bitstream_filename(mode="sram"))
|
|
|
|
|
|
|
|
if args.flash:
|
|
|
|
prog = soc.platform.create_programmer()
|
|
|
|
prog.flash(0, builder.get_bitstream_filename(mode="flash", ext=".fs"), external=True)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|