2021-05-06 23:47:17 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
#
|
|
|
|
# This file is part of LiteX-Boards.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2021 Hans Baier <hansfbaier@gmail.com>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
2021-05-07 02:57:34 -04:00
|
|
|
# iCESugar FPGA: https://www.aliexpress.com/item/4001201771358.html
|
2021-05-06 23:47:17 -04:00
|
|
|
|
|
|
|
from migen import *
|
|
|
|
from migen.genlib.resetsync import AsyncResetSynchronizer
|
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
from litex.gen import LiteXModule
|
|
|
|
|
2021-05-06 23:47:17 -04:00
|
|
|
from litex_boards.platforms import muselab_icesugar
|
|
|
|
|
|
|
|
from litex.soc.cores.ram import Up5kSPRAM
|
|
|
|
from litex.soc.cores.clock import iCE40PLL
|
|
|
|
from litex.soc.integration.soc_core import *
|
|
|
|
from litex.soc.integration.soc import SoCRegion
|
|
|
|
from litex.soc.integration.builder import *
|
|
|
|
from litex.soc.cores.led import LedChaser
|
|
|
|
|
|
|
|
kB = 1024
|
|
|
|
mB = 1024*kB
|
|
|
|
|
|
|
|
# CRG ----------------------------------------------------------------------------------------------
|
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
class _CRG(LiteXModule):
|
2021-05-06 23:47:17 -04:00
|
|
|
def __init__(self, platform, sys_clk_freq):
|
2022-10-27 10:58:55 -04:00
|
|
|
self.rst = Signal()
|
|
|
|
self.cd_sys = ClockDomain()
|
|
|
|
self.cd_por = ClockDomain()
|
2021-05-06 23:47:17 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
# Clk/Rst
|
|
|
|
clk12 = platform.request("clk12")
|
|
|
|
|
|
|
|
# Power On Reset
|
|
|
|
por_count = Signal(16, reset=2**16-1)
|
|
|
|
por_done = Signal()
|
|
|
|
self.comb += self.cd_por.clk.eq(ClockSignal())
|
|
|
|
self.comb += por_done.eq(por_count == 0)
|
|
|
|
self.sync.por += If(~por_done, por_count.eq(por_count - 1))
|
|
|
|
|
|
|
|
# PLL
|
2022-10-27 10:58:55 -04:00
|
|
|
self.pll = pll = iCE40PLL(primitive="SB_PLL40_PAD")
|
2021-05-06 23:47:17 -04:00
|
|
|
self.comb += pll.reset.eq(self.rst)
|
|
|
|
pll.register_clkin(clk12, 12e6)
|
|
|
|
pll.create_clkout(self.cd_sys, sys_clk_freq, with_reset=False)
|
|
|
|
self.specials += AsyncResetSynchronizer(self.cd_sys, ~por_done | ~pll.locked)
|
|
|
|
platform.add_period_constraint(self.cd_sys.clk, 1e9/sys_clk_freq)
|
|
|
|
|
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseSoC(SoCCore):
|
2021-07-06 17:39:37 -04:00
|
|
|
def __init__(self, bios_flash_offset, sys_clk_freq=int(24e6), with_led_chaser=True,
|
|
|
|
with_video_terminal=False, **kwargs):
|
2021-05-06 23:47:17 -04:00
|
|
|
platform = muselab_icesugar.Platform()
|
|
|
|
|
2022-04-21 06:17:26 -04:00
|
|
|
# CRG --------------------------------------------------------------------------------------
|
2022-10-27 10:58:55 -04:00
|
|
|
self.crg = _CRG(platform, sys_clk_freq)
|
2022-04-21 06:17:26 -04:00
|
|
|
|
|
|
|
# SoCCore ----------------------------------------------------------------------------------
|
2021-05-06 23:47:17 -04:00
|
|
|
# Disable Integrated ROM/SRAM since too large for iCE40 and UP5K has specific SPRAM.
|
|
|
|
kwargs["integrated_sram_size"] = 0
|
|
|
|
kwargs["integrated_rom_size"] = 0
|
2022-01-07 04:34:47 -05:00
|
|
|
# Set CPU variant
|
|
|
|
if kwargs.get("cpu_type", "vexriscv") == "vexriscv":
|
|
|
|
kwargs["cpu_variant"] = "lite"
|
2022-04-21 06:17:26 -04:00
|
|
|
SoCCore.__init__(self, platform, sys_clk_freq, ident="LiteX SoC on Muselab iCESugar", **kwargs)
|
2021-05-06 23:47:17 -04:00
|
|
|
|
|
|
|
# 128KB SPRAM (used as SRAM) ---------------------------------------------------------------
|
2022-10-27 10:58:55 -04:00
|
|
|
self.spram = Up5kSPRAM(size=64*kB)
|
2021-05-06 23:47:17 -04:00
|
|
|
self.bus.add_slave("sram", self.spram.bus, SoCRegion(size=64*kB))
|
|
|
|
|
|
|
|
# SPI Flash --------------------------------------------------------------------------------
|
2021-07-29 13:55:32 -04:00
|
|
|
from litespi.modules import W25Q64FV
|
|
|
|
from litespi.opcodes import SpiNorFlashOpCodes as Codes
|
2021-07-29 13:59:22 -04:00
|
|
|
self.add_spi_flash(mode="1x", module=W25Q64FV(Codes.READ_1_1_1), with_master=False)
|
2021-05-06 23:47:17 -04:00
|
|
|
|
|
|
|
# Add ROM linker region --------------------------------------------------------------------
|
|
|
|
self.bus.add_region("rom", SoCRegion(
|
2022-01-07 04:34:47 -05:00
|
|
|
origin = self.bus.regions["spiflash"].origin + bios_flash_offset,
|
2021-05-06 23:47:17 -04:00
|
|
|
size = 32*kB,
|
|
|
|
linker = True)
|
|
|
|
)
|
2022-01-07 09:19:23 -05:00
|
|
|
self.cpu.set_reset_address(self.bus.regions["rom"].origin)
|
2021-05-06 23:47:17 -04:00
|
|
|
|
|
|
|
# Leds -------------------------------------------------------------------------------------
|
2021-07-06 17:39:37 -04:00
|
|
|
if with_led_chaser:
|
|
|
|
led_pads = platform.request_all("user_led_n")
|
2022-10-27 10:58:55 -04:00
|
|
|
self.leds = LedChaser(
|
2021-07-06 17:39:37 -04:00
|
|
|
pads = led_pads,
|
|
|
|
sys_clk_freq = sys_clk_freq)
|
2021-05-06 23:47:17 -04:00
|
|
|
|
|
|
|
# Flash --------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def flash(bios_flash_offset):
|
|
|
|
from litex.build.lattice.programmer import IceSugarProgrammer
|
|
|
|
prog = IceSugarProgrammer()
|
|
|
|
prog.flash(bios_flash_offset, "build/muselab_icesugar/software/bios/bios.bin")
|
|
|
|
prog.flash(0x00000000, "build/muselab_icesugar/gateware/muselab_icesugar.bin")
|
|
|
|
|
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
2022-11-06 15:39:52 -05:00
|
|
|
from litex.build.parser import LiteXArgumentParser
|
2022-11-05 03:07:14 -04:00
|
|
|
parser = LiteXArgumentParser(platform=muselab_icesugar.Platform, description="LiteX SoC on iCEBreaker")
|
|
|
|
parser.add_target_argument("--flash", action="store_true", help="Flash Bitstream.")
|
|
|
|
parser.add_target_argument("--sys-clk-freq", default=24e6, help="System clock frequency.")
|
|
|
|
parser.add_target_argument("--bios-flash-offset", default="0x40000", help="BIOS offset in SPI Flash.")
|
2021-05-06 23:47:17 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
soc = BaseSoC(
|
2021-12-20 15:41:12 -05:00
|
|
|
bios_flash_offset = int(args.bios_flash_offset, 0),
|
2021-05-06 23:47:17 -04:00
|
|
|
sys_clk_freq = int(float(args.sys_clk_freq)),
|
2022-11-05 03:07:14 -04:00
|
|
|
**parser.soc_core_argdict
|
2021-05-06 23:47:17 -04:00
|
|
|
)
|
2022-11-05 03:07:14 -04:00
|
|
|
builder = Builder(soc, **parser.builder_argdict)
|
2022-05-06 09:14:32 -04:00
|
|
|
if args.build:
|
2022-11-05 03:07:14 -04:00
|
|
|
builder.build(**parser.toolchain_argdict)
|
2021-05-06 23:47:17 -04:00
|
|
|
|
|
|
|
if args.load:
|
|
|
|
prog = soc.platform.create_programmer()
|
2022-03-17 04:21:05 -04:00
|
|
|
prog.load_bitstream(builder.get_bitstream_filename(mode="sram", ext=".bin")) # FIXME
|
2021-05-06 23:47:17 -04:00
|
|
|
|
|
|
|
if args.flash:
|
2021-12-20 15:41:12 -05:00
|
|
|
flash(int(args.bios_flash_offset, 0))
|
2021-05-06 23:47:17 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|