diff --git a/litex/soc/cores/led.py b/litex/soc/cores/led.py index 020bf858f..2781eb00f 100644 --- a/litex/soc/cores/led.py +++ b/litex/soc/cores/led.py @@ -19,27 +19,31 @@ _CHASER_MODE = 0 _CONTROL_MODE = 1 class LedChaser(Module, AutoCSR): - def __init__(self, pads, sys_clk_freq, period=1e0): - self.pads = pads - self._out = CSRStorage(len(pads), description="Led Output(s) Control.") + def __init__(self, pads, sys_clk_freq, period=1e0, polarity=0): + self.pads = pads + self.polarity = polarity + self.n = len(pads) + self._out = CSRStorage(len(pads), description="Led Output(s) Control.") # # # - n = len(pads) - chaser = Signal(n) + + chaser = Signal(self.n) mode = Signal(reset=_CHASER_MODE) - timer = WaitTimer(int(period*sys_clk_freq/(2*n))) + timer = WaitTimer(int(period*sys_clk_freq/(2*self.n))) + leds = Signal(self.n) self.submodules += timer self.comb += timer.wait.eq(~timer.done) self.sync += If(timer.done, chaser.eq(Cat(~chaser[-1], chaser))) self.sync += If(self._out.re, mode.eq(_CONTROL_MODE)) self.comb += [ If(mode == _CONTROL_MODE, - pads.eq(self._out.storage) + leds.eq(self._out.storage) ).Else( - pads.eq(chaser) + leds.eq(chaser) ) ] + self.comb += pads.eq(leds ^ (self.polarity*(2**self.n-1))) def add_pwm(self, default_width=512, default_period=1024, with_csr=True): from litex.soc.cores.pwm import PWM @@ -50,7 +54,7 @@ class LedChaser(Module, AutoCSR): default_period = default_period ) # Use PWM as Output Enable for pads. - self.comb += If(~self.pwm.pwm, self.pads.eq(0)) + self.comb += If(~self.pwm.pwm, self.pads.eq(self.polarity*(2**self.n-1))) # WS2812/NeoPixel ----------------------------------------------------------------------------------