cores/leds: Add polarity support.

This commit is contained in:
Florent Kermarrec 2022-07-14 12:24:07 +02:00
parent dd8645ca40
commit 96ba7ae79e
1 changed files with 13 additions and 9 deletions

View File

@ -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 ----------------------------------------------------------------------------------