timer: add prescaler

This commit is contained in:
Florent Kermarrec 2015-04-10 13:58:44 +02:00
parent d6c19858fa
commit 80ef7291c1
1 changed files with 19 additions and 3 deletions

View File

@ -1,12 +1,14 @@
from migen.fhdl.std import * from migen.fhdl.std import *
from migen.bank.description import * from migen.bank.description import *
from migen.bank.eventmanager import * from migen.bank.eventmanager import *
from migen.genlib.misc import Counter
class Timer(Module, AutoCSR): class Timer(Module, AutoCSR):
def __init__(self, width=32): def __init__(self, width=32, prescaler_width=32):
self._load = CSRStorage(width) self._load = CSRStorage(width)
self._reload = CSRStorage(width) self._reload = CSRStorage(width)
self._en = CSRStorage() self._en = CSRStorage()
self._prescaler = CSRStorage(prescaler_width, reset=1)
self._update_value = CSR() self._update_value = CSR()
self._value = CSRStatus(width) self._value = CSRStatus(width)
@ -15,14 +17,28 @@ class Timer(Module, AutoCSR):
self.ev.finalize() self.ev.finalize()
### ###
enable = self._en.storage
tick = Signal()
counter = Counter(prescaler_width)
self.submodules += counter
self.comb += [
If(enable,
tick.eq(counter.value >= (self._prescaler.storage-1)),
counter.ce.eq(1),
counter.reset.eq(tick),
).Else(
counter.reset.eq(1)
)
]
value = Signal(width) value = Signal(width)
self.sync += [ self.sync += [
If(self._en.storage, If(enable,
If(value == 0, If(value == 0,
# set reload to 0 to disable reloading # set reload to 0 to disable reloading
value.eq(self._reload.storage) value.eq(self._reload.storage)
).Else( ).Elif(tick,
value.eq(value - 1) value.eq(value - 1)
) )
).Else( ).Else(