2021-05-04 06:19:21 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
#
|
|
|
|
# This file is part of LiteX-Boards.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2019 Sean Cross <sean@xobs.io>
|
|
|
|
# Copyright (c) 2018 David Shah <dave@ds0.me>
|
|
|
|
# Copyright (c) 2020 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
|
|
from migen import *
|
|
|
|
from migen.genlib.resetsync import AsyncResetSynchronizer
|
|
|
|
|
2023-02-23 03:09:33 -05:00
|
|
|
from litex.gen import *
|
2022-10-27 10:58:55 -04:00
|
|
|
|
2021-05-04 06:19:21 -04:00
|
|
|
from litex_boards.platforms import lattice_ice40up5k_evn
|
|
|
|
from litex.build.lattice.programmer import IceStormProgrammer
|
|
|
|
|
|
|
|
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-04 06:19:21 -04:00
|
|
|
def __init__(self, platform, sys_clk_freq):
|
|
|
|
assert sys_clk_freq == 12e6
|
2022-10-27 10:58:55 -04:00
|
|
|
self.rst = Signal()
|
|
|
|
self.cd_sys = ClockDomain()
|
|
|
|
self.cd_por = ClockDomain()
|
2021-05-04 06:19:21 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
# Clk/Rst
|
|
|
|
sys = platform.request("clk12")
|
|
|
|
platform.add_period_constraint(sys, 1e9/12e6)
|
|
|
|
|
|
|
|
# Power On Reset
|
|
|
|
por_count = Signal(16, reset=2**16-1)
|
|
|
|
por_done = Signal()
|
|
|
|
self.comb += self.cd_por.clk.eq(ClockSignal("sys"))
|
|
|
|
self.comb += por_done.eq(por_count == 0)
|
|
|
|
self.sync.por += If(~por_done, por_count.eq(por_count - 1))
|
|
|
|
|
|
|
|
# Sys Clk
|
|
|
|
self.comb += self.cd_sys.clk.eq(sys)
|
|
|
|
self.specials += AsyncResetSynchronizer(self.cd_sys, ~por_done)
|
|
|
|
|
|
|
|
|
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseSoC(SoCCore):
|
2022-11-08 06:29:11 -05:00
|
|
|
def __init__(self, bios_flash_offset, sys_clk_freq=12e6,
|
|
|
|
with_led_chaser = True,
|
|
|
|
**kwargs):
|
2021-05-04 06:19:21 -04:00
|
|
|
platform = lattice_ice40up5k_evn.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-04 06:19:21 -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-04-21 06:17:26 -04:00
|
|
|
SoCCore.__init__(self, platform, sys_clk_freq, ident="LiteX SoC on Lattice iCE40UP5k EVN breakout board", **kwargs)
|
2021-05-04 06:19:21 -04:00
|
|
|
|
|
|
|
# 128KB SPRAM (used as SRAM) ---------------------------------------------------------------
|
2022-10-27 10:58:55 -04:00
|
|
|
self.spram = Up5kSPRAM(size=128*kB)
|
2021-05-04 06:19:21 -04:00
|
|
|
self.bus.add_slave("sram", self.spram.bus, SoCRegion(size=128*kB))
|
|
|
|
|
|
|
|
# SPI Flash --------------------------------------------------------------------------------
|
|
|
|
# 4x mode is not possible on this board since WP and HOLD pins are not connected to the FPGA
|
2021-07-29 13:50:36 -04:00
|
|
|
from litespi.modules import N25Q032A
|
|
|
|
from litespi.opcodes import SpiNorFlashOpCodes as Codes
|
|
|
|
self.add_spi_flash(mode="1x", module=N25Q032A(Codes.READ_1_1_1))
|
2021-05-04 06:19:21 -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-04 06:19:21 -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-04 06:19:21 -04:00
|
|
|
|
|
|
|
# Leds -------------------------------------------------------------------------------------
|
2021-07-06 17:39:37 -04:00
|
|
|
if with_led_chaser:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.leds = LedChaser(
|
2021-07-06 17:39:37 -04:00
|
|
|
pads = platform.request_all("user_led_n"),
|
|
|
|
sys_clk_freq = sys_clk_freq)
|
2021-05-04 06:19:21 -04:00
|
|
|
|
|
|
|
# Flash --------------------------------------------------------------------------------------------
|
|
|
|
|
2022-03-21 11:59:40 -04:00
|
|
|
def flash(bios_flash_offset, target="lattice_ice40up5k_evn"):
|
2021-05-04 06:19:21 -04:00
|
|
|
from litex.build.dfu import DFUProg
|
|
|
|
prog = IceStormProgrammer()
|
|
|
|
bitstream = open("build/"+target+"/gateware/"+target+".bin", "rb")
|
|
|
|
bios = open("build/"+target+"/software/bios/bios.bin", "rb")
|
|
|
|
image = open("build/"+target+"/image.bin", "wb")
|
|
|
|
# Copy bitstream at 0x00000000
|
|
|
|
for i in range(0x00000000, 0x0020000):
|
|
|
|
b = bitstream.read(1)
|
|
|
|
if not b:
|
|
|
|
image.write(0xff.to_bytes(1, "big"))
|
|
|
|
else:
|
|
|
|
image.write(b)
|
|
|
|
# Copy bios at 0x00020000
|
|
|
|
for i in range(0x00000000, 0x00010000):
|
|
|
|
b = bios.read(1)
|
|
|
|
if not b:
|
|
|
|
image.write(0xff.to_bytes(1, "big"))
|
|
|
|
else:
|
|
|
|
image.write(b)
|
|
|
|
bitstream.close()
|
|
|
|
bios.close()
|
|
|
|
image.close()
|
|
|
|
print("Flashing bitstream (+bios)")
|
|
|
|
prog.flash(0x0, "build/"+target+"/image.bin")
|
|
|
|
|
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
2022-11-06 15:39:52 -05:00
|
|
|
from litex.build.parser import LiteXArgumentParser
|
2022-11-08 04:41:35 -05:00
|
|
|
parser = LiteXArgumentParser(platform=lattice_ice40up5k_evn.Platform, description="LiteX SoC on Lattice iCE40UP5k EVN breakout board.")
|
|
|
|
parser.add_target_argument("--sys-clk-freq", default=12e6, type=float, help="System clock frequency.")
|
|
|
|
parser.add_target_argument("--bios-flash-offset", default="0x20000", help="BIOS offset in SPI Flash.")
|
|
|
|
parser.add_target_argument("--flash", action="store_true", help="Flash Bitstream.")
|
2021-05-04 06:19:21 -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),
|
2022-11-08 04:41:35 -05:00
|
|
|
sys_clk_freq = args.sys_clk_freq,
|
2022-11-07 02:43:26 -05:00
|
|
|
**parser.soc_argdict
|
2021-05-04 06:19:21 -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-04 06:19:21 -04:00
|
|
|
|
|
|
|
if args.flash:
|
|
|
|
flash(args.bios_flash_offset)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|