mibuild: sanitize default clock management
authorSebastien Bourdeauducq <sb@m-labs.hk>
Fri, 13 Mar 2015 23:10:08 +0000 (00:10 +0100)
committerSebastien Bourdeauducq <sb@m-labs.hk>
Fri, 13 Mar 2015 23:10:08 +0000 (00:10 +0100)
21 files changed:
examples/cordic/cordic_impl.py
mibuild/altera/quartus.py
mibuild/crg.py [deleted file]
mibuild/generic_platform.py
mibuild/platforms/apf27.py
mibuild/platforms/apf51.py
mibuild/platforms/de0nano.py
mibuild/platforms/kc705.py
mibuild/platforms/lx9_microboard.py
mibuild/platforms/m1.py
mibuild/platforms/mixxeo.py
mibuild/platforms/ml605.py
mibuild/platforms/papilio_pro.py
mibuild/platforms/pipistrello.py
mibuild/platforms/rhino.py
mibuild/platforms/sim.py
mibuild/platforms/usrp_b100.py
mibuild/platforms/zedboard.py
mibuild/platforms/ztex_115d.py
mibuild/xilinx/platform.py
migen/genlib/io.py

index f63418d192d4b3cff60cbea2dad42aa70bb6a5d7..594f2bda8f335a00daaeffda8e4a9f5305372fef 100644 (file)
@@ -5,7 +5,6 @@ from migen.fhdl.std import *
 from migen.genlib.cordic import Cordic
 from mibuild.tools import mkdir_noerror
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform
 
 class CordicImpl(Module):
@@ -28,9 +27,11 @@ class CordicImpl(Module):
                self.platform.build(self, build_name=self.name)
 
 class Platform(XilinxPlatform):
+       default_clk_name = "clk"
+       default_clk_period = 20.0
+
        _io = [
                ("clk", 0, Pins("AB13")),
-               ("rst", 0, Pins("V5")),
                ("do", 0,
                        Pins("Y2 W3 W1 P8 P7 P6 P5 T4 T3",
                                "U4 V3 N6 N7 M7 M8 R4 P4 M6 L6 P3 N4",
@@ -38,8 +39,7 @@ class Platform(XilinxPlatform):
                ),
        ]
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc6slx45-fgg484-2", self._io,
-                       lambda p: SimpleCRG(p, "clk", "rst"))
+               XilinxPlatform.__init__(self, "xc6slx45-fgg484-2", self._io)
 
 if __name__ == "__main__":
        default = dict(width=16, guard=0, eval_mode="pipelined",
index 348596a51af3ae952fa899934f22afe242188bdd..b5468af0e03dff18de71b29d63de63244c7a34d1 100644 (file)
@@ -94,5 +94,6 @@ class AlteraQuartusPlatform(GenericPlatform):
                return vns
 
        def add_period_constraint(self, clk, period):
+               # TODO: handle differential clk
                self.add_platform_command("""set_global_assignment -name DUTY_CYCLE 50 -section_id {clk}""", clk=clk)
                self.add_platform_command("""set_global_assignment -name FMAX_REQUIREMENT "{freq} MHz" -section_id {clk}\n""".format(freq=str(float(1/period)*1000), clk="{clk}"), clk=clk)
diff --git a/mibuild/crg.py b/mibuild/crg.py
deleted file mode 100644 (file)
index 5d51a13..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-from migen.fhdl.std import *
-
-class SimpleCRG(Module):
-       def __init__(self, platform, clk_name, rst_name, rst_invert=False):
-                       reset_less = rst_name is None
-                       self.clock_domains.cd_sys = ClockDomain(reset_less=reset_less)
-                       self._clk = platform.request(clk_name)
-                       self.comb += self.cd_sys.clk.eq(self._clk)
-
-                       if not reset_less:
-                               if rst_invert:
-                                       self.comb += self.cd_sys.rst.eq(~platform.request(rst_name))
-                               else:
-                                       self.comb += self.cd_sys.rst.eq(platform.request(rst_name))
index d8781e8a6f86655d21a38e1248828b469a584c6f..4b4a6e3cfdb7fd487ab31d798ca6d859b9fe2854 100644 (file)
@@ -3,6 +3,7 @@ import os, sys
 from migen.fhdl.std import *
 from migen.fhdl.structure import _Fragment
 from migen.genlib.record import Record
+from migen.genlib.io import CRG
 from migen.fhdl import verilog, edif
 from migen.util.misc import autotype
 
@@ -177,10 +178,9 @@ class ConstraintManager:
                return self.platform_commands
 
 class GenericPlatform:
-       def __init__(self, device, io, default_crg_factory=None, connectors=[], name=None):
+       def __init__(self, device, io, connectors=[], name=None):
                self.device = device
                self.constraint_manager = ConstraintManager(io, connectors)
-               self.default_crg_factory = default_crg_factory
                if name is None:
                        name = self.__module__.split(".")[-1]
                self.name = name
@@ -194,6 +194,9 @@ class GenericPlatform:
        def lookup_request(self, *args, **kwargs):
                return self.constraint_manager.lookup_request(*args, **kwargs)
 
+       def add_period_constraint(self, clk, period):
+               raise NotImplementedError
+
        def add_platform_command(self, *args, **kwargs):
                return self.constraint_manager.add_platform_command(*args, **kwargs)
 
@@ -205,17 +208,20 @@ class GenericPlatform:
                        raise ConstraintError("Already finalized")
                # if none exists, create a default clock domain and drive it
                if not fragment.clock_domains:
-                       if self.default_crg_factory is None:
-                               raise NotImplementedError("No clock/reset generator defined by either platform or user")
-                       crg = self.default_crg_factory(self)
+                       if not hasattr(self, "default_clk_name"):
+                               raise NotImplementedError("No default clock and no clock domain defined")
+                       crg = CRG(self.request(self.default_clk_name))
                        fragment += crg.get_fragment()
                self.do_finalize(fragment, *args, **kwargs)
                self.finalized = True
 
        def do_finalize(self, fragment, *args, **kwargs):
-               """overload this and e.g. add_platform_command()'s after the
-               modules had their say"""
-               pass
+               """overload this and e.g. add_platform_command()'s after the modules had their say"""
+               if hasattr(self, "default_clk_period"):
+                       try:
+                               self.add_period_constraint(self.lookup_request(self.default_clk_name), self.default_clk_period)
+                       except ConstraintError:
+                               pass
 
        def add_source(self, filename, language=None):
                if language is None:
index 984dff36a5cae4b274e0e276883781416b1ce846..fc2fe653af5f86a6513c83b121fbf1c811d70b9a 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform
 
 _ios = [
@@ -146,11 +145,4 @@ class Platform(XilinxPlatform):
        default_clk_period = 10
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc3s200a-ft256-4", _ios,
-                       lambda p: SimpleCRG(p, "clk0", None), _connectors)
-
-       def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk0"), 10)
-               except ConstraintError:
-                       pass
+               XilinxPlatform.__init__(self, "xc3s200a-ft256-4", _ios, _connectors)
index 856f99217663367b28897b65272c177065f355d8..d83dd16e2af410d60325671a6c06113007b7c2f2 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform
 
 _ios = [
@@ -173,11 +172,4 @@ class Platform(XilinxPlatform):
        default_clk_period = 10.526
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc6slx9-2csg225", _ios,
-                       lambda p: SimpleCRG(p, "clk3", None), _connectors)
-
-       def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk3"), 10.526)
-               except ConstraintError:
-                       pass
+               XilinxPlatform.__init__(self, "xc6slx9-2csg225", _ios, _connectors)
index 2ce73fc7ff7bf0b64957e2683fc7c20d15f0e6db..0e0fe3263535568c48df8d9d2b5d4634bccb1c8c 100644 (file)
@@ -2,7 +2,6 @@
 # License: BSD
 
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.altera.quartus import AlteraQuartusPlatform
 from mibuild.altera.programmer import USBBlaster
 
@@ -96,14 +95,7 @@ class Platform(AlteraQuartusPlatform):
        default_clk_period = 20
 
        def __init__(self):
-               AlteraQuartusPlatform.__init__(self, "EP4CE22F17C6", _io,
-                       lambda p: SimpleCRG(p, "clk50", None))
+               AlteraQuartusPlatform.__init__(self, "EP4CE22F17C6", _io)
 
        def create_programmer(self):
                return USBBlaster()
-
-       def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk50"), 20)
-               except ConstraintError:
-                       pass
index 4d9e923f29f43af0bbcd854a6466ee79643d5e63..b2df99d03bc00b359859f890140e88c8499b5aff 100644 (file)
@@ -1,8 +1,6 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform, XC3SProg, VivadoProgrammer
 from mibuild.xilinx.ise import XilinxISEToolchain
-from mibuild.xilinx.common import CRG_DS
 
 _io = [
        ("user_led", 0, Pins("AB8"), IOStandard("LVCMOS15")),
@@ -383,9 +381,7 @@ class Platform(XilinxPlatform):
        default_clk_period = 6.4
 
        def __init__(self, toolchain="vivado", programmer="xc3sprog"):
-               XilinxPlatform.__init__(self, "xc7k325t-ffg900-2", _io, 
-                       default_crg_factory=lambda p: CRG_DS(p, "clk156", "cpu_reset"),
-                       connectors=_connectors,
+               XilinxPlatform.__init__(self, "xc7k325t-ffg900-2", _io, _connectors,
                        toolchain=toolchain)
                self.bitgen_opt = "-g LCK_cycle:6 -g Binary:Yes -w -g ConfigRate:12 -g SPI_buswidth:4"
                self.programmer = programmer
@@ -399,10 +395,7 @@ class Platform(XilinxPlatform):
                        raise ValueError("{} programmer is not supported".format(programmer))
 
        def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk156").p, 6.4)
-               except ConstraintError:
-                       pass
+               XilinxPlatform.do_finalize(self, fragment)
                try:
                        self.add_period_constraint(self.lookup_request("clk200").p, 5.0)
                except ConstraintError:
index e47a0f53354276ba66e395981c735ecc5782d808..cbf03b3bf137e871dc087da6ea00ab3e6d25989e 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform
 
 _io = [
@@ -107,8 +106,7 @@ class Platform(XilinxPlatform):
        default_clk_period = 10
        
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc6slx9-2csg324", _io,
-                               lambda p: SimpleCRG(p, "clk_y3", "user_btn"))
+               XilinxPlatform.__init__(self, "xc6slx9-2csg324", _io)
                self.add_platform_command("""
 CONFIG VCCAUX = "3.3";
 """)
@@ -118,10 +116,7 @@ promgen -w -spi -c FF -p mcs -o {build_name}.mcs -u 0 {build_name}.bit
 """
 
        def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk_y3"), 10)
-               except ConstraintError:
-                       pass
+               XilinxPlatform.do_finalize(self, fragment)
 
                try:
                        eth_clocks = self.lookup_request("eth_clocks")
index 7884d3d9251bf573fc93ba659143ab575fa175ee..8cd7fb1995b3aa7b51e724ed89d791819b0d2bd1 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform
 from mibuild.xilinx.programmer import UrJTAG
 
@@ -124,17 +123,13 @@ class Platform(XilinxPlatform):
        default_clk_period = 20
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc6slx45-fgg484-2", _io,
-                       lambda p: SimpleCRG(p, "clk50", None))
+               XilinxPlatform.__init__(self, "xc6slx45-fgg484-2", _io)
 
        def create_programmer(self):
                return UrJTAG("fjmem-m1.bit")
 
        def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk50"), 20)
-               except ConstraintError:
-                       pass
+               XilinxPlatform.do_finalize(self, fragment)
 
                try:
                        eth_clocks = self.lookup_request("eth_clocks")
index e571f944d3d462f6d841a7b523714f2ef888130d..356f988301267cdb490e735e07811a7ae653d90b 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform
 from mibuild.xilinx.programmer import UrJTAG
 
@@ -160,18 +159,14 @@ class Platform(XilinxPlatform):
        default_clk_period = 20
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc6slx45-fgg484-2", _io,
-                       lambda p: SimpleCRG(p, "clk50", None))
+               XilinxPlatform.__init__(self, "xc6slx45-fgg484-2", _io)
                self.add_platform_command("CONFIG VCCAUX=\"3.3\";\n")
 
        def create_programmer(self):
                return UrJTAG("fjmem-mixxeo.bit")
 
        def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk50"), 20)
-               except ConstraintError:
-                       pass
+               XilinxPlatform.do_finalize(self, fragment)
 
                try:
                        eth_clocks = self.lookup_request("eth_clocks")
index 1bdea3bf7a31262a9139db75326b1fccf5d082d7..ac4cf9f97ea3d1aa207d3f079e8bfb693c6daccc 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.xilinx.common import CRG_DS
 from mibuild.xilinx import XilinxPlatform
 
 _io = [
@@ -56,11 +55,4 @@ class Platform(XilinxPlatform):
        default_clk_period = 5
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc6vlx240t-ff1156-1", _io,
-                       lambda p: CRG_DS(p, "clk200", "user_btn"))
-
-       def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk200").p, 5)
-               except ConstraintError:
-                       pass
+               XilinxPlatform.__init__(self, "xc6vlx240t-ff1156-1", _io)
index bd54ac51101b14c908a950036576c0d3bdb0b97f..2de08e9479af65db2a5dae88f967deaafa646ccd 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform
 from mibuild.xilinx.programmer import XC3SProg
 
@@ -55,14 +54,7 @@ class Platform(XilinxPlatform):
        default_clk_period = 31.25
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc6slx9-tqg144-2", _io,
-                       lambda p: SimpleCRG(p, "clk32", None), _connectors)
+               XilinxPlatform.__init__(self, "xc6slx9-tqg144-2", _io, _connectors)
 
        def create_programmer(self):
                return XC3SProg("papilio", "bscan_spi_lx9_papilio.bit")
-
-       def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk32"), 31.25)
-               except ConstraintError:
-                       pass
index 831285e85a1b24537a93afa3cb873b4c1323220c..dc9bebd4209487a7f760e6ea5a47c3a9cdc985af 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform
 from mibuild.xilinx.programmer import XC3SProg
 
@@ -130,14 +129,7 @@ class Platform(XilinxPlatform):
        default_clk_period = 20
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc6slx45-csg324-2", _io,
-                       lambda p: SimpleCRG(p, "clk50", None), _connectors)
+               XilinxPlatform.__init__(self, "xc6slx45-csg324-2", _io, _connectors)
 
        def create_programmer(self):
                return XC3SProg("papilio", "bscan_spi_lx45_csg324.bit")
-
-       def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk50"), 20.)
-               except ConstraintError:
-                       pass
index 77993cc7230383dcb6b624a4f5ff7d8b7b2f4276..cb2f043f7885adfc1f5dd52e00674417e6ddd482 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.xilinx.common import CRG_DS
 from mibuild.xilinx import XilinxPlatform
 
 _io = [
@@ -138,11 +137,4 @@ class Platform(XilinxPlatform):
        default_clk_period = 10
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc6slx150t-fgg676-3", _io,
-                       lambda p: CRG_DS(p, "clk100", "gpio"))
-
-       def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk100").p, 10)
-               except ConstraintError:
-                       pass
+               XilinxPlatform.__init__(self, "xc6slx150t-fgg676-3", _io)
index 697edaa3cfd1a85fd75c5141996e0d3936a48b96..a0f83a5d4f393b18289c6a66f5e8294d9b2985ad 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.sim.verilator import VerilatorPlatform
 
 class SimPins(Pins):
index be4b2130a6d93d32fbf4bf9ab6f5b73914cc8228..ac2edb49d4d3db97009ec1971b6808dd0369ae40 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.xilinx.common import CRG_DS
 from mibuild.xilinx import XilinxPlatform
 
 _io = [
@@ -118,18 +117,11 @@ class Platform(XilinxPlatform):
        default_clk_period = 15.625
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc3s1400a-ft256-4", _io,
-                       lambda p: CRG_DS(p, "clk64", "reset_n", rst_invert=True))
+               XilinxPlatform.__init__(self, "xc3s1400a-ft256-4", _io)
                self.bitgen_opt = "-g LCK_cycle:6 -g Binary:Yes -w -g UnusedPin:PullUp"
 
        def do_finalize(self, fragment):
-               try:
-                       self.add_platform_command("""
-NET "{clk64}" TNM_NET = "GRPclk64";
-TIMESPEC "TSclk64" = PERIOD "GRPclk64" 15.625 ns HIGH 50%;
-""", clk64=self.lookup_request("clk64"))
-               except ConstraintError:
-                       pass
+               XilinxPlatform.do_finalize(self, fragment)
 
                self.add_platform_command("""
 TIMESPEC TS_Pad2Pad = FROM PADS TO PADS 7 ns;
index 74ba5d818c9c9663c326787f166f1334560e83e4..7be76018d59b279106eb2495c47c541d00bf5c3f 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform
 
 # Bank 34 and 35 voltage depend on J18 jumper setting
@@ -142,11 +141,4 @@ class Platform(XilinxPlatform):
        default_clk_period = 10
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc7z020-clg484-1", _io,
-                       lambda p: SimpleCRG(p, "clk100", None))
-
-       def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk100"), 10)
-               except ConstraintError:
-                       pass
+               XilinxPlatform.__init__(self, "xc7z020-clg484-1", _io)
index a6880ef110ef09c4d9b65ff869434236dd575c8b..7b0a601d2c45a7ed422e12434b7c7ecdca2fa1a4 100644 (file)
@@ -1,5 +1,4 @@
 from mibuild.generic_platform import *
-from mibuild.crg import SimpleCRG
 from mibuild.xilinx import XilinxPlatform
 
 _io = [
@@ -86,17 +85,13 @@ class Platform(XilinxPlatform):
        default_clk_period = 20
 
        def __init__(self):
-               XilinxPlatform.__init__(self, "xc6slx150-3csg484", _io,
-                               lambda p: SimpleCRG(p, "clk_if", "rst"))
+               XilinxPlatform.__init__(self, "xc6slx150-3csg484", _io)
                self.add_platform_command("""
 CONFIG VCCAUX = "2.5";
 """)
 
        def do_finalize(self, fragment):
-               try:
-                       self.add_period_constraint(self.lookup_request("clk_if"), 20)
-               except ConstraintError:
-                       pass
+               XilinxPlatform.do_finalize(self, fragment)
 
                try:
                        clk_if = self.lookup_request("clk_if")
index f5d3cd87cd62c974a3aabd15e76bc6f26653c445..6ff6470d2aa3396aa28e82242f263b9e089883c2 100644 (file)
@@ -36,4 +36,6 @@ class XilinxPlatform(GenericPlatform):
                return self.toolchain.build(self, *args, **kwargs)
 
        def add_period_constraint(self, clk, period):
+               if hasattr(clk, "p"):
+                       clk = clk.p
                self.toolchain.add_period_constraint(self, clk, period)
index 5e27eae35bf179818209c01b1f60b5dcca1a005e..67cef088f96b0eeb45424a37b6373b3e789f259e 100644 (file)
@@ -33,3 +33,22 @@ class DifferentialOutput(Special):
        @staticmethod
        def lower(dr):
                raise NotImplementedError("Attempted to use a differential output, but platform does not support them")
+
+class CRG(Module):
+       def __init__(self, clk):
+               self.clock_domains.cd_sys = ClockDomain()
+               self.clock_domains.cd_por = ClockDomain(reset_less=True)
+
+               if hasattr(clk, "p"):
+                       clk_se = Signal()
+                       self.specials += DifferentialInput(clk.p, clk.n, clk_se)
+                       clk = clk_se
+
+               # Power on Reset (vendor agnostic)
+               rst_n = Signal()
+               self.sync.por += rst_n.eq(1)
+               self.comb += [
+                       self.cd_sys.clk.eq(clk),
+                       self.cd_por.clk.eq(clk),
+                       self.cd_sys.rst.eq(~rst_n)
+               ]