Skip to content
Snippets Groups Projects
ecp5_evn.py 3.09 KiB
Newer Older
  • Learn to ignore specific revisions
  • DurandA's avatar
    DurandA committed
    #!/usr/bin/env python3
    
    # This file is Copyright (c) 2019 Arnaud Durand <arnaud.durand@unifr.ch>
    # License: BSD
    
    
    DurandA's avatar
    DurandA committed
    import argparse
    
    from migen import *
    from migen.genlib.resetsync import AsyncResetSynchronizer
    
    
    from litex_boards.platforms import ecp5_evn
    
    DurandA's avatar
    DurandA committed
    
    from litex.soc.cores.clock import *
    from litex.soc.integration.soc_core import *
    from litex.soc.integration.builder import *
    
    # CRG ----------------------------------------------------------------------------------------------
    
    class _CRG(Module):
    
        def __init__(self, platform, sys_clk_freq, x5_clk_freq):
    
    DurandA's avatar
    DurandA committed
            self.clock_domains.cd_sys = ClockDomain()
    
            # # #
    
            # clk / rst
    
            clk = clk12 = platform.request("clk12")
    
    DurandA's avatar
    DurandA committed
            rst_n = platform.request("rst_n")
    
            if x5_clk_freq is not None:
                clk = clk50 = platform.request("ext_clk50")
                self.comb += platform.request("ext_clk50_en").eq(1)
                platform.add_period_constraint(clk50, 1e9/x5_clk_freq)
    
    DurandA's avatar
    DurandA committed
    
            # pll
    
            self.submodules.pll = pll = ECP5PLL()
            self.comb += pll.reset.eq(~rst_n)
            pll.register_clkin(clk, x5_clk_freq or 12e6)
            pll.create_clkout(self.cd_sys, sys_clk_freq)
            self.specials += AsyncResetSynchronizer(self.cd_sys, ~rst_n)
    
    DurandA's avatar
    DurandA committed
    
    # BaseSoC ------------------------------------------------------------------------------------------
    
    class BaseSoC(SoCCore):
    
        def __init__(self, sys_clk_freq=int(50e6), x5_clk_freq=None, toolchain="trellis", **kwargs):
    
    DurandA's avatar
    DurandA committed
            platform = ecp5_evn.Platform(toolchain=toolchain)
    
    
            # SoCCore ----------------------------------------------------------------------------------
    
            SoCCore.__init__(self, platform, clk_freq=sys_clk_freq, **kwargs)
    
    DurandA's avatar
    DurandA committed
    
    
            # CRG --------------------------------------------------------------------------------------
    
            crg = _CRG(platform, sys_clk_freq, x5_clk_freq)
    
    DurandA's avatar
    DurandA committed
            self.submodules.crg = crg
    
    # Build --------------------------------------------------------------------------------------------
    
    def main():
        parser = argparse.ArgumentParser(description="LiteX SoC on ECP5 Evaluation Board")
    
        parser.add_argument("--build", action="store_true", help="Build bitstream")
        parser.add_argument("--load",  action="store_true", help="Load bitstream")
        parser.add_argument("--gateware-toolchain", dest="toolchain", default="trellis", help="Gateware toolchain to use, trellis (default) or diamond")
    
    DurandA's avatar
    DurandA committed
        builder_args(parser)
        soc_core_args(parser)
    
        parser.add_argument("--sys-clk-freq", default=60e6, help="System clock frequency (default=60MHz)")
        parser.add_argument("--x5-clk-freq",  type=int,     help="Use X5 oscillator as system clock at the specified frequency")
    
    DurandA's avatar
    DurandA committed
        args = parser.parse_args()
    
    
        soc = BaseSoC(toolchain=args.toolchain,
    
            sys_clk_freq = int(float(args.sys_clk_freq)),
            x5_clk_freq  = args.x5_clk_freq,
    
            **soc_core_argdict(args))
    
    DurandA's avatar
    DurandA committed
        builder = Builder(soc, **builder_argdict(args))
    
        builder.build(run=args.build)
    
        if args.load:
            prog = soc.platform.create_programmer()
            prog.load_bitstream(os.path.join(builder.gateware_dir, "top.svf"))
    
    DurandA's avatar
    DurandA committed
    
    if __name__ == "__main__":
        main()