1
1
#!/usr/bin/env python3
2
2
3
3
#
4
- # Minimum tang nano 9k example. Should work with most board with minimal changes
4
+ # This file is part of LiteX-Boards.
5
5
#
6
+ # Copyright (c) 2022 Icenowy Zheng <icenowy@aosc.io>
7
+ # SPDX-License-Identifier: BSD-2-Clause
6
8
7
9
import os
8
10
from migen import *
9
11
10
12
from litex .gen import *
11
13
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
13
15
14
- from litex .build . io import CRG
16
+ from litex .soc . cores . clock . gowin_gw1n import GW1NPLL
15
17
from litex .soc .integration .soc_core import *
16
18
from litex .soc .integration .soc import SoCRegion
17
19
from litex .soc .integration .builder import *
18
20
19
21
kB = 1024
20
22
mB = 1024 * kB
21
23
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
+
22
42
# BaseSoC ------------------------------------------------------------------------------------------
23
43
class BaseSoC (SoCCore ):
24
- def __init__ (self , ** kwargs ):
44
+ def __init__ (self , sys_clk_freq = 27e6 , bios_flash_offset = 0x0 ,
45
+ ** kwargs ):
25
46
platform = sipeed_tang_nano_9K_platform .Platform ()
26
47
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 )
31
50
32
51
# SoCCore ----------------------------------------------------------------------------------
33
- kwargs ["integrated_rom_size" ] = 64 * kB
52
+ kwargs ["integrated_rom_size" ] = 64 * kB
34
53
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
+
36
56
37
57
# 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!
38
60
def main ():
39
61
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." )
41
63
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." )
42
66
args = parser .parse_args ()
43
67
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
+ )
45
73
46
74
builder = Builder (soc , ** parser .builder_argdict )
47
75
if args .build :
@@ -54,7 +82,7 @@ def main():
54
82
if args .flash :
55
83
prog = soc .platform .create_programmer ("openfpgaloader" )
56
84
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 )
58
86
59
87
if __name__ == "__main__" :
60
88
main ()
0 commit comments