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
79import os
810from migen import *
911
1012from 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
1517from litex .soc .integration .soc_core import *
1618from litex .soc .integration .soc import SoCRegion
1719from litex .soc .integration .builder import *
1820
1921kB = 1024
2022mB = 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 ------------------------------------------------------------------------------------------
2343class 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!
3860def 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
5987if __name__ == "__main__" :
6088 main ()
0 commit comments