Skip to content

Commit 108e3e3

Browse files
committed
Slighty extended, added own CRG
1 parent 58fd161 commit 108e3e3

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

sipeed_tang_nano_9k.py

+41-13
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,75 @@
11
#!/usr/bin/env python3
22

33
#
4-
# Minimum tang nano 9k example. Should work with most board with minimal changes
4+
# This file is part of LiteX-Boards.
55
#
6+
# Copyright (c) 2022 Icenowy Zheng <icenowy@aosc.io>
7+
# SPDX-License-Identifier: BSD-2-Clause
68

79
import os
810
from migen import *
911

1012
from litex.gen import *
1113

12-
import sipeed_tang_nano_9K_platform # It can be imported from litex_boards of course, but this way changes can be kept in the same repo
14+
import sipeed_tang_nano_9K_platform
1315

14-
from litex.build.io import CRG
16+
from litex.soc.cores.clock.gowin_gw1n import GW1NPLL
1517
from litex.soc.integration.soc_core import *
1618
from litex.soc.integration.soc import SoCRegion
1719
from litex.soc.integration.builder import *
1820

1921
kB = 1024
2022
mB = 1024*kB
2123

24+
# CRG stands for Clock Reset Generator
25+
# Here at least a clock and reset are added, and any PLLs.
26+
# In this case a PLL with the same in and output frequency and a user button is used as reset for the PLL.
27+
class _CRG(LiteXModule):
28+
def __init__(self, platform, sys_clk_freq, with_video_pll=False):
29+
self.rst = Signal()
30+
self.cd_sys = ClockDomain()
31+
32+
# Clk / Rst
33+
clk27 = platform.request("clk27")
34+
rst_n = platform.request("user_btn", 0)
35+
36+
# PLL
37+
self.pll = pll = GW1NPLL(devicename=platform.devicename, device=platform.device)
38+
self.comb += pll.reset.eq(~rst_n)
39+
pll.register_clkin(clk27, 27e6)
40+
pll.create_clkout(self.cd_sys, sys_clk_freq)
41+
2242
# BaseSoC ------------------------------------------------------------------------------------------
2343
class BaseSoC(SoCCore):
24-
def __init__(self, **kwargs):
44+
def __init__(self, sys_clk_freq=27e6, bios_flash_offset=0x0,
45+
**kwargs):
2546
platform = sipeed_tang_nano_9K_platform.Platform()
2647

27-
sys_clk_freq = int(1e9/platform.default_clk_period)
28-
29-
# CRG --------------------------------------------------------------------------------------
30-
self.crg = CRG(platform.request(platform.default_clk_name))
48+
# Notice the custom CRG here, _CRG instead of CRG
49+
self.crg = _CRG(platform, sys_clk_freq)
3150

3251
# SoCCore ----------------------------------------------------------------------------------
33-
kwargs["integrated_rom_size"] = 64*kB
52+
kwargs["integrated_rom_size"] = 64*kB
3453
kwargs["integrated_sram_size"] = 8*kB
35-
SoCCore.__init__(self, platform, sys_clk_freq, ident="Tiny LiteX SoC on Tang Nano 9K", **kwargs)
54+
SoCCore.__init__(self, platform, sys_clk_freq, ident="Still tiny LiteX SoC on Tang Nano 9K", **kwargs)
55+
3656

3757
# Build --------------------------------------------------------------------------------------------
58+
# Some extra arguments got added, sys-clk-freq to change the CPU frequency, we got a PLL after all. and a bios flash offset
59+
# Try --sys-clk-freq=54e6 to run it at twice the clock speed!
3860
def main():
3961
from litex.build.parser import LiteXArgumentParser
40-
parser = LiteXArgumentParser(platform=sipeed_tang_nano_9K_platform.Platform, description="Tiny LiteX SoC on Tang Nano 9K.")
62+
parser = LiteXArgumentParser(platform=sipeed_tang_nano_9K_platform.Platform, description="Still tiny LiteX SoC on Tang Nano 9K.")
4163
parser.add_target_argument("--flash", action="store_true", help="Flash Bitstream.")
64+
parser.add_target_argument("--sys-clk-freq", default=27e6, type=float, help="System clock frequency.")
65+
parser.add_target_argument("--bios-flash-offset", default="0x0", help="BIOS offset in SPI Flash.")
4266
args = parser.parse_args()
4367

44-
soc = BaseSoC( **parser.soc_argdict)
68+
soc = BaseSoC(
69+
sys_clk_freq = args.sys_clk_freq,
70+
bios_flash_offset = int(args.bios_flash_offset, 0),
71+
**parser.soc_argdict
72+
)
4573

4674
builder = Builder(soc, **parser.builder_argdict)
4775
if args.build:
@@ -54,7 +82,7 @@ def main():
5482
if args.flash:
5583
prog = soc.platform.create_programmer("openfpgaloader")
5684
prog.flash(0, builder.get_bitstream_filename(mode="flash", ext=".fs")) # FIXME
57-
prog.flash(0, builder.get_bios_filename(), external=True)
85+
prog.flash(int(args.bios_flash_offset, 0), builder.get_bios_filename(), external=True)
5886

5987
if __name__ == "__main__":
6088
main()

0 commit comments

Comments
 (0)