soc/cores/led: add add_pwm method to allow adjusting brightness dynamically (or not).

LedChaser without PWM:

self.submodules.leds = LedChaser(
    pads         = platform.request_all("user_led"),
    sys_clk_freq = sys_clk_freq)
self.add_csr("leds")

Add PWM to it (with default values: 50% duty cycle):
self.leds.add_pwm()

Add PWM with custom default values (25% duty cycle here):
self.leds.add_pwm(default_width=128, default_period=1024)

Then adjust brightness dynamically from the BIOS or your software:

$cat csr.csv:
csr_register,leds_out,0x82003000,1,rw
csr_register,leds_pwm_enable,0x82003004,1,rw
csr_register,leds_pwm_width,0x82003008,1,rw
csr_register,leds_pwm_period,0x8200300c,1,rw

Set PWM to 0%:
$mem_write 0x82003008 0

Set PWM to 25%:
$mem_write 0x82003008 256

Set PWM to 50%:
$mem_write 0x82003008 512

Set PWM to 75%:
$mem_write 0x82003008 768

Set PWM to 100%:
$mem_write 0x82003008 1024

You can also only use default values and disable CSR is dynamic configuration is not
required (with_csr=False) or adjust PWM period if want to use a specific PWM period
in your system.
This commit is contained in:
Florent Kermarrec 2021-02-18 09:46:30 +01:00
parent fc282b3084
commit 7ce5aef428
1 changed files with 12 additions and 0 deletions

View File

@ -16,6 +16,7 @@ _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.")
# # #
@ -35,3 +36,14 @@ class LedChaser(Module, AutoCSR):
pads.eq(chaser)
)
]
def add_pwm(self, default_width=512, default_period=1024, with_csr=True):
from litex.soc.cores.pwm import PWM
self.submodules.pwm = PWM(
with_csr = with_csr,
default_enable = 1,
default_width = default_width,
default_period = default_period
)
# Use PWM as Output Enable for pads.
self.comb += If(~self.pwm.pwm, self.pads.eq(0))