2022-10-25 15:12:02 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
#
|
|
|
|
# This file is part of LiteX-Boards.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2022 JM Robles <roblesjm@gmail.com>
|
|
|
|
# Copyright (c) 2021 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
|
|
|
|
2022-10-25 15:12:02 -04:00
|
|
|
from litex_boards.platforms import fpgawars_alhambra2
|
|
|
|
|
|
|
|
from litex.build.lattice.programmer import IceStormProgrammer
|
|
|
|
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
|
|
|
|
|
2024-06-13 04:04:19 -04:00
|
|
|
# CRG ----------------------------------------------------------------------------------------------
|
2022-10-25 15:12:02 -04:00
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
class _CRG(LiteXModule):
|
2022-10-25 15:12:02 -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()
|
2022-10-25 15:12:02 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-06-13 04:04:19 -04:00
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
2022-10-25 15:12:02 -04:00
|
|
|
class BaseSoC(SoCCore):
|
|
|
|
|
2022-11-08 05:54:17 -05:00
|
|
|
def __init__(self, sys_clk_freq=12e6, with_led_chaser=True, bios_flash_offset=0x50000, **kwargs):
|
2022-10-25 15:12:02 -04:00
|
|
|
|
|
|
|
platform = fpgawars_alhambra2.Platform()
|
|
|
|
kwargs["integrated_rom_size"] = 0
|
|
|
|
|
|
|
|
# SoC
|
|
|
|
SoCCore.__init__(self, platform, sys_clk_freq, ident='Litex on Alhambra II', **kwargs)
|
|
|
|
# SPI Flash
|
|
|
|
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), with_master=False)
|
|
|
|
self.bus.add_region("rom", SoCRegion(
|
|
|
|
origin=self.bus.regions["spiflash"].origin + bios_flash_offset,
|
2024-06-13 04:04:19 -04:00
|
|
|
size=32 * KILOBYTE,
|
2022-10-25 15:12:02 -04:00
|
|
|
linker=True
|
|
|
|
))
|
|
|
|
self.cpu.set_reset_address(self.bus.regions["rom"].origin)
|
|
|
|
|
|
|
|
# CRG
|
2022-10-27 10:58:55 -04:00
|
|
|
self.crg = _CRG(platform, sys_clk_freq)
|
2022-10-25 15:12:02 -04:00
|
|
|
|
|
|
|
# Leds
|
|
|
|
if with_led_chaser:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.leds = LedChaser(pads=platform.request_all("user_leds"), sys_clk_freq=sys_clk_freq)
|
2022-10-25 15:12:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
2022-11-06 15:39:52 -05:00
|
|
|
from litex.build.parser import LiteXArgumentParser
|
2022-11-05 03:07:14 -04:00
|
|
|
|
2022-11-08 04:41:35 -05:00
|
|
|
parser = LiteXArgumentParser(platform=fpgawars_alhambra2.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="0x50000", help="BIOS offset in SPI flash.")
|
|
|
|
parser.add_target_argument("--flash", action="store_true", help="Flash Bitstream.")
|
2022-10-25 15:12:02 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
soc = BaseSoC(
|
2022-10-26 14:45:53 -04: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
|
2022-10-25 15:12:02 -04:00
|
|
|
)
|
2022-11-05 03:07:14 -04:00
|
|
|
builder = Builder(soc, **parser.builder_argdict)
|
2022-10-26 14:36:18 -04:00
|
|
|
if args.build:
|
2022-11-05 03:07:14 -04:00
|
|
|
builder.build(**parser.toolchain_argdict)
|
2022-10-26 14:36:18 -04:00
|
|
|
|
|
|
|
if args.flash:
|
|
|
|
flash(args.bios_flash_offset)
|
2022-10-25 15:12:02 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|