2020from litex .soc .integration .builder import *
2121from litex .soc .cores .gpio import GPIOIn
2222from litex .soc .cores .led import LedChaser , WS2812
23+ from litex .soc .cores .video import VideoGowinHDMIPHY
2324
2425from litedram .modules import M12L64322A # FIXME: use the real model number
2526from litedram .phy import GENSDRPHY
2930# CRG ----------------------------------------------------------------------------------------------
3031
3132class _CRG (LiteXModule ):
32- def __init__ (self , platform , sys_clk_freq ):
33+ def __init__ (self , platform , sys_clk_freq , with_hdmi = False ):
3334 self .rst = Signal ()
3435 self .cd_sys = ClockDomain ()
3536 self .cd_por = ClockDomain ()
37+ if with_hdmi :
38+ self .cd_hdmi = ClockDomain ()
39+ self .cd_hdmi5x = ClockDomain ()
3640
3741 # Clk
3842 clk27 = platform .request ("clk27" )
@@ -50,24 +54,46 @@ def __init__(self, platform, sys_clk_freq):
5054 pll .register_clkin (clk27 , 27e6 )
5155 pll .create_clkout (self .cd_sys , sys_clk_freq )
5256
57+ # HDMI PLL
58+ if with_hdmi :
59+ self .video_pll = video_pll = GW2APLL (devicename = platform .devicename , device = platform .device )
60+ video_pll .register_clkin (clk27 , 27e6 )
61+ video_pll .create_clkout (self .cd_hdmi5x , 125e6 , margin = 1e-2 )
62+ self .specials += Instance ("CLKDIV" ,
63+ p_DIV_MODE = "5" ,
64+ i_RESETN = 1 , # Disable reset signal.
65+ i_CALIB = 0 , # No calibration.
66+ i_HCLKIN = self .cd_hdmi5x .clk ,
67+ o_CLKOUT = self .cd_hdmi .clk
68+ )
69+
5370# BaseSoC ------------------------------------------------------------------------------------------
5471
5572class BaseSoC (SoCCore ):
5673 def __init__ (self , toolchain = "gowin" , sys_clk_freq = 48e6 ,
5774 with_led_chaser = True ,
5875 with_rgb_led = False ,
5976 with_buttons = True ,
77+ with_spi_flash = False ,
78+ with_video_terminal = False ,
79+ with_video_colorbars = False ,
6080 ** kwargs ):
6181
6282 platform = sipeed_tang_nano_20k .Platform (toolchain = toolchain )
6383
84+ with_hdmi = with_video_terminal or with_video_colorbars
85+
6486 # CRG --------------------------------------------------------------------------------------
65- self .crg = _CRG (platform , sys_clk_freq )
87+ self .crg = _CRG (platform , sys_clk_freq , with_hdmi = with_hdmi )
6688
6789 # SoCCore ----------------------------------------------------------------------------------
6890 SoCCore .__init__ (self , platform , sys_clk_freq , ident = "LiteX SoC on Tang Nano 20K" , ** kwargs )
6991
70- # TODO: XTX SPI Flash
92+ # SPI Flash: XT25F64B ----------------------------------------------------------------------
93+ if with_spi_flash :
94+ from litespi .modules import W25Q64 as SpiFlashModule # compatible with XT25F64B
95+ from litespi .opcodes import SpiNorFlashOpCodes as Codes
96+ self .add_spi_flash (mode = "1x" , module = SpiFlashModule (Codes .READ_1_1_1 ))
7197
7298 # SDR SDRAM --------------------------------------------------------------------------------
7399 if not self .integrated_main_ram_size :
@@ -94,6 +120,15 @@ def __init__(self):
94120 l2_cache_size = 128 ,
95121 )
96122
123+ # Video ------------------------------------------------------------------------------------
124+ if with_hdmi :
125+ self .videophy = VideoGowinHDMIPHY (platform .request ("hdmi" ), clock_domain = "hdmi" )
126+ if with_video_terminal :
127+ # self.add_video_terminal(phy=self.videophy, timings="800x600@60Hz", clock_domain="hdmi")
128+ self .add_video_terminal (phy = self .videophy , timings = "640x480@60Hz" , clock_domain = "hdmi" )
129+ if with_video_colorbars :
130+ self .add_video_colorbars (phy = self .videophy , timings = "800x600@60Hz" , clock_domain = "hdmi" )
131+
97132 # Leds -------------------------------------------------------------------------------------
98133 if with_led_chaser :
99134 self .leds = LedChaser (
@@ -125,14 +160,23 @@ def main():
125160 parser = LiteXArgumentParser (platform = sipeed_tang_nano_20k .Platform , description = "LiteX SoC on Tang Nano 20K." )
126161 parser .add_target_argument ("--flash" , action = "store_true" , help = "Flash Bitstream." )
127162 parser .add_target_argument ("--sys-clk-freq" , default = 48e6 , type = float , help = "System clock frequency." )
163+ parser .add_target_argument ("--with-spi-flash" , action = "store_true" , help = "Enable SPI Flash (MMAPed)." )
164+ parser .add_target_argument ("--with-rbg-led" , action = "store_true" , help = "Enable WS2812 RGB Led." )
128165 sdopts = parser .target_group .add_mutually_exclusive_group ()
129166 sdopts .add_argument ("--with-spi-sdcard" , action = "store_true" , help = "Enable SPI-mode SDCard support." )
130167 sdopts .add_argument ("--with-sdcard" , action = "store_true" , help = "Enable SDCard support." )
168+ viopts = parser .target_group .add_mutually_exclusive_group ()
169+ viopts .add_argument ("--with-video-terminal" , action = "store_true" , help = "Enable Video Terminal (HDMI)." )
170+ viopts .add_argument ("--with-video-colorbars" , action = "store_true" , help = "Enable Video Colorbars (HDMI)." )
131171 args = parser .parse_args ()
132172
133173 soc = BaseSoC (
134174 toolchain = args .toolchain ,
135175 sys_clk_freq = args .sys_clk_freq ,
176+ with_rgb_led = args .with_rbg_led ,
177+ with_spi_flash = args .with_spi_flash ,
178+ with_video_terminal = args .with_video_terminal ,
179+ with_video_colorbars = args .with_video_colorbars ,
136180 ** parser .soc_argdict
137181 )
138182 if args .with_spi_sdcard :
0 commit comments