|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from migen import * |
| 4 | +from migen.genlib.resetsync import AsyncResetSynchronizer |
| 5 | + |
| 6 | +from litex_boards.platforms import icebreaker |
| 7 | + |
| 8 | +from litex.build.generic_platform import * |
| 9 | + |
| 10 | +from litex.soc.cores.clock import iCE40PLL |
| 11 | +from litex.soc.integration.soc_core import * |
| 12 | +from litex.soc.integration.builder import * |
| 13 | + |
| 14 | +from orbtrace.trace import TraceCore |
| 15 | + |
| 16 | +# CRG ---------------------------------------------------------------------------------------------- |
| 17 | + |
| 18 | +class _CRG(Module): |
| 19 | + def __init__(self, platform, sys_clk_freq): |
| 20 | + self.rst = Signal() |
| 21 | + self.clock_domains.cd_sys = ClockDomain() |
| 22 | + self.clock_domains.cd_por = ClockDomain() |
| 23 | + |
| 24 | + self.clock_domains.cd_swo = ClockDomain() |
| 25 | + self.clock_domains.cd_swo2x = ClockDomain() |
| 26 | + |
| 27 | + # # # |
| 28 | + |
| 29 | + # Clk/Rst |
| 30 | + clk12 = platform.request("clk12") |
| 31 | + rst_n = platform.request("user_btn_n") |
| 32 | + |
| 33 | + # Power On Reset |
| 34 | + por_count = Signal(16, reset=2**16-1) |
| 35 | + por_done = Signal() |
| 36 | + self.comb += self.cd_por.clk.eq(ClockSignal()) |
| 37 | + self.comb += por_done.eq(por_count == 0) |
| 38 | + self.sync.por += If(~por_done, por_count.eq(por_count - 1)) |
| 39 | + |
| 40 | + # PLL |
| 41 | + self.submodules.pll = pll = iCE40PLL(primitive="SB_PLL40_PAD") |
| 42 | + self.comb += pll.reset.eq(~rst_n) # FIXME: Add proper iCE40PLL reset support and add back | self.rst. |
| 43 | + pll.register_clkin(clk12, 12e6) |
| 44 | + pll.create_clkout(self.cd_sys, sys_clk_freq, with_reset=False) |
| 45 | + self.specials += AsyncResetSynchronizer(self.cd_sys, ~por_done | ~pll.locked) |
| 46 | + platform.add_period_constraint(self.cd_sys.clk, 1e9/sys_clk_freq) |
| 47 | + |
| 48 | +# BaseSoC ------------------------------------------------------------------------------------------ |
| 49 | + |
| 50 | +class BaseSoC(SoCCore): |
| 51 | + def __init__(self, sys_clk_freq=int(24e6), **kwargs): |
| 52 | + platform = icebreaker.Platform() |
| 53 | + |
| 54 | + platform.add_extension([ |
| 55 | + ('trace', 0, |
| 56 | + Subsignal('clk', Pins('PMOD1A:1')), |
| 57 | + Subsignal('data', Pins('PMOD1A:2 PMOD1A:3 PMOD1A:4 PMOD1A:5')), |
| 58 | + ), |
| 59 | + ]) |
| 60 | + |
| 61 | + # SoCCore ---------------------------------------------------------------------------------- |
| 62 | + SoCCore.__init__(self, platform, sys_clk_freq, |
| 63 | + ident = "LiteX SoC on iCEBreaker", |
| 64 | + **kwargs) |
| 65 | + |
| 66 | + # CRG -------------------------------------------------------------------------------------- |
| 67 | + self.submodules.crg = _CRG(platform, sys_clk_freq) |
| 68 | + |
| 69 | + self.submodules.trace = TraceCore(platform) |
| 70 | + |
| 71 | + self.comb += self.trace.input_format.eq(0x03) # Hardwire for 4-bit parallel trace. |
| 72 | + |
| 73 | + self.comb += self.trace.source.connect(self.uart.sink) |
| 74 | + |
| 75 | + |
| 76 | +# Flash -------------------------------------------------------------------------------------------- |
| 77 | + |
| 78 | +def flash(build_dir, build_name, bios_flash_offset): |
| 79 | + from litex.build.lattice.programmer import IceStormProgrammer |
| 80 | + prog = IceStormProgrammer() |
| 81 | + prog.flash(bios_flash_offset, f"{build_dir}/software/bios/bios.bin") |
| 82 | + prog.flash(0x00000000, f"{build_dir}/gateware/{build_name}.bin") |
| 83 | + |
| 84 | +# Build -------------------------------------------------------------------------------------------- |
| 85 | + |
| 86 | +def main(): |
| 87 | + from litex.soc.integration.soc import LiteXSoCArgumentParser |
| 88 | + parser = LiteXSoCArgumentParser(description="LiteX SoC on iCEBreaker") |
| 89 | + target_group = parser.add_argument_group(title="Target options") |
| 90 | + target_group.add_argument("--build", action="store_true", help="Build bitstream.") |
| 91 | + target_group.add_argument("--load", action="store_true", help="Load bitstream.") |
| 92 | + target_group.add_argument("--flash", action="store_true", help="Flash Bitstream and BIOS.") |
| 93 | + target_group.add_argument("--sys-clk-freq", default=24e6, help="System clock frequency.") |
| 94 | + builder_args(parser) |
| 95 | + soc_core_args(parser) |
| 96 | + args = parser.parse_args() |
| 97 | + |
| 98 | + soc = BaseSoC( |
| 99 | + sys_clk_freq = int(float(args.sys_clk_freq)), |
| 100 | + **soc_core_argdict(args) |
| 101 | + ) |
| 102 | + builder = Builder(soc, **builder_argdict(args)) |
| 103 | + builder.build(run=args.build) |
| 104 | + |
| 105 | + if args.load: |
| 106 | + prog = soc.platform.create_programmer() |
| 107 | + prog.load_bitstream(builder.get_bitstream_filename(mode="sram", ext=".bin")) # FIXME |
| 108 | + |
| 109 | + if args.flash: |
| 110 | + flash(builder.output_dir, soc.build_name, args.bios_flash_offset) |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + main() |
0 commit comments